package com.lantone.security.facade;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.lantone.security.dto.GetRealHospitalDTO;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.exception.ApiException;
import com.lantone.common.util.StringUtil;
import com.lantone.common.util.SysUserUtils;
import com.lantone.security.vo.GetHospitalSetVO;
import com.lantone.security.vo.SaveHospitalSetVO;
import com.lantone.security.entity.Hospital;
import com.lantone.security.entity.HospitalSet;
import com.lantone.security.facade.base.HospitalFacade;
import com.lantone.security.facade.base.HospitalSetFacade;
import com.lantone.security.enums.HospitalTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: cy
 * @time: 2021/7/30 16:30
 */
@Component
public class HospitalSetManagementFacade {
    @Autowired
    private HospitalSetFacade hospitalSetFacade;
    @Autowired
    private HospitalFacade hospitalFacade;


    /**
     * @Description:  新增参数
     * @Param: [saveHospitalSetVO]
     * @return: java.lang.Object
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Object addHospitalSet(SaveHospitalSetVO saveHospitalSetVO) {
       // 数据校验
        checkHospitalSet(saveHospitalSetVO);
        HospitalSet hospitalSet = new HospitalSet();
        BeanUtils.copyProperties(saveHospitalSetVO, hospitalSet);
        return addHospitalSet(saveHospitalSetVO.getUserId(),hospitalSet);
    }

    /**
     * @Description:  修改参数
     * @Param: [saveHospitalSetVO]
     * @return: java.lang.Object
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Object updateHospitalSet(SaveHospitalSetVO saveHospitalSetVO) {
        saveHospitalSetVO.setUserId(SysUserUtils.getCurrentPrincipleId());
        HospitalSet hospitalSet = new HospitalSet();
        BeanUtils.copyProperties(saveHospitalSetVO, hospitalSet);
        return updateHospitalSet(saveHospitalSetVO.getUserId(),hospitalSet);
    }

    /**
     * @Description:  查看参数管理
     * @Param: [getHospitalSetVO]
     * @return: IPage<com.lantone.security.entity.HospitalSet>
     * @Author: cy
     * @Date: 2021/7/30
     */
    public IPage<HospitalSet> getHospitalSet(GetHospitalSetVO getHospitalSetVO) {
        IPage<HospitalSet> pageInfo = new Page();
        pageInfo.setCurrent(getHospitalSetVO.getCurrent());
        pageInfo.setSize(getHospitalSetVO.getSize());
        LambdaQueryChainWrapper<HospitalSet> hospitalSetLambdaQuery = hospitalSetFacade.lambdaQuery();
        hospitalSetLambdaQuery.eq(HospitalSet::getIsDeleted, IsDeleteEnum.N.getKey());
        if (StringUtil.isNotEmpty(getHospitalSetVO.getHospitalName())) {
            hospitalSetLambdaQuery.like(HospitalSet::getHospitalName, getHospitalSetVO.getHospitalName());
        }
        if (StringUtil.isNotEmpty(getHospitalSetVO.getName())) {
            hospitalSetLambdaQuery.like(HospitalSet::getName, getHospitalSetVO.getName());
        }
        if (null != getHospitalSetVO.getCode()) {
            hospitalSetLambdaQuery.like(HospitalSet::getCode, getHospitalSetVO.getCode());
        }
        hospitalSetLambdaQuery.eq(null != getHospitalSetVO.getId(),HospitalSet::getId, getHospitalSetVO.getId());
        hospitalSetLambdaQuery.orderByDesc(HospitalSet::getGmtCreate);
        IPage<HospitalSet> page = hospitalSetLambdaQuery.page(pageInfo);
        return page;
    }

    /**
     * @Description: 删除参数管理-逻辑删除
     * @Param: [id]
     * @return: java.lang.Boolean
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Boolean deleteHospitalSet(Integer id) {
        return hospitalSetFacade.lambdaUpdate().eq(HospitalSet::getId,id)
                .set(HospitalSet::getIsDeleted, IsDeleteEnum.Y.getKey()).update();
    }




    /**
     * @Description: 新增-校验重复数据
     * @Param: [saveHospitalSetVO]
     * @return: void
     * @Author: cy
     * @Date: 2021/7/30
     */
    public void checkHospitalSet(SaveHospitalSetVO saveHospitalSetVO) {
        saveHospitalSetVO.setUserId(SysUserUtils.getCurrentPrincipleId());
        if (null == saveHospitalSetVO.getId()) {
            Integer count = hospitalSetFacade.lambdaQuery()
                    .eq(HospitalSet::getHospitalName, saveHospitalSetVO.getHospitalName())
                    .eq(HospitalSet::getCode, saveHospitalSetVO.getCode())
                    .eq(HospitalSet::getIsDeleted, IsDeleteEnum.N.getKey())
                    .eq(HospitalSet::getName, saveHospitalSetVO.getName())
                    .eq(HospitalSet::getVal, saveHospitalSetVO.getVal())
                    .count();
            if (count > 0) {
                throw new ApiException("当前参数添加重复");
            }
        }
    }

    /**
     * @Description:   新增参数
     * @Param: [hospitalSet]
     * @return: java.lang.Boolean
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Boolean addHospitalSet(Long userId,HospitalSet hospitalSet) {
        hospitalSet.setGmtCreate(new Date());
        hospitalSet.setCreator(userId.toString());
        return hospitalSetFacade.save(hospitalSet);
    }

    /**
     * @Description: 修改参数
     * @Param: [hospitalSet]
     * @return: java.lang.Boolean
     * @Author: cy
     * @Date: 2021/7/30
     */
    public Boolean updateHospitalSet(Long userId,HospitalSet hospitalSet) {
        hospitalSet.setGmtModified(new Date());
        hospitalSet.setModifier(userId.toString());
        return hospitalSetFacade.updateById(hospitalSet);
    }

    /**
     * @Description:  查看参数管理所属组织
     * @Param: [hospitalName]
     * @return: java.util.List<java.lang.String>
     * @Author: cy
     * @Date: 2021/8/9
     */
    public List<GetRealHospitalDTO> getHospitalNames(String hospitalName) {
        List<GetRealHospitalDTO> getRealHospitalDTOS = new ArrayList<>();
        LambdaQueryChainWrapper<Hospital> hospitalSetLambdaQuery = hospitalFacade.lambdaQuery();
        if(StringUtil.isNotBlank(hospitalName)){
            hospitalSetLambdaQuery.like(Hospital::getName,hospitalName);
        }
        hospitalSetLambdaQuery.eq(Hospital::getIsDeleted, IsDeleteEnum.N.getKey());
        hospitalSetLambdaQuery.eq(Hospital::getType, HospitalTypeEnum.SINGLE_HOSPITAL.getKey());
        List<Hospital> hspitals = hospitalSetLambdaQuery.list().stream().filter(obj -> StringUtil.isNotBlank(obj.getName())).sorted(Comparator.comparing(Hospital::getId)).collect(Collectors.toList());
        hspitals.forEach(kfc->{
            GetRealHospitalDTO getRealHospitalDTO = new GetRealHospitalDTO();
            getRealHospitalDTO.setHospitalId(kfc.getId());
            getRealHospitalDTO.setHospitalName(kfc.getName());
            getRealHospitalDTOS.add(getRealHospitalDTO);
        });
        return getRealHospitalDTOS;
    }

    public boolean initHospitalSetMap(){
        hospitalSetFacade.initHospitalSetMap();
        return true;
    }

}