package com.lantone.security.facade;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.lantone.security.dto.ViewRegionDTO;
import com.lantone.common.exception.ApiException;
import com.lantone.common.util.SysUserUtils;
import com.lantone.security.vo.CheckSolveRegionVO;
import com.lantone.security.vo.SaveRegionVO;
import com.lantone.security.vo.GetRegionVO;
import com.lantone.security.vo.UpdateRegionVO;
import com.lantone.security.entity.Region;
import com.lantone.security.entity.RegionDept;
import com.lantone.security.facade.base.RegionFacade;
import com.lantone.security.service.RegionDeptService;
import com.lantone.common.enums.IsDeleteEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.util.Date;

/**
 * <p>
 * 病区表 服务实现类Facade
 * </p>
 */
@Component
public class RegionManagementFacade {
    @Autowired
    @Qualifier("regionDeptServiceImpl")
    private RegionDeptService regionDeptService;
    @Autowired
    private RegionFacade regionFacade;

    /**
     * @Description: 新增病区
     * @Param: [saveRegionVo]
     * @return: java.lang.Object
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Object addRegion(SaveRegionVO saveRegionVo) {
        //数据校验
        getCheckRegion(saveRegionVo);
        Region sysRegion = new Region();
        BeanUtils.copyProperties(saveRegionVo, sysRegion);
        sysRegion.setGmtCreate(new Date());
        sysRegion.setCreator(saveRegionVo.getUserId().toString());
        return regionFacade.save(sysRegion);
    }

    /**
     * @Description: 修改病区
     * @Param: [saveRegionVo]
     * @return: java.lang.Object
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Object updateRegion(UpdateRegionVO updateRegionVO) {
        updateRegionVO.setHospitalId(SysUserUtils.getCurrentHospitalId());
        updateRegionVO.setUserId(SysUserUtils.getCurrentPrincipleId());
        //回显
        if (1 == updateRegionVO.getMark()) {
            GetRegionVO getRegionVO = new GetRegionVO();
            BeanUtils.copyProperties(updateRegionVO, getRegionVO);
            return regionFacade.getBaseMapper().getWard(getRegionVO);
        }
        Region sysRegion = new Region();
        BeanUtils.copyProperties(updateRegionVO, sysRegion);
        sysRegion.setGmtModified(new Date());
        sysRegion.setModifier(updateRegionVO.getUserId().toString());
        return regionFacade.updateById(sysRegion);
    }

    /**
     * @Description: 查看病区管理
     * @Param: [getRegionVO]
     * @return: IPage<com.lantone.security.dto.ViewRegionDTO>
     * @Author: cy
     * @Date: 2021/7/30
     */
    public IPage<ViewRegionDTO> getRegion(GetRegionVO getRegionVO) {
        getRegionVO.setHospitalId(SysUserUtils.getCurrentHospitalId());
        getRegionVO.setUserId(SysUserUtils.getCurrentPrincipleId());
        return regionFacade.getBaseMapper().getWard(getRegionVO);
    }

    /**
     * @Description: 删除病区管理
     * @Param: [id]
     * @return: java.lang.Boolean
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Boolean deleteRegion(Integer id) {
        Long hospitalId = SysUserUtils.getCurrentHospitalId();
        boolean flag = regionFacade.lambdaUpdate()
                .eq(Region::getId, id)
                .eq(Region::getHospitalId, hospitalId)
                .set(Region::getIsDeleted, IsDeleteEnum.Y.getKey())
                .update();
        return flag;
    }

    /**
     * @Description: 新增-校验重复数据
     * @Param: [saveRegionVo]
     * @return: void
     * @Author: cy
     * @Date: 2021/7/30
     */
    public void getCheckRegion(SaveRegionVO saveRegionVo) {
        saveRegionVo.setHospitalId(SysUserUtils.getCurrentHospitalId());
        saveRegionVo.setUserId(SysUserUtils.getCurrentPrincipleId());
        int count = regionFacade.lambdaQuery()
                .eq(Region::getIsDeleted, IsDeleteEnum.N.getKey())
                .eq(Region::getName, saveRegionVo.getName())
                .eq(Region::getHospitalId, saveRegionVo.getHospitalId())
                .count();
        if (count > 0) {
            throw new ApiException("上级组织和病区名称已重复");
        }
    }

    /**
     * @Description: 校验/禁用-启用病区管理
     * @Param: [checkSolveRegionVO]
     * @return: java.lang.Object
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Object checkSolveRegion(CheckSolveRegionVO checkSolveRegionVO) {
        checkSolveRegionVO.setHospitalId(SysUserUtils.getCurrentHospitalId());
        checkSolveRegionVO.setUserId(SysUserUtils.getCurrentPrincipleId());
        //禁用校验
        if (null != checkSolveRegionVO.getId() && 0 == checkSolveRegionVO.getMark()) {
            long count = regionDeptService.lambdaQuery()
                    .eq(RegionDept::getIsDeleted, IsDeleteEnum.N.getKey())
                    .eq(RegionDept::getRegionId, checkSolveRegionVO.getId())
                    .eq(RegionDept::getHospitalId, checkSolveRegionVO.getHospitalId())
                    .list().stream().count();
            if (count>0) {
                //当前病区存在与之关联的科室
                return 1;
            }
            return 0;
        }

        //禁用-启用病区处理
        if (null != checkSolveRegionVO.getId() && (1 == checkSolveRegionVO.getMark() || 2 == checkSolveRegionVO.getMark())) {
            LambdaUpdateChainWrapper<Region> regionLambdaUpdate = regionFacade.lambdaUpdate();
            regionLambdaUpdate.eq(Region::getIsDeleted, IsDeleteEnum.N.getKey());
            regionLambdaUpdate.eq(Region::getId, checkSolveRegionVO.getId());
            regionLambdaUpdate.eq(Region::getHospitalId, checkSolveRegionVO.getHospitalId());
            if (1 == checkSolveRegionVO.getMark()) {
                regionLambdaUpdate.set(Region::getStatus, 0).update();
            }
            if (2 == checkSolveRegionVO.getMark()) {
                regionLambdaUpdate.set(Region::getStatus, 1).update();
            }
            regionLambdaUpdate.set(Region::getModifier, checkSolveRegionVO.getUserId().toString()).update();
            regionLambdaUpdate.set(Region::getGmtModified, new Date()).update();
        }
        return true;
    }
}