package com.ruoyi.syzy.service.impl;

import java.util.List;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.syzy.mapper.BBusiWishUniversitiesEnrollBrochureMapper;
import com.ruoyi.syzy.domain.BBusiWishUniversitiesEnrollBrochure;
import com.ruoyi.syzy.service.IBBusiWishUniversitiesEnrollBrochureService;

/**
 * 院校招生简章Service业务层处理
 *
 * @author voluntary
 * @date 2024-09-27
 */
@Service
public class BBusiWishUniversitiesEnrollBrochureServiceImpl implements IBBusiWishUniversitiesEnrollBrochureService
{
    private static final Logger log = LoggerFactory.getLogger(BBusiWishUniversitiesEnrollBrochureServiceImpl.class);

    @Autowired
    private BBusiWishUniversitiesEnrollBrochureMapper bBusiWishUniversitiesEnrollBrochureMapper;

    /**
     * 查询院校招生简章
     *
     * @param id 院校招生简章主键
     * @return 院校招生简章
     */
    @Override
    public BBusiWishUniversitiesEnrollBrochure selectBBusiWishUniversitiesEnrollBrochureById(Long id)
    {
        return bBusiWishUniversitiesEnrollBrochureMapper.selectBBusiWishUniversitiesEnrollBrochureById(id);
    }

    /**
     * 查询院校招生简章列表
     *
     * @param bBusiWishUniversitiesEnrollBrochure 院校招生简章
     * @return 院校招生简章
     */
    @Override
    public List<BBusiWishUniversitiesEnrollBrochure> selectBBusiWishUniversitiesEnrollBrochureList(BBusiWishUniversitiesEnrollBrochure bBusiWishUniversitiesEnrollBrochure)
    {
        return bBusiWishUniversitiesEnrollBrochureMapper.selectBBusiWishUniversitiesEnrollBrochureList(bBusiWishUniversitiesEnrollBrochure);
    }

    /**
     * 新增院校招生简章
     *
     * @param bBusiWishUniversitiesEnrollBrochure 院校招生简章
     * @return 结果
     */
    @Override
    public int insertBBusiWishUniversitiesEnrollBrochure(BBusiWishUniversitiesEnrollBrochure bBusiWishUniversitiesEnrollBrochure)
    {
        bBusiWishUniversitiesEnrollBrochure.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isEmpty(bBusiWishUniversitiesEnrollBrochure.getEditor())){
            bBusiWishUniversitiesEnrollBrochure.setEditor(bBusiWishUniversitiesEnrollBrochure.getTitle());
        }
        if (StringUtils.isEmpty(bBusiWishUniversitiesEnrollBrochure.getSummary())){
            bBusiWishUniversitiesEnrollBrochure.setSummary(bBusiWishUniversitiesEnrollBrochure.getTitle());
        }
        return bBusiWishUniversitiesEnrollBrochureMapper.insertBBusiWishUniversitiesEnrollBrochure(bBusiWishUniversitiesEnrollBrochure);
    }

    /**
     * 修改院校招生简章
     *
     * @param bBusiWishUniversitiesEnrollBrochure 院校招生简章
     * @return 结果
     */
    @Override
    public int updateBBusiWishUniversitiesEnrollBrochure(BBusiWishUniversitiesEnrollBrochure bBusiWishUniversitiesEnrollBrochure)
    {
        bBusiWishUniversitiesEnrollBrochure.setUpdateTime(DateUtils.getNowDate());
        return bBusiWishUniversitiesEnrollBrochureMapper.updateBBusiWishUniversitiesEnrollBrochure(bBusiWishUniversitiesEnrollBrochure);
    }

    /**
     * 批量删除院校招生简章
     *
     * @param ids 需要删除的院校招生简章主键
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitiesEnrollBrochureByIds(Long[] ids)
    {
        return bBusiWishUniversitiesEnrollBrochureMapper.deleteBBusiWishUniversitiesEnrollBrochureByIds(ids);
    }

    /**
     * 删除院校招生简章信息
     *
     * @param id 院校招生简章主键
     * @return 结果
     */
    @Override
    public int deleteBBusiWishUniversitiesEnrollBrochureById(Long id)
    {
        return bBusiWishUniversitiesEnrollBrochureMapper.deleteBBusiWishUniversitiesEnrollBrochureById(id);
    }

    /**
     * 查询所有类型（去重）
     *
     * @return 类型列表
     */
    @Override
    public List<BBusiWishUniversitiesEnrollBrochure> getBrochureTypes()
    {
        return bBusiWishUniversitiesEnrollBrochureMapper.selectDistinctTypes();
    }

    /**
     * 导入章程简章数据
     *
     * @param brochureList 章程简章数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importUniversityBrochure(List<BBusiWishUniversitiesEnrollBrochure> brochureList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(brochureList) || brochureList.size() == 0)
        {
            throw new ServiceException("导入章程简章数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BBusiWishUniversitiesEnrollBrochure brochure : brochureList)
        {
            try
            {
                // 根据年份、学校编码、标题判断是否存在
                BBusiWishUniversitiesEnrollBrochure query = new BBusiWishUniversitiesEnrollBrochure();
                query.setYear(brochure.getYear());
                query.setCollegeCode(brochure.getCollegeCode());
                query.setTitle(brochure.getTitle());
                List<BBusiWishUniversitiesEnrollBrochure> existList = bBusiWishUniversitiesEnrollBrochureMapper.selectBBusiWishUniversitiesEnrollBrochureList(query);

                if (StringUtils.isNull(existList) || existList.size() == 0)
                {
                    // 不存在，执行新增
                    insertBBusiWishUniversitiesEnrollBrochure(brochure);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、章程简章 " + brochure.getTitle() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    // 存在且支持更新，执行更新
                    BBusiWishUniversitiesEnrollBrochure existBrochure = existList.get(0);
                    brochure.setId(existBrochure.getId());
                    updateBBusiWishUniversitiesEnrollBrochure(brochure);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、章程简章 " + brochure.getTitle() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、章程简章 " + brochure.getTitle() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、章程简章 " + brochure.getTitle() + " 导入失败：";
                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();
    }
}
