package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.ModelHospitalAllDTO;
import com.diagbot.entity.MedicalRecord;
import com.diagbot.entity.ModelHospital;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.ModelHospitalService;
import com.diagbot.service.impl.ModelHospitalServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.vo.ModelHospitalAllVO;
import com.diagbot.vo.ModelHospitalUpdataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author wangfeng
 * @Description:
 * @date 2020-05-15 14:42
 */
@Component
public class ModelHospitalFacade extends ModelHospitalServiceImpl {

    @Autowired
    ModelHospitalService modelHospitalService;
    @Autowired
    MedicalRecordFacade medicalRecordFacade;
    @Autowired
    QcAbnormalFacade qcAbnormalFacade;

    /**
     *
     * @param modelHospitalAllVO
     * @return
     */
    public IPage<List<ModelHospitalAllDTO>> getModelHospitalAlls(ModelHospitalAllVO modelHospitalAllVO) {
        IPage<List<ModelHospitalAllDTO>> data =  modelHospitalService.getModelHospitalAll(modelHospitalAllVO);
        return data;
    }

    /**
     *
     * @param modeId
     * @return
     */
    public ModelHospitalAllDTO getModelHospitalById(Long modeId) {
        QueryWrapper<ModelHospital> queryModelHospital = new QueryWrapper<>();
        queryModelHospital.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("id",modeId);
        ModelHospital modelHospital = modelHospitalService.getOne(queryModelHospital, false);
        if(modelHospital == null){
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "该数据不存在！");
        }
        ModelHospitalAllDTO modelHospitalData =  new ModelHospitalAllDTO();
        BeanUtil.copyProperties(modelHospital,modelHospitalData);
        return  modelHospitalData;
    }

    /**
     * 先查询qc_model_hospital表， 把该医院数据模块的数据全删， 在重新添加，更新med_medical_record
     * 得到的qc_model_hospital的id更新到med_medical_record表的modeId,再把qc_abnormal的状态更行。
     * @param modelHospitalUpdataVO
     * @return
     */
    public Boolean updataModelHospitals(ModelHospitalUpdataVO modelHospitalUpdataVO) {
        //把该医院数据模块的数据全删
        QueryWrapper<ModelHospital> queryModelHospital = new QueryWrapper<>();
        queryModelHospital.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id", modelHospitalUpdataVO.getHospitalId())
                .ne("id", modelHospitalUpdataVO.getId() == null ? -1 : modelHospitalUpdataVO.getId())
                .eq("hospital_model_name", modelHospitalUpdataVO.getHospitalModelName());
        modelHospitalService.remove(queryModelHospital);

        boolean res = false;
        Date date = DateUtil.now();
        //保存数据
        ModelHospital modelHospitalDatas = new ModelHospital();
        BeanUtil.copyProperties(modelHospitalUpdataVO, modelHospitalDatas);
        modelHospitalDatas.setGmtCreate(date);
        modelHospitalDatas.setGmtModified(date);
        res = modelHospitalService.save(modelHospitalDatas);
        if (res) {
            UpdateWrapper<MedicalRecord> medicalRecordData = new UpdateWrapper<>();
            medicalRecordData.eq("rec_title", modelHospitalUpdataVO.getHospitalModelName())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .eq("hospital_id", modelHospitalUpdataVO.getHospitalId())
                    .set("mode_id", modelHospitalDatas.getId())
                    .set("gmt_modified", date);
            //更新med_medical_record表的modeId
            medicalRecordFacade.update(medicalRecordData);
            //更新异常数据状态
            qcAbnormalFacade.saveQcAnnormalNameAll(modelHospitalUpdataVO.getQcAbnormalName());
        }
        /* }*/

        return res;
    }

    public Boolean clearModelHospital(Long modeId) {
        boolean res= false;
        Date date = DateUtil.now();
        UpdateWrapper<ModelHospital> modelHospitalData = new UpdateWrapper<>();
        modelHospitalData.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("id", modeId)
                .set("is_deleted", IsDeleteEnum.Y.getKey())
                .set("gmt_create",date);
        res = modelHospitalService.update(modelHospitalData);
        UpdateWrapper<MedicalRecord> medicalRecordData = new UpdateWrapper<>();
        medicalRecordData.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("mode_id", modeId)
                .set("mode_id", 0)
                .set("gmt_create",date);
        medicalRecordFacade.update(medicalRecordData);
        return  res ;
    }
}
