package com.gree.mall.miniapp.logic.material.parts;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gree.mall.miniapp.bean.material.parts.PartsWorkerStockBean;
import com.gree.mall.miniapp.bean.material.parts.WorkerStockCensusBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.MaterialMapper;
import com.gree.mall.miniapp.enums.material.PartsAttrEnum;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.AdminWebsit;
import com.gree.mall.miniapp.plus.entity.WebsitPartsRefundConfig;
import com.gree.mall.miniapp.plus.entity.WebsitPartsSalesItem;
import com.gree.mall.miniapp.plus.service.AdminWebsitService;
import com.gree.mall.miniapp.plus.service.WebsitPartsRefundConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WorkerPartsStockLogic {

    @Resource
    MaterialMapper materialMapper;
    @Resource
    CommonLogic commonLogic;
    @Resource
    AdminWebsitService adminWebsitService;
    @Resource
    WebsitPartsRefundConfigService websitPartsRefundConfigService;


    public List<AdminWebsit> existWebsitList(String identity) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        return materialMapper.existWebsitList(wechat.getCompanyWechatId(), identity);
    }

    public List<String> existWebsitGroupList(String identity, String websitId, String partsWebsitId, String partsAttr) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        return materialMapper.existWebsitGroupList(wechat.getCompanyWechatId(), identity, websitId, partsWebsitId, partsAttr);
    }

    public List<PartsWorkerStockBean> existWorkerStockList(String identity, String websitId, String partsWebsitId,
                                                           String partsAttr, String partsNumber, String partsName,
                                                           String materialGroupName) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        List<PartsWorkerStockBean> stockList = materialMapper.existWorkerStockList(wechat.getCompanyWechatId(), identity,
                websitId, partsWebsitId, partsAttr, partsNumber, partsName, materialGroupName);
        // 过期天数
        if (partsAttr.equals(PartsAttrEnum.NEW.getKey())) {
            // 第一步 查询配件网点
            AdminWebsit shop = adminWebsitService.getById(websitId);
            if (Objects.nonNull(shop)) {
                List<WebsitPartsSalesItem> itemList = materialMapper.querySalesEndTimeByIdentity(wechat.getCompanyWechatId(),
                        websitId, partsWebsitId, identity);
                Map<String, WebsitPartsSalesItem> itemMap = itemList.stream()
                        .collect(Collectors.toMap(WebsitPartsSalesItem::getPartsNumber, Function.identity()));
                // 第二步 查询中心配件返还配置
                WebsitPartsRefundConfig oneConfig = websitPartsRefundConfigService.lambdaQuery()
                        .eq(WebsitPartsRefundConfig::getCompanyWechatId, wechat.getCompanyWechatId())
                        .eq(WebsitPartsRefundConfig::getIsDefault, Boolean.TRUE)
                        .one();
                // 第三步 查询网点配件返还配置
                WebsitPartsRefundConfig shopConfig = websitPartsRefundConfigService.lambdaQuery()
                        .eq(WebsitPartsRefundConfig::getCompanyWechatId, wechat.getCompanyWechatId())
                        .eq(WebsitPartsRefundConfig::getWebsitId, websitId)
                        .one();
                // 超期时间
                Integer expire;

                if (shop.getPartsWebsitAttr()) {
                    // 内区天数
                    expire = Objects.isNull(shopConfig) ? oneConfig.getNewRefundInner() : shopConfig.getNewRefundInner();
                } else {
                    // 外区天数
                    expire = Objects.isNull(shopConfig) ? oneConfig.getNewRefundOutside() : shopConfig.getNewRefundOutside();
                }

                for (PartsWorkerStockBean partsWorkerStockBean : stockList) {
                    WebsitPartsSalesItem partsSalesOrderItem = itemMap.get(partsWorkerStockBean.getGoodsId());
                    if (Objects.nonNull(partsSalesOrderItem)) {
                        DateTime offsetDateTime = DateUtil.offset(partsSalesOrderItem.getExamineTime(), DateField.DAY_OF_MONTH, expire);
                        DateTime endOfDay = DateUtil.endOfDay(offsetDateTime);
                        long betweenDay = DateUtil.between(endOfDay, DateUtil.date(), DateUnit.DAY) + 1;
                        partsWorkerStockBean.setExpireDay(betweenDay > expire ? null : betweenDay);
                    }
                }
            }
        }
        return stockList;
    }

    public WorkerStockCensusBean queryWorkerStock(String identity, String websitId) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        WorkerStockCensusBean initWorkStockCensusBean = new WorkerStockCensusBean();
        initWorkStockCensusBean.setIdentity("");
        initWorkStockCensusBean.setWorkerId("");
        initWorkStockCensusBean.setWorkerName("");
        initWorkStockCensusBean.setNewQty(new BigDecimal(0));
        initWorkStockCensusBean.setOldQty(new BigDecimal(0));
        initWorkStockCensusBean.setNewRefundAmount(new BigDecimal(0));
        initWorkStockCensusBean.setNewRefundQty(new BigDecimal(0));
        initWorkStockCensusBean.setOldRefundAmount(new BigDecimal(0));
        initWorkStockCensusBean.setOldRefundQty(new BigDecimal(0));
        if(StringUtils.isBlank(identity)) {
            return initWorkStockCensusBean;
        }
        WorkerStockCensusBean workerStockStatistics = materialMapper.workerStockStatistics(wechat.getCompanyWechatId(), identity, websitId);
        workerStockStatistics.setWorkerName(wechat.getUser().getNickName());
        if(!Objects.isNull(workerStockStatistics)) {
            WorkerStockCensusBean workerNewStockStatistics =  materialMapper.workerNewStockStatistics(wechat.getCompanyWechatId(), identity, websitId);
            workerStockStatistics.setNewRefundQty(Objects.isNull(workerNewStockStatistics) ? new BigDecimal(0) : workerNewStockStatistics.getNewRefundQty());
            workerStockStatistics.setNewRefundAmount(Objects.isNull(workerNewStockStatistics) ? new BigDecimal(0) : workerNewStockStatistics.getNewRefundAmount());
            WorkerStockCensusBean workerOldStockStatistics =  materialMapper.workerOldStockStatistics(wechat.getCompanyWechatId(), identity, websitId);
            workerStockStatistics.setOldRefundQty(Objects.isNull(workerOldStockStatistics) ? new BigDecimal(0) : workerOldStockStatistics.getOldRefundQty());
            workerStockStatistics.setOldRefundAmount(Objects.isNull(workerOldStockStatistics) ? new BigDecimal(0) : workerOldStockStatistics.getOldRefundAmount());
            return workerStockStatistics;
        }
        return initWorkStockCensusBean;
    }
}
