package cn.gov.customs.wxjy.generator.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.DateUtils;
import cn.gov.customs.wxjy.common.utils.bean.BeanValidators;
import cn.gov.customs.wxjy.generator.dao.GenTableColumnMapper;
import cn.gov.customs.wxjy.generator.pojo.GenTableColumn;
import cn.gov.customs.wxjy.generator.pojo.GenTableColumnQuery;
import cn.gov.customs.wxjy.generator.service.IGenTableColumnService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 代码生成业务字段Service业务层处理
 *
 * @author xiong
 * @date 2025-12-25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class GenTableColumnServiceImpl implements IGenTableColumnService {

    private final GenTableColumnMapper genTableColumnMapper;

    private final Validator validator;

    /**
     * 查询代码生成业务字段
     *
     * @param columnId 代码生成业务字段主键
     * @return 代码生成业务字段
     */
    @Override
    public GenTableColumn selectByColumnId(String columnId) {
        return genTableColumnMapper.selectByColumnId(columnId);
    }

    /**
     * 分页查询代码生成业务字段列表
     *
     * @param query
     * @return 代码生成业务字段
     */
    @Override
    public PageInfo<GenTableColumn> selectPageList(GenTableColumnQuery query) {
        return PageInfo.of(genTableColumnMapper.selectList(query));
    }

    /**
     * 查询代码生成业务字段列表
     *
     * @param query
     * @return 代码生成业务字段
     */
    @Override
    public List<GenTableColumn> selectList(GenTableColumnQuery query) {
        return genTableColumnMapper.selectList(query);
    }

    /**
     * 新增代码生成业务字段
     *
     * @param genTableColumn 代码生成业务字段
     * @return 结果
     */
    @Override
    public int insert(GenTableColumn genTableColumn) {
            return genTableColumnMapper.insert(genTableColumn);
    }

    /**
     * 修改代码生成业务字段
     *
     * @param genTableColumn 代码生成业务字段
     * @return 结果
     */
    @Override
    public int update(GenTableColumn genTableColumn) {
        return genTableColumnMapper.update(genTableColumn);
    }

    /**
     * 批量删除代码生成业务字段
     *
     * @param ids 需要删除的代码生成业务字段主键
     * @return 结果
     */
    @Override
    public int deleteByColumnIds(String[] ids) {
        return genTableColumnMapper.deleteByColumnIds(ids);
    }

    /**
     * 删除代码生成业务字段信息
     *
     * @param columnId 代码生成业务字段主键
     * @return 结果
     */
    @Override
    public int deleteByColumnId(String columnId) {
        return genTableColumnMapper.deleteByColumnId(columnId);
    }

    /**
     * 逻辑删除代码生成业务字段信息
     *
     * @param columnId 代码生成业务字段主键
     * @return 结果
     */
    @Override
    public int logicDelete(String columnId) {

        return genTableColumnMapper.logicDelete(columnId);
    }

    /**
     * 导入代码生成业务字段数据
     *
     * @param genTableColumnList 代码生成业务字段数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param cacpTransUser 操作用户
     * @return 结果
     */
    @Override
    public String importGenTableColumn(List<GenTableColumn> genTableColumnList, Boolean isUpdateSupport, CacpTransUser cacpTransUser) {
        if (CollectionUtils.isEmpty(genTableColumnList)) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GenTableColumn genTableColumn : genTableColumnList) {
            try {

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

                genTableColumnMapper.insert(genTableColumn);
                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();
    }

    @Override
    public List<GenTableColumn> selectGenTableColumnListByTableId(String tableId) {
        return genTableColumnMapper.selectGenTableColumnListByTableId(tableId);
    }
}
