package com.srs.dormitory.service.impl;

import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.dormitory.domain.DmsApprovalProcess;
import com.srs.dormitory.domain.DmsHolidayStayApplication;
import com.srs.dormitory.domain.Dto.ApplicationListDto;
import com.srs.dormitory.domain.Dto.HolidayStayApplicationDto;
import com.srs.dormitory.domain.SrsDormitoryStudent;
import com.srs.dormitory.mapper.DmsApprovalProcessMapper;
import com.srs.dormitory.mapper.DmsHolidayStayApplicationMapper;
import com.srs.dormitory.service.IDmsHolidayStayApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.dormitory.service.ISrsDormitoryStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 假期留宿申请表 服务实现类
 * </p>
 *
 * @author szz
 * @since 2023年07月31日
 */
@Service
public class IDmsHolidayStayApplicationServiceImpl extends ServiceImpl<DmsHolidayStayApplicationMapper, DmsHolidayStayApplication> implements IDmsHolidayStayApplicationService {
    @Autowired
    private ISrsDormitoryStudentService srsDormitoryStudentService;
    @Autowired
    private DmsApprovalProcessMapper dmsApprovalProcessMapper;
    @Autowired
    private SrsStudentMapper srsStudentMapper;

    /**
     * 提交假期留校申请表
     * @param applicationDto
     * @return
     */
    @Override
    public AjaxResult addHolidayStayApplication(HolidayStayApplicationDto applicationDto) {
        String username = SecurityUtils.getUsername();
        List<Integer> status = new ArrayList<>();
        status.add(0);
        status.add(1);
        status.add(2);
        Long count = lambdaQuery().eq(DmsHolidayStayApplication::getStuNo, username)
                .in(DmsHolidayStayApplication::getStatus, status)
                .count();
        if (count > 0) return AjaxResult.error("您已经提交申请，无法重复提交");
        SrsDormitoryStudent dormitoryStudent = srsDormitoryStudentService.lambdaQuery()
                .eq(SrsDormitoryStudent::getStuNo, username)
                .one();
        if (dormitoryStudent==null) return AjaxResult.error("您不在本校居住");
        if (applicationDto.getStartDate().after(applicationDto.getEndDate())) return AjaxResult.error("时间填写有误");
        DmsHolidayStayApplication holidayStayApplication = new DmsHolidayStayApplication();
        holidayStayApplication.setStartDate(applicationDto.getStartDate());
        holidayStayApplication.setEndDate(applicationDto.getEndDate());
        holidayStayApplication.setReason(applicationDto.getReason());
        holidayStayApplication.setStuNo(username);
        holidayStayApplication.setCreateBy(SecurityUtils.getUsername());
        holidayStayApplication.setDormId(dormitoryStudent.getDormitoryId());
        boolean save = save(holidayStayApplication);
        if (!save) return AjaxResult.error("添加申请表错误");
        DmsApprovalProcess approvalProcess = new DmsApprovalProcess();
        approvalProcess.setApplyId(holidayStayApplication.getId());
        int insert = dmsApprovalProcessMapper.insert(approvalProcess);
        if (insert<=0)return AjaxResult.error("添加审核信息错误");
        return AjaxResult.success();
    }

    /**
     * 查看假期留宿申请表详情
     * @param id 假期留宿申请表Id
     * @return
     */
    @Override
    public AjaxResult getHolidayStayApplication(Long id) {
        Map<String, Object> holidayStayApplication = baseMapper.getHolidayStayApplication(id);
        if (holidayStayApplication.get("createTime") != null){
            holidayStayApplication.put("createTime",holidayStayApplication.get("createTime").toString().replace('T',' '));
        }
        return AjaxResult.success(holidayStayApplication);
    }

    /**
     * 批量删除假期留宿申请表
     * @param ids 假期留宿申请表Id列表
     * @return
     */
    @Transactional
    @Override
    public AjaxResult batchDeleteHolidayStayApplication(List<Long> ids) {
//        boolean removeApproval = dmsApprovalProcessService.lambdaUpdate()
//                .eq(DmsApprovalProcess::getApplicationType, 4)
//                .in(DmsApprovalProcess::getApplicationId, ids)
//                .remove();
//        if (!removeApproval) throw new RuntimeException("删除审核流程失败");
//        boolean removeApplication = removeBatchByIds(ids);
//        if (!removeApplication) throw new RuntimeException("删除申请表失败");
//        return AjaxResult.success();
        return null;
    }

    /**
     * 查看假期留宿申请表列表
     * @param dto
     * @return
     */
    @Override
    public List<Map<String, Object>> getList(ApplicationListDto dto) {
        String username = SecurityUtils.getUsername();
        dto.setStudentNo(username);
        SecurityUtils.getLoginUser().getUser().getRoles().forEach(item -> {
            if (item.isAdmin()){
                dto.setStudentNo(null);
            }
        });
        return baseMapper.getList(dto);
    }

    /**
     * 取消申请
     * @param id 申请表id
     * @return
     */
    @Override
    public AjaxResult cancel(Long id) {
        DmsHolidayStayApplication application = getById(id);
        String username = SecurityUtils.getUsername();
        if (application == null) return AjaxResult.error("id错误");
        if (!application.getStuNo().equals(username)) return AjaxResult.error("无法取消该申请表");
        if (application.getStatus() != 0) return AjaxResult.error("无法取消该申请表");
        application.setStatus(7);
        boolean b = updateById(application);
        if (!b) return AjaxResult.error("取消失败");
        return AjaxResult.success();
    }
}
