package com.xjrsoft.module.room.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.RoomApplicantTypeEnum;
import com.xjrsoft.common.enums.StudyStatusEnum;
import com.xjrsoft.module.room.entity.WfRoomApplicant;
import com.xjrsoft.module.room.mapper.WfRoomApplicantMapper;
import com.xjrsoft.module.room.service.IWfRoomApplicantService;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.mapper.BaseStudentSchoolRollMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @title: 寝室申请
* @Author dzx
* @Date: 2024-01-02
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class WfRoomApplicantServiceImpl extends MPJBaseServiceImpl<WfRoomApplicantMapper, WfRoomApplicant> implements IWfRoomApplicantService {

    private final BaseStudentSchoolRollMapper baseStudentSchoolRollMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean dataHandle(Long formId) {
        WfRoomApplicant wfRoomApplicant = this.getById(formId);
        //根据申请人编号修改该用户的就读状态(study_status)
        //留宿
        if(ObjectUtil.isNotNull(wfRoomApplicant)){
            LambdaQueryWrapper<BaseStudentSchoolRoll> queryWrapperStuSchRoll = new LambdaQueryWrapper<>();
            queryWrapperStuSchRoll
                    .eq(BaseStudentSchoolRoll::getUserId,wfRoomApplicant.getApplicantUserId());
            BaseStudentSchoolRoll baseStudentSchoolRoll = baseStudentSchoolRollMapper.selectOne(queryWrapperStuSchRoll);

            if(ObjectUtil.isNotNull(baseStudentSchoolRoll)){
                //申请住校
                if(wfRoomApplicant.getRecedeType().equals(RoomApplicantTypeEnum.ToBeBoarder.getCode())) {
                    baseStudentSchoolRollMapper.updateById(new BaseStudentSchoolRoll(){{
                        setId(baseStudentSchoolRoll.getId());
                        setStduyStatus(StudyStatusEnum.InResidence.getCode());
                    }});
                }

                if(wfRoomApplicant.getRecedeType().equals(RoomApplicantTypeEnum.ToBeDayPupil.getCode())) {
                    baseStudentSchoolRollMapper.updateById(new BaseStudentSchoolRoll(){{
                        setId(baseStudentSchoolRoll.getId());
                        setStduyStatus(StudyStatusEnum.AttendDaySchool.getCode());
                    }});
                }
            }
        }

        return true;
    }
}
