package com.lantone.security.facade;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.exception.Asserts;
import com.lantone.common.util.SysUserUtils;
import com.lantone.security.dto.GetMedoupManageDTO;
import com.lantone.security.entity.DeptMedoup;
import com.lantone.security.entity.Medoup;
import com.lantone.security.entity.RegionMedoup;
import com.lantone.security.facade.base.DeptMedoupFacade;
import com.lantone.security.facade.base.MedoupFacade;
import com.lantone.security.facade.base.RegionMedoupFacade;
import com.lantone.security.vo.AddMedoupManageVO;
import com.lantone.security.vo.DeleteMedoupManageVO;
import com.lantone.security.vo.GetMedoupManageVO;
import com.lantone.security.vo.UpdateMedoupManageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description: 医疗组管理API
 * @author： zhanghang
 * @time: 2022/3/17 13:20
 */
@Component
public class MedoupManagementFacade {
    @Autowired
    MedoupFacade medoupFacade;
    @Autowired
    DeptMedoupFacade deptMedoupFacade;
    @Autowired
    RegionMedoupFacade regionMedoupFacade;

    /**
     * 新增医疗组信息
     *
     * @param addMedoupManageVO
     * @return: 是否成功
     */
    public Boolean addMedoupManage(AddMedoupManageVO addMedoupManageVO) {
        Long currentPrincipleId = SysUserUtils.getCurrentPrincipleId();
        //医疗组表
        Medoup medoup = new Medoup();
        medoup.setCode(addMedoupManageVO.getCode());
        medoup.setName(addMedoupManageVO.getName());
        medoup.setGmtCreate(new Date());
        medoup.setCreator(currentPrincipleId.toString());
        try {
            medoupFacade.getBaseMapper().insert(medoup);
        } catch (Exception e) {
            Asserts.fail("新增医疗组失败!");
        }
        Long medoupId = medoup.getId();
        //病区与医疗组关联表
        RegionMedoup regionMedoup = new RegionMedoup();
        regionMedoup.setMedoupId(medoupId);
        regionMedoup.setRegionId(addMedoupManageVO.getRegionId());
        regionMedoup.setGmtCreate(new Date());
        regionMedoup.setCreator(currentPrincipleId.toString());
        try {
            regionMedoupFacade.getBaseMapper().insert(regionMedoup);
        } catch (Exception e) {
            Asserts.fail("新增病区与医疗组关联表失败!");
        }
        //科室与医疗组关联表
        DeptMedoup deptMedoup = new DeptMedoup();
        deptMedoup.setMedoupId(medoupId);
        deptMedoup.setDeptId(addMedoupManageVO.getDeptId());
        deptMedoup.setGmtCreate(new Date());
        deptMedoup.setCreator(currentPrincipleId.toString());
        try {
            deptMedoupFacade.getBaseMapper().insert(deptMedoup);
        } catch (Exception e) {
            Asserts.fail("新增科室与医疗组关联表失败!");
        }
        return true;
    }

    /**
     * 修改医疗组信息
     *
     * @param updateMedoupManageVO
     * @return: 是否成功
     */
    public Boolean updateMedoupManage(UpdateMedoupManageVO updateMedoupManageVO) {
        Long currentPrincipleId = SysUserUtils.getCurrentPrincipleId();
        //医疗组表
        medoupFacade.update(new UpdateWrapper<Medoup>()
                .eq("id", updateMedoupManageVO.getId())
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .set("code", updateMedoupManageVO.getCode())
                .set("name", updateMedoupManageVO.getName())
                .set("modifier", currentPrincipleId)
                .set("gmt_modified", new Date())
        );
        Long manageVOId = updateMedoupManageVO.getId();
        //删除病区与医疗组关联表
        regionMedoupFacade.remove(new QueryWrapper<RegionMedoup>()
                .eq("medoup_id", manageVOId)
                .eq("is_deleted", IsDeleteEnum.N.getKey())
        );
        //病区与医疗组关联表
        RegionMedoup regionMedoup = new RegionMedoup();
        regionMedoup.setMedoupId(manageVOId);
        regionMedoup.setRegionId(updateMedoupManageVO.getRegionId());
        regionMedoup.setGmtCreate(new Date());
        regionMedoup.setCreator(currentPrincipleId.toString());
        regionMedoupFacade.getBaseMapper().insert(regionMedoup);
        //删除科室与医疗组关联表
        deptMedoupFacade.remove(new QueryWrapper<DeptMedoup>()
                .eq("medoup_id", manageVOId)
                .eq("is_deleted", IsDeleteEnum.N.getKey())
        );
        //科室与医疗组关联表
        DeptMedoup deptMedoup = new DeptMedoup();
        deptMedoup.setMedoupId(manageVOId);
        deptMedoup.setDeptId(updateMedoupManageVO.getDeptId());
        deptMedoup.setGmtCreate(new Date());
        deptMedoup.setCreator(currentPrincipleId.toString());
        deptMedoupFacade.getBaseMapper().insert(deptMedoup);
        return true;
    }

    /**
     * 查看医疗组具体信息
     *
     * @param getMedoupManageVO
     * @return:
     */
    public GetMedoupManageDTO getMedoupManage(GetMedoupManageVO getMedoupManageVO) {
        return medoupFacade.getBaseMapper().getMedoupManage(getMedoupManageVO);
    }

    /**
     * 删除医疗组具体信息
     *
     * @param deleteMedoupManageVO
     * @return:
     */
    public Boolean deleteMedoupManage(DeleteMedoupManageVO deleteMedoupManageVO) {
        Long currentPrincipleId = SysUserUtils.getCurrentPrincipleId();
        String manageVOId = deleteMedoupManageVO.getId();
        regionMedoupFacade.update(new UpdateWrapper<RegionMedoup>()
                .eq("medoup_id", manageVOId)
                .set("is_deleted", IsDeleteEnum.Y.getKey())
                .set("modifier", currentPrincipleId)
                .set("gmt_modified", new Date())
        );
        deptMedoupFacade.update(new UpdateWrapper<DeptMedoup>()
                .eq("medoup_id", manageVOId)
                .set("is_deleted", IsDeleteEnum.Y.getKey())
                .set("modifier", currentPrincipleId)
                .set("gmt_modified", new Date())
        );
        medoupFacade.update(new UpdateWrapper<Medoup>()
                .eq("id", manageVOId)
                .eq("is_deleted", IsDeleteEnum.Y.getKey())
                .set("modifier", currentPrincipleId)
                .set("gmt_modified", new Date())
        );
        return true;
    }
}
