package com.fjhx.form.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.form.entity.EmailCount;
import com.fjhx.form.entity.EmployeeProductivity;
import com.fjhx.form.mapper.EmployeeProductivityMapper;
import com.fjhx.form.service.EmployeeProductivityService;
import com.fjhx.sale.service.contract.ContractService;
import com.ruoyi.common.core.domain.BaseSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmployeeProductivityServiceImpl implements EmployeeProductivityService {

    @Autowired
    private EmployeeProductivityMapper employeeProductivityMapper;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ContractService contractService;

    @Override
    public List<EmployeeProductivity> getList(BaseSelectDto dto) {
        //获取业务员列表
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        List<EmployeeProductivity> salesmanList = employeeProductivityMapper.getList(IWrapper.getWrapper()
                .eq("sr.role_key", "salesman"), dto
        );
        DynamicDataSourceContextHolder.poll();
        if (ObjectUtil.isEmpty(salesmanList)) {
            return salesmanList;
        }
        List<Long> userIds = salesmanList.stream().map(EmployeeProductivity::getUserId).collect(Collectors.toList());

        //获取收邮件数列表
//        DynamicDataSourceContextHolder.push(SourceConstant.MAIL);
        List<EmailCount> receiveEmailCount = employeeProductivityMapper.getUserEmailCount(IWrapper.getWrapper()
                .in("t1.user_id", userIds)
                .ge("t1.send_date", dto.getBeginTime())
                .le("t1.send_date", dto.getEndTime())
                .apply("t1.from_email != t1.mail_user")
                .groupBy("t1.user_id")
        );
        //获取发邮件数列表
        List<EmailCount> sendEmailCount = employeeProductivityMapper.getUserEmailCount(IWrapper.getWrapper()
                .in("t1.user_id", userIds)
                .ge("t1.send_date", dto.getBeginTime())
                .le("t1.send_date", dto.getEndTime())
                .apply("t1.from_email = t1.mail_user")
                .groupBy("t1.user_id")
        );
//        DynamicDataSourceContextHolder.poll();

        Map<Long, Integer> receiveEmailCountMap = receiveEmailCount.stream().collect(Collectors.toMap(EmailCount::getUserId, EmailCount::getCount));
        Map<Long, Integer> sendEmailCountMap = sendEmailCount.stream().collect(Collectors.toMap(EmailCount::getUserId, EmailCount::getCount));

        //赋值数据
        for (EmployeeProductivity employeeProductivity : salesmanList) {
            Long userId = employeeProductivity.getUserId();
            //赋值发件数
            employeeProductivity.setSendEmailCount(sendEmailCountMap.getOrDefault(userId, 0));
            //赋值收件数
            employeeProductivity.setReceiveEmailCount(receiveEmailCountMap.getOrDefault(userId, 0));
        }
        return salesmanList;
    }

    @Override
    public EmployeeProductivity getListStatistics(BaseSelectDto dto) {
        Integer sendEmailCount = 0;
        Integer receiveEmailCount = 0;
        Integer quotationCount = 0;
        Integer newCustomerCount = 0;
        Integer sampleCustomerCount = 0;
        Integer newDealCustomerCount = 0;
        Integer contractCustomerCount = 0;
        Integer customerCount = 0;
        Integer sampleCount = 0;
        Integer newCustomerContractCount = 0;
        BigDecimal newCustomerContractCnyAmount = BigDecimal.ZERO;
        Integer oldCustomerContractCount = 0;
        BigDecimal oldCustomerContractCnyAmount = BigDecimal.ZERO;
        Integer contractCount = 0;
        List<EmployeeProductivity> list = getList(dto);
        for (EmployeeProductivity employeeProductivity : list) {
            sendEmailCount += employeeProductivity.getSendEmailCount();
            receiveEmailCount += employeeProductivity.getReceiveEmailCount();
            quotationCount += employeeProductivity.getQuotationCount();
            newCustomerCount += employeeProductivity.getNewCustomerCount();
            sampleCustomerCount += employeeProductivity.getSampleCustomerCount();
            newDealCustomerCount += employeeProductivity.getNewDealCustomerCount();
            contractCustomerCount += employeeProductivity.getContractCustomerCount();
            customerCount += employeeProductivity.getCustomerCount();
            sampleCount += employeeProductivity.getSampleCount();
            newCustomerContractCount += employeeProductivity.getNewCustomerContractCount();
            newCustomerContractCnyAmount = newCustomerContractCnyAmount.add(employeeProductivity.getNewCustomerContractCnyAmount());
            oldCustomerContractCount += employeeProductivity.getOldCustomerContractCount();
            oldCustomerContractCnyAmount = oldCustomerContractCnyAmount.add(employeeProductivity.getOldCustomerContractCnyAmount());
            contractCount += employeeProductivity.getContractCount();
        }

        EmployeeProductivity reData = new EmployeeProductivity();
        reData.setSendEmailCount(sendEmailCount);
        reData.setReceiveEmailCount(receiveEmailCount);
        reData.setQuotationCount(quotationCount);
        reData.setNewCustomerCount(newCustomerCount);
        reData.setSampleCustomerCount(sampleCustomerCount);
        reData.setNewDealCustomerCount(newDealCustomerCount);
        reData.setContractCustomerCount(contractCustomerCount);
        reData.setCustomerCount(customerCount);
        reData.setSampleCount(sampleCount);
        reData.setNewCustomerContractCount(newCustomerContractCount);
        reData.setNewCustomerContractCnyAmount(newCustomerContractCnyAmount);
        reData.setOldCustomerContractCount(oldCustomerContractCount);
        reData.setOldCustomerContractCnyAmount(oldCustomerContractCnyAmount);
        reData.setContractCount(contractCount);

        return reData;
    }

}
