package com.srs.comprehensive.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.domain.Vo.CphIamSearchVo;
import com.srs.comprehensive.domain.Vo.CphIamVo;
import com.srs.comprehensive.domain.Vo.CphRecord;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.comprehensive.mapper.CphIamMapper;
import com.srs.comprehensive.mapper.SrsAddRecordMapper;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.comprehensive.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.srs.common.utils.SecurityUtils.getUserId;
import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 思想品德Service业务层处理
 * 
 * @author zhy
 * @date 2023-07-03
 */
@Service
public class CphIamServiceImpl implements ICphIamService
{
    @Autowired
    private CphIamMapper cphIamMapper;
    @Autowired
    private SrsStudentMapper SrsStudentMapper;
    @Autowired
    private ICphRulesService _rulesService;
    @Autowired
    private ICphAuditDetailsService _auditService;
    @Autowired
    private ISrsStudentService _stuService;
    @Autowired
    private ISrsAddRecordService srsAddRecordService; //加分记录
    //加分记录
    @Autowired
    private SrsAddRecordMapper srsAddRecordMapper;


    @Override
    public AjaxResult deleteError(Long id) {
        try {
            SrsAddRecord record = srsAddRecordMapper.selectById(id);
            if (record == null) {
                throw new Exception("该记录不存在");
            }

            int stuYearId = record.getStuYearId();
            String stuNo = record.getStuNo();

            QueryWrapper<SrsAddRecord> recordQueryWrapper = new QueryWrapper<>();
            recordQueryWrapper.eq("iam_id", record.getIamId());
            List<SrsAddRecord> recordList = srsAddRecordMapper.selectList(recordQueryWrapper);
            if (recordList.size() == 1) {
                throw new Exception("该记录没有重复，不可删除");
            }

            int res = srsAddRecordMapper.deleteSrsAddRecordById(id);
            if (res == 0) {
                throw new Exception("删除失败");
            }
            cphIamMapper.executeTestIam(stuYearId, stuNo);
            return AjaxResult.success();
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }


    @Override
    public List<Map<String, Object>> listErrorAddRecord(CphSearch param) {
        return cphIamMapper.listErrorAddRecord(param);
    }


    @Override
    public List<CphIamSearchVo> listIamByStuNoYear(CphSearch param) {
        return cphIamMapper.listIamByStuNoYear(param);
    }

    @Override
    public List<CphRecord> getOwnRecord(String stuNo, String year) {
        return cphIamMapper.getOwnRecord(stuNo, year);
    }


    /**
     * 查询思想品德
     * 
     * @param id 思想品德主键
     * @return 思想品德
     */
    @Override
    public CphIam selectCphIamById(Long id)
    {
        return cphIamMapper.selectCphIamById(id);
    }

    /**
     * 查询思想品德列表 连表
     * 
     * @param cphIam 思想品德
     * @return 思想品德
     */
    @Override
    public List<CphIam> selectCphIamList(CphIam cphIam)
    {

        return cphIamMapper.selectCphIamList(cphIam);
    }

    /**
     * 查询思想品德列表 连表查询
     *
     * @param cphIam 思想品德
     * @return 思想品德集合
     */
    @Override
    public List<CphIam> selectCphIamListJoin(CphIam cphIam){
        System.out.println(cphIam);
        List<CphIam> cphIam1 = cphIamMapper.selectCphIamListJoin(cphIam);

//        for (CphIam iam : cphIam1) {
//            SrsStudent srsStudent = SrsStudentMapper.selectSrsStudentByStuId(iam.getToAdd());//加分人id
//            if (srsStudent != null){
//                iam.setStudent(srsStudent);
//            }
//        }
        //System.out.println("K");
        //System.out.println(cphIam1);
        return cphIam1;
    }

    /**
     * 查询学生思想品德总分
     * @param cphIam
     * @return
     */
    public List<CphIdeologyScore> selectCphIamSumList(CphIdeologyScore cphIam){
        return cphIamMapper.selectCphIamSumList(cphIam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCphIamCounsellor(CphIamVo cphIamVo) {
        CphRules rule = _rulesService.selectCphRulesByRuleId(cphIamVo.getRuleId());//规则信息
        SrsStudent stu = _stuService.selectSrsStudentByStuNo(cphIamVo.getStuNo());//学生信息
        cphIamVo.setCreateTime(DateUtils.getNowDate());
        CphIam cphIam = new CphIam();
        cphIam.setStuNo(cphIamVo.getStuNo());
        cphIam.setSubmitter(getUserId());
        cphIam.setToAdd(stu.getStuId());//加分人id
        cphIam.setIamScore(rule.getMaxScore());//得分
        cphIam.setRuleId(cphIamVo.getRuleId());
        cphIam.setMaterial(cphIamVo.getMaterial());//佐证材料
        cphIam.setDescription(cphIamVo.getDescription());
        cphIam.setAuditStatus("6");
        cphIam.setStuYearId(cphIamVo.getStuYearId());
        cphIam.setCreateTime(DateUtils.getNowDate());
        cphIam.setCreateBy(getUsername());
        cphIamMapper.insertCphIam(cphIam);//新增思想品德
        CphAuditDetails temp = new CphAuditDetails();//  审核记录表
        temp.setProjectId(cphIam.getId());//cph_iam的id
        temp.setSubmitterId(stu.getStuId());//加分人id
        temp.setSubmitterName(stu.getName());// 加分人姓名
        temp.setProjectName(rule.getRuleName());//加分名称
        temp.setProjectTypeId(cphIamVo.getRuleId());//项目类别
        temp.setStatusCode( 6L );//审核状态
        temp.setCreateTime(DateUtils.getNowDate());
        temp.setCreateBy(getUsername());
        _auditService.insertCphAuditDetails(temp);//审核记录表
        SrsAddRecord srsAddRecord=new SrsAddRecord();//加分记录表
        srsAddRecord.setStuNo(stu.getStuNo());//加分人学号
        srsAddRecord.setApplicant(getUserId());//申请人
        srsAddRecord.setRuleId(cphIamVo.getRuleId());//规则类别标示
        srsAddRecord.setStuYearId(Math.toIntExact(cphIamVo.getStuYearId()));//学年id
        srsAddRecord.setDescription(cphIamVo.getDescription());//加分描述
        srsAddRecord.setMaterial(cphIamVo.getMaterial());//佐证材料
        srsAddRecord.setRecordScore(rule.getMaxScore());//加分分数
        srsAddRecord.setStatusCode("6");//审核状态
        srsAddRecord.setCreateTime(DateUtils.getNowDate());
        srsAddRecord.setIamId(cphIam.getId());
        srsAddRecord.setCreateBy(getUsername());
        int i = srsAddRecordMapper.insertAllAddRecordInt(srsAddRecord);
        if (i > 0){
            cphIamMapper.executeTestIam(Math.toIntExact(cphIamVo.getStuYearId()),stu.getStuNo());
        }
        return i;
    }



    /**
     * 新增思想品德
     * 
     * @param cphIam 思想品德
     * @return 结果
     */
    @Override
    public int insertCphIam(CphIam cphIam)
    {
        CphRules rule = _rulesService.selectCphRulesByRuleId(cphIam.getRuleId());
        cphIam.setCreateTime(DateUtils.getNowDate());

        int res = cphIamMapper.insertCphIam(cphIam);

        long id = cphIam.getId();
        /**
         *提交时同时添加到CphAuditDetails
         * @author zhy
         **/
        SrsStudent stu = _stuService.selectSrsStudentByStuId(cphIam.getToAdd());
        CphAuditDetails temp = new CphAuditDetails();
        temp.setProjectId(id);
        temp.setSubmitterId(cphIam.getToAdd());
        temp.setSubmitterName(stu.getName());
        temp.setProjectName(rule.getRuleName());
        temp.setProjectTypeId(rule.getParentId());
        temp.setStatusCode( 1L );
        _auditService.insertCphAuditDetails(temp);
        return res;
    }

    /**
     * 修改思想品德
     * 
     * @param cphIam 思想品德
     * @return 结果
     */
    @Override
    public int updateCphIam(CphIam cphIam)
    {
        cphIam.setUpdateTime(DateUtils.getNowDate());
        return cphIamMapper.updateCphIam(cphIam);
    }

    /**
     * 批量删除思想品德
     * 
     * @param ids 需要删除的思想品德主键
     * @return 结果
     */
    @Override
    public int deleteCphIamByIds(Long[] ids)
    {
        return cphIamMapper.deleteCphIamByIds(ids);
    }

    /**
     * 删除思想品德信息
     * 
     * @param id 思想品德主键
     * @return 结果
     */
    @Override
    public int deleteCphIamById(Long id)
    {
        return cphIamMapper.deleteCphIamById(id);
    }


    }
