package com.dsk.cscec.service.impl;

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.bo.DCustomerSearchBo;
import com.dsk.cscec.mapper.DCustomerMapper;
import com.dsk.cscec.domain.DCustomer;
import com.dsk.cscec.mapper.DSubcontractMapper;
import com.dsk.cscec.service.IDCustomerService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

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

    @Resource
    private DSubcontractMapper subcontractMapper;

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

