package com.ygtx.gxt.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.gxt.mapper.GxtFanInspectionScoreMapper;
import com.ygtx.gxt.domain.GxtFanInspectionScore;
import com.ygtx.gxt.service.IGxtFanInspectionScoreService;
import com.ygtx.common.exception.ServiceException;
import com.ygtx.common.utils.StringUtils;

/**
 * 风机类型检查分值Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-06
 */
@Service
public class GxtFanInspectionScoreServiceImpl implements IGxtFanInspectionScoreService 
{
    @Autowired
    private GxtFanInspectionScoreMapper gxtFanInspectionScoreMapper;

    /**
     * 查询风机类型检查分值
     * 
     * @param id 风机类型检查分值主键
     * @return 风机类型检查分值
     */
    @Override
    public GxtFanInspectionScore selectGxtFanInspectionScoreById(Long id)
    {
        return gxtFanInspectionScoreMapper.selectGxtFanInspectionScoreById(id);
    }

    /**
     * 查询风机类型检查分值列表
     * 
     * @param gxtFanInspectionScore 风机类型检查分值
     * @return 风机类型检查分值
     */
    @Override
    public List<GxtFanInspectionScore> selectGxtFanInspectionScoreList(GxtFanInspectionScore gxtFanInspectionScore)
    {
        return gxtFanInspectionScoreMapper.selectGxtFanInspectionScoreList(gxtFanInspectionScore);
    }

    /**
     * 根据风机类型查询风机类型检查分值列表
     * 
     * @param fanType 风机类型
     * @return 风机类型检查分值集合
     */
    @Override
    public List<GxtFanInspectionScore> selectGxtFanInspectionScoreByFanType(String fanType)
    {
        return gxtFanInspectionScoreMapper.selectGxtFanInspectionScoreByFanType(fanType);
    }

    /**
     * 新增风机类型检查分值
     * 
     * @param gxtFanInspectionScore 风机类型检查分值
     * @return 结果
     */
    @Override
    public int insertGxtFanInspectionScore(GxtFanInspectionScore gxtFanInspectionScore)
    {
        return gxtFanInspectionScoreMapper.insertGxtFanInspectionScore(gxtFanInspectionScore);
    }

    /**
     * 修改风机类型检查分值
     * 
     * @param gxtFanInspectionScore 风机类型检查分值
     * @return 结果
     */
    @Override
    public int updateGxtFanInspectionScore(GxtFanInspectionScore gxtFanInspectionScore)
    {
        return gxtFanInspectionScoreMapper.updateGxtFanInspectionScore(gxtFanInspectionScore);
    }

    /**
     * 批量删除风机类型检查分值
     * 
     * @param ids 需要删除的风机类型检查分值主键
     * @return 结果
     */
    @Override
    public int deleteGxtFanInspectionScoreByIds(Long[] ids)
    {
        return gxtFanInspectionScoreMapper.deleteGxtFanInspectionScoreByIds(ids);
    }

    /**
     * 删除风机类型检查分值信息
     * 
     * @param id 风机类型检查分值主键
     * @return 结果
     */
    @Override
    public int deleteGxtFanInspectionScoreById(Long id)
    {
        return gxtFanInspectionScoreMapper.deleteGxtFanInspectionScoreById(id);
    }
    
    /**
     * 批量导入风机类型检查分值
     * 
     * @param fanInspectionScoreList 风机类型检查分值列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户名
     * @return 结果
     */
    @Override
    public String importFanInspectionScoreList(List<GxtFanInspectionScore> fanInspectionScoreList, Boolean updateSupport, String operName)
    {
        if (StringUtils.isNull(fanInspectionScoreList) || fanInspectionScoreList.size() == 0)
        {
            throw new ServiceException("导入风机类型检查分值数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GxtFanInspectionScore fanInspectionScore : fanInspectionScoreList)
        {
            try
            {
                // 校验必填字段
                if (StringUtils.isEmpty(fanInspectionScore.getFanType()))
                {
                    throw new ServiceException("机型不能为空");
                }
                if (fanInspectionScore.getScore()==null)
                {
                    throw new ServiceException("分值不能为空");
                }
                if (fanInspectionScore.getInspectionTypeId()==null) {
                    throw new ServiceException("检查类型不存在");
                }
                
                // 验证是否存在这个数据（根据fanType和inspectionTypeId查重）
                GxtFanInspectionScore existsData = new GxtFanInspectionScore();
                existsData.setFanType(fanInspectionScore.getFanType());
                existsData.setInspectionTypeId(fanInspectionScore.getInspectionTypeId());
                List<GxtFanInspectionScore> list = gxtFanInspectionScoreMapper.selectGxtFanInspectionScoreList(existsData);
                
                if (list.isEmpty())
                {
                    // 没有重复数据，直接新增
                    fanInspectionScore.setCreateBy(operName);
                    insertGxtFanInspectionScore(fanInspectionScore);
                    successMsg.append("<br/>" + successNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " + 
                        fanInspectionScore.getInspectionTypeId() + " 导入成功");
                    successNum++;
                } else if (list.size()>1) {
                    failureMsg.append("<br/>" + failureNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " +
                            fanInspectionScore.getInspectionTypeId() + " 存在多条数据，请处理后再导入");
                    failureNum++;
                } else
                {
                    if (Boolean.TRUE.equals(updateSupport))
                    {
                        // 存在相同数据，且支持更新，则更新
                        fanInspectionScore.setId(list.get(0).getId());
                        fanInspectionScore.setUpdateBy(operName);
                        updateGxtFanInspectionScore(fanInspectionScore);
                        successMsg.append("<br/>" + successNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " + 
                            fanInspectionScore.getInspectionTypeId() + " 更新成功");
                        successNum++;
                    }
                    else
                    {
                        failureMsg.append("<br/>" + failureNum + "、机型 " + fanInspectionScore.getFanType() + " 维保类型ID " + 
                            fanInspectionScore.getInspectionTypeId() + " 已存在");
                        failureNum++;
                    }
                }
            }
            catch (Exception e)
            {
                failureMsg.append("<br/>" + failureNum + "、导入失败:" + e.getMessage());
                failureNum++;
            }
        }
        StringBuilder resultMsg = new StringBuilder();
        resultMsg.append("已成功导入 " + successNum + " 条数据");
        if (successNum > 0)
        {
            resultMsg.append("，成功导入数据如下：");
            resultMsg.append(successMsg);
        }
        if (failureNum > 0)
        {
            resultMsg.append("<br/>导入失败 " + failureNum + " 条数据，错误如下：");
            resultMsg.append(failureMsg);
        }
        return resultMsg.toString();
    }
}