package com.ruoyi.ehsLawEnforce.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.ehsLawEnforce.domain.LawEnforceImportTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

import com.ruoyi.ehsLawEnforce.mapper.EhsLawEnforcementMapper;
import com.ruoyi.ehsLawEnforce.domain.EhsLawEnforcement;
import com.ruoyi.ehsLawEnforce.service.IEhsLawEnforcementService;

/**
 * 行政执法台账Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@Service
public class EhsLawEnforcementServiceImpl extends ServiceImpl<EhsLawEnforcementMapper, EhsLawEnforcement> implements IEhsLawEnforcementService {

    @Autowired
    private EhsLawEnforcementMapper ehsLawEnforcementMapper;

    /**
     * 查询行政执法台账
     *
     * @param id 行政执法台账主键
     * @return 行政执法台账
     */
    @Override
    public EhsLawEnforcement selectEhsLawEnforcementById(Long id) {
        return ehsLawEnforcementMapper.selectEhsLawEnforcementById(id);
    }

    /**
     * 查询行政执法台账列表
     *
     * @param ehsLawEnforcement 行政执法台账
     * @return 行政执法台账
     */
    @Override
    public List<EhsLawEnforcement> selectEhsLawEnforcementList(EhsLawEnforcement ehsLawEnforcement) {
        return ehsLawEnforcementMapper.selectEhsLawEnforcementList(ehsLawEnforcement);
    }

    /**
     * 批量导入模板数据
     *
     * @param list          模板中用户输入的数据
     * @param updateSupport
     * @param params        这里是classFirst的值
     * @return
     */
    @Override
    public String importData(List<LawEnforceImportTemplate> list, Boolean updateSupport, Long params) throws Exception {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new Exception("导入行政执法台账数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LawEnforceImportTemplate lawEnforceImportTemplate : list) {

            EhsLawEnforcement temp = new EhsLawEnforcement();
            /**
             * 因为是插入数据,所以id需要处理,如果id是String类型,那么生成个uuid插入到数据库
             * 如果id是整数,并且自动递增,那么无需设置id
             */
            // temp.setId(IdUtils.fastSimpleUUID());
            temp.setEnterpriseName(lawEnforceImportTemplate.getEnterpriseName());// 单位名称
            temp.setLocation(lawEnforceImportTemplate.getLocation());// 地址
            temp.setResponsiblePerson(lawEnforceImportTemplate.getResponsiblePerson());// 负责人
            temp.setPhone(lawEnforceImportTemplate.getPhone());// 联系电话
            temp.setRisk(lawEnforceImportTemplate.getRisk());// 存在隐患
            temp.setIsLargeRisk(lawEnforceImportTemplate.getIsLargeRisk().toUpperCase());// 是否重大隐患
            temp.setRectificationMeasure(lawEnforceImportTemplate.getRectificationMeasure());// 整改措施
            temp.setRectificationPeople(lawEnforceImportTemplate.getRectificationPeople());// 整改责任人
            temp.setEnterpriseCategory(lawEnforceImportTemplate.getEnterpriseCategory());// 企业类别
            temp.setEmployeeNum(lawEnforceImportTemplate.getEmployeeNum());// 企业人数
            temp.setIsEstablishPrevention(lawEnforceImportTemplate.getIsEstablishPrevention().toUpperCase());// 是否建立双预防
            temp.setTime(lawEnforceImportTemplate.getTime());// 时间
            temp.setRemark(lawEnforceImportTemplate.getRemark());// 备注
            temp.setOther(lawEnforceImportTemplate.getOther());// 其他
            temp.setClassFirst(params);// 类别
            try {
                //插入数据
                this.save(temp);
                successNum++;
                successMsg.append("<br />" + successNum + "、" + temp.getEnterpriseName() + "行政执法台账信息导入成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getLocalizedMessage());
                failureNum++;
                successMsg.append("<br /><font color='red'>" + failureNum + "、" + temp.getEnterpriseName() + "行政执法台账信息导入异常，请联系管理员！</font>");
                continue;
            }
        }
        return successMsg.toString();
    }


    private LambdaQueryWrapper<EhsLawEnforcement> buildQueryWrapper(EhsLawEnforcement query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<EhsLawEnforcement> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getEnterpriseName()), EhsLawEnforcement::getEnterpriseName, query.getEnterpriseName());
        lqw.like(StringUtils.isNotBlank(query.getLocation()), EhsLawEnforcement::getLocation, query.getLocation());
        lqw.like(StringUtils.isNotBlank(query.getResponsiblePerson()), EhsLawEnforcement::getResponsiblePerson, query.getResponsiblePerson());
        lqw.eq(StringUtils.isNotBlank(query.getPhone()), EhsLawEnforcement::getPhone, query.getPhone());
        lqw.eq(StringUtils.isNotBlank(query.getRisk()), EhsLawEnforcement::getRisk, query.getRisk());
        lqw.eq(StringUtils.isNotBlank(query.getIsLargeRisk()), EhsLawEnforcement::getIsLargeRisk, query.getIsLargeRisk());
        lqw.eq(StringUtils.isNotBlank(query.getRectificationMeasure()), EhsLawEnforcement::getRectificationMeasure, query.getRectificationMeasure());
        lqw.eq(StringUtils.isNotBlank(query.getRectificationPeople()), EhsLawEnforcement::getRectificationPeople, query.getRectificationPeople());
        lqw.eq(query.getEnterpriseCategory() != null, EhsLawEnforcement::getEnterpriseCategory, query.getEnterpriseCategory());
        lqw.eq(query.getEmployeeNum() != null, EhsLawEnforcement::getEmployeeNum, query.getEmployeeNum());
        lqw.eq(StringUtils.isNotBlank(query.getIsEstablishPrevention()), EhsLawEnforcement::getIsEstablishPrevention, query.getIsEstablishPrevention());
        lqw.eq(query.getTime() != null, EhsLawEnforcement::getTime, query.getTime());
        lqw.eq(StringUtils.isNotBlank(query.getOther()), EhsLawEnforcement::getOther, query.getOther());
        lqw.eq(query.getClassFirst() != null, EhsLawEnforcement::getClassFirst, query.getClassFirst());
        lqw.orderByDesc(EhsLawEnforcement::getCreateTime);
        return lqw;
    }

}
