package com.srs.dormitory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.dormitory.domain.DmsApprovalProcess;
import com.srs.dormitory.domain.DmsDelayApplication;
import com.srs.dormitory.domain.Dto.AddDelayApplicationDto;
import com.srs.dormitory.domain.Dto.ApplicationListDto;
import com.srs.dormitory.domain.Dto.CheckoutApplicationDto;
import com.srs.dormitory.domain.Dto.UpdateTransferApplicationDto;
import com.srs.dormitory.domain.SrsDormitoryStudent;
import com.srs.dormitory.mapper.DmsApprovalProcessMapper;
import com.srs.dormitory.mapper.DmsDelayApplicationMapper;
import com.srs.dormitory.service.IDmsApprovalProcessService;
import com.srs.dormitory.service.IDmsDelayApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.dormitory.service.ISrsDormitoryStudentService;
import com.srs.stureg.domain.SrsStuReg;
import com.srs.stureg.mapper.SrsStuRegMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 延迟退宿申请表 服务实现类
 * </p>
 *
 * @author szz
 * @since 2023年08月01日
 */
@Service
public class IDmsDelayApplicationServiceImpl extends ServiceImpl<DmsDelayApplicationMapper, DmsDelayApplication> implements IDmsDelayApplicationService {
     @Autowired
     private ISrsDormitoryStudentService dormitoryStudentService;
     @Autowired
     private DmsApprovalProcessMapper dmsApprovalProcessMapper;
     @Autowired
     private IDmsApprovalProcessService dmsApprovalProcessService;
     @Autowired
     private SrsStudentMapper srsStudentMapper;
    /**
     * 提交延迟退宿申请表
     * @param applicationDto 请求参数
     * @return
     */
    @Override
    public AjaxResult addDelayApplication(AddDelayApplicationDto applicationDto) {

        SrsStudent srsStudent = srsStudentMapper.selectSrsStudentByStuNo(applicationDto.getStuNo());
        if (srsStudent == null) return AjaxResult.error("学号不存在");
        //校验该学生是否有一个待审核的申请，防止重复提交
        DmsDelayApplication check = lambdaQuery()
                .eq(DmsDelayApplication::getStuNo, srsStudent.getStuNo())
                .eq(DmsDelayApplication::getStatus, 0)
                .one();
        if (check!=null) return AjaxResult.error("您已经有一个待审核的申请，无法重复提交");
        //校验最终退宿时间填写是否有误
        if (applicationDto.getDelayTime().isBefore(LocalDateTime.now())) return AjaxResult.error("最终退宿时间有误");
        //校验宿舍和床位ID是否和该学生绑定
        SrsDormitoryStudent checkStudentDormitory = dormitoryStudentService.lambdaQuery()
                .eq(SrsDormitoryStudent::getStuNo, srsStudent.getStuNo())
                .eq(SrsDormitoryStudent::getDormitoryId, applicationDto.getDormitoryId())
                .eq(SrsDormitoryStudent::getBedId, applicationDto.getBedId())
                .one();
        if (checkStudentDormitory==null) return AjaxResult.error("床位和宿舍ID填写错误");
        DmsDelayApplication delayApplication = new DmsDelayApplication();
        BeanUtil.copyProperties(applicationDto,delayApplication);
        delayApplication.setCreateBy(SecurityUtils.getUsername());
        boolean success = save(delayApplication);
        if (!success) return AjaxResult.error();
        return AjaxResult.success();
    }
    /**
     * 查看延迟退宿申请表列表
     * @param params 请求参数
     * @return
     */
    @Override
    public List<Map<String, Object>> getDelayApplicationList(ApplicationListDto params) {
        return baseMapper.getDelayApplicationList(params);
    }

    /**
     * 查看延迟退宿申请详情
     * @param id 延迟退宿申请ID
     * @return
     */
    @Override
    public AjaxResult getDelayApplicationInfo(Long id) {
//        Map<String,Object> info = baseMapper.getDelayApplicationInfo(id);
//        if (info==null) return AjaxResult.error("id不存在");
//        List<Map<String, Object>> approvalList = dmsApprovalProcessMapper.selectApprovalList(id,3);
//        info.put("approvalList",approvalList);
//        return AjaxResult.success(info);
        return null;
    }

    /**
     * 批量删除延迟退宿申请列表
     * @param ids 延迟退宿申请ID列表
     * @return
     */
    @Transactional
    @Override
    public AjaxResult batchDeleteDelayApplication(List<Long> ids) {
//        boolean removeApproval = dmsApprovalProcessService.lambdaUpdate()
//                .eq(DmsApprovalProcess::getApplicationType, 3)
//                .in(DmsApprovalProcess::getApplicationId, ids)
//                .remove();
//        if (!removeApproval) throw new RuntimeException("删除审核流程错误");
//        boolean removeApplication = removeBatchByIds(ids);
//        if (!removeApplication) throw new RuntimeException("删除申请表失败");
        return AjaxResult.success();
    }
}
