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

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.count.*;
import com.gree.mall.manager.commonmapper.CountMapper;
import com.gree.mall.manager.enums.*;
import com.gree.mall.manager.enums.engin.EnginPayManageStatusEnum;
import com.gree.mall.manager.enums.material.*;
import com.gree.mall.manager.enums.workorder.SourceChannelEnum;
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.utils.CommonUtils;
import com.gree.mall.manager.utils.DateUtils;
import com.gree.mall.manager.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.java2d.pipe.SpanShapeRenderer;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CountLogic {

    @Autowired
    CommonLogic commonLogic;
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    OrderRefundService orderRefundService;
    @Autowired
    OrderShareService orderShareService;
    @Autowired
    UserService userService;
    @Autowired
    CountMapper countMapper;
    @Autowired
    UserVisitService userVisitService;

    @Autowired
    RpMaterialOrderService rpMaterialOrderService;

    @Autowired
    RpProjectRepairService rpProjectRepairService;

    @Autowired
    PgIncreService pgIncreService;

    @Autowired
    PgIncreOrderService pgIncreOrderService;

    @Autowired
    PgOrderBaseService pgOrderBaseService;

    @Autowired
    WebsitGoodsService websitGoodsService;

    @Autowired
    WebsitNormChargeService websitNormChargeService;

    @Autowired
    WebsitVenderService websitVenderService;

    @Autowired
    WebsitPurchaseService websitPurchaseService;

    @Autowired
    WebsitPurchaseRetService websitPurchaseRetService;


    @Autowired
    WebsitPriceUpdService websitPriceUpdService;

    @Autowired
    WebsitSalesService websitSalesService;

    @Autowired
    WebsitSalesRetService websitSalesRetService;

    @Autowired
    WebsitPartsRetService websitPartsRetService;

    @Autowired
    SettlementOrderService settlementOrderService;

    @Autowired
    WebsitSalesManagerService websitSalesManagerService;

    @Autowired
    SettlementOrderManualService settlementOrderManualService;

    @Autowired
    WithdrawalOrderService withdrawalOrderService;

    @Autowired
    WebsitGoodsAloneService websitGoodsAloneService;

    @Autowired
    EnginPayManageService enginPayManageService;

    /**
     * 概况统计
     */
    public CountBean info(HttpServletRequest request){
        AdminUserCom adminUser = commonLogic.getAdminUser(request);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //累计售出商品数量，已售中商品数量，待发货订单数量，维权中订单数量，今日订单数，昨日订单数，今日收益，昨日收益
        CountBean countBean = countMapper.countInfo(adminUser.getCompanyWechatIds());
        //今日访客
        Integer todayVisit = countMapper.countVisit(sdf.format(DateUtils.getStartTime()),sdf.format(DateUtils.getEndTime()),adminUser.getCompanyWechatIds());
        //昨日访客
        Integer yestodayVisit = countMapper.countVisit(sdf.format(DateUtils.getBeginDayOfYesterday()),sdf.format(DateUtils.getEndDayOfYesterDay()),adminUser.getCompanyWechatIds());
        //查询待结算金额
        List<OrderShare> list = orderShareService.lambdaQuery()
                .eq(OrderShare::getStatus, OrderShareStatusEnum.ING.toString())
                .in(CollectionUtils.isNotEmpty(adminUser.getCompanyWechatIds()),OrderShare::getCompanyWechatId,adminUser.getCompanyWechatIds())
                //.//in(CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds()),OrderShare::getWebsitId,adminUser.getAdminWebsitIds())
                .list();
        BigDecimal totalShareAmount = list.stream().map(OrderShare::getAmount).reduce(BigDecimal::add).orElse(new BigDecimal(0));
        countBean.setOrderShareAmount(totalShareAmount);
        countBean.setTodayVisit(todayVisit);
        countBean.setYestodayVisit(yestodayVisit);
        return countBean;
    }


    /**
     * 商品销售排行
     */
    public List<GoodsRankBean> countGoodsRank(Integer flag,HttpServletRequest request){

        AdminUserCom adminUserCom = commonLogic.getAdminUser(request);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //默认今天
        Date startTime = this.getStartTime(flag);
        Date endTime = this.getEndTime(flag);
        List<GoodsRankBean> goodsRankBeans = countMapper.countGoodsRank(sdf.format(startTime), sdf.format(endTime),
                adminUserCom.getCompanyWechatIds(),adminUserCom.getAdminWebsitIds());
        return goodsRankBeans;
    }


    /**
     * 交易概述
     */
    public CountOrderBean order(String type,Integer flag,HttpServletRequest request){
        AdminUserCom adminUserCom = commonLogic.getAdminUser(request);
        //默认今天
        Date startTime = this.getStartTime(flag);
        Date endTime = this.getEndTime(flag);
        CountOrderBean countOrderBean = countMapper.countSaleOrder(DateUtils.formatDate(startTime), DateUtils.formatDate(endTime), type,
                adminUserCom.getCompanyWechatIds(),adminUserCom.getAdminWebsitIds());
        //各时段统计
        List<OrderInfo> list = orderInfoService.lambdaQuery()
                .in(CollectionUtils.isNotEmpty(adminUserCom.getCompanyWechatIds()),OrderInfo::getCompanyWechatId,adminUserCom.getCompanyWechatIds())
                .notIn(OrderInfo::getOrderStatus,Arrays.asList(OrderStatusEnum.CLOSE.toString(),OrderStatusEnum.NOPAY.toString(),OrderStatusEnum.TIMEOUT.toString()))
                .between(OrderInfo::getCreateTime, startTime, endTime).list();
        SimpleDateFormat hh = new SimpleDateFormat("HH");
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        int[] day = new int[24];
        int[] week = new int[7];
        int[] month = new int[DateUtils.DateSubOfDay(DateUtils.getEndMonth(), DateUtils.getStartMonth()) + 1];
        for(OrderInfo orderInfo : list) {
            Date time = orderInfo.getCreateTime();
            if (flag == 1 || flag == 2) {
                int index = Integer.parseInt(hh.format(time));
                //天  按24小时维度 0-23
                day[index]++;
            }
            if (flag == 3) {
                int index = DateUtils.DateSubOfDay(time,startTime);
                //7天 按天维度 日不是从0号开始，所以-1
                week[index]++;
            }
            if (flag == 4) {
                int index = Integer.parseInt(dd.format(time));
                //月 按天维度 日不是从0号开始，所以-1
                month[index-1]++;
            }
        }
        if(flag == 1 || flag== 2) {
            countOrderBean.setCountAry(day);
            countOrderBean.setX(new String[24]);
        }
        if(flag == 3) {
            countOrderBean.setCountAry(week);
            countOrderBean.setX(getX(7,null));
        }
        if(flag == 4) {
            countOrderBean.setCountAry(month);
            countOrderBean.setX(getX(DateUtils.DateSubOfDay(DateUtils.getEndMonth(), DateUtils.getStartMonth()) + 1,DateUtils.getEndMonth()));
        }

        return countOrderBean;
    }


    /**
     * 师傅的销售金额统计
     */
    public List<Map<String,Object>> queryWorkAmount(String startTime,String endTime,HttpServletRequest request){
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<Map<String, Object>> maps = countMapper.countWorkAmount(startTime, endTime,adminUser.getCompanyWechatIds());
        return maps;
    }


    /**
     * 网点的销售金额统计
     */
    public WebsitSaleCountBean queryWebsitAmount(List<String> websitNumbers){
        //当月
        Map<String, Object> map = countMapper.countWebsitAmount(DateUtils.formatDate(DateUtils.getStartMonth()),DateUtils.formatDate(DateUtils.getEndMonth()),websitNumbers);
        //当日
        Map<String, Object> map2 = countMapper.countWebsitAmount(DateUtils.formatDate(DateUtils.getStartTime()),DateUtils.formatDate(DateUtils.getEndTime()),websitNumbers);

        WebsitSaleCountBean bean = new WebsitSaleCountBean();
        bean.setMonthNum((Integer)map.get("num"));
        bean.setMonthAmount(new BigDecimal(map.get("amount").toString()));
        bean.setDayNum((Integer)map2.get("num"));
        bean.setDayAmount(new BigDecimal(map2.get("amount").toString()));
        return bean;
    }



    private Date getStartTime(Integer flag){
        Date startTime = DateUtils.getStartTime();
        //昨天
        if(flag == 2){
            startTime = DateUtils.getTimeByDay(DateUtils.getStartTime(),-1);
        }
        //最近7天
        if(flag == 3 ){
            startTime = DateUtils.getTimeByDay(startTime,-6);
        }
        //本月
        if(flag == 4){
            startTime = DateUtils.getStartMonth();
        }
        return startTime;
    }

    private Date getEndTime(Integer flag){
        Date endTime = DateUtils.getEndTime();
        //昨天
        if(flag == 2){
            endTime =  DateUtils.getStartTime();
        }
        //最近7天
        if(flag == 3 ){
            endTime = DateUtils.getStartTime();
        }
        //本月
        if(flag == 4){
            endTime = DateUtils.getEndMonth();
        }
        return endTime;
    }


    private static String[] getX(int dayNum,Date date){
        if(date == null){
            date = new Date();
        }
        String[] x = new String[dayNum];
        for(int i = 0 ;i <dayNum;i++){
            Date timeByDay = DateUtils.getTimeByDay(date,i-dayNum+1);
            x[i] = DateUtils.formatDate1(timeByDay);
        }
        return x;
    }


    public RepairProjectCount repairProject(HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<RpMaterialOrder> list = rpMaterialOrderService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), RpMaterialOrder::getWebsitId,adminUser.getAdminWebsitIds())
                .select(RpMaterialOrder::getExamineStatus, RpMaterialOrder::getOrderId,RpMaterialOrder::getIsAllFee).list();

        List<RpProjectRepair> rpProjectRepairList = rpProjectRepairService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), RpProjectRepair::getCompanyWechatId, adminUser.getCompanyWechatIds())
                .select(RpProjectRepair::getStartTime,RpProjectRepair::getEndTime,RpProjectRepair::getId)
                .list();


        RepairProjectCount repairProjectCount = new RepairProjectCount();

        repairProjectCount.setAllProjectNum(rpProjectRepairList.size());
        repairProjectCount.setOnProjectNum(Convert.toInt(rpProjectRepairList.stream().filter(item -> item.getEndTime().after(new Date())).count()));
        repairProjectCount.setEndProjectNum(Convert.toInt(rpProjectRepairList.stream().filter(item -> item.getEndTime().before(new Date())).count()));

        repairProjectCount.setNoPriceWaitNum(Convert.toInt(list.stream().filter(item -> item.getIsAllFee().equals(IsYesNoEnum.NO.getKey())
            && item.getExamineStatus().equals(MaterialExamineStatusEnum.WAIT.getKey())
        ).count()));

        repairProjectCount.setNoPriceOKNum(Convert.toInt(list.stream().filter(item -> item.getIsAllFee().equals(IsYesNoEnum.NO.getKey())
                && item.getExamineStatus().equals(MaterialExamineStatusEnum.OK.getKey())
        ).count()));

        repairProjectCount.setNoPriceFailNum((Convert.toInt(list.stream().filter(item -> item.getIsAllFee().equals(IsYesNoEnum.NO.getKey())
                && item.getExamineStatus().equals(MaterialExamineStatusEnum.FAIL.getKey())
        ).count())));


        repairProjectCount.setNoPriceNoNum((Convert.toInt(list.stream().filter(item -> item.getIsAllFee().equals(IsYesNoEnum.NO.getKey())
                && item.getExamineStatus().equals(MaterialExamineStatusEnum.NO.getKey())
        ).count())));

        repairProjectCount.setNoPriceNoPaidNum((Convert.toInt(list.stream().filter(item -> item.getIsAllFee().equals(IsYesNoEnum.NO.getKey())
                && item.getExamineStatus().equals(MaterialExamineStatusEnum.PAID.getKey())
        ).count())));


        repairProjectCount.setNoPricePaidNum((Convert.toInt(list.stream().filter(item -> item.getIsAllFee().equals(IsYesNoEnum.NO.getKey())
                && item.getExamineStatus().equals(MaterialExamineStatusEnum.NO_PAID.getKey())
        ).count())));


        repairProjectCount.setPriceOrderNum((Convert.toInt(list.stream().filter(item -> item.getIsAllFee().equals(IsYesNoEnum.YES.getKey())
        ).count())));

        return repairProjectCount;

    }

    public IncreCount increCount(String startTime,String endTime,HttpServletRequest request) {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<PgIncre> pgIncreList = pgIncreService.lambdaQuery().in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), PgIncre::getCompanyWechatId, adminUser.getCompanyWechatIds())
                .select(PgIncre::getStatus, PgIncre::getId).list();

        IncreCount increCount = new IncreCount();

        increCount.setAllIncreNum(pgIncreList.size());
        increCount.setOnIncreNum(Convert.toInt(pgIncreList.stream().filter(item -> item.getStatus()).count()));
        increCount.setOffIncreNum(Convert.toInt(pgIncreList.stream().filter(item -> !item.getStatus()).count()));

        List<PgOrderBase> pgOrderBaseList = pgOrderBaseService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), PgOrderBase::getCompanyWechatId, adminUser.getCompanyWechatIds())
                .ne(PgOrderBase::getPgIncreId,"")
                .select(PgOrderBase::getId, PgOrderBase::getOrderStatus).list();

        increCount.setLpOrderNumber(pgOrderBaseList.size());


        List<PgIncreOrder> pgIncreOrderList = pgIncreOrderService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), PgIncreOrder::getWebsitId, adminUser.getAdminWebsitIds())
                .select(PgIncreOrder::getId, PgIncreOrder::getPayStatus, PgIncreOrder::getOrderChannel, PgIncreOrder::getFlag,PgIncreOrder::getRefundNo,PgIncreOrder::getServiceEndTime)
                .list();

        increCount.setWaitLineNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.SELF.getKey())
            && item.getFlag().equals(ExamineStatusEnum.WAIT.getKey())
        ).count()));

        increCount.setOkLineOrderNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.SELF.getKey())
                && item.getFlag().equals(ExamineStatusEnum.OK.getKey())
        ).count()));

        increCount.setFailLineOrderNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.SELF.getKey())
                && item.getFlag().equals(ExamineStatusEnum.FAIL.getKey())
        ).count()));


        increCount.setPaidOnLineOrderNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.ONLINE.getKey())
                && item.getPayStatus().equals(PayStatusEnum.PAID.getKey())
        ).count()));

        increCount.setWaitOnLineOrderNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.ONLINE.getKey())
                && item.getPayStatus().equals(PayStatusEnum.WAIT.getKey())
        ).count()));


        increCount.setRefundOnLineOrderNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.ONLINE.getKey())
                && !StringUtil.isEmpty(item.getRefundNo())
        ).count()));

        increCount.setEndOnLineOrderNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.ONLINE.getKey())
                && item.getServiceEndTime() != null && item.getPayStatus().equals(PayStatusEnum.PAID.getKey()) && StringUtil.isEmpty(item.getRefundNo())
                && item.getServiceEndTime().before(new Date())
        ).count()));

        increCount.setCancelOnLineOrderNum(Convert.toInt(pgIncreOrderList.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.ONLINE.getKey())
                && item.getPayStatus().equals(PayStatusEnum.CANCEL.getKey())
        ).count()));


        Date startBe = StringUtil.isEmpty(startTime)  ? DateUtil.beginOfDay(DateUtil.offsetDay(new Date(),-14)):DateUtil.parse(startTime);
        Date endBe =  StringUtil.isEmpty(endTime) ?  DateUtil.endOfDay(new Date()):DateUtil.parse(endTime);

        List<PgIncreOrder> pgIncreOrders = pgIncreOrderService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), PgIncreOrder::getWebsitId, adminUser.getAdminWebsitIds())
                .between(PgIncreOrder::getCreateTime,startBe,endBe)
                .select(PgIncreOrder::getId, PgIncreOrder::getPayStatus, PgIncreOrder::getOrderChannel, PgIncreOrder::getFlag,PgIncreOrder::getRefundNo,
                        PgIncreOrder::getServiceEndTime,PgIncreOrder::getCreateTime)
                .list();

        List<Integer> onLineList = new ArrayList<>();
        List<Integer> lineList = new ArrayList<>();

        Integer integer = 0;


        while (true){

            DateTime dateTime = DateUtil.offsetDay(startBe, integer);
            long lineCount = pgIncreOrders.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.SELF.getKey())
                    && item.getCreateTime().after(dateTime) && item.getCreateTime().before(DateUtil.endOfDay(dateTime))).count();


            long onLineCount = pgIncreOrders.stream().filter(item -> item.getOrderChannel().equals(SourceChannelEnum.ONLINE.getKey())
                    && item.getCreateTime().after(dateTime) && item.getCreateTime().before(DateUtil.endOfDay(dateTime))).count();

            integer++;

            onLineList.add(Convert.toInt(onLineCount));
            lineList.add(Convert.toInt(lineCount));

            if (DateUtil.beginOfDay(DateUtil.offsetDay(startBe,integer)).after(endBe)) {
                break;
            }
        }

        increCount.setOnLineList(onLineList);
        increCount.setLineList(lineList);

        return increCount;

    }

    public MaterialCount materialCount(HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        MaterialCount materialCount = new MaterialCount();

        List<WebsitGoods> websitGoods = websitGoodsService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), WebsitGoods::getCompanyWechatId, adminUser.getCompanyWechatId())
                .select(WebsitGoods::getGoodsId, WebsitGoods::getStatus, WebsitGoods::getGoodsType).list();


        List<WebsitNormCharge> websitNormCharges = websitNormChargeService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), WebsitNormCharge::getCompanyWechatId, adminUser.getCompanyWechatId())
                .select(WebsitNormCharge::getNormId, WebsitNormCharge::getStatus, WebsitNormCharge::getNormType).list();


        materialCount.setAllmGoodsNum(
               Convert.toInt(websitGoods.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey())
                ).count())
        );

        materialCount.setOnMGoodsNum(
                Convert.toInt(websitGoods.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey())
                        && item.getStatus().equals(StateEnum.ON.getKey())
                ).count())
        );

        materialCount.setOffMGoodsNum(
                Convert.toInt(websitGoods.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey())
                        && item.getStatus().equals(StateEnum.OFF.getKey())
                ).count())
        );


        materialCount.setAllmChargeGoodsNum(
                Convert.toInt(websitNormCharges.stream()
                .count())
        );

        materialCount.setOnmChargeGoodsNum(
                Convert.toInt(websitNormCharges.stream().filter(item ->
                         item.getStatus().equals(StateEnum.ON.getKey())
                ).count())
        );

        materialCount.setOffmChargeGoodsNum(
                Convert.toInt(websitNormCharges.stream().filter(item ->
                         item.getStatus().equals(StateEnum.OFF.getKey())
                ).count())
        );


        materialCount.setAllpGoodsNum(
                Convert.toInt(websitGoods.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey())
                ).count())
        );

        materialCount.setOnPGoodsNum(
                Convert.toInt(websitGoods.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey())
                        && item.getStatus().equals(StateEnum.ON.getKey())
                ).count())
        );

        materialCount.setOffPGoodsNum(
                Convert.toInt(websitGoods.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey())
                        && item.getStatus().equals(StateEnum.OFF.getKey())
                ).count())
        );


        List<WebsitVender> websitVenders = websitVenderService.lambdaQuery()
                .in(!StringUtil.isEmpty(adminUser.getCompanyWechatId()), WebsitVender::getCompanyWechatId, adminUser.getCompanyWechatId())
                .select(WebsitVender::getStatus, WebsitVender::getVenderId).list();

        materialCount.setAllVendNum(
                Convert.toInt(websitVenders.size())
        );

        materialCount.setOnVendNum(
                Convert.toInt(websitVenders.stream().filter(item ->
                        item.getStatus().equals(StateEnum.ON.getKey())
                ).count())
        );

        materialCount.setOffVendNum(
                Convert.toInt(websitVenders.stream().filter(item ->
                        item.getStatus().equals(StateEnum.OFF.getKey())
                ).count())
        );

        List<WebsitPurchase> websitPurchases = websitPurchaseService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), WebsitPurchase::getWebsitId, adminUser.getAdminWebsitIds())
                .select(WebsitPurchase::getFlag, WebsitPurchase::getPurchaseId,WebsitPurchase::getGoodsType).list();

        materialCount.setSavePurchaseNum(
                Convert.toInt(websitPurchases.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setOkPurchaseNum(
                Convert.toInt(websitPurchases.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        materialCount.setFailPurchaseNum(
                Convert.toInt(websitPurchases.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.FAIL.getKey())
                ).count())
        );



        materialCount.setSavePurchasePNum(
                Convert.toInt(websitPurchases.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setOkPurchasePNum(
                Convert.toInt(websitPurchases.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        materialCount.setFailPurchasePNum(
                Convert.toInt(websitPurchases.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.FAIL.getKey())
                ).count())
        );


        List<WebsitPurchaseRet> websitPurchaseRets = websitPurchaseRetService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), WebsitPurchaseRet::getWebsitId, adminUser.getAdminWebsitIds())
                .select(WebsitPurchaseRet::getFlag, WebsitPurchaseRet::getPurchaseId, WebsitPurchaseRet::getGoodsType).list();


        materialCount.setSavePurchaseRetNum(
                Convert.toInt(websitPurchaseRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setOkPurchaseRetNum(
                Convert.toInt(websitPurchaseRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        materialCount.setFailPurchaseRetNum(
                Convert.toInt(websitPurchaseRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.FAIL.getKey())
                ).count())
        );

        materialCount.setSavePurchaseRetPNum(
                Convert.toInt(websitPurchaseRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setOkPurchaseRetPNum(
                Convert.toInt(websitPurchaseRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        materialCount.setFailPurchaseRetPNum(
                Convert.toInt(websitPurchaseRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.FAIL.getKey())
                ).count())
        );

        List<WebsitPriceUpd> websitPriceUpds = websitPriceUpdService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), WebsitPriceUpd::getWebsitId, adminUser.getAdminWebsitIds())
                .select(WebsitPriceUpd::getFlag, WebsitPriceUpd::getSheetId, WebsitPriceUpd::getGoodsType).list();

        materialCount.setSavePriceUpdNum(
                Convert.toInt(websitPriceUpds.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );


        materialCount.setOkPriceUpdNum(
                Convert.toInt(websitPriceUpds.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        List<WebsitSales> websitSales = websitSalesService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), WebsitSales::getWebsitId, adminUser.getAdminWebsitIds())
                .select(WebsitSales::getFlag, WebsitSales::getSalesId, WebsitSales::getGoodsType).list();


        materialCount.setSaveSalesNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );


        materialCount.setSubmitSalesNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SUBMIT.getKey())
                ).count())
        );

        materialCount.setPayNoTakeSalesNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(MaterialSalesFlagEnum.PAY_NOT_TAKE.getKey())
                ).count())
        );

        materialCount.setPaytakeSalesNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(MaterialSalesFlagEnum.PAY_TAKE.getKey())
                ).count())
        );

        materialCount.setCancelSalesNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getFlag().equals(MaterialSalesFlagEnum.CANCEL.getKey())
                ).count())
        );


        materialCount.setSaveSalesPNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );


        materialCount.setSubmitSalesPgNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SUBMIT.getKey())
                ).count())
        );

        materialCount.setPayNoTakeSalesPgNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(MaterialSalesFlagEnum.PAY_NOT_TAKE.getKey())
                ).count())
        );

        materialCount.setPayTakeSalesPgNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(MaterialSalesFlagEnum.PAY_TAKE.getKey())
                ).count())
        );

        materialCount.setCancelSalesPgNum(
                Convert.toInt(websitSales.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getFlag().equals(MaterialSalesFlagEnum.CANCEL.getKey())
                ).count())
        );

        List<WebsitSalesRet> websitSalesRets = websitSalesRetService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), WebsitSalesRet::getWebsitId, adminUser.getAdminWebsitIds())
                .select(WebsitSalesRet::getRetState, WebsitSalesRet::getSalesId, WebsitSalesRet::getGoodsType).list();


        materialCount.setSaveSalesRetNum(
                Convert.toInt(websitSalesRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getRetState().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setSubmitSalesRetNum(
                Convert.toInt(websitSalesRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getRetState().equals(ExamineStatusEnum.SUBMIT.getKey())
                ).count())
        );

        materialCount.setOkSalesRetNum(
                Convert.toInt(websitSalesRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getRetState().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        materialCount.setWaitSalesRetNum(
                Convert.toInt(websitSalesRets.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getRetState().equals(ExamineStatusEnum.WAIT.getKey())
                ).count())
        );


        List<WebsitPartsRet> websitPartsRets = websitPartsRetService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), WebsitPartsRet::getWebsitId, adminUser.getAdminWebsitIds())
                .select(WebsitPartsRet::getFlag, WebsitPartsRet::getPartsRetId, WebsitPartsRet::getGoodsType,WebsitPartsRet::getType).list();


        materialCount.setSaveNewNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.NEW.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setSubmitNewNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.NEW.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SUBMIT.getKey())
                ).count())
        );

        materialCount.setOkNewNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.NEW.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        materialCount.setSaveOldNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.OLD.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setSubmitOldNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.OLD.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SUBMIT.getKey())
                ).count())
        );

        materialCount.setOkOldNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.OLD.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );


        materialCount.setOkOldFactoryNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.FACTORY.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.OK.getKey())
                ).count())
        );

        materialCount.setSaveOldFactoryNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.FACTORY.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SAVE.getKey())
                ).count())
        );

        materialCount.setSubmitOldFactoryNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.FACTORY.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.SUBMIT.getKey())
                ).count())
        );

        materialCount.setFailOldFactoryNum(
                Convert.toInt(websitPartsRets.stream().filter(item -> item.getType().equals(PartsAttrEnum.FACTORY.getKey()) &&
                        item.getFlag().equals(ExamineStatusEnum.FAIL.getKey())
                ).count())
        );

        List<SettlementOrder> settlementOrders = settlementOrderService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), SettlementOrder::getWebsitId, adminUser.getAdminWebsitIds())
                .select(SettlementOrder::getStatus, SettlementOrder::getPayStatus, SettlementOrder::getSettlementOrderId, SettlementOrder::getPayType).list();
        materialCount.setWaitWechatNumOne(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(ExamineStatusEnum.WAIT.getKey())
                ).count())
        );

        materialCount.setOkWechatNumOne(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(SettlementStatusNewEnum.OK.getKey())
                ).count())
        );

        materialCount.setYcWechatNumOne(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(SettlementStatusNewEnum.YC.getKey())
                ).count())
        );

        materialCount.setLineWechatNumOne(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(SettlementStatusNewEnum.LINE.getKey())
                ).count())
        );





        materialCount.setWaitWechatNum(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(ExamineStatusEnum.WAIT.getKey())
                ).count())
        );

        materialCount.setWaitingWechatNum(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(SettlementStatusEnum.WAIT_ING.getKey())
                ).count())
        );

        materialCount.setIngWechatNum(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(SettlementStatusEnum.ING.getKey())
                ).count())
        );

        materialCount.setOverWechatNum(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.WECHAT.getKey()) &&
                        item.getStatus().equals(SettlementStatusEnum.OVER.getKey())
                ).count())
        );

        materialCount.setWaitWechatLineNum(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.LINE.getKey()) &&
                        item.getStatus().equals(SettlementStatusEnum.WAIT.getKey())
                ).count())
        );

        materialCount.setOverWechatLineNum(
                Convert.toInt(settlementOrders.stream().filter(item -> item.getPayType().equals(PayTypeEnum.LINE.getKey()) &&
                        item.getStatus().equals(SettlementStatusEnum.OVER.getKey())
                ).count())
        );

        List<WebsitSalesManager> websitSalesManagers = websitSalesManagerService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), WebsitSalesManager::getCompanyWechatId, adminUser.getCompanyWechatIds())
                .select(WebsitSalesManager::getStatus, WebsitSalesManager::getWebsitSalesManagerId, WebsitSalesManager::getGoodsType).list();

        materialCount.setWaitSalesManagerPNum(
                Convert.toInt(websitSalesManagers.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getStatus().equals(SettlementStatusEnum.WAIT.getKey())
                ).count())
        );

        materialCount.setNoSalesManagerPNum(
                Convert.toInt(websitSalesManagers.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getStatus().equals(ExamineStatusManagerEnum.NO.getKey())
                ).count())
        );

        materialCount.setOkSalesManagerPNum(
                Convert.toInt(websitSalesManagers.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getStatus().equals(ExamineStatusManagerEnum.OK.getKey())
                ).count())
        );

        materialCount.setWaitSalesManagerNum(
                Convert.toInt(websitSalesManagers.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getStatus().equals(SettlementStatusEnum.WAIT.getKey())
                ).count())
        );

        materialCount.setNoSalesManagerNum(
                Convert.toInt(websitSalesManagers.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getStatus().equals(ExamineStatusManagerEnum.NO.getKey())
                ).count())
        );

        materialCount.setOkSalesManagerNum(
                Convert.toInt(websitSalesManagers.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getStatus().equals(ExamineStatusManagerEnum.OK.getKey())
                ).count())
        );

        List<SettlementOrderManual> settlementOrderManuals = settlementOrderManualService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), SettlementOrderManual::getWebsitId, adminUser.getAdminWebsitIds())
                .select(SettlementOrderManual::getStatus, SettlementOrderManual::getSettlementOrderId, SettlementOrderManual::getGoodsType).list();


        materialCount.setWaitManualNum(
                Convert.toInt(settlementOrderManuals.stream().filter(item ->
                        item.getStatus().equals(ExamineStatusManagerEnum.WAIT.getKey())
                ).count())
        );

        materialCount.setOkManual(
                Convert.toInt(settlementOrderManuals.stream().filter(item ->
                        item.getStatus().equals(SettlementStatusEnum.OVER.getKey())
                ).count())
        );

        List<WithdrawalOrder> withdrawalOrders = withdrawalOrderService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), WithdrawalOrder::getCompanyWechatId, adminUser.getCompanyWechatIds())
                .select(WithdrawalOrder::getStatus, WithdrawalOrder::getId).list();

        materialCount.setWaitWithdrawalNum(
                Convert.toInt(withdrawalOrders.stream().filter(item ->
                        item.getStatus().equals(0)
                ).count())
        );

        materialCount.setOkWithdrawalNum(
                Convert.toInt(withdrawalOrders.stream().filter(item ->
                        item.getStatus().equals(1)
                ).count())
        );

        List<WebsitGoodsAlone> websitGoodsAlones = websitGoodsAloneService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getCompanyWechatIds()), WebsitGoodsAlone::getCompanyWechatId, adminUser.getCompanyWechatIds())
                .select(WebsitGoodsAlone::getGoodsId, WebsitGoodsAlone::getGoodsType, WebsitGoodsAlone::getStatus).list();

        materialCount.setAllAloneNum(
                Convert.toInt(websitGoodsAlones.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey())
                ).count())
        );

        materialCount.setOnAloneNum(
                Convert.toInt(websitGoodsAlones.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getStatus().equals(StateEnum.ON.getKey())

                ).count())
        );

        materialCount.setOffAloneNum(
                Convert.toInt(websitGoodsAlones.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.M.getKey()) &&
                        item.getStatus().equals(StateEnum.OFF.getKey())

                ).count())
        );

        materialCount.setAllAlonePNum(
                Convert.toInt(websitGoodsAlones.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey())
                ).count())
        );

        materialCount.setOnAlonePNum(
                Convert.toInt(websitGoodsAlones.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getStatus().equals(StateEnum.ON.getKey())

                ).count())
        );

        materialCount.setOffAlonePNum(
                Convert.toInt(websitGoodsAlones.stream().filter(item -> item.getGoodsType().equals(WebsitGoodsTypeEnum.P.getKey()) &&
                        item.getStatus().equals(StateEnum.OFF.getKey())

                ).count())
        );

        List<EnginPayManage> enginPayManages = enginPayManageService.lambdaQuery()
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), EnginPayManage::getWebsitId, adminUser.getAdminWebsitIds())
                .select(EnginPayManage::getId, EnginPayManage::getGoodsType, EnginPayManage::getStatus).list();

        materialCount.setWaitPayManagerNum(
                Convert.toInt(enginPayManages.stream().filter(item ->
                        item.getStatus().equals(ExamineStatusEnum.WAIT.getKey())

                ).count())
        );

        materialCount.setWaitPAYPayManagerNum(
                Convert.toInt(enginPayManages.stream().filter(item ->
                        item.getStatus().equals(EnginPayManageStatusEnum.WAIT_PAY.getKey())

                ).count())
        );

        materialCount.setPartpayPayManagerNum(
                Convert.toInt(enginPayManages.stream().filter(item ->
                        item.getStatus().equals(EnginPayManageStatusEnum.PART_PAY.getKey())

                ).count())
        );

        materialCount.setPayPayManagerNum(
                Convert.toInt(enginPayManages.stream().filter(item ->
                        item.getStatus().equals(EnginPayManageStatusEnum.PAY.getKey())

                ).count())
        );

        return materialCount;
    }
}
