package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.utils.StringUtils;
import com.dsk.cscec.domain.DCustomer;
import com.dsk.cscec.domain.bo.DCustomerSearchBo;
import com.dsk.cscec.domain.vo.DCustomerListVo;
import com.dsk.cscec.mapper.DCustomerMapper;
import com.dsk.cscec.mapper.DSubcontractMapper;
import com.dsk.cscec.service.IDCustomerService;
import com.dsk.search.service.BusinessOpportunityRadarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 组织维表(DCustomer)表服务实现类
 *
 * @author lcl
 * @since 2023-12-08 14:57:26
 */
@Slf4j
@Service
public class DCustomerServiceImpl extends ServiceImpl<DCustomerMapper, DCustomer> implements IDCustomerService {

    @Resource
    private DSubcontractMapper subcontractMapper;
    @Autowired
    private BusinessOpportunityRadarService opportunityRadarService;

    @Override
    public TableDataInfo<DCustomerListVo> allSearchList(DCustomerSearchBo bo, PageQuery query) {
        dealWithCustomerClass(bo);
        Page<DCustomerListVo> page = baseMapper.allSearchList(query.build(), bo);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            page.getRecords().parallelStream().forEach(item -> {
                DCustomerListVo vo = subcontractMapper.selectStatisticByCustomerId(item.getCustomerId());
                //企业合作数量
                item.setEnterpriseCooperationCount(vo.getEnterpriseCooperationCount());
                //项目合作数量
                item.setProjectCooperationCount(vo.getProjectCooperationCount());
                showCustomerClass(item);
                try {
                    Map<String, Object> map = opportunityRadarService.enterpriseByName(item.getCustomerName());
                    if (!ObjectUtils.isEmpty(map.get("data"))) {
                        Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
                        Integer companyId = MapUtil.getInt(data, "jskEid");
                        item.setCompanyId(companyId);
                    }
                } catch (Exception e) {
                    //
                }
                //关键字标红
                if (!ObjectUtils.isEmpty(bo.getCustomerName())) {
                    item.setCustomerName(StringUtils.markInRed(item.getCustomerName(), bo.getCustomerName()));
                }
            });
        }
        return TableDataInfo.build(page);
    }

    @Override
    public List<DCustomerListVo> exportList(DCustomerSearchBo bo) {
        dealWithCustomerClass(bo);
        PageQuery query = new PageQuery();
        List<DCustomerListVo> records = baseMapper.allSearchList(query.build(), bo).getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            records.parallelStream().forEach(item -> {
                DCustomerListVo vo = subcontractMapper.selectStatisticByCustomerId(item.getCustomerId());
                //企业合作数量
                item.setEnterpriseCooperationCount(vo.getEnterpriseCooperationCount());
                //项目合作数量
                item.setProjectCooperationCount(vo.getProjectCooperationCount());
                showCustomerClass(item);
            });
        }
        return records;
    }

    @Override
    public DCustomer info(String customerKey) {
        DCustomer customer = baseMapper.selectById(customerKey);
        setCertificate(customer);
        return customer;
    }

    private void setCertificate(DCustomer customer) {
        if (ObjectUtils.isEmpty(customer)) return;
        //营业执照url
        customer.setBusinessLicenseList(baseMapper.selectBusinessLicense(customer.getCustomerId()));
        //组织机构代码证url
        customer.setOrgCertificateList(baseMapper.selectOrgCertificate(customer.getCustomerId()));
        //税务登记证url
        customer.setTaxCertificateList(baseMapper.selectTaxCertificate(customer.getCustomerId()));
        //资质证书url
        customer.setQualificationCertificateList(baseMapper.selectQualificationCertificate(customer.getCustomerId()));
        //安全生产url
        customer.setSafetyCertificateList(baseMapper.selectSafetyCertificate(customer.getCustomerId()));
        //增值税一般纳税人资格证明url
//        customer.setRatepayerCertificateList(baseMapper.selectRatepayerCertificate(customer.getCustomerId()));
        //业绩报告url
        customer.setPerformanceCertificateList(baseMapper.selectPerformanceCertificate(customer.getCustomerId()));
        //考察报告url
        customer.setInspectionCertificateList(baseMapper.selectInspectionCertificate(customer.getCustomerId()));
        //其他url
        customer.setOtherCertificateList(baseMapper.selectOtherCertificate(customer.getCustomerId()));
    }

    private void dealWithCustomerClass(DCustomerSearchBo bo) {
        if (!ObjectUtils.isEmpty(bo.getCustomerClass())) {
            List<String> list = new ArrayList<>();
            Integer isTeam = null;
            for (String customerClass : bo.getCustomerClass()) {
                switch (customerClass) {
                    case "专业分包":
                        list.add("专业分包");
                        break;
                    case "物资设备":
                        list.add("分供");
                        break;
                    case "施工机械":
                        list.add("租赁");
                        break;
                    case "劳务分包":
                        if (isTeam == null) {
                            isTeam = 0;
                        } else if (isTeam == 1) {
                            isTeam = null;
                        }
                        list.add("劳务分包");
                        break;
                    case "劳务队伍":
                        if (isTeam == null) {
                            isTeam = 1;
                        } else if (isTeam == 0) {
                            isTeam = null;
                        }
                        list.add("劳务分包");
                        break;
                    default:
                        break;
                }
            }
            bo.setCustomerClass(list);
            bo.setIsTeam(isTeam);
        }
    }

    private void showCustomerClass(DCustomerListVo vo) {
        if (!ObjectUtils.isEmpty(vo.getCustomerClass())) {
            switch (vo.getCustomerClass()) {
                case "分供":
                    vo.setCustomerClass("物资设备");
                    break;
                case "租赁":
                    vo.setCustomerClass("施工机械");
                    break;
                case "劳务分包":
                    if (!ObjectUtils.isEmpty(vo.getLeaderName())) {
                        vo.setCustomerClass("劳务队伍");
                    }
                    break;
                default:
                    break;
            }
        }
    }
}

