package com.bjtdba.order.service.impl;


import com.bjtdba.entity.order.StoreProduct;
import com.bjtdba.entity.order.StoreProductAttrValue;
import com.bjtdba.order.dao.StoreProductAttrValueDao;
import com.bjtdba.order.dao.StoreProductDao;
import com.bjtdba.order.dao.SystemConfigValueDao;
import com.bjtdba.order.service.StoreProductService;
import com.bjtdba.order.util.redis.RedisUtil;
import com.bjtdba.order.vo.ProductXihuan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 商品表(StoreProduct)表服务实现类
 *
 * @author makejava
 * @since 2022-09-28 11:05:36
 */
@Service("storeProductService")
public class StoreProductServiceImpl implements StoreProductService {
    @Resource
    private StoreProductDao storeProductDao;
    @Autowired
    private StoreProductAttrValueDao storeProductAttrValueDao;
    @Autowired
    private SystemConfigValueDao systemConfigValueDao;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param productId 主键
     * @return 实例对象
     */
    @Override
    public StoreProduct queryById(Long productId) {
        return this.storeProductDao.queryById(productId);
    }

    /**
     * 分页查询
     *
     * @param storeProduct 筛选条件
     * @return 查询结果
     */
//    @Override
//    public Page<StoreProduct> queryByPage(StoreProduct storeProduct, PageRequest pageRequest) {
//        long total = this.storeProductDao.count(storeProduct);
//        return new PageImpl<>(this.storeProductDao.queryAllByLimit(storeProduct, pageRequest), pageRequest, total);
//    }

    public List<StoreProduct> queryList(StoreProduct storeProduct){
        return this.storeProductDao.queryAllByLimit(storeProduct);
    }

    /**
     * 新增数据
     *
     * @param storeProduct 实例对象
     * @return 实例对象
     */
    @Override
    public StoreProduct insert(StoreProduct storeProduct) {
        this.storeProductDao.insert(storeProduct);
        return storeProduct;
    }

    /**
     * 修改数据
     *
     * @param storeProduct 实例对象
     * @return 实例对象
     */
    @Override
    public StoreProduct update(StoreProduct storeProduct) {
        this.storeProductDao.update(storeProduct);
        return this.queryById(storeProduct.getProductId());
    }

    /**
     * 通过主键删除数据
     *
     * @param productId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long productId) {
        return this.storeProductDao.deleteById(productId) > 0;
    }
    /**
     * 统计养老金
     * @param price
     * @param dc
     * @return
     */
    public BigDecimal yanglaojin(StoreProduct storeProduct, BigDecimal price, BigDecimal dc){
        StoreProduct extension=new StoreProduct();
        BigDecimal yanglao=new BigDecimal(0);
//        Long productId=Long.parseLong(good_id+"");
        float yanglao_card=0l;
        float extension_bili =0l;
        if(dc==null){
            dc=new BigDecimal(0);
        }

        //平台商品线上
        if(storeProduct.getProductId()>0){
            extension=storeProduct;
//            if(dc.equals(1) && extension.getType()==5){
//                extension.setPrice(extension.getDcPrice());
//            }

            if(extension.getExtensionType()>0){
                StoreProductAttrValue storeProductAttr=storeProductAttrValueDao.qeryByqueryByproduct(storeProduct.getProductId()+"");
                extension_bili=Float.parseFloat(storeProductAttr.getExtensionOne()+"");
            }

            if (extension_bili==0){
                if (redisUtil.hasKey("base_commission")){
                    extension_bili=Float.parseFloat(redisUtil.get("base_commission")+"");
                }else{
                    extension_bili=Float.parseFloat(systemConfigValueDao.selectValueByConfig("base_commission",extension.getMerId()).replace("\"",""));
                    redisUtil.set("base_commission",extension_bili,60*60*24);
                }
            }
            if (extension_bili==0){
                if (redisUtil.hasKey("extension_one_rate")){
                    extension_bili=Float.parseFloat(redisUtil.get("extension_one_rate")+"");
                }else {
                    extension_bili = Float.parseFloat(systemConfigValueDao.selectValueByConfig("extension_one_rate", null));
                    redisUtil.set("extension_one_rate",extension_bili,60*60*24);
                }
            }
            //刷卡费比例线下0.6
//            if (redisUtil.hasKey("online_yanglao")){
//                yanglao_card=Float.parseFloat(redisUtil.get("online_yanglao")+"");
//            }else {
//                yanglao_card =  Float.parseFloat(systemConfigValueDao.selectValueByConfig("online_yanglao",null));
//                redisUtil.set("online_yanglao",yanglao_card,60*60*24);
//
//            }

//            if(extension.getType()==5){
//                extension_bili=2;
//            }

        }else {
            if (redisUtil.hasKey("extension_tao_rate")) {
                extension_bili = Float.parseFloat(redisUtil.get("extension_tao_rate") + "");
            } else{
                extension_bili = Float.parseFloat(systemConfigValueDao.selectValueByConfig("extension_tao_rate", null));
                redisUtil.set("extension_tao_rate",extension_bili,60*60*24);
            }
            extension.setPrice(price);
        }



        //养老金比例
        float yanglaojin_admin=0l;
        if (redisUtil.hasKey("yanglaojin_admin")) {
            yanglaojin_admin = Float.parseFloat(redisUtil.get("yanglaojin_admin") + "");
        } else {
            yanglaojin_admin = Float.parseFloat(systemConfigValueDao.selectValueByConfig("yanglaojin_admin", null));
            redisUtil.set("yanglaojin_admin",yanglaojin_admin,60*60*24);
        }
        //会费比例
//        float yanglao_Meeting=0l;
//
//        if (redisUtil.hasKey("yanglao_Meeting")) {
//            yanglao_Meeting = Float.parseFloat(redisUtil.get("yanglao_Meeting") + "");
//        } else {
//            yanglao_Meeting = Float.parseFloat(systemConfigValueDao.selectValueByConfig("yanglao_Meeting", null));
//            redisUtil.set("yanglao_Meeting",yanglao_Meeting,60*60*24);
//        }
//        $yanglao=0;
        if (extension_bili>0){
//            $yanglao = ($extension['price'] - $extension['price'] * $yanglao_card / 100) * $extension_bili / 100 ;  //除去刷卡费  乘以返利比例
//            $yanglao = $yanglao * $yanglaojin_admin /100  ;//养老金比例
//            $yanglao = $yanglao - $yanglao * $yanglao_Meeting / 100 ;  //除去会费这里没有除去年会费扣完后的情况
//            $yanglao=round($yanglao,2);

//            yanglao = (extension.getPrice().subtract(extension.getPrice().multiply(new BigDecimal(yanglao_card)).divide(new BigDecimal(100)))).multiply(new BigDecimal(extension_bili)).divide(new BigDecimal(100));  //除去刷卡费  乘以返利比例$yanglao = $yanglao * $yanglaojin_admin /100  ;//养老金比例
//            yanglao= yanglao.multiply(new BigDecimal(yanglaojin_admin)).divide(new BigDecimal(100));
//            yanglao = yanglao.subtract(yanglao.multiply(new BigDecimal(yanglao_Meeting)).divide(new BigDecimal(100))) ;  //除去会费这里没有除去年会费扣完后的情况
//            yanglao= yanglao.setScale(2,BigDecimal.ROUND_HALF_UP);


            //20221025 按照林总最后php的修改功能调整

//            $yanglao = $extension['price'] * $extension_bili / 100 ;  //除去刷卡费  乘以返利比例
            yanglao=extension.getPrice().multiply(BigDecimal.valueOf(extension_bili)).divide(new BigDecimal(100));

            //大仓商品按照交易价格的2%算养老金  普通商品按照商户设置的返利比例的50%
//            if (empty($extension['type']) || $extension['type'] != 5){
//                $yanglao = $yanglao * $yanglaojin_admin /100  ;//养老金比例
//            }
//            if(extension.getType()!=null&&extension.getType()!=5){
//                yanglao=yanglao.multiply(BigDecimal.valueOf(yanglaojin_admin)).divide(BigDecimal.valueOf(100));
//            }
            yanglao=yanglao.multiply(BigDecimal.valueOf(yanglaojin_admin)).divide(BigDecimal.valueOf(100));
            yanglao= yanglao.setScale(2,BigDecimal.ROUND_HALF_UP);

        }

        return yanglao;

    }

    /**
     * 函数查询 子集id
     * @param cateId
     * @return
     */

    public String getCateIds(Integer cateId){
        return storeProductDao.getCateIds(cateId);
    }
    /**
     * 猜你喜欢列表
     */
    public List<ProductXihuan> Productxihuan() {
        return this.storeProductDao.Productxihuan();
    }
}
