package com.srs.comprehensive.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.mapper.SrsStuYearMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.KnzzZsgApplyMapper;
import com.srs.comprehensive.domain.KnzzZsgApply;
import com.srs.comprehensive.service.IKnzzZsgApplyService;

import static com.srs.common.utils.DateUtils.getNowDate;
import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 中职升高职补助Service业务层处理
 *
 * @author srs
 * @date 2025-05-12
 */
@Service
public class KnzzZsgApplyServiceImpl extends ServiceImpl<KnzzZsgApplyMapper,KnzzZsgApply> implements IKnzzZsgApplyService {
    @Autowired
    private KnzzZsgApplyMapper knzzZsgApplyMapper;

    @Autowired
    private SrsStuYearMapper _yearMapper;


    @Override
    public AjaxResult doReApply( KnzzZsgApply param){
        try{
            KnzzZsgApply apply = knzzZsgApplyMapper.selectById(param.getId());
            if(apply == null){
                throw new Exception("申请不存在");
            }
            if(!Objects.equals(apply.getStuNo(), getUsername())){
                throw new Exception("只能重新申请自己的申请");
            }
            if(!Objects.equals(apply.getApplyStatus(), "10")){
                throw new Exception("申请已处理");
            }
            UpdateWrapper<KnzzZsgApply> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", apply.getId())
                    // 需要重置的审批流程相关字段
                    .set("fdy_cmt", null)
                    .set("fdy_no", null)
                    .set("fdy_date", null)
                    .set("fdy_sign", null)
                    .set("xw_cmt", null)
                    .set("xw_no", null)
                    .set("xw_date", null)
                    .set("xw_sign", null)
                    .set("xg_cmt", null)
                    .set("xg_no", null)
                    .set("xg_date", null)
                    .set("xg_sign", null)
                    // 其它需要更新的字段
                    .set("apply_status", "1")
                    .set("apply_date", getNowDate())
                    .set("update_time", getNowDate())
                    .set("update_by", getUsername())
                    .set("in_time", param.getInTime())
                    .set("zzxx", param.getZzxx())
                    .set("family_num", param.getFamilyNum())
                    .set("month_money", param.getMonthMoney())
                    .set("per_money", param.getPerMoney())
                    .set("money_src", param.getMoneySrc())
                    .set("apply_reason_num", param.getApplyReasonNum())
                    .set("apply_reason", param.getApplyReason())
                    .set("apply_reason1", param.getApplyReason1())
                    .set("apply_extra_reason", param.getApplyExtraReason())
                    .set("apply_file", param.getApplyFile())
                    .set("apply_sign", param.getApplySign());

            int res = knzzZsgApplyMapper.update(null, updateWrapper);

            if(res == 0){
                throw new Exception("重新申请失败");
            }

            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    @Override
    public AjaxResult doEdit(KnzzZsgApply param) {
        try{
//            打印param
            System.out.println(JSON.toJSONString(param));
            KnzzZsgApply apply = knzzZsgApplyMapper.selectById(param.getId());
            if(apply == null){
                throw new Exception("申请不存在");
            }

            if(!Objects.equals(apply.getStuNo(), getUsername())){
                throw new Exception("只能修改自己的申请");
            }

            if(!Objects.equals(apply.getApplyStatus(), "1")){
                throw new Exception("申请已处理");
            }

            apply.setInTime(param.getInTime());
            apply.setZzxx(param.getZzxx());
            apply.setFamilyNum(param.getFamilyNum());
            apply.setMonthMoney(param.getMonthMoney());
            apply.setPerMoney(param.getPerMoney());
            apply.setMoneySrc(param.getMoneySrc());
            apply.setApplyReasonNum(param.getApplyReasonNum());
            apply.setApplyReason(param.getApplyReason());
            apply.setApplyReason1(param.getApplyReason1());
            apply.setApplyExtraReason(param.getApplyExtraReason());
            apply.setApplyFile(param.getApplyFile());
            apply.setApplySign(param.getApplySign());
            apply.setUpdateTime(getNowDate());
            apply.setUpdateBy(getUsername());
            apply.setDiplomaCertificate(param.getDiplomaCertificate());
            int res = knzzZsgApplyMapper.updateById(apply);
            if(res == 0){
                throw new Exception("修改失败");
            }


            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    @Override
    public AjaxResult doAudit(KnzzZsgApply param){
        try{
            KnzzZsgApply apply = knzzZsgApplyMapper.selectById(param.getId());
            if(apply == null){
                throw new Exception("申请不存在");
            }
            if(Objects.equals(param.applyStatus, "2")){
                param.fdyNo = getUsername();
                param.fdyDate = getNowDate();
            }
            if(Objects.equals(param.applyStatus, "3")){
                param.xwNo = getUsername();
                param.xwDate = getNowDate();
            }
            if(Objects.equals(param.applyStatus, "4")){
                param.xgNo = getUsername();
                param.xgDate = getNowDate();
            }

            if(Objects.equals(param.applyStatus, "10")){
                if(Objects.equals(apply.applyStatus, "1")){
                    param.fdyNo = getUsername();
                    param.fdyDate = getNowDate();
                }
                if(Objects.equals(apply.applyStatus, "2")){
                    param.xwNo = getUsername();
                    param.xwDate = getNowDate();
                }
                if(Objects.equals(apply.applyStatus, "3")){
                    param.xgNo = getUsername();
                    param.xgDate = getNowDate();
                }
            }


            param.setUpdateTime(getNowDate());
            param.setUpdateBy(getUsername());
            int res = knzzZsgApplyMapper.updateById(param);
            if(res == 0){
                throw new Exception("审核失败");
            }

            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    @Override
    public AjaxResult doCancel(Long id){
        try{

            KnzzZsgApply apply = knzzZsgApplyMapper.selectById(id);
            if(apply == null){
                throw new Exception("申请不存在");
            }
            if(!Objects.equals(apply.getStuNo(), getUsername())){
                throw new Exception("只能取消自己的申请");
            }
            if(!Objects.equals(apply.getApplyStatus(), "1")){
                throw new Exception("申请已处理");
            }
            int res = knzzZsgApplyMapper.deleteById(id);
            if(res == 0){
                throw new Exception("取消失败");
            }

            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }




    //填写中职升高职申请表
    @Override
    public AjaxResult doApply(KnzzZsgApply param){
        try{
            String stuNo = getUsername();
            param.stuNo = getUsername();
            param.setCreateTime(getNowDate());
            param.setCreateBy(getUsername());
            QueryWrapper<SrsStuYear> yearQuery = new QueryWrapper<>();
            yearQuery.eq("status","1");
            yearQuery.last("limit 1");
            SrsStuYear year = _yearMapper.selectOne(yearQuery);
            if(year == null){
                throw new Exception("当前学年未启用");
            }

            QueryWrapper<KnzzZsgApply> applyQuery = new QueryWrapper<>();
            applyQuery.eq("stu_no",stuNo);
            applyQuery.eq("stu_year_id",year.getId());
            applyQuery.ne("apply_status","10");
            List<KnzzZsgApply> applyList = knzzZsgApplyMapper.selectList(applyQuery);
            if(applyList!= null && !applyList.isEmpty()){
                throw new Exception("当前学年已提交申请");
            }

            param.stuYearId = year.getId();
            param.setApplyDate(getNowDate());
            param.setApplyStatus("1");

            int res = knzzZsgApplyMapper.insert(param);
            if(res == 0){
                throw new Exception("提交申请失败");
            }



            return AjaxResult.success();
        }catch (Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }



    @Override
    public List<KnzzZsgApply> listView(KnzzZsgApply param){
        return knzzZsgApplyMapper.listView(param);
    }

    @Override
    public AjaxResult doMany(List<KnzzZsgApply> param){
        try{
            List<KnzzZsgApply> updateList = new ArrayList<>();
            param.forEach(x->{
                KnzzZsgApply apply = knzzZsgApplyMapper.selectById(x.getId());
                if (apply != null){
                    if(Objects.equals(x.applyStatus, "2")){
                        x.fdyNo = getUsername();
                        x.fdyDate = getNowDate();
                    }
                    if(Objects.equals(x.applyStatus, "3")){
                        x.xwNo = getUsername();
                        x.xwDate = getNowDate();
                    }
                    if(Objects.equals(x.applyStatus, "4")){
                        x.xgNo = getUsername();
                        x.xgDate = getNowDate();
                    }
                    if(Objects.equals(x.applyStatus, "10")){
                        if(Objects.equals(apply.applyStatus, "1")){
                            x.fdyNo = getUsername();
                            x.fdyDate = getNowDate();
                        }
                        if(Objects.equals(apply.applyStatus, "2")){
                            x.xwNo = getUsername();
                            x.xwDate = getNowDate();
                        }
                        if(Objects.equals(apply.applyStatus, "3")){
                            x.xgNo = getUsername();
                            x.xgDate = getNowDate();
                        }
                    }
                    x.setUpdateTime(getNowDate());
                    x.setUpdateBy(getUsername());
                    updateList.add(x);
                }
            });
            boolean res = this.updateBatchById(updateList);

            if(!res){
                throw new Exception("批量更新失败");
            }

            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }




    /**
     * 查询中职升高职补助
     *
     * @param id 中职升高职补助主键
     * @return 中职升高职补助
     */
    @Override
    public KnzzZsgApply selectKnzzZsgApplyById(Long id) {
        return knzzZsgApplyMapper.selectKnzzZsgApplyById(id);
    }

    /**
     * 查询中职升高职补助列表
     *
     * @param knzzZsgApply 中职升高职补助
     * @return 中职升高职补助
     */
    @Override
    public List<KnzzZsgApply> selectKnzzZsgApplyList(KnzzZsgApply knzzZsgApply) {
        return knzzZsgApplyMapper.selectKnzzZsgApplyList(knzzZsgApply);
    }

    /**
     * 新增中职升高职补助
     *
     * @param knzzZsgApply 中职升高职补助
     * @return 结果
     */
    @Override
    public int insertKnzzZsgApply(KnzzZsgApply knzzZsgApply) {
                knzzZsgApply.setCreateTime(DateUtils.getNowDate());
            return knzzZsgApplyMapper.insertKnzzZsgApply(knzzZsgApply);
    }

    /**
     * 修改中职升高职补助
     *
     * @param knzzZsgApply 中职升高职补助
     * @return 结果
     */
    @Override
    public int updateKnzzZsgApply(KnzzZsgApply knzzZsgApply) {
                knzzZsgApply.setUpdateTime(DateUtils.getNowDate());
        return knzzZsgApplyMapper.updateKnzzZsgApply(knzzZsgApply);
    }

    /**
     * 批量删除中职升高职补助
     *
     * @param ids 需要删除的中职升高职补助主键
     * @return 结果
     */
    @Override
    public int deleteKnzzZsgApplyByIds(Long[] ids) {
        return knzzZsgApplyMapper.deleteKnzzZsgApplyByIds(ids);
    }

    /**
     * 删除中职升高职补助信息
     *
     * @param id 中职升高职补助主键
     * @return 结果
     */
    @Override
    public int deleteKnzzZsgApplyById(Long id) {
        return knzzZsgApplyMapper.deleteKnzzZsgApplyById(id);
    }
}
