package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.utils.StringUtils;
import com.dsk.cscec.domain.DProject;
import com.dsk.cscec.domain.LaborLeaderCustomerRisk;
import com.dsk.cscec.domain.bo.AddLaborLeaderCustomerRiskBo;
import com.dsk.cscec.domain.bo.QueryLaborLeaderCustomerRisk;
import com.dsk.cscec.mapper.LaborLeaderCustomerRiskMapper;
import com.dsk.cscec.service.LaborLeaderCustomerRiskService;
import com.dsk.search.service.BusinessOpportunityRadarService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

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

/**
 * 劳务队长供应商挂靠风险表(LaborLeaderCustomerRisk)表服务实现类
 *
 * @author makejava
 * @since 2024-06-04 17:28:40
 */
@Service("laborLeaderCustomerRiskService")
public class LaborLeaderCustomerRiskServiceImpl extends ServiceImpl<LaborLeaderCustomerRiskMapper, LaborLeaderCustomerRisk> implements LaborLeaderCustomerRiskService {
    @Resource
    private LaborLeaderCustomerRiskMapper baseMapper;
    @Resource
    private BusinessOpportunityRadarService opportunityRadarService;

    /**
     * 添加挂靠风险记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addRiskRecord(AddLaborLeaderCustomerRiskBo addBo) {
        LaborLeaderCustomerRisk risk = BeanUtil.toBean(addBo, LaborLeaderCustomerRisk.class);
        risk.setRiskId(IdUtil.getSnowflakeNextId());
        //查询供应商cid
        Map<String, Object> map = opportunityRadarService.enterpriseByName(addBo.getCusomerName());
        if (!ObjectUtils.isEmpty(map.get("data"))) {
            Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
            Integer companyId = MapUtil.getInt(data, "jskEid");
            Assert.notNull(companyId, "挂靠单位不存在或请填写完整单位名称");
            risk.setCustomerCid(companyId);
        }
        risk.setLaborCaptainIdCard(addBo.getLaborCaptainIdcard());
        risk.setCustomerName(addBo.getCusomerName());
        return baseMapper.insert(risk);
    }

    /**
     * 查询挂靠风险列表
     */
    @Override
    public TableDataInfo<LaborLeaderCustomerRisk> queryRiskList(QueryLaborLeaderCustomerRisk queryBo, PageQuery pageQuery) {
        QueryWrapper<DProject> wrapper = Wrappers.query();
        wrapper
                .like(StringUtils.isNotBlank(queryBo.getLeaderName()), "leader_name", queryBo.getLeaderName())
                .like(StringUtils.isNotBlank(queryBo.getCustomerName()), "customer_name", queryBo.getCustomerName())
                .eq(ObjectUtil.isNotNull(queryBo.getCustomerCid()),"customer_cid",queryBo.getCustomerCid())
                .gt(ObjectUtil.isNotNull(queryBo.getStartDate()), "create_time", queryBo.getStartDate())
                .lt(ObjectUtil.isNotNull(queryBo.getEndDate()), "create_time", queryBo.getEndDate())
                .between(ObjectUtil.isNotNull(queryBo.getStartDate()) && ObjectUtil.isNotNull(queryBo.getEndDate()),
                        "create_time",
                        queryBo.getStartDate(),
                        queryBo.getEndDate())
                .orderByDesc("create_time");
        Page<LaborLeaderCustomerRisk> page = baseMapper.queryRiskList(pageQuery.build(), wrapper);
        page.getRecords().parallelStream().forEach(item -> {
            //关键字标红
            if (StringUtils.isNotBlank(queryBo.getLeaderName())) {
                item.setLeaderName(StringUtils.markInRed(item.getLeaderName(), queryBo.getLeaderName()));
            }
            if (StringUtils.isNotBlank(queryBo.getCustomerName())) {
                item.setCustomerName(StringUtils.markInRed(item.getCustomerName(), queryBo.getCustomerName()));
            }
        });
        return TableDataInfo.build(page);
    }
}

