package com.srs.comprehensive.service.impl;

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

import com.srs.common.core.domain.AjaxResult;
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.SrsHandAllKnrd;
import com.srs.comprehensive.domain.Vo.CphSearch;
import com.srs.comprehensive.mapper.SrsKnrdApprovalRecordMapper;
import com.srs.comprehensive.mapper.SrsStuYearMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.SrsKnrdApplyMapper;
import com.srs.comprehensive.service.ISrsKnrdApplyService;
import org.springframework.transaction.annotation.Transactional;


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

/**
 * 困难认定Service业务层处理
 *
 * @author srs
 * @date 2024-03-19
 */
@Service
public class SrsKnrdApplyServiceImpl implements ISrsKnrdApplyService {
    @Autowired
    private SrsKnrdApplyMapper srsKnrdApplyMapper;
    @Autowired
    private SrsKnrdApprovalRecordMapper srsKnrdApprovalRecordMapper;

    @Autowired
    private SrsStuYearMapper srsStuYearMapper;
    @Override
    public List<SrsKnrdApply> listView(CphSearch param) {
        return srsKnrdApplyMapper.listView(param);
    }

    /**
     * 查询困难认定
     *
     * @param id 困难认定主键
     * @return 困难认定
     */
    @Override
    public SrsKnrdApply selectSrsKnrdApplyById(Long id) {
        return srsKnrdApplyMapper.selectSrsKnrdApplyById(id);
    }

    /**
     * 查询困难认定列表
     *
     * @param srsKnrdApply 困难认定
     * @return 困难认定
     */
    @Override
    public List<SrsKnrdApply> selectSrsKnrdApplyList(SrsKnrdApply srsKnrdApply) {
        return srsKnrdApplyMapper.selectSrsKnrdApplyList(srsKnrdApply);
    }

    /**
     * 新增困难认定
     *
     * @param srsKnrdApply 困难认定
     * @return 结果
     */
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public AjaxResult insertSrsKnrdApply(SrsKnrdApply srsKnrdApply) {
        Long step = srsKnrdApply.getStep();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        //设置年份
        SrsStuYear srsStuYear = srsStuYearMapper.selectSrsStuYearById(srsKnrdApply.getStuYearId());
//        查询当前学生是否已经申请
        //判断有没有这个身份证号码
//        if (srsKnrdApply.getSfzhm().length() != 18) {
//
//            return AjaxResult.error("身份证长度错误");
//        }
        //查询当前学年下有没有申请
        srsKnrdApply.setStuYearId(srsStuYear.getId());
        SrsKnrdApply s = srsKnrdApplyMapper.selectByXhAndYear(srsKnrdApply);
//        SrsKnrdApply selectsfzhm = srsKnrdApplyMapper.selectsfzhm(srsKnrdApply.getSfzhm());
//        if (selectsfzhm == null) {
//            return AjaxResult.error("身份证错误");
//        }
//        //
        if (s != null && s.getStep() == 0 && s.getStuYearId()==srsStuYear.getId() && srsKnrdApply.getId() == null) {
            return AjaxResult.error("本学年已存在申请记录,请不要重复申请");
        }
        //新增
        if (s == null ||  s.getStuYearId() != srsStuYear.getId()) {
            SrsKnrdApprovalRecord record = new SrsKnrdApprovalRecord();
            srsKnrdApply.setCreateId(getUserId());
            if (step == 1) {
                srsKnrdApply.setStep(Long.valueOf(2));
                record.setStatus("提交申请");
                record.setStep(1L);
            } else if (step == 2) {
                srsKnrdApply.setStep(Long.valueOf(3));
                srsKnrdApply.setFdymc(sysUser.getNickName());
                srsKnrdApply.setFdygh(sysUser.getUserName());

            } else if (step == 3) {
                srsKnrdApply.setStep(Long.valueOf(4));
                srsKnrdApply.setEjxyldmc(sysUser.getNickName());
                srsKnrdApply.setEjxyldgh(sysUser.getUserName());
            }else if(step == 4){
                srsKnrdApply.setXsmc(sysUser.getNickName());
                srsKnrdApply.setXsgh(sysUser.getUserName());
            }
            srsKnrdApply.setStuYearId(srsStuYear.getId());
            srsKnrdApply.setApplyYear(srsStuYear.getStuYearName());
            int res = srsKnrdApplyMapper.insertSrsKnrdApply(srsKnrdApply);
            //新增记录
            if(record.getStep() == null){
                record.setStep(0L);
            }
            record.setApplyId(srsKnrdApply.getId());
            record.setApprover(sysUser.getNickName());
            record.setCreateTime(new Date());
            srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(record);

            if (res > 0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("操作失败");
            }
        }
        //更新
        if (srsKnrdApply.getId() != null) {
            SrsKnrdApprovalRecord record = new SrsKnrdApprovalRecord();
            record.setStep(step);
            if (step == 1) {
                srsKnrdApply.setStep(Long.valueOf(2));

            } else if (step== 2) {
                srsKnrdApply.setStep(Long.valueOf(3));
                srsKnrdApply.setFdymc(sysUser.getNickName());
                srsKnrdApply.setFdygh(sysUser.getUserName());

            } else if (step == 3) {
                srsKnrdApply.setStep(Long.valueOf(4));
                srsKnrdApply.setEjxyldmc(sysUser.getNickName());
                srsKnrdApply.setEjxyldgh(sysUser.getUserName());

            }else if(step == 4){
                srsKnrdApply.setXsmc(sysUser.getNickName());
                srsKnrdApply.setXsgh(sysUser.getUserName());

            }else if(step == 0){

                //新增记录

                record.setStep(0L);


            }
            record.setApplyId(srsKnrdApply.getId());
            record.setApprover(sysUser.getNickName());
            record.setCreateTime(new Date());
            srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(record);
            srsKnrdApply.setApplyYear(null);
            srsKnrdApplyMapper.updateSrsKnrdApply(srsKnrdApply);
            return AjaxResult.success();
        }
        srsKnrdApply.setCreateId(getUserId());
        if (step == 1 ) {
            srsKnrdApply.setStep(Long.valueOf(2));

        } else if (step == 2) {
            srsKnrdApply.setStep(Long.valueOf(3));

        } else if (step == 3) {
            srsKnrdApply.setStep(Long.valueOf(4));
            srsKnrdApply.setFdymc(sysUser.getNickName());
            srsKnrdApply.setFdygh(sysUser.getUserName());


        }else if(step == 4){
            srsKnrdApply.setEjxyldmc(sysUser.getNickName());
            srsKnrdApply.setEjxyldgh(sysUser.getUserName());

        }else if(step == 5){
            srsKnrdApply.setXsmc(sysUser.getNickName());
            srsKnrdApply.setXsgh(sysUser.getUserName());
        }
        srsKnrdApply.setStuYearId(srsStuYear.getId());
        srsKnrdApply.setApplyYear(srsStuYear.getStuYearName());
        int res = srsKnrdApplyMapper.insertSrsKnrdApply(srsKnrdApply);
        if (res > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 修改困难认定
     *
     * @param srsKnrdApply 困难认定
     * @return 结果
     */
    @Override
    public int updateSrsKnrdApply(SrsKnrdApply srsKnrdApply) {
        long step = srsKnrdApply.getStep();

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SrsKnrdApprovalRecord srsKnrdApprovalRecord = new SrsKnrdApprovalRecord();
        srsKnrdApprovalRecord.setApplyId(srsKnrdApply.getId());
        srsKnrdApprovalRecord.setApprover(sysUser.getNickName());
        if(step  == 0){
            //保存

            srsKnrdApprovalRecord.setStep(0L);
            srsKnrdApprovalRecord.setCreateTime(new Date());
            srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(srsKnrdApprovalRecord);
        }

        if (srsKnrdApply.getStep() > 1) {

            String bjpyyj = srsKnrdApply.getBjpyyj();
            String ejxyldqmyj = srsKnrdApply.getEjxyldqmyj();
            String xsqmyj = srsKnrdApply.getXsqmyj();
//            if (bjpyyj == "4" || ejxyldqmyj == "4" || xsqmyj == "4") {
//                srsKnrdApply.setStep(Long.valueOf(6));
//            }
//                Long step1 = null;
//                if (srsKnrdApply.getStep()!=6){
//                     step1 = srsKnrdApply.getStep();
//                }
            if(step- 1==2){//辅导员
                srsKnrdApprovalRecord.setStatus( srsKnrdApply.getBjpyyj());
            }
            if(step - 1==3){//学院
                srsKnrdApprovalRecord.setStatus( srsKnrdApply.getEjxyldqmyj());
            }
            if(step - 1==4){//学校
                srsKnrdApprovalRecord.setStatus( srsKnrdApply.getXsqmyj());
            }
            srsKnrdApprovalRecord.setStep(step - 1);
            srsKnrdApprovalRecord.setCreateTime(new Date());

            srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(srsKnrdApprovalRecord);
        }

//        srsKnrdApply.setStep(step);
//        if (srsKnrdApply.getBjpyyj().equals("")){
//
//        }


         if (step == 3) {
            srsKnrdApply.setFdymc(sysUser.getNickName());
            srsKnrdApply.setFdygh(sysUser.getUserName());


        }else if(step == 4){
            srsKnrdApply.setEjxyldmc(sysUser.getNickName());
            srsKnrdApply.setEjxyldgh(sysUser.getUserName());
        }else if (step == 5){
            srsKnrdApply.setXsmc(sysUser.getNickName());
            srsKnrdApply.setXsgh(sysUser.getUserName());
        }
        return srsKnrdApplyMapper.updateSrsKnrdApply(srsKnrdApply);
    }

    /**
     * 批量删除困难认定
     *
     * @param ids 需要删除的困难认定主键
     * @return 结果
     */
    @Override
    public int deleteSrsKnrdApplyByIds(Long[] ids) {
        return srsKnrdApplyMapper.deleteSrsKnrdApplyByIds(ids);
    }

    /**
     * 删除困难认定信息
     *
     * @param id 困难认定主键
     * @return 结果
     */
    @Override
    public int deleteSrsKnrdApplyById(Long id) {
        return srsKnrdApplyMapper.deleteSrsKnrdApplyById(id);
    }

    @Override
    public SrsStudent getInfoxh(String xh) {
        SrsStudent srsStudentList = srsKnrdApplyMapper.getInfoxh(xh);
        if (srsStudentList == null) {
            return null;
        }
        List<SrsFemilyMenber> srsFemilyMenber = srsKnrdApplyMapper.getjtxx(srsStudentList.getStuId());
//        SrsStudent srsStudent = new SrsStudent(srsFemilyMenber);
        srsStudentList.setSrsFemilyMenberList(srsFemilyMenber);
        return srsStudentList;
    }

    /**
     * 批量审核
     *
     * @param srsKnrdApply
     * @return
     */
    @Override
    public int updateAllSrsKnrdApply(SrsHandAllKnrd srsKnrdApply) {
        if (srsKnrdApply.getStep() > 1) {
            for (int id : srsKnrdApply.getIds()) {
                SrsKnrdApprovalRecord srsKnrdApprovalRecord = new SrsKnrdApprovalRecord();
                srsKnrdApprovalRecord.setApplyId((long) id);
                SysUser sysUser = SecurityUtils.getLoginUser().getUser();
                srsKnrdApprovalRecord.setApprover(sysUser.getNickName());
                String bjpyyj = srsKnrdApply.getBjpyyj();
                String ejxyldqmyj = srsKnrdApply.getEjxyldqmyj();
                String xsqmyj = srsKnrdApply.getXsqmyj();
//                if (bjpyyj == "4" || ejxyldqmyj == "4" || xsqmyj == "4") {
//                    srsKnrdApply.setStep(6);
//                }
//                Long step1 = null;
//                if (srsKnrdApply.getStep()!=6){
//                     step1 = srsKnrdApply.getStep();
//                }
                if(srsKnrdApply.getStep() - 1==2){//辅导员
                    srsKnrdApprovalRecord.setStatus( srsKnrdApply.getBjpyyj());
                }
                if(srsKnrdApply.getStep() - 1==3){//学院
                    srsKnrdApprovalRecord.setStatus( srsKnrdApply.getEjxyldqmyj());
                }
                if(srsKnrdApply.getStep() - 1==4){//学校
                    srsKnrdApprovalRecord.setStatus( srsKnrdApply.getXsqmyj());
                }
                srsKnrdApprovalRecord.setStep((long) (srsKnrdApply.getStep() - 1));
                srsKnrdApprovalRecord.setCreateTime(new Date());
                srsKnrdApprovalRecordMapper.batchInsertSrsKnrdApprovalRecord(srsKnrdApprovalRecord);
            }
        }
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if(srsKnrdApply.getStep() == 4){
            srsKnrdApply.setEjxyldmc(sysUser.getNickName());
            srsKnrdApply.setEjxyldgh(sysUser.getUserName());
        }
        if(srsKnrdApply.getStep() == 5){
            srsKnrdApply.setXsmc(sysUser.getNickName());
            srsKnrdApply.setXsgh(sysUser.getUserName());
        }
        return srsKnrdApplyMapper.updateAllSrsKnrdApply(srsKnrdApply);
    }

    @Override
    public SrsKnrdApply selectByXhAndYear(SrsKnrdApply srsKnrdApply) {


        return srsKnrdApplyMapper.selectByXhAndYear(srsKnrdApply);
    }

    @Override
    public int fdySave(SrsKnrdApply srsKnrdApply) {

        //辅导员保存
        int row = srsKnrdApplyMapper.updateSrsKnrdApply(srsKnrdApply);
        //保存流程记录
        SrsKnrdApprovalRecord record = new SrsKnrdApprovalRecord();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();

        String username = user.getNickName();//操作人
        record.setStep(0L);
        record.setApprover(username);
        record.setCreateTime(new Date());
        record.setApplyId(srsKnrdApply.getId());
        srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(record);
        return row;
    }

    @Override
    public int reject(SrsKnrdApprovalRecord record) {
        //驳回
        SrsKnrdApply srsKnrdApply = new SrsKnrdApply();
        srsKnrdApply.setStep(record.getTargetStep());
        srsKnrdApply.setId(record.getApplyId());

        //驳回至学生
        if(record.getTargetStep() == 0){
            //清空所有已经
            srsKnrdApply.setBjpyyj("");
            srsKnrdApply.setFdygh("");
            srsKnrdApply.setFdyqm("");
            srsKnrdApply.setFdyqmrq("");
            srsKnrdApply.setFdymc("");

            srsKnrdApply.setEjxyldgh("");
            srsKnrdApply.setEjxyldmc("");
            srsKnrdApply.setEjxyldqmrq("");
            srsKnrdApply.setEjxyldqm("");
            srsKnrdApply.setEjxyldqmyj("");

            srsKnrdApply.setXsqmyj("");
            srsKnrdApply.setXsgh("");
            srsKnrdApply.setXsqm("");
            srsKnrdApply.setXsmc("");
            srsKnrdApply.setXsqmrq("");
        }
        //驳回辅导员
        if(record.getTargetStep() == 2){
            //清空所有已经
            srsKnrdApply.setBjpyyj("");
            srsKnrdApply.setFdygh("");
            srsKnrdApply.setFdyqm("");
            srsKnrdApply.setFdyqmrq("");
            srsKnrdApply.setFdymc("");
            srsKnrdApply.setEjxyldgh("");
            srsKnrdApply.setEjxyldmc("");
            srsKnrdApply.setEjxyldqmrq("");
            srsKnrdApply.setEjxyldqm("");
            srsKnrdApply.setEjxyldqmyj("");
            srsKnrdApply.setXsqmyj("");
            srsKnrdApply.setXsgh("");
            srsKnrdApply.setXsqm("");
            srsKnrdApply.setXsmc("");
            srsKnrdApply.setXsqmrq("");
        }
        //驳回辅导员学务
        if(record.getTargetStep() == 3){
            srsKnrdApply.setEjxyldgh("");
            srsKnrdApply.setEjxyldmc("");
            srsKnrdApply.setEjxyldqmrq("");
            srsKnrdApply.setEjxyldqm("");
            srsKnrdApply.setEjxyldqmyj("");
            srsKnrdApply.setXsqmyj("");
            srsKnrdApply.setXsgh("");
            srsKnrdApply.setXsqm("");
            srsKnrdApply.setXsmc("");
            srsKnrdApply.setXsqmrq("");
        }
        srsKnrdApplyMapper.updateSrsKnrdApply(srsKnrdApply);
        //增加记录
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        record.setApprover(sysUser.getNickName());
        record.setCreateTime(new Date());
        return srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(record);

    }

    @Override
    @Transactional
    public int revoke(SrsKnrdApply srsKnrdApply) {
        //先查询当前的认定信息
        SrsKnrdApply result = srsKnrdApplyMapper.selectSrsKnrdApplyById(srsKnrdApply.getId());
        if(result.getStep() == srsKnrdApply.getStep()){
            Long step = result.getStep();
            SrsKnrdApply parma = new SrsKnrdApply();
            parma.setId(result.getId());
            parma.setStep(result.getStep());
            if(step == 2){
                //学生撤回
                parma.setTargetStep(0L);

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

            }
            else if (step == 5){
                //学工处
                parma.setTargetStep(4L);

            }
            int revoke = srsKnrdApplyMapper.revoke(parma);

            if(revoke > 0){
                //撤回成功增加一条操作记录
                SrsKnrdApprovalRecord record = new SrsKnrdApprovalRecord();
                SysUser sysUser = SecurityUtils.getLoginUser().getUser();
                record.setApprover(sysUser.getNickName());
                record.setApplyId(result.getId());
                record.setCreateTime(new Date());
                record.setStep(-1L);
                srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(record);
            }
            return revoke;
        }else{
            return  0;
        }

    }

    @Override
    @Transactional
    public int fdyRevoke(SrsKnrdApply srsKnrdApply) {
        //先查询当前的认定信息
        SrsKnrdApply result = srsKnrdApplyMapper.selectSrsKnrdApplyById(srsKnrdApply.getId());
        SrsKnrdApply parma = new SrsKnrdApply();
        parma.setId(result.getId());

        int revoke = srsKnrdApplyMapper.fdyRevoke(parma);
        if(revoke > 0){
            //撤回成功增加一条操作记录
            SrsKnrdApprovalRecord record = new SrsKnrdApprovalRecord();
            SysUser sysUser = SecurityUtils.getLoginUser().getUser();
            record.setApprover(sysUser.getNickName());
            record.setApplyId(result.getId());
            record.setCreateTime(new Date());
            record.setStep(-1L);
            srsKnrdApprovalRecordMapper.insertSrsKnrdApprovalRecord(record);
        }
        return revoke;


    }

    @Override
    public int lock(SrsKnrdApply srsKnrdApply) {
        return srsKnrdApplyMapper.lock(srsKnrdApply);
    }

    @Override
    public int unlock(SrsKnrdApply srsKnrdApply) {
        return srsKnrdApplyMapper.unlock(srsKnrdApply);
    }

    @Override
    public List<SrsKnrdApply> countXw(CphSearch cphSearch) {
        return srsKnrdApplyMapper.countXw(cphSearch);
    }


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


}
