package com.fjhx.form.service.impl;

import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.service.account.AccountRunningWaterService;
import com.fjhx.common.enums.FlowStatusEnum1;
import com.fjhx.flow.entity.flow.po.FlowExample;
import com.fjhx.flow.service.flow.FlowExampleService;
import com.fjhx.form.mapper.StatisticsMapper;
import com.fjhx.form.service.StatisticsService;
import com.fjhx.mes.entity.production.po.ProductionOrder;
import com.fjhx.mes.service.production.ProduceOrderService;
import com.fjhx.sale.entity.contract.vo.ContractVo;
import com.fjhx.sale.entity.purchase.vo.EhsdPurchaseVo;
import com.fjhx.sale.entity.sale.po.SaleQuotation;
import com.fjhx.sale.service.sale.SaleQuotationService;
import com.fjhx.wms.entity.stock.dto.StockSelectDto;
import com.fjhx.wms.entity.stock.dto.StockWaitSelectDto;
import com.fjhx.wms.service.stock.StockService;
import com.fjhx.wms.service.stock.StockWaitDetailsService;
import com.fjhx.wms.service.stock.StockWaitService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private FlowExampleService flowExampleService;
    @Autowired
    private AccountRunningWaterService accountRunningWaterService;
    @Autowired
    private StatisticsMapper statisticsMapper;
    @Autowired
    private SaleQuotationService saleQuotationService;
    @Autowired
    private ProduceOrderService produceOrderService;
    @Autowired
    private StockService stockService;
    @Autowired
    private StockWaitDetailsService stockWaitDetailsService;
    @Autowired
    private StockWaitService stockWaitService;

    /**
     * 工作统计
     */
    @Override
    public Map<String, Long> workStatistics() {
        Long userId = SecurityUtils.getUserId();

        //权限过滤：子公司看自己的,总公司看全部
        List<Long> companyIds = SecurityUtils.getCompanyIds();

        //是否是总公司
        boolean isHeadOffice = companyIds.contains(100L);

        //统计待审批数据
        long waitFlowCount = flowExampleService.count(q -> q.in(FlowExample::getStatus, 1, 0).eq(FlowExample::getHandleUserId, userId));
        //待报价 总公司对内报价单 子公司 报价评估
        long waitQuotationCount = saleQuotationService.count(q -> q
                //总公司
                .in(SaleQuotation::getCompanyId, companyIds)
                .ne(SaleQuotation::getStatus, FlowStatusEnum1.UPDATE.getKey())
                //子公司
                .ne(!isHeadOffice, SaleQuotation::getStatus, FlowStatusEnum1.DRAFT.getKey())
        );
        //待投产
        long waitProduceCount = produceOrderService.count(q -> q
                .isNull(ProductionOrder::getProduceTime)
                .in(!isHeadOffice, ProductionOrder::getCompanyId, companyIds)
        );
        //待入库
        StockWaitSelectDto stockWaitSelectDto = new StockWaitSelectDto();
        stockWaitSelectDto.setType(1);
        long waitInStockCount = stockWaitService.getPage(stockWaitSelectDto).getTotal();
        //待出库
        StockWaitSelectDto stockWaitSelectDto1 = new StockWaitSelectDto();
        stockWaitSelectDto1.setType(2);
        long waitOutStockCount = stockWaitService.getPage(stockWaitSelectDto1).getTotal();
        //到账认领待认领计数-总公司
        long waitClaimCount = accountRunningWaterService.count(q -> q
                .eq(AccountRunningWater::getReceived, 10)
                .eq(AccountRunningWater::getIsClaim, 0)
                .in(!isHeadOffice, AccountRunningWater::getCompanyId, companyIds)
        );
        //库存预警-子公司看
        long stockWarnCount = stockService.stockWarningPage(new StockSelectDto()).getTotal();

        //总数
        long sumCount = waitFlowCount + waitQuotationCount + waitProduceCount + waitInStockCount + waitOutStockCount;

        if (isHeadOffice) {
            sumCount += waitClaimCount;//总公司待认领
        } else {
            sumCount += stockWarnCount;//子公司库存预警
        }

        //输出结果
        Map<String, Long> map = new HashMap<>();
        map.put("waitFlowCount", waitFlowCount);
        map.put("waitQuotationCount", waitQuotationCount);
        map.put("waitProduceCount", waitProduceCount);
        map.put("waitInStockCount", waitInStockCount);
        map.put("waitOutStockCount", waitOutStockCount);
        map.put("waitClaimCount", waitClaimCount);
        map.put("stockWarnCount", stockWarnCount);
        map.put("sumCount", sumCount);

        return map;
    }

    @Override
    public List<ContractVo> getRecentContractShipmentList() {
        IWrapper<Object> wrapper = IWrapper.getWrapper();

        //权限过滤：近期出货合同
        wrapper.in("c.company_id", SecurityUtils.getCompanyIds());


        wrapper.eq("c.status", 30);
        wrapper.ge("DATEDIFF(c.delivery_time,NOW())", 0);
        wrapper.le("DATEDIFF(c.delivery_time,NOW())", 7);
        List<ContractVo> contractSaitShipmentList = statisticsMapper.getRecentContractShipmentList(wrapper);
        return contractSaitShipmentList;
    }

    @Override
    public List<EhsdPurchaseVo> getRecentPurchaseArrivalList() {
        IWrapper<Object> wrapper = IWrapper.getWrapper();

        //权限过滤：近期出货合同
        wrapper.in("p.company_id", SecurityUtils.getCompanyIds());


        wrapper.ge("DATEDIFF(p.delivery_time,NOW())", 0);
        wrapper.le("DATEDIFF(p.delivery_time,NOW())", 7);
        wrapper.eq("p.status", 30);
        List<EhsdPurchaseVo> contractSaitShipmentList = statisticsMapper.getRecentPurchaseArrivalList(wrapper);
        return contractSaitShipmentList;
    }
}
