package com.xinelu.familydoctor.applet.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.xinelu.manage.service.messagepush.MessagePushService;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.DateUtils;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.common.utils.uuid.IdUtils;
import com.xinelu.familydoctor.applet.mapper.ResidentRescindApplyMapper;
import com.xinelu.familydoctor.applet.pojo.body.ApprovalBody;
import com.xinelu.familydoctor.applet.pojo.body.MessagePushBody;
import com.xinelu.familydoctor.applet.pojo.body.ResidentRescindApplyBody;
import com.xinelu.familydoctor.applet.pojo.entity.PatientInfo;
import com.xinelu.familydoctor.applet.pojo.entity.ResidentRescindApplyEntity;
import com.xinelu.familydoctor.applet.pojo.query.ApplyQuery;
import com.xinelu.familydoctor.applet.pojo.vo.ResidentRescindApplyVo;
import com.xinelu.familydoctor.applet.service.IResidentPatientInfoService;
import com.xinelu.familydoctor.applet.service.IResidentRescindApplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author mengkuiliang
 * @Description 解约申请Impl
 * @Date 2023-09-27 027 17:06
 * @Param
 * @return
 **/
@Service
public class ResidentRescindApplyServiceImpl implements IResidentRescindApplyService {

    @Resource
    private ResidentRescindApplyMapper residentRescindApplyMapper;
    @Resource
    private IResidentPatientInfoService patientInfoService;
    @Resource
    private MessagePushService messagePushService;

    @Override
    public List<ResidentRescindApplyVo> findList(ApplyQuery query) {
        if (query.getStartDate() != null) {
            query.setStartDate(DateUtils.parseDate(DateUtils.parseDateToStr("yyyy-MM-dd 00:00:00", query.getStartDate())));
        }
        if (query.getEndDate() != null) {
            query.setEndDate(DateUtils.parseDate(DateUtils.parseDateToStr("yyyy-MM-dd 23:59:59", query.getEndDate())));
        }
        return residentRescindApplyMapper.findByBody(query);
    }

    @Override
    public void insert(ResidentRescindApplyBody body) {

        // 查询解约申请记录
        ApplyQuery applyQuery = new ApplyQuery();
        applyQuery.setIdentity(body.getIdentity());
        applyQuery.setBookingStatus("0");
        applyQuery.setApprovalStatus("0");
        List<ResidentRescindApplyVo> applyVoList = residentRescindApplyMapper.findByBody(applyQuery);
        if (applyVoList != null && applyVoList.size() > 0) {
            throw new ServiceException("已经提交解约申请，不能重复提交");
        }
        ResidentRescindApplyEntity entity = new ResidentRescindApplyEntity();
        BeanUtils.copyProperties(body, entity);

        PatientInfo patientInfo = patientInfoService.getByCardNo(body.getIdentity());
        if (patientInfo == null) {
            throw new ServiceException("未查询到注册信息");
        }
        if(StringUtils.isBlank(body.getPatientId())) {
            entity.setPatientId(patientInfo.getPatientCode());
        }

        entity.setApplyNo(IdUtils.simpleUUID());
        entity.setApplyTime(new Date());
        entity.setBookingStatus("0");
        entity.setApprovalStatus("0");
        residentRescindApplyMapper.insert(entity);

        MessagePushBody messagePushBody = new MessagePushBody();
        messagePushBody.setMessageCategory("4");
        messagePushBody.setBusType("10");
        messagePushBody.setOpenid(patientInfo.getBindOpenid());
        messagePushBody.setReceiveName(patientInfo.getPatientName());
        messagePushBody.setText1(body.getOrgName());
        messagePushBody.setText2("解约申请");
        messagePushBody.setSendTitle("解约申请");
        messagePushBody.setSendTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date()));
        messagePushBody.setSendContent(patientInfo.getPatientName() + "已提交申请");
        messagePushBody.setCardNo(body.getIdentity());
        messagePushService.fdApprovePush(JSONObject.parseObject(JSONObject.toJSONString(messagePushBody)));
    }

    /**
     * 修改审批
     *
     * @param body :
     * @return Integer
     * @author lixinying
     * @date 2022/11/21 17:07
     */
    @Override
    public void updateFd(ApprovalBody body) {
        ResidentRescindApplyVo vo = residentRescindApplyMapper.detail(body.getApplyNo());
        if (vo != null) {
            residentRescindApplyMapper.updateEx(body);
        }
    }

    /**
     * @return com.xinelu.mp.sign.pojo.vo.FdResidentRescindApplyVo
     * @Author mengkuiliang
     * @Description 解约申请详情
     * @Date 2022-11-23 10:03
     * @Param [applyNo]
     **/
    @Override
    public ResidentRescindApplyVo detail(String applyNo) {
        return residentRescindApplyMapper.detail(applyNo);
    }

    /**
     * @return void
     * @Author mengkuiliang
     * @Description 取消解约申请
     * @Date 2022-11-23 14:41
     * @Param [applyNo]
     **/
    @Override
    public void cancel(String applyNo) {
        ResidentRescindApplyVo residentRescindApplyVo = residentRescindApplyMapper.detail(applyNo);
        if(residentRescindApplyVo == null) {
            throw new ServiceException("未查询到解约申请记录");
        }
        if(!StringUtils.isBlank(residentRescindApplyVo.getBookingStatus()) && residentRescindApplyVo.getBookingStatus().equals("1")) {
            throw new ServiceException("已取消，不能再次取消");
        }
        if(!StringUtils.isBlank(residentRescindApplyVo.getApprovalStatus())) {
            if(residentRescindApplyVo.getApprovalStatus().equals("1")) {
                throw new ServiceException("已同意，不能取消");
            } else if(residentRescindApplyVo.getApprovalStatus().equals("2")) {
                throw new ServiceException("已拒绝，不能取消");
            }
        }
        residentRescindApplyMapper.cancel(applyNo);
    }
}




