package cn.gov.customs.wxjy.ibmmq.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.ibmmq.dao.PushLogMapper;
import cn.gov.customs.wxjy.ibmmq.pojo.PushLog;
import cn.gov.customs.wxjy.ibmmq.pojo.PushLogQuery;
import cn.gov.customs.wxjy.ibmmq.service.IPushLogService;

/**
 * 推送记录Service业务层处理
 *
 * @author xiong
 * @date 2025-12-22
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PushLogServiceImpl implements IPushLogService {

    private final PushLogMapper pushLogMapper;

    private final Validator validator;

    /**
     * 查询推送记录
     *
     * @param logId 推送记录主键
     * @return 推送记录
     */
    @Override
    public PushLog selectByLogId(String logId) {
        return pushLogMapper.selectByLogId(logId);
    }

    /**
     * 分页查询推送记录列表
     *
     * @param query
     * @return 推送记录
     */
    @Override
    public PageInfo<PushLog> selectPageList(PushLogQuery query) {
        return PageInfo.of(pushLogMapper.selectList(query));
    }

    /**
     * 查询推送记录列表
     *
     * @param query
     * @return 推送记录
     */
    @Override
    public List<PushLog> selectList(PushLogQuery query) {
        return pushLogMapper.selectList(query);
    }

    /**
     * 新增推送记录
     *
     * @param pushLog 推送记录
     * @return 结果
     */
    @Override
    public int insert(PushLog pushLog) {
            return pushLogMapper.insert(pushLog);
    }

    /**
     * 修改推送记录
     *
     * @param pushLog 推送记录
     * @return 结果
     */
    @Override
    public int update(PushLog pushLog) {
        return pushLogMapper.update(pushLog);
    }

    /**
     * 批量删除推送记录
     *
     * @param ids 需要删除的推送记录主键
     * @return 结果
     */
    @Override
    public int deleteByLogIds(String[] ids) {
        return pushLogMapper.deleteByLogIds(ids);
    }

    /**
     * 删除推送记录信息
     *
     * @param logId 推送记录主键
     * @return 结果
     */
    @Override
    public int deleteByLogId(String logId) {
        return pushLogMapper.deleteByLogId(logId);
    }

    /**
     * 逻辑删除推送记录信息
     *
     * @param logId 推送记录主键
     * @return 结果
     */
    @Override
    public int logicDelete(String logId) {

        return pushLogMapper.logicDelete(logId);
    }

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

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

                pushLogMapper.insert(pushLog);
                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();
    }
}
