package com.fjhx.from.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.account.entity.account.po.AccountPayment;
import com.fjhx.account.entity.account.po.AccountRunningWater;
import com.fjhx.account.service.account.AccountPaymentService;
import com.fjhx.account.service.account.AccountRunningWaterService;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.flow.entity.flow.po.FlowExample;
import com.fjhx.flow.service.flow.FlowExampleService;
import com.fjhx.from.service.StatisticsService;
import com.fjhx.mail.entity.enterprise.po.EnterpriseMailbox;
import com.fjhx.mail.entity.enterprise.po.EnterpriseMessage;
import com.fjhx.mail.entity.personal.po.PersonalMailbox;
import com.fjhx.mail.entity.personal.po.PersonalMessage;
import com.fjhx.mail.service.enterprise.EnterpriseMailboxService;
import com.fjhx.mail.service.enterprise.EnterpriseMessageService;
import com.fjhx.mail.service.personal.PersonalMailboxService;
import com.fjhx.mail.service.personal.PersonalMessageService;
import com.fjhx.purchase.entity.subscribe.po.SubscribeDetail;
import com.fjhx.purchase.service.subscribe.SubscribeDetailService;
import com.fjhx.sale.entity.contract.po.ContractProduct;
import com.fjhx.sale.entity.sample.po.Sample;
import com.fjhx.sale.mapper.contract.ContractProductMapper;
import com.fjhx.sale.mapper.sample.SampleMapper;
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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private PersonalMailboxService personalMailboxService;
    @Autowired
    private PersonalMessageService personalMessageService;
    @Autowired
    private EnterpriseMailboxService enterpriseMailboxService;
    @Autowired
    private EnterpriseMessageService enterpriseMessageService;
    @Autowired
    private FlowExampleService flowExampleService;
    @Autowired
    private AccountRunningWaterService accountRunningWaterService;
    @Autowired
    private SubscribeDetailService subscribeDetailService;
    @Autowired
    private SampleMapper sampleMapper;
    @Autowired
    private ContractProductMapper contractProductMapper;
    @Autowired
    private AccountPaymentService accountPaymentService;

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

        //统计未读邮件数
        Long unreadMailCount = 0L;
        List<Long> mailboxIds = personalMailboxService.listObject(PersonalMailbox::getId, q -> q.eq(PersonalMailbox::getUserId, userId));
        if (ObjectUtil.isNotEmpty(mailboxIds)) {
            unreadMailCount += personalMessageService.count(q -> q
                    .in(PersonalMessage::getMailboxId, mailboxIds)
            );
        }
        List<Long> eMailboxIds = enterpriseMailboxService.listObject(EnterpriseMailbox::getId, q -> q.eq(EnterpriseMailbox::getUserId, userId));
        if (ObjectUtil.isNotEmpty(eMailboxIds)) {
            unreadMailCount += enterpriseMessageService.count(q -> q
                    .in(EnterpriseMessage::getMailboxId, eMailboxIds)
            );
        }
        //统计待审批数据
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        long waitFlowCount = flowExampleService.count(q -> q.eq(FlowExample::getStatus, 2).eq(FlowExample::getHandleUserId, userId));
        DynamicDataSourceContextHolder.poll();
        //到账认领计数
        long claimCount = accountRunningWaterService.count(q -> q.eq(AccountRunningWater::getReceived, 10).ne(AccountRunningWater::getIsClaim, 1));
        //待采购数量
        long waitPurchaseCount = subscribeDetailService.count(q -> q.in(SubscribeDetail::getStatus, Arrays.asList(15, 30)));
        //样品交接单
        Page<Object> page = new Page<>();
        page.setCurrent(1);
        page.setSize(1);
        IWrapper<Sample> wrapper = IWrapper.getWrapper();
        wrapper.eq("t2.`status`", 30);
        wrapper.ge("t1.expend_quantity", 0);
        DynamicDataSourceContextHolder.push(SourceConstant.SALE);
        long sampleHandoverCount = sampleMapper.sampleHandoverList(page, wrapper).getTotal();
        DynamicDataSourceContextHolder.poll();
        //合同样品单
        IWrapper<ContractProduct> wrapper1 = IWrapper.getWrapper();
        wrapper.eq("t2.`status`", 30);
        wrapper.ge("t1.expend_quantity", 0);
        DynamicDataSourceContextHolder.push(SourceConstant.SALE);
        long contractHandoverCount = contractProductMapper.contractHandoverPage(page, wrapper1).getTotal();
        DynamicDataSourceContextHolder.poll();
        //待打款
        long waitAccountPayCount = accountPaymentService.count(q -> q.ne(AccountPayment::getStatus, 10));

        //总数
        long sumCount = unreadMailCount + waitFlowCount + claimCount + waitPurchaseCount + sampleHandoverCount + contractHandoverCount + waitAccountPayCount;

        //输出结果
        Map<String, Long> map = new HashMap<>();
        map.put("unreadMailCount", unreadMailCount);
        map.put("waitFlowCount", waitFlowCount);
        map.put("claimCount", claimCount);
        map.put("waitPurchaseCount", waitPurchaseCount);
        map.put("sampleHandoverCount", sampleHandoverCount);
        map.put("contractHandoverCount", contractHandoverCount);
        map.put("waitAccountPayCount", waitAccountPayCount);
        map.put("sumCount", sumCount);


        return map;
    }
}
