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.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());
                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){
                    //
                }
            });
        }
        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());
            });
        }
        return records;
    }

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

