package com.ruoyi.mall.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.*;
import com.ruoyi.mall.domain.Bo.MallSchoolAppraiseExcelBo;
import com.ruoyi.mall.domain.Bo.MallTestSchoolRecordBo;
import com.ruoyi.mall.mapper.MallMajorClassMapper;
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.MallTestSchoolRecordMapper;
import com.ruoyi.mall.service.IMallTestSchoolRecordService;

import javax.annotation.Resource;

/**
 * 校考院校记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@Service
public class MallTestSchoolRecordServiceImpl implements IMallTestSchoolRecordService
{
    @Resource
    private MallTestSchoolRecordMapper mallTestSchoolRecordMapper;

    @Resource
    private MallTestSchoolMapper mallTestSchoolMapper;

    @Resource
    private MallMajorClassMapper mallMajorClassMapper;

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

    /**
     * 查询校考院校记录
     *
     * @param testInfoId 校考院校记录主键
     * @return 校考院校记录
     */
    @Override
    public MallTestSchoolRecord selectMallTestSchoolRecordByTestInfoId(Long testInfoId)
    {
        return mallTestSchoolRecordMapper.selectMallTestSchoolRecordByTestInfoId(testInfoId);
    }

    /**
     * 查询校考院校记录列表
     *
     * @param mallTestSchoolRecord 校考院校记录
     * @return 校考院校记录
     */
    @Override
    public List<MallTestSchoolRecord> selectMallTestSchoolRecordList(MallTestSchoolRecord mallTestSchoolRecord)
    {
        return mallTestSchoolRecordMapper.selectMallTestSchoolRecordList(mallTestSchoolRecord);
    }

    /**
     * 新增校考院校记录
     *
     * @param mallTestSchoolRecord 校考院校记录
     * @return 结果
     */
    @Override
    public int insertMallTestSchoolRecord(MallTestSchoolRecord mallTestSchoolRecord)
    {
        mallTestSchoolRecord.setCreateTime(DateUtils.getNowDate());
        mallTestSchoolRecord.setDelFlag(0);
        return mallTestSchoolRecordMapper.insertMallTestSchoolRecord(mallTestSchoolRecord);
    }

    /**
     * 修改校考院校记录
     *
     * @param mallTestSchoolRecord 校考院校记录
     * @return 结果
     */
    @Override
    public int updateMallTestSchoolRecord(MallTestSchoolRecord mallTestSchoolRecord)
    {
        mallTestSchoolRecord.setUpdateTime(DateUtils.getNowDate());
        return mallTestSchoolRecordMapper.updateMallTestSchoolRecord(mallTestSchoolRecord);
    }

    /**
     * 批量删除校考院校记录
     *
     * @param testInfoIds 需要删除的校考院校记录主键
     * @return 结果
     */
    @Override
    public int deleteMallTestSchoolRecordByTestInfoIds(Long[] testInfoIds)
    {
        return mallTestSchoolRecordMapper.deleteMallTestSchoolRecordByTestInfoIds(testInfoIds);
    }

    /**
     * 删除校考院校记录信息
     *
     * @param testInfoId 校考院校记录主键
     * @return 结果
     */
    @Override
    public int deleteMallTestSchoolRecordByTestInfoId(Long testInfoId)
    {
        return mallTestSchoolRecordMapper.deleteMallTestSchoolRecordByTestInfoId(testInfoId);
    }

    /** 导入校考院校记录
     *
     * @param list
     * @param updateSupport
     * @param operName
     * @return
     */
    @Override
    public String importTestSchoolRecord(List<MallTestSchoolRecordBo> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入校考院校记录不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;

        List<MallTestSchool> testSchoolList = mallTestSchoolMapper.selectMallTestSchoolList(null);

        MallMajorClass mallMajorClass = new MallMajorClass();
        List<MallMajorClass> majorClassList = mallMajorClassMapper.selectMallMajorClassList(mallMajorClass);
        List<MallTestSchoolRecord> schoolRecordList = mallTestSchoolRecordMapper.selectMallTestSchoolRecordList(null);

        List<MallTestSchoolRecordBo> insertSchoolRecordList = new ArrayList<>();
        List<MallTestSchoolRecordBo> updateSchoolRecordList = new ArrayList<>();

        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MallTestSchoolRecordBo item : list)
        {
            try
            {
                Optional<MallTestSchool> firstTestSchool = testSchoolList.stream().filter(s -> s.getSchoolName().equals(item.getSchoolName())).findFirst();
                Optional<MallMajorClass> firstMajorClass = majorClassList.stream().filter(s -> s.getMajorName().equals(item.getMajorName())).findFirst();


                    if (firstTestSchool.isPresent()) {
                        MallTestSchool testSchool = firstTestSchool.orElse(null);
                        item.setTestSchoolId(testSchool.getTestSchoolId());
                    }
                    if (firstMajorClass.isPresent()) {
                        MallMajorClass majorClass = firstMajorClass.orElse(null);
                        item.setMajorId(majorClass.getMajorId());
                    }
                    item.setNeedCulture(1);
                    item.setPassScore(60);
                    item.setFullScore(100);

                    insertSchoolRecordList.add(item);
                    // this.insertMallSchoolAppraise(new_major);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "校考院校记录" + item.getSchoolName() + " 导入成功");

                    // 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() + " 已存在");
                        // }
                    // }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "校考院校记录" + item.getSchoolName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (insertSchoolRecordList.size() > 0) {
            this.insertSchoolRecordBatch(insertSchoolRecordList);
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 批量新增校考院校记录
     *
     * @param list 校考院校记录
     * @return 结果
     */
    @Override
    public int insertSchoolRecordBatch(List<MallTestSchoolRecordBo> list)
    {
        mallTestSchoolRecordMapper.insertSchoolRecordBatch(list);
        return 1;
    }
}
