package com.fs.statis.service.impl;

import com.fs.company.mapper.CompanyUserMapper;
import com.fs.crm.mapper.CrmCustomerAssistMapper;
import com.fs.crm.mapper.CrmCustomerMapper;
import com.fs.statis.domain.Report;
import com.fs.statis.param.ReportParam;
import com.fs.statis.service.ReportService;
import com.fs.store.mapper.FsStoreOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private CrmCustomerMapper crmCustomerMapper;
    @Autowired
    private CrmCustomerAssistMapper crmCustomerAssistMapper;
    @Autowired
    private FsStoreOrderMapper storeOrderMapper;
    @Autowired
    private CompanyUserMapper companyUserMapper;
    @Override
    public List<Report> getReport(ReportParam param) {
        Set<Long> mergedSet = new HashSet<>();

        if (param.getCompanyUserId()!=null){
            //查询当前销售的客户以及协作客户
            List<Long> customerIds = Optional.ofNullable(crmCustomerMapper.selectCustomerIdByCompanyUserId(param.getCompanyUserId()))
                    .orElse(Collections.emptyList());
            List<Long> assistCustomerIds = Optional.ofNullable(crmCustomerAssistMapper.selectCustomerIdByCompanyUserId(param.getCompanyUserId()))
                    .orElse(Collections.emptyList());
            mergedSet = Stream.concat(customerIds.stream(), assistCustomerIds.stream())
                    .collect(Collectors.toSet());
            mergedSet.addAll(customerIds);
            mergedSet.addAll(assistCustomerIds);
            if (mergedSet.isEmpty()){
                return Collections.emptyList();
            }
            param.setCustomerIds(new ArrayList<>(mergedSet));
        }
        //查询客户的订单记录
        List<Report> reports = storeOrderMapper.selectOrderByCustomerIds(param);
        for (Report report : reports) {
            //查询用户下单时是否共享客户，有哪些销售共享
            List<Long> companyUserIds = crmCustomerAssistMapper.selectCompanyUserIdByCustomerId(report.getCustomerId(), report.getPayTime());
            if (companyUserIds.size()>0){
                report.setStatus("1");
                String s = companyUserMapper.selectCompanyUserNameByIds(companyUserIds).toString();
                String replace = s.replace("[", "");
                String replace1 = replace.replace("]", "");
                report.setCompanyUserNames(replace1);
            }
            //计算销售的业绩
            //分成比例
            BigDecimal bigDecimal = new BigDecimal("100.00");
            BigDecimal ratio = bigDecimal.divide(new BigDecimal(companyUserIds.size()), 2, RoundingMode.HALF_UP);
            report.setProportion(ratio);
            report.setMyPerformance(ratio.multiply(report.getMoney()).divide(bigDecimal));
            //修改对象
        }
        return reports;
    }
}
