package cn.gov.customs.wxjy.base.service.impl;

import cn.gov.customs.cacp.sdks.core.user.trans.CacpTransUser;
import cn.gov.customs.wxjy.common.exception.ServiceException;
import cn.gov.customs.wxjy.common.utils.StringUtils;
import cn.gov.customs.wxjy.common.utils.bean.BeanValidators;
import cn.gov.customs.wxjy.common.utils.uuid.SnowflakeIdWorker;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.validation.Validator;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import cn.gov.customs.wxjy.common.utils.DateUtils;
import com.github.pagehelper.PageInfo;
import cn.gov.customs.wxjy.common.utils.DateUtils;
import cn.gov.customs.wxjy.base.dao.ChemicalsCatalogMapper;
import cn.gov.customs.wxjy.base.pojo.ChemicalsCatalog;
import cn.gov.customs.wxjy.base.pojo.ChemicalsCatalogQuery;
import cn.gov.customs.wxjy.base.service.IChemicalsCatalogService;

/**
 * 基数数据-危化品目录Service业务层处理
 *
 * @author xiong
 * @date 2025-12-16
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ChemicalsCatalogServiceImpl implements IChemicalsCatalogService {

    private final ChemicalsCatalogMapper chemicalsCatalogMapper;

    private final Validator validator;

    /**
     * 查询基数数据-危化品目录
     *
     * @param ID 基数数据-危化品目录主键
     * @return 基数数据-危化品目录
     */
    @Override
    public ChemicalsCatalog selectByID(String ID) {
        return chemicalsCatalogMapper.selectByID(ID);
    }

    /**
     * 分页查询基数数据-危化品目录列表
     *
     * @param query
     * @return 基数数据-危化品目录
     */
    @Override
    public PageInfo<ChemicalsCatalog> selectPageList(ChemicalsCatalogQuery query) {
        return PageInfo.of(chemicalsCatalogMapper.selectList(query));
    }

    /**
     * 查询基数数据-危化品目录列表
     *
     * @param query
     * @return 基数数据-危化品目录
     */
    @Override
    public List<ChemicalsCatalog> selectList(ChemicalsCatalogQuery query) {
        return chemicalsCatalogMapper.selectList(query);
    }

    /**
     * 新增基数数据-危化品目录
     *
     * @param chemicalsCatalog 基数数据-危化品目录
     * @return 结果
     */
    @Override
    public int insert(ChemicalsCatalog chemicalsCatalog) {
            return chemicalsCatalogMapper.insert(chemicalsCatalog);
    }

    /**
     * 修改基数数据-危化品目录
     *
     * @param chemicalsCatalog 基数数据-危化品目录
     * @return 结果
     */
    @Override
    public int update(ChemicalsCatalog chemicalsCatalog) {
        return chemicalsCatalogMapper.update(chemicalsCatalog);
    }

    /**
     * 批量删除基数数据-危化品目录
     *
     * @param ids 需要删除的基数数据-危化品目录主键
     * @return 结果
     */
    @Override
    public int deleteByIDs(String[] ids) {
        return chemicalsCatalogMapper.deleteByIDs(ids);
    }

    /**
     * 删除基数数据-危化品目录信息
     *
     * @param ID 基数数据-危化品目录主键
     * @return 结果
     */
    @Override
    public int deleteByID(String ID) {
        return chemicalsCatalogMapper.deleteByID(ID);
    }

    /**
     * 逻辑删除基数数据-危化品目录信息
     *
     * @param ID 基数数据-危化品目录主键
     * @return 结果
     */
    @Override
    public int logicDelete(String ID) {

        return chemicalsCatalogMapper.logicDelete(ID);
    }

    /**
     * 导入基数数据-危化品目录数据
     *
     * @param chemicalsCatalogList 基数数据-危化品目录数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param cacpTransUser 操作用户
     * @return 结果
     */
    @Override
    public String importChemicalsCatalog(List<ChemicalsCatalog> chemicalsCatalogList, Boolean isUpdateSupport, CacpTransUser cacpTransUser) {
        if (CollectionUtils.isEmpty(chemicalsCatalogList)) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ChemicalsCatalog chemicalsCatalog : chemicalsCatalogList) {
            try {

                BeanValidators.validateWithException(validator, chemicalsCatalog);
                //id名称不统一，自行处理
                chemicalsCatalog.setCreateTime(DateUtils.getNowDate());
                chemicalsCatalog.setUpdateTime(DateUtils.getNowDate());
                chemicalsCatalog.setCreateUser(cacpTransUser.getUserId());
                chemicalsCatalog.setCreateDept(cacpTransUser.getParentGuid());

                chemicalsCatalogMapper.insert(chemicalsCatalog);
                successNum++;
                successMsg.append("<br/>" + successNum + "、 " + cacpTransUser.getUserName() + " 导入成功");

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、" + cacpTransUser.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
