package cn.gov.customs.wxjy.analyze.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.analyze.dao.EntryHeadMapper;
import cn.gov.customs.wxjy.analyze.pojo.EntryHead;
import cn.gov.customs.wxjy.analyze.pojo.EntryHeadQuery;
import cn.gov.customs.wxjy.analyze.service.IEntryHeadService;

/**
 * 危险品报关单头Service业务层处理
 *
 * @author xiong
 * @date 2025-12-04
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class EntryHeadServiceImpl implements IEntryHeadService {

    private final EntryHeadMapper entryHeadMapper;

    private final Validator validator;

    /**
     * 查询危险品报关单头
     *
     * @param entryId 危险品报关单头主键
     * @return 危险品报关单头
     */
    @Override
    public EntryHead selectByEntryId(String entryId) {
        return entryHeadMapper.selectByEntryId(entryId);
    }

    /**
     * 分页查询危险品报关单头列表
     *
     * @param query
     * @return 危险品报关单头
     */
    @Override
    public PageInfo<EntryHead> selectPageList(EntryHeadQuery query) {
        return PageInfo.of(entryHeadMapper.selectList(query));
    }

    /**
     * 查询危险品报关单头列表
     *
     * @param query
     * @return 危险品报关单头
     */
    @Override
    public List<EntryHead> selectList(EntryHeadQuery query) {
        return entryHeadMapper.selectList(query);
    }

    /**
     * 新增危险品报关单头
     *
     * @param entryHead 危险品报关单头
     * @return 结果
     */
    @Override
    public int insert(EntryHead entryHead) {
            return entryHeadMapper.insert(entryHead);
    }

    /**
     * 修改危险品报关单头
     *
     * @param entryHead 危险品报关单头
     * @return 结果
     */
    @Override
    public int update(EntryHead entryHead) {
        return entryHeadMapper.update(entryHead);
    }

    /**
     * 批量删除危险品报关单头
     *
     * @param ids 需要删除的危险品报关单头主键
     * @return 结果
     */
    @Override
    public int deleteByEntryIds(String[] ids) {
        return entryHeadMapper.deleteByEntryIds(ids);
    }

    /**
     * 删除危险品报关单头信息
     *
     * @param entryId 危险品报关单头主键
     * @return 结果
     */
    @Override
    public int deleteByEntryId(String entryId) {
        return entryHeadMapper.deleteByEntryId(entryId);
    }

    /**
     * 逻辑删除危险品报关单头信息
     *
     * @param entryId 危险品报关单头主键
     * @return 结果
     */
    @Override
    public int logicDelete(String entryId) {

        return entryHeadMapper.logicDelete(entryId);
    }

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

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

                entryHeadMapper.insert(entryHead);
                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();
    }
}
