package com.ruoyi.mall.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallSchoolAcceptRecordMapper;
import com.ruoyi.mall.domain.MallSchoolAcceptRecord;
import com.ruoyi.mall.service.IMallSchoolAcceptRecordService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 院校录取数据Service业务层处理
 *
 * @author lsm
 * @date 2023-10-19
 */
@Service
public class MallSchoolAcceptRecordServiceImpl implements IMallSchoolAcceptRecordService
{
    @Resource
    private MallSchoolAcceptRecordMapper mallSchoolAcceptRecordMapper;

    /**
     * 查询院校录取数据
     *
     * @param schoolAcceptRecordId 院校录取数据主键
     * @return 院校录取数据
     */
    @Override
    public MallSchoolAcceptRecord selectMallSchoolAcceptRecordBySchoolAcceptRecordId(Long schoolAcceptRecordId)
    {
        return mallSchoolAcceptRecordMapper.selectMallSchoolAcceptRecordBySchoolAcceptRecordId(schoolAcceptRecordId);
    }

    /**
     * 查询院校录取数据列表
     *
     * @param mallSchoolAcceptRecord 院校录取数据
     * @return 院校录取数据
     */
    @Override
    public List<MallSchoolAcceptRecord> selectMallSchoolAcceptRecordList(MallSchoolAcceptRecord mallSchoolAcceptRecord)
    {
        return mallSchoolAcceptRecordMapper.selectCMSMallSchoolAcceptRecordList(mallSchoolAcceptRecord);
    }

    /**
     * 新增院校录取数据
     *
     * @param mallSchoolAcceptRecord 院校录取数据
     * @return 结果
     */
    @Override
    public int insertMallSchoolAcceptRecord(MallSchoolAcceptRecord mallSchoolAcceptRecord)
    {
        mallSchoolAcceptRecord.setCreateAt(DateUtils.getTime());
        mallSchoolAcceptRecord.setDelFlag("0");
        return mallSchoolAcceptRecordMapper.insertMallSchoolAcceptRecord(mallSchoolAcceptRecord);
    }

    /**
     * 修改院校录取数据
     *
     * @param mallSchoolAcceptRecord 院校录取数据
     * @return 结果
     */
    @Override
    public int updateMallSchoolAcceptRecord(MallSchoolAcceptRecord mallSchoolAcceptRecord)
    {
        mallSchoolAcceptRecord.setUpdateAt(DateUtils.getTime());
        return mallSchoolAcceptRecordMapper.updateMallSchoolAcceptRecord(mallSchoolAcceptRecord);
    }

    /**
     * 批量删除院校录取数据
     *
     * @param schoolAcceptRecordIds 需要删除的院校录取数据主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAcceptRecordBySchoolAcceptRecordIds(Long[] schoolAcceptRecordIds)
    {
        return mallSchoolAcceptRecordMapper.deleteMallSchoolAcceptRecordBySchoolAcceptRecordIds(schoolAcceptRecordIds);
    }

    /**
     * 删除院校录取数据信息
     *
     * @param schoolAcceptRecordId 院校录取数据主键
     * @return 结果
     */
    @Override
    public int deleteMallSchoolAcceptRecordBySchoolAcceptRecordId(Long schoolAcceptRecordId)
    {
        return mallSchoolAcceptRecordMapper.deleteMallSchoolAcceptRecordBySchoolAcceptRecordId(schoolAcceptRecordId);
    }

    /**
     * 各层次院校录取分析列表
     *
     * @param batchId 批次ID
     * @param years 年份
     * @return 层次院校录取分析集合
     */
    @Override
    public List<Map<String, Object>> selectAllLevelsMallSchoolAcceptRecordList(Long batchId, String years) {
        return mallSchoolAcceptRecordMapper.selectAllLevelsMallSchoolAcceptRecordList(batchId, years);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(){
        // 学校录取记录
        List<MallSchoolAcceptRecord> records = mallSchoolAcceptRecordMapper.selectCMSMallSchoolAcceptRecordList(null);
        int count = 0;
        for (MallSchoolAcceptRecord record : records) {
            String acceptYear = record.getAcceptYear();
            Long schoolId = record.getSchoolId();
            Long batchId = record.getBatchId();
            Long schoolAcceptRecordId = record.getSchoolAcceptRecordId();
            String subjectType = record.getSubjectType();
            int i = mallSchoolAcceptRecordMapper.selectSum(schoolId, acceptYear, batchId, subjectType);

            MallSchoolAcceptRecord recordUpdate = new MallSchoolAcceptRecord();
            recordUpdate.setSchoolAcceptRecordId(schoolAcceptRecordId);
            recordUpdate.setAcceptNum(i);
            recordUpdate.setUpdateAt(DateUtils.getTime());

            mallSchoolAcceptRecordMapper.updateMallSchoolAcceptRecord(recordUpdate);
            count++;
        }
        System.out.println("+++++++++++" + count);
    }


}
