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.cscec.domain.DCustomer;
import com.dsk.cscec.domain.bo.DCustomerSearchBo;
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<DCustomer> allSearchList(DCustomerSearchBo bo, PageQuery query) {
        dealWithCustomerClass(bo);
        Page<DCustomer> page = baseMapper.allSearchList(query.build(), bo);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            for (DCustomer customer : page.getRecords()) {
                //企业合作数量
                customer.setEnterpriseCooperationCount(subcontractMapper.selectEnterpriseCountByCustomerId(customer.getCustomerId()));
                //项目合作数量
                customer.setProjectCooperationCount(subcontractMapper.selectProjectCountByCustomerId(customer.getCustomerId()));
                try {
                    Map<String, Object> map = opportunityRadarService.enterpriseByName(customer.getCustomerName());
                    if (!ObjectUtils.isEmpty(map.get("data"))) {
                        Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
                        Integer companyId = MapUtil.getInt(data, "jskEid");
                        customer.setCompanyId(companyId);
                    }
                }catch (Exception e){
                    //
                }
            }
        }
        return TableDataInfo.build(page);
    }

    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);
        }
    }
}

