package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.client.UserServiceClient;
import com.diagbot.dto.DeptInfoDTO;
import com.diagbot.dto.GetDeptInfoDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.DeptInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.DeptInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.AddDeptInfoVO;
import com.diagbot.vo.DeleteDeptInfoVO;
import com.diagbot.vo.GetDeptInfoDetialsVO;
import com.diagbot.vo.GetDeptInfoVO;
import com.diagbot.vo.UpdateDeptInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2018/11/22 11:44
 */
@Component
public class DeptInfoFacade extends DeptInfoServiceImpl {

    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 添加科室信息
     *
     * @param addDeptInfoVO
     * @return
     */
    public Boolean addDeptInfo(AddDeptInfoVO addDeptInfoVO) {
        //判断科室是否重名
        QueryWrapper<DeptInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        queryWrapper.eq("name", addDeptInfoVO.getName());
        if (this.count(queryWrapper) > 0) {
            throw new CommonException(CommonErrorCode.IS_EXISTS, "添加失败，科室已存在");
        }
        //添加科室
        DeptInfo deptInfo = new DeptInfo();
        BeanUtil.copyProperties(addDeptInfoVO, deptInfo);
        Date now = DateUtil.now();
        String userId = UserUtils.getCurrentPrincipleID();
        deptInfo.setCreator(userId);
        deptInfo.setGmtCreate(now);
        deptInfo.setGmtModified(now);
        deptInfo.setModifier(userId);
        this.save(deptInfo);
        return true;
    }

    /**
     * 修改科室信息
     *
     * @param updateDeptInfoVO
     * @return
     */
    public Boolean updateDeptInfo(UpdateDeptInfoVO updateDeptInfoVO) {
        //判断科室是否已被删除
        if(deptInfoIsDeletedStatus(updateDeptInfoVO.getId())){
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "科室已删除");
        }
        //修改操作
        DeptInfo deptInfo = this.getById(updateDeptInfoVO.getId());
        BeanUtil.copyProperties(updateDeptInfoVO, deptInfo);
        deptInfo.setModifier(UserUtils.getCurrentPrincipleID());
        deptInfo.setGmtModified(DateUtil.now());
        this.updateById(deptInfo);
        return true;
    }

    /**
     * 删除科室信息
     *
     * @param deleteDeptInfoVO
     * @return
     */
    public Boolean deleteDeptInfo(DeleteDeptInfoVO deleteDeptInfoVO) {
        //判断科室是否已被删除
        if(deptInfoIsDeletedStatus(deleteDeptInfoVO.getId())){
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "科室已删除");
        }
        //删除操作
        DeptInfo deptInfo = new DeptInfo();
        BeanUtil.copyProperties(deleteDeptInfoVO, deptInfo);
        deptInfo.setModifier(UserUtils.getCurrentPrincipleID());
        deptInfo.setIsDeleted(IsDeleteEnum.Y.getKey());
        deptInfo.setGmtModified(DateUtil.now());
        this.updateById(deptInfo);
        return true;
    }


    /**
     * 获取科室信息
     *
     * @param getDeptInfoVO
     * @return
     */
    public IPage<GetDeptInfoDTO> getDeptInfo(GetDeptInfoVO getDeptInfoVO) {
        IPage<GetDeptInfoDTO> iPage = this.getAllDeptInfo(getDeptInfoVO);
        List<String> ids = new ArrayList<>();
        for (GetDeptInfoDTO getDeptInfoDTO : iPage.getRecords()) {
            ids.add(getDeptInfoDTO.getModifier());
        }
        //获取用户信息
        RespDTO<Map<String, String>> respDTO = userServiceClient.getUserInfoByIds(ids);
        if (respDTO == null || !"0".equals(respDTO.code)) {
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "获取用户信息失败");
        }
        //将用户信息放入实体
        for (GetDeptInfoDTO getDeptInfoDTO : iPage.getRecords()) {
            getDeptInfoDTO.setUserName(respDTO.data.get(getDeptInfoDTO.getModifier()));
        }
        return iPage;
    }

    /**
     * 常用标签维护获取科室名称
     *
     * @return
     */
    public List<DeptInfoDTO> getAllDeptInfo() {
        List<DeptInfoDTO> deptInfoList = this.getDeptName();
        return deptInfoList;
    }

    /**
     * 科室维护详情
     *
     * @param getDeptInfoDetialsVO
     * @return
     */
    public DeptInfo getDeptInfoDetials(GetDeptInfoDetialsVO getDeptInfoDetialsVO) {
        QueryWrapper<DeptInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",IsDeleteEnum.N.getKey())
                    .eq("id",getDeptInfoDetialsVO.getId());
        DeptInfo deptInfo = this.getOne(queryWrapper);
        if(deptInfo == null){
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "科室信息不存在");
        }
        return deptInfo;
    }

    /**
     * 判断科室是否被删除
     *
     * @param deptId
     * @return
     */
    public Boolean deptInfoIsDeletedStatus(Long deptId){
        QueryWrapper<DeptInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",IsDeleteEnum.Y.getKey());
        queryWrapper.eq("id",deptId);
        return this.count(queryWrapper) > 0 ? true:false;
    }
}
