package com.gree.mall.miniapp.logic.engin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.gree.mall.miniapp.enums.ExamineStatusEnum;
import com.gree.mall.miniapp.enums.ExamineStatusManagerEnum;
import com.gree.mall.miniapp.enums.MaterialSalesFlagEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.plus.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class WebsitGoodsMangerLogic {

    @Autowired
    CommonLogic commonLogic;



    @Autowired
    WebsitSalesManagerService websitSalesManagerService;

    @Autowired
    OrderEnginBaseService orderEnginBaseService;

    @Autowired
    WebsitSalesManagerPriceService websitSalesManagerPriceService;

    @Autowired
    WebsitSalesService websitSalesService;

    @Autowired
    WebsitSalesRetService websitSalesRetService;



    @Transactional(rollbackFor = Exception.class)
    public void givePrice(BigDecimal price, String orderEnginBaseId, String urlPrc, String remark,String orderId,String goodsType) {
        WebsitSalesManagerPrice websitSalesManagerPrice = new WebsitSalesManagerPrice();
        websitSalesManagerPrice.setPrice(price);
        websitSalesManagerPrice.setOrderId(orderId);
        websitSalesManagerPrice.setOrderEnginBaseId(orderEnginBaseId);
        websitSalesManagerPrice.setUrlPrc(urlPrc);
        websitSalesManagerPrice.setRemark(remark);
        websitSalesManagerPrice.setGoodsType(goodsType);
        websitSalesManagerPrice.insert();
        this.upManage(orderEnginBaseId, goodsType);

        WebsitSalesManager websitSalesManager = websitSalesManagerService.lambdaQuery().eq(WebsitSalesManager::getOrderEnginBaseId, orderEnginBaseId)
                .eq(WebsitSalesManager::getGoodsType,goodsType)
                .one();

        if (websitSalesManager.getStatus().equals(ExamineStatusManagerEnum.OK.getKey())) {
            websitSalesManagerPrice.setStatus(ExamineStatusManagerEnum.OK.getKey());
            websitSalesManagerPrice.updateById();
        }

    }


    /**
    * 汇总销售金额
     * salesId 销售单id
     * goodsType 辅材配件类型
    * */
    @Transactional(rollbackFor = Exception.class)
    public void upManage(String orderEnginBaseId,String goodsType) {


        OrderEnginBase orderEnginBase = orderEnginBaseService.getById(orderEnginBaseId);

        WebsitSalesManager websitSalesManager = websitSalesManagerService.lambdaQuery()
                .eq(WebsitSalesManager::getOrderEnginBaseId, orderEnginBase.getId())
                .eq(WebsitSalesManager::getGoodsType,goodsType)
                .one();

        if (websitSalesManager == null){

            websitSalesManager = new WebsitSalesManager();

            websitSalesManager.setCompanyWechatId(orderEnginBase.getCompanyWechatId());
            websitSalesManager.setCompanyWechatName(orderEnginBase.getCompanyWechatName());
            websitSalesManager.setOrderEnginBaseId(orderEnginBaseId);
            websitSalesManager.setGoodsType(goodsType);

        }

        websitSalesManager.setSalesNum(websitSalesService.lambdaQuery().eq(WebsitSales::getOrderEnginBaseId,orderEnginBase.getId())
                .notIn(WebsitSales::getFlag, MaterialSalesFlagEnum.SAVE.getKey(),MaterialSalesFlagEnum.CANCEL.getKey())
                .eq(WebsitSales::getGoodsType, goodsType)
                .count());
        websitSalesManager.setSalesPrice(
                websitSalesService.lambdaQuery().eq(WebsitSales::getOrderEnginBaseId,orderEnginBaseId)
                        .eq(WebsitSales::getGoodsType, goodsType)
                        .notIn(WebsitSales::getFlag, MaterialSalesFlagEnum.SAVE.getKey(),MaterialSalesFlagEnum.CANCEL.getKey()).list()
                        .stream().map(WebsitSales::getTotalAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO)
        );

        websitSalesManager.setRetNum(websitSalesRetService.lambdaQuery()
                .eq(WebsitSalesRet::getOrderEnginBaseId,orderEnginBaseId)
                .eq(WebsitSalesRet::getGoodsType, goodsType)
                .notIn(WebsitSalesRet::getRetState, MaterialSalesFlagEnum.SAVE.getKey(),MaterialSalesFlagEnum.CANCEL.getKey()).count());
        websitSalesManager.setRetPrice(
                websitSalesRetService.lambdaQuery().eq(WebsitSalesRet::getOrderEnginBaseId,orderEnginBaseId)
                        .eq(WebsitSalesRet::getGoodsType, goodsType)
                        .eq(WebsitSalesRet::getRetState, ExamineStatusEnum.OK.getKey()).list()
                        .stream().map(WebsitSalesRet::getRetAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO)
        );


        websitSalesManager.setRetPrice(websitSalesManager.getRetPrice() != null?websitSalesManager.getRetPrice():new BigDecimal("0"));
        websitSalesManager.setPrice(websitSalesManager.getSalesPrice().subtract(websitSalesManager.getRetPrice()));
        websitSalesManager.setGivePrice(
                websitSalesManagerPriceService.lambdaQuery().eq(WebsitSalesManagerPrice::getOrderEnginBaseId,orderEnginBaseId)
                        .eq(WebsitSalesManagerPrice::getGoodsType, goodsType)
                        .list()
                        .stream().map(WebsitSalesManagerPrice::getPrice).reduce(BigDecimal::add).orElse(BigDecimal.ZERO)
        );
        websitSalesManager.setWaitPrice(websitSalesManager.getPrice().subtract(websitSalesManager.getGivePrice()));

        websitSalesManager.setStatus(websitSalesManager.getWaitPrice().compareTo(new BigDecimal("0")) == 0 ? ExamineStatusManagerEnum.OK.getKey():
                websitSalesManager.getGivePrice().compareTo(new BigDecimal("0")) == 0  ?
                        ExamineStatusManagerEnum.NO.getKey():ExamineStatusManagerEnum.WAIT.getKey());

        websitSalesManager.insertOrUpdate();

    }
}
