package com.srs.dormitory.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.page.TableDataInfo;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.dormitory.domain.*;
import com.srs.dormitory.domain.Dto.DmsAuditDto;
import com.srs.dormitory.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.dormitory.service.IDmsApprovalProcessService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 宿舍管理审核Service业务层处理
 *
 * @author srs
 * @date 2023-10-31
 */
@Service
public class IDmsApprovalProcessServiceImpl extends ServiceImpl<DmsApprovalProcessMapper,DmsApprovalProcess> implements IDmsApprovalProcessService {
    @Autowired
    private DmsApprovalProcessMapper dmsApprovalProcessMapper;
    @Autowired
    private DmsTransferApplicationMapper transferApplicationMapper;
    @Autowired
    private DmsCheckoutApplicationMapper checkoutApplicationMapper;
    @Autowired
    private DmsHolidayStayApplicationMapper holidayStayApplicationMapper;
    @Autowired
    private SrsDormitoryStudentMapper dormitoryStudentMapper;
    @Autowired
    private DmsDormitoryRecordsMapper dormitoryRecordsMapper;
    @Autowired
    private DmsSemesterMapper semesterMapper;
    @Autowired
    private DmsDormitoryMapper dormitoryMapper;
    @Autowired
    private DmsLiveDetailMapper liveDetailMapper;

    /**
     * 查询宿舍管理审核
     *
     * @param id 宿舍管理审核主键
     * @return 宿舍管理审核
     */
    @Override
    public DmsApprovalProcess selectDmsApprovalProcessById(Long id) {
        return dmsApprovalProcessMapper.selectDmsApprovalProcessById(id);
    }

    /**
     * 查询宿舍管理审核列表
     *
     * @param dmsApprovalProcess 宿舍管理审核
     * @return 宿舍管理审核
     */
    @Override
    public List<DmsApprovalProcess> selectDmsApprovalProcessList(DmsApprovalProcess dmsApprovalProcess) {
        return dmsApprovalProcessMapper.selectDmsApprovalProcessList(dmsApprovalProcess);
    }

    /**
     * 新增宿舍管理审核
     *
     * @param dmsApprovalProcess 宿舍管理审核
     * @return 结果
     */
    @Override
    public int insertDmsApprovalProcess(DmsApprovalProcess dmsApprovalProcess) {
        dmsApprovalProcess.setCreateTime(DateUtils.getNowDate());
        return dmsApprovalProcessMapper.insertDmsApprovalProcess(dmsApprovalProcess);
    }

    /**
     * 修改宿舍管理审核
     *
     * @param dmsApprovalProcess 宿舍管理审核
     * @return 结果
     */
    @Override
    public int updateDmsApprovalProcess(DmsApprovalProcess dmsApprovalProcess) {
        dmsApprovalProcess.setUpdateTime(DateUtils.getNowDate());
        return dmsApprovalProcessMapper.updateDmsApprovalProcess(dmsApprovalProcess);
    }

    /**
     * 批量删除宿舍管理审核
     *
     * @param ids 需要删除的宿舍管理审核主键
     * @return 结果
     */
    @Override
    public int deleteDmsApprovalProcessByIds(Long[] ids) {
        return dmsApprovalProcessMapper.deleteDmsApprovalProcessByIds(ids);
    }

    /**
     * 删除宿舍管理审核信息
     *
     * @param id 宿舍管理审核主键
     * @return 结果
     */
    @Override
    public int deleteDmsApprovalProcessById(Long id) {
        return dmsApprovalProcessMapper.deleteDmsApprovalProcessById(id);
    }

    @Override
    public List<Map<String, Object>> myApproval(DmsApprovalProcess dmsApprovalProcess) {
        return null;
    }

    /**
     * 辅导员审核换宿申请表
     * @param dmsAuditDto
     * @return
     */
    @Override
    public AjaxResult auditTSSQ(DmsAuditDto dmsAuditDto) {
//        String username = SecurityUtils.getUsername();
//        DmsApprovalProcess tssqb = lambdaQuery().eq(DmsApprovalProcess::getApplyType, "TSSQB")
//                .eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .one();
//        DmsTransferApplication application = transferApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        if (application.getStatus() != 0) return AjaxResult.error("您无法审核该申请表");
//        tssqb.setFdyNo(username);
//        tssqb.setFdyTime(DateUtils.getNowDate());
//        tssqb.setFdyComment(dmsAuditDto.getComment());
//        if (dmsAuditDto.getStatus() == 6){
//            SrsDormitoryStudent dormitoryStudent = new LambdaQueryChainWrapper<>(dormitoryStudentMapper)
//                    .eq(SrsDormitoryStudent::getStuNo, application.getStuNo())
//                    .one();
//            application.setStatus(1);
//            application.setTime(dmsAuditDto.getTSSQTime());
//            application.setTargetDormId(dmsAuditDto.getDormId());
//            //计算换宿前住宿时间
//            Calendar endTime = Calendar.getInstance();
//            Calendar startTime = Calendar.getInstance();
//            startTime.setTime(Date.from(dormitoryStudent.getCheckinTime().atZone(ZoneId.systemDefault()).toInstant()));
//            endTime.setTime(dmsAuditDto.getTSSQTime());
//            int i1 = startTime.get(Calendar.DAY_OF_MONTH);
//            int i2 = endTime.get(Calendar.DAY_OF_MONTH);
//            double month = 0;
//            if (i2>=15&&i1<=15) month = 1;
//            int yearDiff = endTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR);
//            int monthDiff = endTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH);
//            double totalMonthDiff = yearDiff * 12 + monthDiff + month;
//            application.setDuration(new BigDecimal(totalMonthDiff+""));
//            tssqb.setFdyStatus("6");
//        }else {
//            tssqb.setFdyStatus(dmsAuditDto.getStatus().toString());
//            application.setStatus(dmsAuditDto.getStatus());
//        }
//        boolean b = updateById(tssqb);
//        if (!b) return AjaxResult.error("修改审核状态失败");
//        int i = transferApplicationMapper.updateById(application);
//        if (i<=0) return AjaxResult.error("修改申请表状态失败");
//        return AjaxResult.success();
        return null;
    }

    /**
     * 辅导员审核退宿申请表
     * @param dmsAuditDto
     * @return
     */
    @Override
    public AjaxResult auditOUT(DmsAuditDto dmsAuditDto) {
//        String username = SecurityUtils.getUsername();
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyType, "OUT")
//                .eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .one();
//        if (!approvalProcess.getFdyStatus().equals("0")) return AjaxResult.error("您无法审核该申请表");
//        DmsCheckoutApplication application = checkoutApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        if (dmsAuditDto.getStatus() == 6){
//            SrsDormitoryStudent dormitoryStudent = new LambdaQueryChainWrapper<>(dormitoryStudentMapper)
//                    .eq(SrsDormitoryStudent::getStuNo, application.getStuNo())
//                    .one();
//            approvalProcess.setFdyStatus("6");
//            //计算住宿时间
//            Calendar endTime = Calendar.getInstance();
//            Calendar startTime = Calendar.getInstance();
//            startTime.setTime(Date.from(dormitoryStudent.getCheckinTime().atZone(ZoneId.systemDefault()).toInstant()));
//            endTime.setTime(application.getOutTime());
//            int i1 = startTime.get(Calendar.DAY_OF_MONTH);
//            int i2 = endTime.get(Calendar.DAY_OF_MONTH);
//            double month = 0;
//            if (i2>=15&&i1<=15) month = 1;
//            int yearDiff = endTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR);
//            int monthDiff = endTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH);
//            double totalMonthDiff = yearDiff * 12 + monthDiff + month;
//            application.setDuration(new BigDecimal(totalMonthDiff));
//            application.setStatus(1);
//        }else {
//            approvalProcess.setFdyStatus(dmsAuditDto.getStatus().toString());
//            application.setStatus(dmsAuditDto.getStatus());
//        }
//        approvalProcess.setFdyNo(username);
//        approvalProcess.setFdyTime(DateUtils.getNowDate());
//        approvalProcess.setFdyComment(dmsAuditDto.getComment());
//        boolean b = updateById(approvalProcess);
//        if (!b) return AjaxResult.error("修改审核状态失败");
//        int i = checkoutApplicationMapper.updateById(application);
//        if (i<=0) return AjaxResult.error("修改申请表状态失败");
//        return AjaxResult.success();
        return null;
    }

    /**
     * 辅导员审核留宿申请
     * @param dmsAuditDto
     * @return
     */
    @Override
    public AjaxResult auditSTAY(DmsAuditDto dmsAuditDto) {
//        String username = SecurityUtils.getUsername();
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyType, "STAY")
//                .eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .one();
//        if (!approvalProcess.getFdyStatus().equals("0")) return AjaxResult.error("您无法审核该申请表");
//        DmsHolidayStayApplication application = holidayStayApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        if (dmsAuditDto.getStatus() == 6){
//            approvalProcess.setFdyStatus("6");
//            application.setStatus(1);
//        }else {
//            approvalProcess.setFdyStatus(dmsAuditDto.getStatus().toString());
//            application.setStatus(dmsAuditDto.getStatus());
//        }
//        approvalProcess.setFdyNo(username);
//        approvalProcess.setFdyTime(DateUtils.getNowDate());
//        approvalProcess.setFdyComment(dmsAuditDto.getComment());
//        boolean b = updateById(approvalProcess);
//        if (!b) return AjaxResult.error("修改审核状态失败");
//        int i = holidayStayApplicationMapper.updateById(application);
//        if (i<=0) return AjaxResult.error("修改申请表状态失败");
//        return AjaxResult.success();
        return null;
    }

    /**
     * 学务查看审核申请表
     * @param approvalProcess
     * @return
     */
    @Override
    public List<Map<String, Object>> workApply(DmsApprovalProcess approvalProcess) {
//        if (approvalProcess.getApplyType().equals("TSSQB")){
//            return transferApplicationMapper.workApply(approvalProcess.getStatus());
//        }else if (approvalProcess.getApplyType().equals("OUT")){
//            return checkoutApplicationMapper.workApply(approvalProcess.getStatus());
//        }else if (approvalProcess.getApplyType().equals("STAY")){
//            return holidayStayApplicationMapper.workApply(approvalProcess.getStatus());
//        }
        return null;
    }

    @Override
    public AjaxResult workAuditTSSQB(DmsAuditDto dmsAuditDto) {
//        String username = SecurityUtils.getUsername();
//        DmsTransferApplication application = transferApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        if (application == null) return AjaxResult.error("id错误");
//        if (application.getStatus() != 1) return AjaxResult.error("该申请表无法审核");
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .eq(DmsApprovalProcess::getApplyType, "TSSQB")
//                .one();
//        if (dmsAuditDto.getStatus() == 6){
//            application.setStatus(2);
//            approvalProcess.setWordkStatus("6");
//        }else {
//            application.setStatus(dmsAuditDto.getStatus());
//            approvalProcess.setWordkStatus(dmsAuditDto.getStatus().toString());
//        }
//        approvalProcess.setWorkNo(username);
//        approvalProcess.setWorkTime(DateUtils.getNowDate());
//        boolean b = updateById(approvalProcess);
//        if (!b) return AjaxResult.error("修改审核状态失败");
//        int i = transferApplicationMapper.updateById(application);
//        if (i<=0) return AjaxResult.error("修改申请表状态失败");
//        return AjaxResult.success();
        return null;
    }

    @Override
    public AjaxResult workAuditOUT(DmsAuditDto dmsAuditDto) {
//        String username = SecurityUtils.getUsername();
//        DmsCheckoutApplication application = checkoutApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        if (application == null) return AjaxResult.error("id错误");
//        if (application.getStatus() != 1) return AjaxResult.error("您无法审核该申请表");
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .eq(DmsApprovalProcess::getApplyType, "OUT")
//                .one();
//        approvalProcess.setWorkTime(DateUtils.getNowDate());
//        approvalProcess.setWorkNo(username);
//        approvalProcess.setWordkStatus(dmsAuditDto.getStatus().toString());
//        if (dmsAuditDto.getStatus() == 6){
//            application.setStatus(2);
//        }else {
//            application.setStatus(dmsAuditDto.getStatus());
//        }
//        boolean b = updateById(approvalProcess);
//        if (!b) return AjaxResult.error("修改审核状态失败");
//        int i = checkoutApplicationMapper.updateById(application);
//        if (i<=0) return AjaxResult.error("修改申请表状态失败");
//        return AjaxResult.success();
        return null;
    }

    /**
     * 学务审核留宿申请
     * @param dmsAuditDto
     * @return
     */
    @Override
    public AjaxResult workAuditSTay(DmsAuditDto dmsAuditDto) {
//        String username = SecurityUtils.getUsername();
//        DmsHolidayStayApplication application = holidayStayApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        if (application == null) return AjaxResult.error("id错误");
//        if (application.getStatus() != 1) return AjaxResult.error("您无法审核该申请表");
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .eq(DmsApprovalProcess::getApplyType, "STAY")
//                .one();
//        approvalProcess.setWorkTime(DateUtils.getNowDate());
//        approvalProcess.setWorkNo(username);
//        approvalProcess.setWordkStatus(dmsAuditDto.getStatus().toString());
//        if (dmsAuditDto.getStatus() == 6){
//            application.setStatus(2);
//        }else {
//            application.setStatus(dmsAuditDto.getStatus());
//        }
//        boolean b = updateById(approvalProcess);
//        if (!b) return AjaxResult.error("修改审核状态失败");
//        int i = holidayStayApplicationMapper.updateById(application);
//        if (i<=0) return AjaxResult.error("修改申请表状态失败");
//        return AjaxResult.success();
        return null;
    }

    @Override
    public List<Map<String, Object>> finalApply(DmsApprovalProcess approvalProcess) {
//        if (approvalProcess.getApplyType().equals("TSSQB")){
//            return transferApplicationMapper.finalApply(approvalProcess.getStatus());
//        }else if (approvalProcess.getApplyType().equals("OUT")){
//            return checkoutApplicationMapper.finalApply(approvalProcess.getStatus());
//        }else if (approvalProcess.getApplyType().equals("STAY")){
//            return holidayStayApplicationMapper.finalApply(approvalProcess.getStatus());
//        }
        return null;
    }

    /**
     * 学校审核调宿申请表
     * @param dmsAuditDto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult finalAuditTSSQB(DmsAuditDto dmsAuditDto) {
        String username = SecurityUtils.getUsername();
        DmsTransferApplication application = transferApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyType, "TSSQB")
//                .eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .one();
        DmsApprovalProcess approvalProcess = null;
        if (dmsAuditDto.getStatus() == 6){
            DmsDormitory currentDor = dormitoryMapper.selectById(application.getCurrentDormId());
            DmsLiveDetail liveDetail = new DmsLiveDetail();
            DmsSemester semester = new LambdaQueryChainWrapper<>(semesterMapper)
                    .eq(DmsSemester::getIsCurrentSemester, 1)
                    .one();
            liveDetail.setStuNo(application.getStuNo());
            liveDetail.setDuration(application.getDuration());
            liveDetail.setSemesterId(semester.getId());
            liveDetail.setDormitoryId(application.getCurrentDormId());
            BigDecimal money = currentDor.getChargeStandard().multiply(new BigDecimal(application.getDuration()+""));
            liveDetail.setMoney(money);
            liveDetailMapper.insert(liveDetail);
            application.setStatus(3);
        }else {
            application.setStatus(dmsAuditDto.getStatus());
        }
//        if (dmsAuditDto.getStatus() == 6){
//            application.setStatus(3);
//            DmsSemester semester = new LambdaQueryChainWrapper<>(semesterMapper)
//                    .eq(DmsSemester::getIsCurrentSemester, 1)
//                    .one();
//            //计算学生退宿之前一共住了多少个月
//            Calendar outTime = Calendar.getInstance();
//            Calendar startTime = Calendar.getInstance();
//            startTime.setTime(semester.getStartTime());
//            outTime.setTime(approvalProcess.getFdyTime());
//            int i = outTime.get(Calendar.DAY_OF_MONTH);
//            double month = 0;
//            //离开时间如果是15号或15号之后则算作住满一个月 否则算 半个月
//            if (i>=15) month = 1;
//            else month = 0.5;
//            int yearDiff = outTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR);
//            int monthDiff = outTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH);
//            double totalMonthDiff = yearDiff * 12 + monthDiff + month;
//            //当前为上学期则添加一条费用数据
//            if (semester.getSemesterType() == 1){
//                DmsDormitoryRecords records = new DmsDormitoryRecords();
//                SrsDormitoryStudent dormitoryStudent = new SrsDormitoryStudent();
//                dormitoryStudent.setStuNo(application.getStuNo());
//                List<Map<String, Object>> mapList = dormitoryStudentMapper.selectAll(dormitoryStudent);
//                Map<String, Object> map = mapList.get(0);
//                if (map.get("parkName").toString().equals("default")) map.put("parkName","");
//                records.setStuName(map.get("stuName").toString());
//                records.setClassName(map.get("className").toString());
//                records.setDormitoryHead(map.get("isHead").toString().equals("1")?"是":"否");
//                records.setStuNo(map.get("stuNo").toString());
//                records.setRoomNo1(map.get("parkName").toString()+map.get("buildingName").toString()+map.get("roomNo").toString());
//                records.setStandardCapacity(Long.valueOf(map.get("bedNum").toString()));
//                records.setActualOccupancy(Long.valueOf(map.get("occupancy").toString()));
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
//                records.setAccommodationDuration1(sdf.format(semester.getStartTime())+"-"+sdf.format(approvalProcess.getFdyTime()));
//                records.setEquivalentMonths1(new BigDecimal(totalMonthDiff+""));
//                records.setChargeStandard1(new BigDecimal(map.get("chargeStandard").toString()));
//                records.setAmountDue1(records.getChargeStandard1().multiply(records.getEquivalentMonths1()).divide(new BigDecimal("10"),BigDecimal.ROUND_HALF_UP, RoundingMode.CEILING));
//                records.setPhone(map.get("stuPhone").toString());
//                records.setTeacher(map.get("teacherName").toString());
//                records.setTeacherPhone(map.get("teacherPhone").toString());
//                records.setYear(semester.getYear());
//                int insert = dormitoryRecordsMapper.insert(records);
//                if (insert <= 0) return AjaxResult.error("修改费用信息失败");
//            }else {
//                //当前学期为下学期则修改已有的费用数据
//                List<DmsDormitoryRecords> list = new LambdaQueryChainWrapper<>(dormitoryRecordsMapper)
//                        .eq(DmsDormitoryRecords::getStuNo, application.getStuNo())
//                        .orderByDesc(DmsDormitoryRecords::getId)
//                        .list();
//                if (!ObjectUtils.isEmpty(list)){
//                    DmsDormitoryRecords records = list.get(0);
//                    SrsDormitoryStudent dormitoryStudent = new SrsDormitoryStudent();
//                    dormitoryStudent.setStuNo(application.getStuNo());
//                    List<Map<String, Object>> mapList = dormitoryStudentMapper.selectAll(dormitoryStudent);
//                    Map<String, Object> map = mapList.get(0);
//                    if (map.get("parkName").toString().equals("default")) map.put("parkName","");
//                    records.setRoomNo2(map.get("parkName").toString()+map.get("buildingName").toString()+map.get("roomNo").toString());
//                    records.setStandardCapacity2(Long.valueOf(map.get("bedNum").toString()));
//                    records.setActualOccupancy2(Long.valueOf(map.get("occupancy").toString()));
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
//                    records.setAccommodationDuration2(sdf.format(semester.getStartTime())+"-"+sdf.format(approvalProcess.getFdyTime()));
//                    records.setChargeStandard2(new BigDecimal(map.get("chargeStandard").toString()));
//                    records.setEquivalentMonths2(new BigDecimal(totalMonthDiff+""));
//                    records.setAmountDue2(records.getChargeStandard2().multiply(records.getEquivalentMonths2()).divide(new BigDecimal("10"),BigDecimal.ROUND_HALF_UP, RoundingMode.CEILING));
//                    records.setYear(semester.getYear());
//                    int updateRecords = dormitoryRecordsMapper.updateById(records);
//                    if (updateRecords <= 0) return AjaxResult.error("修改费用信息失败");
//                }
//            }
//            //修改学生住宿信息
//            boolean update = new LambdaUpdateChainWrapper<>(dormitoryStudentMapper)
//                    .eq(SrsDormitoryStudent::getStuNo, application.getStuNo())
//                    .eq(SrsDormitoryStudent::getDormitoryId, application.getCurrentDormId())
//                    .set(SrsDormitoryStudent::getDormitoryId, application.getTargetDormId())
//                    .update();
//            if (!update) return AjaxResult.error("修改住宿信息失败");
//        }else {
//            application.setStatus(dmsAuditDto.getStatus());
//        }
        approvalProcess.setFinalNo(username);
        approvalProcess.setFinalStatus(dmsAuditDto.getStatus().toString());
        approvalProcess.setFinalTime(DateUtils.getNowDate());
        boolean b = updateById(approvalProcess);
        if (!b) return AjaxResult.error("修改审核状态失败");
        int i = transferApplicationMapper.updateById(application);
        if (i<=0) return AjaxResult.error("修改申请表失败");
        return AjaxResult.success();
    }

    /**
     * 学校审核退宿申请表
     * @param dmsAuditDto
     * @return
     */
    @Transactional
    @Override
    public AjaxResult finalAuditOUT(DmsAuditDto dmsAuditDto) {
        String username = SecurityUtils.getUsername();
        DmsCheckoutApplication application = checkoutApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyType, "OUT")
//                .eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .one();
        DmsApprovalProcess approvalProcess = null;
        approvalProcess.setFinalNo(username);
        approvalProcess.setFinalTime(DateUtils.getNowDate());
        approvalProcess.setFinalStatus(dmsAuditDto.getStatus().toString());
        if (dmsAuditDto.getStatus() == 6){
            application.setStatus(3);
            DmsSemester semester = new LambdaQueryChainWrapper<>(semesterMapper)
                    .eq(DmsSemester::getIsCurrentSemester, 1)
                    .one();
            DmsDormitory dormitory = dormitoryMapper.selectById(application.getCurrentDormId());
            DmsLiveDetail liveDetail = new DmsLiveDetail();
            liveDetail.setStuNo(application.getStuNo());
            liveDetail.setDuration(application.getDuration());
            liveDetail.setSemesterId(semester.getId());
            liveDetail.setMoney(dormitory.getChargeStandard().multiply(new BigDecimal(application.getDuration()+"")));
            liveDetailMapper.insert(liveDetail);
            boolean remove = new LambdaUpdateChainWrapper<>(dormitoryStudentMapper)
                    .eq(SrsDormitoryStudent::getStuNo, application.getStuNo())
                    .remove();
        }else {
            application.setStatus(dmsAuditDto.getStatus());
        }
//        if (dmsAuditDto.getStatus() == 6){
//            application.setStatus(3);
//            //修改费用信息
//            DmsSemester semester = new LambdaQueryChainWrapper<>(semesterMapper)
//                    .eq(DmsSemester::getIsCurrentSemester, 1)
//                    .one();
//            //计算学生退宿之前一共住了多少个月
//            Calendar outTime = Calendar.getInstance();
//            Calendar startTime = Calendar.getInstance();
//            startTime.setTime(semester.getStartTime());
//            outTime.setTime(application.getOutTime());
//            int i = outTime.get(Calendar.DAY_OF_MONTH);
//            double month = 0;
//            //离开时间如果是15号或15号之后则算作住满一个月
//            if (i>=15) month = 1;
//            else month = 0.5;
//            int yearDiff = outTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR);
//            int monthDiff = outTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH);
//            double totalMonthDiff = yearDiff * 12 + monthDiff + month;
//            //当前为上学期则添加一条费用数据
//            if (semester.getSemesterType() == 1){
//                DmsDormitoryRecords records = new DmsDormitoryRecords();
//                SrsDormitoryStudent dormitoryStudent = new SrsDormitoryStudent();
//                dormitoryStudent.setStuNo(application.getStuNo());
//                List<Map<String, Object>> mapList = dormitoryStudentMapper.selectAll(dormitoryStudent);
//                Map<String, Object> map = mapList.get(0);
//                if (map.get("parkName").toString().equals("default")) map.put("parkName","");
//                records.setStuName(map.get("stuName").toString());
//                records.setClassName(map.get("className").toString());
//                records.setDormitoryHead(map.get("isHead").toString().equals("1")?"是":"否");
//                records.setStuNo(map.get("stuNo").toString());
//                records.setRoomNo1(map.get("parkName").toString()+map.get("buildingName").toString()+map.get("roomNo").toString());
//                records.setStandardCapacity(Long.valueOf(map.get("bedNum").toString()));
//                records.setActualOccupancy(Long.valueOf(map.get("occupancy").toString()));
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
//                records.setAccommodationDuration1(sdf.format(semester.getStartTime())+"-"+sdf.format(application.getOutTime()));
//                records.setEquivalentMonths1(new BigDecimal(totalMonthDiff+""));
//                records.setChargeStandard1(new BigDecimal(map.get("chargeStandard").toString()));
//                records.setAmountDue1(records.getChargeStandard1().multiply(records.getEquivalentMonths1()).divide(new BigDecimal("10"),BigDecimal.ROUND_HALF_UP, RoundingMode.CEILING));
//                records.setPhone(map.get("stuPhone").toString());
//                records.setTeacher(map.get("teacherName").toString());
//                records.setTeacherPhone(map.get("teacherPhone").toString());
//                records.setYear(semester.getYear());
//                int insert = dormitoryRecordsMapper.insert(records);
//                if (insert <= 0) return AjaxResult.error("修改费用信息失败");
//            }else {
//                //当前学期为下学期则修改已有的费用数据
//                List<DmsDormitoryRecords> list = new LambdaQueryChainWrapper<>(dormitoryRecordsMapper)
//                        .eq(DmsDormitoryRecords::getStuNo, application.getStuNo())
//                        .orderByDesc(DmsDormitoryRecords::getId)
//                        .list();
//                DmsDormitoryRecords records = list.get(0);
//                SrsDormitoryStudent dormitoryStudent = new SrsDormitoryStudent();
//                dormitoryStudent.setStuNo(application.getStuNo());
//                List<Map<String, Object>> mapList = dormitoryStudentMapper.selectAll(dormitoryStudent);
//                Map<String, Object> map = mapList.get(0);
//                if (map.get("parkName").toString().equals("default")) map.put("parkName","");
//                records.setRoomNo2(map.get("parkName").toString()+map.get("buildingName").toString()+map.get("roomNo").toString());
//                records.setStandardCapacity2(Long.valueOf(map.get("bedNum").toString()));
//                records.setActualOccupancy2(Long.valueOf(map.get("occupancy").toString()));
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
//                records.setAccommodationDuration2(sdf.format(semester.getStartTime())+"-"+sdf.format(application.getOutTime()));
//                records.setChargeStandard2(new BigDecimal(map.get("chargeStandard").toString()));
//                records.setEquivalentMonths2(new BigDecimal(totalMonthDiff+""));
//                records.setAmountDue2(records.getChargeStandard2().multiply(records.getEquivalentMonths2()).divide(new BigDecimal("10"),BigDecimal.ROUND_HALF_UP, RoundingMode.CEILING));
//                records.setYear(semester.getYear());
//                int update = dormitoryRecordsMapper.updateById(records);
//                if (update <= 0) return AjaxResult.error("修改费用信息失败");
//            }
//            //删除住宿信息
//            boolean remove = new LambdaUpdateChainWrapper<>(dormitoryStudentMapper)
//                    .eq(SrsDormitoryStudent::getStuNo, application.getStuNo())
//                    .eq(SrsDormitoryStudent::getDormitoryId, application.getCurrentDormId())
//                    .remove();
//            if (!remove) return AjaxResult.error("删除住宿信息失败");
//        }else {
//            application.setStatus(dmsAuditDto.getStatus());
//        }
        boolean b = updateById(approvalProcess);
        if (!b) return AjaxResult.error("修改审核状态失败");
        int i = checkoutApplicationMapper.updateById(application);
        if (i<=0) return AjaxResult.error("修改申请表失败");
        return AjaxResult.success();
    }

    /**
     * 学校审核留宿申请表
     * @param dmsAuditDto
     * @return
     */
    @Override
    public AjaxResult finalAuditSTAY(DmsAuditDto dmsAuditDto) {
        String username = SecurityUtils.getUsername();
        DmsHolidayStayApplication application = holidayStayApplicationMapper.selectById(dmsAuditDto.getApplyId());
//        DmsApprovalProcess approvalProcess = lambdaQuery().eq(DmsApprovalProcess::getApplyId, dmsAuditDto.getApplyId())
//                .eq(DmsApprovalProcess::getApplyType, "STAY")
//                .one();
        DmsApprovalProcess approvalProcess = null;
        approvalProcess.setFinalStatus(dmsAuditDto.getStatus().toString());
        approvalProcess.setFinalNo(username);
        approvalProcess.setFinalTime(DateUtils.getNowDate());
        if (dmsAuditDto.getStatus() == 6){
            application.setStatus(3);
            DmsSemester semester = new LambdaQueryChainWrapper<>(semesterMapper)
                    .eq(DmsSemester::getIsCurrentSemester, 1)
                    .one();
            DmsDormitory dormitory = dormitoryMapper.selectById(application.getDormId());
            //计算留宿时间
            Calendar endTime = Calendar.getInstance();
            Calendar startTime = Calendar.getInstance();
            startTime.setTime(application.getStartDate());
            endTime.setTime(application.getEndDate());
            int i1 = startTime.get(Calendar.DAY_OF_MONTH);
            int i2 = endTime.get(Calendar.DAY_OF_MONTH);
            double month = 0;
            if (i2>=15&&i1<=15) month = 1;
            int yearDiff = endTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR);
            int monthDiff = endTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH);
            double totalMonthDiff = yearDiff * 12 + monthDiff + month;
            DmsLiveDetail liveDetail = new DmsLiveDetail();
            liveDetail.setStuNo(application.getStuNo());
            liveDetail.setSemesterId(semester.getId());
            liveDetail.setDuration(new BigDecimal(totalMonthDiff+""));
            liveDetail.setMoney(dormitory.getChargeStandard().multiply(liveDetail.getDuration()));
            liveDetailMapper.insert(liveDetail);
        }else {
            application.setStatus(dmsAuditDto.getStatus());
        }
//        if (dmsAuditDto.getStatus() == 6){
//            application.setStatus(3);
//            //修改费用信息
//            DmsSemester semester = new LambdaQueryChainWrapper<>(semesterMapper)
//                    .eq(DmsSemester::getIsCurrentSemester, 1)
//                    .one();
//            //计算学生退宿之前一共住了多少个月
//            Calendar endTime = Calendar.getInstance();
//            Calendar startTime = Calendar.getInstance();
//            startTime.setTime(semester.getStartTime());
//            endTime.setTime(application.getEndDate());
//            int i = endTime.get(Calendar.DAY_OF_MONTH);
//            double month = 0;
//            //离开时间如果是15号或15号之后则算作住满一个月
//            if (i>=15) month = 1;
//            else month = 0.5;
//            int yearDiff = endTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR);
//            int monthDiff = endTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH);
//            double totalMonthDiff = yearDiff * 12 + monthDiff + month + semester.getTotalMonths();
//            //当前为上学期则添加一条费用数据
//            if (semester.getSemesterType() == 1){
//                DmsDormitoryRecords records = new DmsDormitoryRecords();
//                SrsDormitoryStudent dormitoryStudent = new SrsDormitoryStudent();
//                dormitoryStudent.setStuNo(application.getStuNo());
//                List<Map<String, Object>> mapList = dormitoryStudentMapper.selectAll(dormitoryStudent);
//                Map<String, Object> map = mapList.get(0);
//                if (map.get("parkName").toString().equals("default")) map.put("parkName","");
//                records.setStuName(map.get("stuName").toString());
//                records.setClassName(map.get("className").toString());
//                records.setDormitoryHead(map.get("isHead").toString().equals("1")?"是":"否");
//                records.setStuNo(map.get("stuNo").toString());
//                records.setRoomNo1(map.get("parkName").toString()+map.get("buildingName").toString()+map.get("roomNo").toString());
//                records.setStandardCapacity(Long.valueOf(map.get("bedNum").toString()));
//                records.setActualOccupancy(Long.valueOf(map.get("occupancy").toString()));
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
//                records.setAccommodationDuration1(sdf.format(semester.getStartTime())+"-"+sdf.format(application.getEndDate()));
//                records.setEquivalentMonths1(new BigDecimal(totalMonthDiff+""));
//                records.setChargeStandard1(new BigDecimal(map.get("chargeStandard").toString()));
//                records.setAmountDue1(records.getChargeStandard1().multiply(records.getEquivalentMonths1()).divide(new BigDecimal("10"),BigDecimal.ROUND_HALF_UP, RoundingMode.CEILING));
//                records.setPhone(map.get("stuPhone").toString());
//                records.setTeacher(map.get("teacherName").toString());
//                records.setTeacherPhone(map.get("teacherPhone").toString());
//                records.setYear(semester.getYear());
//                int insert = dormitoryRecordsMapper.insert(records);
//                if (insert <= 0) return AjaxResult.error("修改费用信息失败");
//            }else {
//                //当前学期为下学期则修改已有的费用数据
//                List<DmsDormitoryRecords> list = new LambdaQueryChainWrapper<>(dormitoryRecordsMapper)
//                        .eq(DmsDormitoryRecords::getStuNo, application.getStuNo())
//                        .orderByDesc(DmsDormitoryRecords::getId)
//                        .list();
//                DmsDormitoryRecords records = list.get(0);
//                SrsDormitoryStudent dormitoryStudent = new SrsDormitoryStudent();
//                dormitoryStudent.setStuNo(application.getStuNo());
//                List<Map<String, Object>> mapList = dormitoryStudentMapper.selectAll(dormitoryStudent);
//                Map<String, Object> map = mapList.get(0);
//                if (map.get("parkName").toString().equals("default")) map.put("parkName","");
//                records.setRoomNo2(map.get("parkName").toString()+map.get("buildingName").toString()+map.get("roomNo").toString());
//                records.setStandardCapacity2(Long.valueOf(map.get("bedNum").toString()));
//                records.setActualOccupancy2(Long.valueOf(map.get("occupancy").toString()));
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
//                records.setAccommodationDuration2(sdf.format(semester.getStartTime())+"-"+sdf.format(application.getEndDate()));
//                records.setChargeStandard2(new BigDecimal(map.get("chargeStandard").toString()));
//                records.setEquivalentMonths2(new BigDecimal(totalMonthDiff+""));
//                records.setAmountDue2(records.getChargeStandard2().multiply(records.getEquivalentMonths2()).divide(new BigDecimal("10"),BigDecimal.ROUND_HALF_UP, RoundingMode.CEILING));
//                records.setYear(semester.getYear());
//                int update = dormitoryRecordsMapper.updateById(records);
//                if (update <= 0) return AjaxResult.error("修改费用信息失败");
//            }
//        }else {
//            application.setStatus(dmsAuditDto.getStatus());
//        }
        boolean b = updateById(approvalProcess);
        if (!b) return AjaxResult.error("修改审核状态失败");
        int i = holidayStayApplicationMapper.updateById(application);
        if (i<=0) return AjaxResult.error("修改申请表失败");
        return AjaxResult.success();
    }


}
