package com.gree.mall.manager.logic.goods;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.goods.*;
import com.gree.mall.manager.commonmapper.CustomGoodsApplyMapper;
import com.gree.mall.manager.enums.ExamineStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class GoodsApplyLogic {


    @Resource
    CommonLogic commonLogic;


    @Resource
    CustomGoodsApplyMapper customGoodsApplyMapper;

    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsSpecService goodsSpecService;

    @Autowired
    GoodsApplyService goodsApplyService;

    @Autowired
    GoodsApplyItemService goodsApplyItemService;

    @Autowired
    AdminWebsitService adminWebsitService;



    public IPage<GoodsApplyVO> applyList(Page page, ZfireParamBean zfireParam) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        //1.组装查询条件
        FieldUtils.supplyParam(zfireParam, GoodsApplyVO.class, adminUser);

        IPage<GoodsApplyVO> goodsVOIPage = customGoodsApplyMapper.goodsList(page, zfireParam);
        return goodsVOIPage;
    }

    public GoodsApplyDetail detail(String goodsApplyId, HttpServletRequest request) {

        GoodsApply goodsApply = goodsApplyService.getById(goodsApplyId);

        Goods goods = goodsService.getById(goodsApply.getGoodsId());

        List<GoodsSpec> goodsSpecs = goodsSpecService.lambdaQuery().eq(GoodsSpec::getGoodsId, goods.getGoodsId())
                .list();

        GoodsApplyDetail goodsApplyDetail = BeanUtil.toBean(goodsApply,GoodsApplyDetail.class);

        goodsApplyDetail.setGoods(goods);

        List<GoodsApplyItemDetail> goodsApplyItemDetails = new ArrayList<>();
        for (GoodsSpec goodsSpec : goodsSpecs) {

            GoodsApplyItem goodsApplyItem = goodsApplyItemService.lambdaQuery()
                    .eq(GoodsApplyItem::getGoodsApplyId,goodsApplyId)
                    .eq(GoodsApplyItem::getGoodsSpecId, goodsSpec.getGoodsSpecId()).one();

            GoodsApplyItemDetail goodsApplyItemDetail = BeanUtil.toBean(goodsApplyItem, GoodsApplyItemDetail.class);

            goodsApplyItemDetail.setGoodsSpec(goodsSpec);

            goodsApplyItemDetails.add(goodsApplyItemDetail);
        }

        goodsApplyDetail.setGoodsApplyItems(goodsApplyItemDetails);

        return goodsApplyDetail;

    }

    @Transactional(rollbackFor = Exception.class)
    public void add(GoodsApplyAddUp goodsApplyAddUp, HttpServletRequest request) {
        goodsApplyAddUp.setGoodsApplyId(IdWorker.getIdStr());

        if (goodsApplyService.lambdaQuery().eq(GoodsApply::getWebsitId,goodsApplyAddUp.getWebsitId())
        .eq(GoodsApply::getGoodsId,goodsApplyAddUp.getGoodsId()).count() > 0)
            throw new RemoteServiceException("商品存在价格调整，请去申请调整修改");


        List<GoodsSpec> goodsSpecs = goodsSpecService.lambdaQuery().eq(GoodsSpec::getGoodsId, goodsApplyAddUp.getGoodsId())
                .list();

        List<GoodsApplyItem> goodsApplyItemList = new ArrayList<>();

        Boolean ifStatus = true;

        for (GoodsSpec goodsSpec : goodsSpecs) {
            List<GoodsApplyItem> goodsApplyItems = goodsApplyAddUp.getGoodsApplyItems().stream()
                    .filter(item -> item.getGoodsSpecId().equals(goodsSpec.getGoodsSpecId()))
                    .collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(goodsApplyItems)) {
                goodsApplyItems.get(0).setGoodsId(goodsSpec.getGoodsId());
                goodsApplyItems.get(0).setGoodsApplyId(goodsApplyAddUp.getGoodsApplyId());
                goodsApplyItemList.add(goodsApplyItems.get(0));

                if (goodsSpec.getMinPrice().doubleValue() > goodsApplyItems.get(0).getApplyPrice().doubleValue() || goodsSpec.getMaxPrice().doubleValue() < goodsApplyItems.get(0).getApplyPrice().doubleValue())
                    ifStatus = false;


            }else {
                GoodsApplyItem goodsApplyItem = new GoodsApplyItem();
                goodsApplyItem.setGoodsId(goodsSpec.getGoodsId());
                goodsApplyItem.setApplyPrice(goodsSpec.getPrice());
                goodsApplyItem.setGoodsSpecId(goodsSpec.getGoodsSpecId());
                goodsApplyItem.setGoodsApplyId(goodsApplyAddUp.getGoodsApplyId());
                goodsApplyItemList.add(goodsApplyItem);

                if (goodsSpec.getMinPrice().doubleValue() > goodsApplyItem.getApplyPrice().doubleValue() || goodsSpec.getMaxPrice().doubleValue() < goodsApplyItem.getApplyPrice().doubleValue())
                    ifStatus = false;
            }

        }

        if (ifStatus) {
            goodsApplyAddUp.setStatus(ExamineStatusEnum.OK.getKey());
            goodsApplyAddUp.setApproveName("系统审核");
            goodsApplyAddUp.setApproveTime(new Date());
        }else
            goodsApplyAddUp.setStatus(ExamineStatusEnum.WAIT.getKey());


        AdminWebsit adminWebsit = adminWebsitService.getById(goodsApplyAddUp.getWebsitId());

        goodsApplyAddUp.setCompanyName(adminWebsit.getCompanyName());
        goodsApplyAddUp.setCompanyId(adminWebsit.getCompanyId());
        goodsApplyAddUp.setWebsitName(adminWebsit.getName());
        goodsApplyAddUp.insert();

        goodsApplyItemService.saveBatch(goodsApplyItemList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(GoodsApplyAddUp goodsApplyAddUp, HttpServletRequest request) {
        goodsApplyService.removeById(goodsApplyAddUp.getGoodsApplyId());
        goodsApplyItemService.lambdaUpdate().eq(GoodsApplyItem::getGoodsApplyId,goodsApplyAddUp.getGoodsApplyId()).remove();


        AdminWebsit adminWebsit = adminWebsitService.getById(goodsApplyAddUp.getWebsitId());

        goodsApplyAddUp.setCompanyName(adminWebsit.getCompanyName());
        goodsApplyAddUp.setWebsitName(adminWebsit.getName());

        goodsApplyAddUp.insert();

        List<GoodsSpec> goodsSpecs = goodsSpecService.lambdaQuery().eq(GoodsSpec::getGoodsId, goodsApplyAddUp.getGoodsId())
                .list();

        List<GoodsApplyItem> goodsApplyItemList = new ArrayList<>();

        Boolean ifStatus = true;

        for (GoodsSpec goodsSpec : goodsSpecs) {
            List<GoodsApplyItem> goodsApplyItems = goodsApplyAddUp.getGoodsApplyItems().stream()
                    .filter(item -> item.getGoodsSpecId().equals(goodsSpec.getGoodsSpecId()))
                    .collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(goodsApplyItems)) {
                goodsApplyItems.get(0).setGoodsId(goodsSpec.getGoodsId());
                goodsApplyItems.get(0).setGoodsApplyId(goodsApplyAddUp.getGoodsApplyId());
                goodsApplyItemList.add(goodsApplyItems.get(0));

                if (goodsSpec.getMinPrice().doubleValue() > goodsApplyItems.get(0).getApplyPrice().doubleValue() || goodsSpec.getMaxPrice().doubleValue() < goodsApplyItems.get(0).getApplyPrice().doubleValue())
                    ifStatus = false;
            }else {
                GoodsApplyItem goodsApplyItem = new GoodsApplyItem();
                goodsApplyItem.setGoodsId(goodsSpec.getGoodsId());
                goodsApplyItem.setApplyPrice(goodsSpec.getPrice());
                goodsApplyItem.setGoodsSpecId(goodsSpec.getGoodsSpecId());
                goodsApplyItem.setGoodsApplyId(goodsApplyAddUp.getGoodsApplyId());
                goodsApplyItemList.add(goodsApplyItem);

                if (goodsSpec.getMinPrice().doubleValue() > goodsApplyItem.getApplyPrice().doubleValue() || goodsSpec.getMaxPrice().doubleValue() < goodsApplyItem.getApplyPrice().doubleValue())
                    ifStatus = false;
            }
        }

        if (ifStatus) {
            goodsApplyAddUp.setStatus(ExamineStatusEnum.OK.getKey());
        }else {
            goodsApplyAddUp.setStatus(ExamineStatusEnum.WAIT.getKey());
            goodsApplyAddUp.updateById();

            goodsApplyService.lambdaUpdate()
                    .set(GoodsApply::getApproveName,null)
                    .set(GoodsApply::getApproveRemark,null)
                    .set(GoodsApply::getApproveTime,null)
                    .eq(GoodsApply::getGoodsApplyId,goodsApplyAddUp.getGoodsApplyId())
                    .update();
        }


        goodsApplyItemService.saveBatch(goodsApplyItemList);
    }

    public void approve(String goodsApplyId, String status,String approveRemark) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        GoodsApply goodsApply = new GoodsApply();
        goodsApply.setGoodsApplyId(goodsApplyId);
        goodsApply.setStatus(status);
        goodsApply.setApproveRemark(approveRemark);
        goodsApply.setApproveName(adminUser.getNickName());
        goodsApply.setApproveTime(new Date());
        goodsApply.updateById();
    }

    public GoodsApplyCount countNum() {

        Integer dsp = goodsApplyService.lambdaQuery().
                eq(GoodsApply::getStatus, "WAIT")
                .count();
        Integer tg = goodsApplyService.lambdaQuery().
                eq(GoodsApply::getStatus, "OK")
                .count();

        Integer bh = goodsApplyService.lambdaQuery().
                eq(GoodsApply::getStatus, "FAIL")
                .count();

        GoodsApplyCount goodsApplyCount = new GoodsApplyCount();
        goodsApplyCount.setDsh(dsp);
        goodsApplyCount.setTg(tg);
        goodsApplyCount.setBh(bh);

        return goodsApplyCount;

    }
}
