package com.xinelu.quartz.task;

import com.xinelu.common.constant.SignRecordServiceStatusConstants;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.domain.signpatientrecord.SignPatientRecord;
import com.xinelu.manage.dto.signpatientrecord.SignPatientListDto;
import com.xinelu.manage.mapper.patientinfo.PatientInfoMapper;
import com.xinelu.manage.mapper.signpatientrecord.SignPatientRecordMapper;
import com.xinelu.manage.service.signpatientrecord.ISignPatientRecordService;
import com.xinelu.manage.vo.signpatientrecord.SignPatientListVo;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * @description: 患者签约服务包到期状态修改定时任务
 * @author: haown
 * @create: 2024-04-10 10:23
 **/
@Component("SignPackageExpireTask")
public class SignPackageExpireTask {
	@Resource
	private ISignPatientRecordService signPatientRecordService;

	@Resource
	private SignPatientRecordMapper signPatientRecordMapper;

	@Resource
	private PatientInfoMapper patientInfoMapper;

	public void SignPackageExpireTask() {
		// 查询签约患者
		SignPatientListDto signPatientListDto = new SignPatientListDto();
		signPatientListDto.setSignStatus(SignRecordServiceStatusConstants.SERVICE_CENTER);
		List<SignPatientListVo> signRecordList = signPatientRecordService.selectList(signPatientListDto);
		for (SignPatientListVo sign : signRecordList) {
			// 当前日期与服务到期日期比较
			if (sign.getServiceEndTime().isBefore(LocalDateTime.now().toLocalDate())) {
				SignPatientRecord signPatientRecord = new SignPatientRecord();
				signPatientRecord.setId(sign.getId());
				signPatientRecord.setServiceStatus(SignRecordServiceStatusConstants.SERVICE_END);
				signPatientRecord.setSignStatus(SignRecordServiceStatusConstants.EXPIRE_SIGN);
				signPatientRecordMapper.updateByPrimaryKeySelective(signPatientRecord);
				// 修改患者表签约状态
				PatientInfo patientInfo = new PatientInfo();
				patientInfo.setId(sign.getPatientId());
				patientInfo.setSignStatus(SignRecordServiceStatusConstants.EXPIRE_SIGN);
				patientInfo.setServiceStatus(SignRecordServiceStatusConstants.SERVICE_END);
				patientInfoMapper.updatePatientInfoSelective(patientInfo);
			}
		}
	}
}
