package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.CustomerInfoBo;
import com.dsk.cscec.domain.vo.CustomerInfoVo;
import com.dsk.cscec.mapper.DCustomerMapper;
import com.dsk.cscec.mapper.DSubcontractMapper;
import com.dsk.cscec.service.ICustomerInfoService;
import com.dsk.search.service.BusinessOpportunityRadarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * 组织维表(DCustomer)表服务实现类
 *
 * @author
 * @since 2023-12-10
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<DCustomerMapper, DCustomer> implements ICustomerInfoService {

    @Autowired
    private DSubcontractMapper subcontractMapper;

    @Autowired
    private BusinessOpportunityRadarService opportunityRadarService;

    @Override
    public TableDataInfo<CustomerInfoVo> queryPageList(CustomerInfoBo bo, PageQuery query) {
        if("劳务分包".equals(bo.getCustomerClass())){
            bo.setServiceTeamLogo("N");
        }
        if("劳务分包队伍".equals(bo.getCustomerClass())){
            bo.setCustomerClass("劳务分包");
            bo.setServiceTeamLogo("Y");
        }
        Page<CustomerInfoVo> page = baseMapper.queryListByType(query.build(), bo);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            page.getRecords().parallelStream().forEach(item->{
                //企业合作数量
                item.setEnterpriseCooperationCount(subcontractMapper.selectEnterpriseCountByCustomerId(item.getCustomerId()));
                //项目合作数量
                item.setProjectCooperationCount(subcontractMapper.selectProjectCountByCustomerId(item.getCustomerId()));
                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 DCustomer queryByName(String name) {
        LambdaQueryWrapper<DCustomer> lqw = Wrappers.lambdaQuery();
        lqw.eq(DCustomer::getCustomerName,name);
        lqw.orderByDesc(DCustomer::getApproveDate2);
        lqw.last("limit 1");
        return baseMapper.selectOne(lqw);
    }
}
