package com.bjtdba.wares.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjtdba.entity.wares.ProductSeckill;
import com.bjtdba.entity.wares.StoreProduct;
import com.bjtdba.utils.ResponseUtil;
import com.bjtdba.wares.dao.ProductSeckillDao;
import com.bjtdba.wares.dao.StoreProductAttrValueDao;
import com.bjtdba.wares.dao.StoreProductDao;
import com.bjtdba.wares.dao.SystemConfigValueDao;
import com.bjtdba.wares.dto.SearchCriteriaDto;
import com.bjtdba.wares.service.PensionService;
import com.bjtdba.wares.service.WaresService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WaresServiceImpl implements WaresService {

    /**
     * 商品列表
     *
     */
    @Autowired
    private StoreProductDao storeProductDao;
    /**
     * 养老金
     */
    @Autowired
    private PensionService pensionService;

    /**
     * StoreProductAttrValueDao
     */
    @Autowired
    private StoreProductAttrValueDao storeProductAttrValueDao;

    /**
     * 查看商品秒杀时间
     */
    @Autowired
    private ProductSeckillDao productSeckillDao;

    @Autowired
    private SystemConfigValueDao systemConfigValueDao;


    @Override
    public Object selectProductList(Integer pageNum,Integer pageSize,SearchCriteriaDto searchCriteriaDto) {
        Map<String, Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        //判断商户ID+分类ID，如果两个都不为空的时候是商铺分类检索
        List<StoreProduct> storeProductList = null;
        if(searchCriteriaDto.getMer_id()!=null&&searchCriteriaDto.getCategory_id()!=null){
            //这个时候就查询
            storeProductList = storeProductDao.selectCateProductList(searchCriteriaDto);
        }else{
            storeProductList = storeProductDao.selectProductList(searchCriteriaDto);
        }
        Long count=storeProductDao.count(searchCriteriaDto);
        storeProductList.forEach(storeProduct->{
            BigDecimal yanglaojin ;
//            if(storeProduct.getSpu_id()!=null){
//                Integer product_id = storeProduct.getProduct_id();
//                StoreProductAttrValue storeProductAttrValue = storeProductAttrValueDao.selectByProductId(product_id);
//                yanglaojin = storeProductAttrValue.getGong_pension();
//            }else{
//                yanglaojin = pensionService.productPension(storeProduct.getProduct_id());
//            }
            if(storeProduct.getSpu_id()!=null){
                // 大仓
                String shopBonusSettingJson = systemConfigValueDao.selectConfigValueByKeyAndMerId("shop_bonus_setting", storeProduct.getMer_id());
                JSONObject shopBonusSetting= JSON.parseObject(shopBonusSettingJson);
                String yanglao_rate=shopBonusSetting.get("yanglao_rate").toString();
                yanglaojin = new BigDecimal(yanglao_rate).multiply(new BigDecimal(storeProduct.getPrice())).divide(new BigDecimal(100));
            }else{
                // 平台 查询平台
                String shopBonusSettingJson = systemConfigValueDao.selectConfigValueByKey("commission_rate");
                JSONObject shopBonusSetting = JSON.parseObject(shopBonusSettingJson);
                String yanglao_rate = "0";
                yanglao_rate = shopBonusSetting.get("yanglao_rate").toString();
                yanglaojin = new BigDecimal(yanglao_rate).multiply(new BigDecimal(storeProduct.getPrice())).divide(new BigDecimal(100));
            }
            DecimalFormat decimalFormat = new DecimalFormat();
            decimalFormat.setGroupingUsed(false);
            storeProduct.setYanglao_price(decimalFormat.format(yanglaojin.setScale(2, BigDecimal.ROUND_DOWN)));

            if(storeProduct.getPrice()!=null) {
                storeProduct.setPrice(decimalFormat.format(Double.parseDouble(storeProduct.getPrice())));
            }
            if(storeProduct.getCost()!=null){
                storeProduct.setCost(decimalFormat.format(Double.parseDouble(storeProduct.getCost())));
            }
            if(storeProduct.getOt_price()!=null) {
                storeProduct.setOt_price(decimalFormat.format(Double.parseDouble(storeProduct.getOt_price())));
            }
            if(storeProduct.getSeckill().equals("1") && storeProduct.getDc_price()!=null){
                ProductSeckill productSeckill = productSeckillDao.selectSeckillByProductId(storeProduct.getProduct_id());
                if(productSeckill!=null && productSeckill.getSeckill_date()!=null){
                    LocalDateTime dateTime = LocalDateTime.parse(productSeckill.getSeckill_date(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));
                    LocalDate today = LocalDate.now();
                    if (dateTime.toLocalDate().isEqual(today)) {
                        //System.err.println("是今天");
                        storeProduct.setPrice(decimalFormat.format(Double.parseDouble(storeProduct.getDc_price())));
                    }
                }
            }
//            storeProduct.setYanglao_price(yanglaojin.setScale(2,BigDecimal.ROUND_DOWN).toString());
        });
        PageInfo<StoreProduct> pageInfo = null;
        pageInfo = new PageInfo<StoreProduct>(storeProductList);

        if(pageInfo.getPages()<pageNum){
            PageHelper.startPage(0,0);
            List<StoreProduct> order = new ArrayList<>();
            pageInfo = new PageInfo<>(order);
            pageInfo.setTotal(count);
        }
        data.put("pageInfo",pageInfo);
        return ResponseUtil.ok(data);
    }
}
