package com.ruoyi.mall.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.Bo.MallSchoolAppraiseExcelBo;
import com.ruoyi.mall.domain.MallSchool;
import com.ruoyi.mall.domain.MallTestSchool;
import com.ruoyi.mall.domain.Vo.SchoolAppraiseVO;
import com.ruoyi.mall.mapper.MallSchoolMapper;
import com.ruoyi.mall.mapper.MallTestSchoolMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallSchoolAppraiseMapper;
import com.ruoyi.mall.domain.MallSchoolAppraise;
import com.ruoyi.mall.service.IMallSchoolAppraiseService;

import javax.annotation.Resource;

/**
 * 院校评估Service业务层处理
 * 
 * @author lsm
 * @date 2023-10-19
 */
@Service
public class MallSchoolAppraiseServiceImpl implements IMallSchoolAppraiseService 
{
    @Resource
    private MallSchoolAppraiseMapper mallSchoolAppraiseMapper;

    @Resource
    private MallSchoolMapper mallSchoolMapper;

    private static final Logger log = LoggerFactory.getLogger(MallSchoolAppraiseServiceImpl.class);

    /** 导入院校评估数据
     *
     * @param list
     * @param updateSupport
     * @param operName
     * @return
     */
    @Override
    public String importSchoolAppraise(List<MallSchoolAppraiseExcelBo> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入院校评估数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        List<MallSchool> schoolList = mallSchoolMapper.selectMallSchoolList(null);
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MallSchoolAppraiseExcelBo item : list)
        {
            try
            {
                Optional<MallSchool> first = schoolList.stream().filter(s -> s.getSchoolName().equals(item.getSchoolName())).findFirst();
                if(first.isPresent()){
                    MallSchool school = first.orElse(null);
                    // 验证是否已存在
                    MallSchoolAppraise param = new MallSchoolAppraise();
                    param.setSchoolId(school.getSchoolId());
                    List<MallSchoolAppraise> schoolAppraises = mallSchoolAppraiseMapper.selectMallSchoolAppraiseList(param);
                    if(StringUtils.isNotBlank(item.getMajorContent())){//学校+专业2
                        Optional<MallSchoolAppraise> old_major = schoolAppraises.stream().filter(s -> s.getAppraiseClassId() == 2).findFirst();
                        if (!old_major.isPresent())//没重复
                        {
                            //student.setStudentName(student.getStudentName());
                            MallSchoolAppraise new_major = new MallSchoolAppraise();
                            new_major.setSchoolId(school.getSchoolId());
                            new_major.setAppraiseClassId(2L);
                            new_major.setContent(item.getMajorContent());
                            this.insertMallSchoolAppraise(new_major);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "院校专业评估信息" + item.getSchoolName() + " 导入成功");
                        }
                        else if (updateSupport)//重复
                        {
                            MallSchoolAppraise update_major = new MallSchoolAppraise();
                            update_major.setSchoolId(school.getSchoolId());
                            update_major.setAppraiseClassId(2L);
                            update_major.setContent(item.getMajorContent());
                            //student.setUpdateBy(operName);
                            this.updateMallSchoolAppraise(update_major);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "院校专业评估信息 " + item.getSchoolName() + " 更新成功");
                        }
                        else
                        {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "院校专业评估信息" + item.getSchoolName() + " 已存在");
                        }
                    }
                    if(StringUtils.isNotBlank(item.getSubjectContent())){//学校+学科1
                        Optional<MallSchoolAppraise> old_subject = schoolAppraises.stream().filter(s -> s.getAppraiseClassId() == 1).findFirst();
                        if (!old_subject.isPresent())//没重复
                        {
                            //student.setStudentName(student.getStudentName());
                            MallSchoolAppraise new_subject = new MallSchoolAppraise();
                            new_subject.setSchoolId(school.getSchoolId());
                            new_subject.setAppraiseClassId(1L);
                            new_subject.setContent(item.getSubjectContent());
                            this.insertMallSchoolAppraise(new_subject);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "院校学科评估信息" + item.getSchoolName() + " 导入成功");
                        }
                        else if (updateSupport)//重复
                        {
                            MallSchoolAppraise update_subject = new MallSchoolAppraise();
                            update_subject.setSchoolId(school.getSchoolId());
                            update_subject.setAppraiseClassId(1L);
                            update_subject.setContent(item.getSubjectContent());
                            this.updateMallSchoolAppraise(update_subject);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "院校学科评估信息 " + item.getSchoolName() + " 更新成功");
                        }
                        else
                        {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "院校学科评估信息" + item.getSchoolName() + " 已存在");
                        }
                    }
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "院校" + item.getSchoolName() + " 不存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "院校评估信息" + item.getSchoolName() + " 导入失败：";
                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();
    }

    /**
     * 查询院校评估
     * 
     * @param appraiseId 院校评估主键
     * @return 院校评估
     */
    @Override
    public MallSchoolAppraise selectMallSchoolAppraiseByAppraiseId(Long appraiseId)
    {
        return mallSchoolAppraiseMapper.selectMallSchoolAppraiseByAppraiseId(appraiseId);
    }

    /**
     * 查询院校评估列表
     * 
     * @param mallSchoolAppraise 院校评估
     * @return 院校评估
     */
    @Override
    public List<MallSchoolAppraise> selectMallSchoolAppraiseList(MallSchoolAppraise mallSchoolAppraise)
    {
        return mallSchoolAppraiseMapper.selectMallSchoolAppraiseList(mallSchoolAppraise);
    }

    /**
     * 新增院校评估
     * 
     * @param mallSchoolAppraise 院校评估
     * @return 结果
     */
    @Override
    public int insertMallSchoolAppraise(MallSchoolAppraise mallSchoolAppraise)
    {
        mallSchoolAppraise.setCreateAt(DateUtils.getDate());
        return mallSchoolAppraiseMapper.insertMallSchoolAppraise(mallSchoolAppraise);
    }

    /**
     * 修改院校评估
     * 
     * @param mallSchoolAppraise 院校评估
     * @return 结果
     */
    @Override
    public int updateMallSchoolAppraise(MallSchoolAppraise mallSchoolAppraise)
    {
        mallSchoolAppraise.setUpdateAt(DateUtils.getDate());
        return mallSchoolAppraiseMapper.updateMallSchoolAppraise(mallSchoolAppraise);
    }

    /**
     * 批量删除院校评估
     * 
     * @param appraiseIds 需要删除的院校评估主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAppraiseByAppraiseIds(Long[] appraiseIds)
    {
        return mallSchoolAppraiseMapper.deleteMallSchoolAppraiseByAppraiseIds(appraiseIds);
    }

    /**
     * 删除院校评估信息
     * 
     * @param appraiseId 院校评估主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAppraiseByAppraiseId(Long appraiseId)
    {
        return mallSchoolAppraiseMapper.deleteMallSchoolAppraiseByAppraiseId(appraiseId);
    }
}
