package com.srs.comprehensive.service.impl;

import java.util.Date;
import java.util.List;

import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.core.domain.model.LoginUser;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.comprehensive.mapper.SrsKnrdApplyMapper;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.comprehensive.mapper.SrsZxjApplyMapper;
import com.srs.comprehensive.mapper.SrsZxjApprovalRecordMapper;
import com.srs.comprehensive.service.ISrsZxjApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author srs
 * @date 2024-03-17
 */
@Service
public class SrsZxjApplyServiceImpl extends ServiceImpl<SrsZxjApplyMapper, SrsZxjApply> implements ISrsZxjApplyService {
    @Autowired
    private SrsZxjApplyMapper srsZxjApplyMapper;

    @Autowired
    private SrsStudentMapper srsStudentMapper;

    @Autowired
    private SrsKnrdApplyMapper srsKnrdApplyMapper;

    @Autowired
    private SrsZxjApprovalRecordMapper srsZxjApprovalRecordMapper;

    @Override
    public SrsZxjApply nextApply(CphSearch param) {

        return srsZxjApplyMapper.nextApply(param);
    }

    @Override
    public List<SrsZxjApply> listView(CphSearch param){
        return srsZxjApplyMapper.listView(param);
    }

    @Override
    public List<SrsZxjApply> listRecordView(CphSearch param) {
        return srsZxjApplyMapper.listRecordView(param);
    }

    //批量
    @Override
    public int batchUpdate(SrsZxjApply srsZxjApply) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Long step = srsZxjApply.getStep();
        //添加记录
        Long[] ids = srsZxjApply.getIds();
        for (Long id : ids) {
            addRecord(step,id,srsZxjApply.getStatus(),srsZxjApply.getRemark());
        }
        //如果是驳回，设置重新提交
        if("2".equals(srsZxjApply.getStatus())){
            srsZxjApply.setStep(srsZxjApply.getTargetStep());
//            srsZxjApply.setStatus("未审核");
//            srsZxjApply.setStatus1("未审核");
//            srsZxjApply.setStatus2("未审核");
//            srsZxjApply.setStatus3("未审核");
            if(srsZxjApply.getTargetStep() == 0){
                //清除所有审批过的

                srsZxjApply.setBjyjdj("");
                srsZxjApply.setFdymc("");
                srsZxjApply.setFdygh("");
                srsZxjApply.setFdyqmrq("");
                srsZxjApply.setFdyqm("");
                //清除二级学院的

                srsZxjApply.setEjxyyjdj("");
                srsZxjApply.setEjxyldgh("");
                srsZxjApply.setEjxyldmc("");
                srsZxjApply.setEjxyldqm("");
                srsZxjApply.setEjxyldqmrq("");
                //清除学务的
                srsZxjApply.setXxmc("");
                srsZxjApply.setXxgh("");

                srsZxjApply.setZzdj("");
                srsZxjApply.setXxqm("");
                srsZxjApply.setXxyjrq("");
            }
            //驳回至辅导员

            if(srsZxjApply.getTargetStep() == 1){

                //清除二级学院的

                srsZxjApply.setEjxyyjdj("");
                srsZxjApply.setEjxyldgh("");
                srsZxjApply.setEjxyldmc("");
                srsZxjApply.setEjxyldqm("");
                srsZxjApply.setEjxyldqmrq("");
                //清除学务的
                srsZxjApply.setXxmc("");
                srsZxjApply.setXxgh("");

                srsZxjApply.setZzdj("");
                srsZxjApply.setXxqm("");
                srsZxjApply.setXxyjrq("");
            }
            //驳回至学务
            if(srsZxjApply.getTargetStep() == 2){

                //清除学务的
                srsZxjApply.setXxmc("");
                srsZxjApply.setXxgh("");

                srsZxjApply.setZzdj("");
                srsZxjApply.setXxqm("");
                srsZxjApply.setXxyjrq("");
            }
        }
        //如果拒绝
        if("0".equals(srsZxjApply.getStatus())){
            srsZxjApply.setStep(-1L);
            srsZxjApply.setStatus("拒绝");
        }

        if("1".equals(srsZxjApply.getStatus())){
            srsZxjApply.setStatus("通过");
        }
        if(step == 2){
            //辅导员
            srsZxjApply.setStatus1(srsZxjApply.getStatus());
        }
        if(step == 3){
            //学务

            srsZxjApply.setEjxyldgh(user.getUserName());
            srsZxjApply.setEjxyldmc(user.getNickName());
            srsZxjApply.setStatus2(srsZxjApply.getStatus());
        }
        if(step == 4){
            //学工
            srsZxjApply.setXxgh(user.getUserName());
            srsZxjApply.setXxmc(user.getNickName());
            srsZxjApply.setStatus3(srsZxjApply.getStatus());
        }
        int result = srsZxjApplyMapper.batchUpdate(srsZxjApply);

        return result;
    }
    private void addRecord(Long step, Long applyId,String status,String remark) {
        //添加历史记录
        SrsZxjApprovalRecord record = new SrsZxjApprovalRecord();
        //获取当前用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        record.setOperator(user.getNickName());
        record.setStep(step);
        record.setStatus(status);
        record.setCreateTime(new Date());
        record.setRemark(remark);
        record.setApplyId(applyId);
        srsZxjApprovalRecordMapper.insertSrsZxjApprovalRecord(record);
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SrsZxjApply selectSrsZxjApplyById(Long id) {
        return srsZxjApplyMapper.selectSrsZxjApplyById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param srsZxjApply 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SrsZxjApply> selectSrsZxjApplyList(SrsZxjApply srsZxjApply) {
        return srsZxjApplyMapper.selectSrsZxjApplyList(srsZxjApply);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param srsZxjApply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSrsZxjApply(SrsZxjApply srsZxjApply) {
//        srsZxjApply.setId();
            return srsZxjApplyMapper.insertSrsZxjApply(srsZxjApply);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param srsZxjApply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSrsZxjApply(SrsZxjApply srsZxjApply) {
        return srsZxjApplyMapper.updateSrsZxjApply(srsZxjApply);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSrsZxjApplyByIds(Long[] ids) {
        //同时删除审核记录
        srsZxjApprovalRecordMapper.deleteSrsZxjApprovalRecordByApplyIds(ids);
        return srsZxjApplyMapper.deleteSrsZxjApplyByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSrsZxjApplyById(Long id) {
        return srsZxjApplyMapper.deleteSrsZxjApplyById(id);
    }

    @Override
    public SrsStudent selectStudentByXh(SrsStudent srsStudent) {
        SrsStudent result = null;
        List<SrsStudent> srsStudents = srsStudentMapper.selectSrsStudentListJoin(srsStudent);
        //根据学号精确查找到一个
        if(srsStudents.size() == 1){
            result = srsStudents.get(0);
            List<SrsFemilyMenber> srsFemilyMenber = srsKnrdApplyMapper.getjtxx(result.getStuId());
//        SrsStudent srsStudent = new SrsStudent(srsFemilyMenber);
            result.setSrsFemilyMenberList(srsFemilyMenber);
        }
        return result;
    }

    @Override
    @Transactional
    public int revoke(SrsZxjApply srsZxjApply) {
        SrsZxjApply result = srsZxjApplyMapper.selectSrsZxjApplyById(srsZxjApply.getId());
        //数据库步骤和前端
        if(result.getStep() == srsZxjApply.getStep()){

            Long step = result.getStep();
            SrsZxjApply parma = new SrsZxjApply();
            parma.setId(result.getId());
            parma.setStep(result.getStep());
            if(step == 1){
                //学生撤回
                parma.setTargetStep(0L);

            }else if (step == 2){
                //辅导员撤回
                parma.setTargetStep(1L);
            }else if (step == 3){
                //二级领导撤回
                parma.setTargetStep(2L);
            }
            else if (step == 4){
                //学工处
                parma.setTargetStep(3L);

            }

            int revoke = srsZxjApplyMapper.revoke(parma);
            if(revoke > 0){
                //撤回成功增加一条操作记录
                SrsZxjApprovalRecord record = new SrsZxjApprovalRecord();
                SysUser sysUser = SecurityUtils.getLoginUser().getUser();
                record.setOperator(sysUser.getNickName());
                record.setApplyId(result.getId());
                record.setCreateTime(new Date());//操作时间
                record.setStep(-1L); //测回操作

                srsZxjApprovalRecordMapper.insertSrsZxjApprovalRecord(record);
            }
            return revoke;
        }
        else{
            return  0;
        }
    }


    @Override
    public int lock(SrsZxjApply srsZxjApply) {
        return srsZxjApplyMapper.lock(srsZxjApply);
    }

    @Override
    public int unlock(SrsZxjApply srsZxjApply) {
        return srsZxjApplyMapper.unlock(srsZxjApply);
    }

    @Override
    public List<SrsZxjApply> countXw(CphSearch param) {
        return srsZxjApplyMapper.countXw(param);
    }

    @Override
    @Transactional
    public int updateSignature(CphSearch cphSearch) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        //当前登录账号
        cphSearch.setGh(sysUser.getUserName());
        // 修改学生的签名
        int row1 = srsZxjApplyMapper.updateStuSignature(cphSearch);
        // 修改辅导员的签名
        int row2 = srsZxjApplyMapper.updateFdySignature(cphSearch);
        //修改二级学院的签名
        int row3 = srsZxjApplyMapper.updateEjxySignature(cphSearch);
        //修改学校签名
        int row4 = srsZxjApplyMapper.updateScSignature(cphSearch);
        return (row1 + row2 + row3 + row4);
    }

    @Override
    public int updateSqly(Long id, String sqly) {
        return srsZxjApplyMapper.updateSqly(id, sqly);
    }

}
