package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.diagbot.client.UserServiceClient;
import com.diagbot.dto.QuestionShortDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.VitalInfoDTO;
import com.diagbot.dto.VitalOrderDTO;
import com.diagbot.entity.QuestionInfo;
import com.diagbot.entity.VitalOrder;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.enums.QuestionTypeEnum;
import com.diagbot.service.VitalOrderService;
import com.diagbot.service.impl.VitalOrderServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.VitalOrderVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2018/11/22 11:45
 */
@Component
public class VitalOrderFacade extends VitalOrderServiceImpl {

    @Autowired
    @Qualifier("vitalOrderServiceImpl")
    VitalOrderService vitalOrderService;
    @Autowired
    QuestionInfoFacade questionInfoFacade;
    @Autowired
    UserServiceClient userServiceClient;

    public Boolean saveAll(List<VitalOrderVO> vitalOrderVOList) {
        //先删除现有排序
        UpdateWrapper<VitalOrder> vitalOrderUpdateWrapper = new UpdateWrapper<>();
        vitalOrderUpdateWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                set("is_deleted", IsDeleteEnum.Y.getKey()).
                set("modifier", UserUtils.getCurrentPrincipleID()).
                set("gmt_modified", DateUtil.now());
        this.update(new VitalOrder(), vitalOrderUpdateWrapper);
        //插入新排序
        List<VitalOrder> vitalOrderList = Lists.newArrayList();
        for (VitalOrderVO vitalOrderVO : vitalOrderVOList) {
            VitalOrder vitalOrder = new VitalOrder();
            vitalOrder.setQuestionId(vitalOrderVO.getVitalId());
            vitalOrder.setOrderNo(vitalOrderVO.getOrderNo());
            vitalOrder.setCreator(UserUtils.getCurrentPrincipleID());
            vitalOrder.setGmtCreate(DateUtil.now());
            vitalOrder.setModifier(UserUtils.getCurrentPrincipleID());
            vitalOrder.setGmtModified(DateUtil.now());
            vitalOrderList.add(vitalOrder);
        }
        vitalOrderService.saveBatch(vitalOrderList);
        return true;
    }

    /**
     * 获取所有查体排序关系
     *
     * @return
     */
    public VitalOrderDTO getVitalOrderList() {
        VitalOrderDTO vitalOrderDTO = new VitalOrderDTO();

        QueryWrapper<VitalOrder> vitalOrderQueryWrapper = new QueryWrapper<>();
        vitalOrderQueryWrapper.orderByAsc("order_no")
                .eq("is_deleted", IsDeleteEnum.N.getKey());
        List<VitalOrder> vitalOrderList = this.list(vitalOrderQueryWrapper);
        List<Long> vitalIds = vitalOrderList.stream().map(vitalOrder -> vitalOrder.getQuestionId()).collect(Collectors.toList());

        //操作人
        if (vitalOrderList.size() > 0) {
            String userId = vitalOrderList.get(0).getModifier();
            vitalOrderDTO.setGmtOperate(vitalOrderList.get(0).getGmtModified());
            vitalOrderDTO.setOperator(userId);
            List<String> userIds = Lists.newArrayList();
            userIds.add(userId);
            RespDTO<Map<String, String>> userInfoMap = userServiceClient.getUserInfoByIds(userIds);
            vitalOrderDTO.setOperatorName(userInfoMap.data.get(userId));
        }

        //已排序查体标签
        List<QuestionInfo> vitals = Lists.newArrayList(questionInfoFacade.listByIds(vitalIds));
        Map<Long, QuestionInfo> vitalMap = EntityUtil.makeEntityMap(vitals, "id");

        List<VitalInfoDTO> vitalInfoDTOList = Lists.newLinkedList();
        for (VitalOrder vitalOrder : vitalOrderList) {
            VitalInfoDTO vitalInfoDTO = new VitalInfoDTO();
            QuestionInfo questionInfo = vitalMap.get(vitalOrder.getQuestionId());
            if (questionInfo != null) {
                BeanUtil.copyProperties(questionInfo, vitalInfoDTO);
            }
            vitalInfoDTO.setOrderNo(vitalOrder.getOrderNo());
            vitalInfoDTOList.add(vitalInfoDTO);
        }
        vitalOrderDTO.setVitals(vitalInfoDTOList);

        //未排序查体标签
        QueryWrapper<QuestionInfo> unOrderedVitalQueryWrapper = new QueryWrapper<>();
        unOrderedVitalQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("type", QuestionTypeEnum.Vital.getKey())
                .eq("sub_type", 0)
                .notIn("id", vitalIds);
        List<QuestionInfo> unOrderVitals = questionInfoFacade.list(unOrderedVitalQueryWrapper);
        List<QuestionShortDTO> unOrderedVitalList = Lists.newLinkedList();
        for (QuestionInfo questionInfo : unOrderVitals) {
            QuestionShortDTO questionShortDTO = new QuestionShortDTO();
            BeanUtil.copyProperties(questionInfo, questionShortDTO);
            unOrderedVitalList.add(questionShortDTO);
        }
        vitalOrderDTO.setUnOrderedVitals(unOrderedVitalList);

        return vitalOrderDTO;
    }

    /**
     * 获取未排序查体标签
     *
     * @return
     */
    public List<QuestionShortDTO> getUnOrderedVitalList() {
        //已排序查体标签
        QueryWrapper<VitalOrder> vitalOrderQueryWrapper = new QueryWrapper<>();
        vitalOrderQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        List<VitalOrder> vitalOrderList = this.list(vitalOrderQueryWrapper);
        List<Long> vitalIds = vitalOrderList.stream().map(vitalOrder -> vitalOrder.getQuestionId()).collect(Collectors.toList());

        //未排序查体标签
        QueryWrapper<QuestionInfo> unOrderedVitalQueryWrapper = new QueryWrapper<>();
        unOrderedVitalQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("type", QuestionTypeEnum.Vital.getKey())
                .eq("sub_type", 0)
                .notIn("id", vitalIds);
        List<QuestionInfo> vitals = questionInfoFacade.list(unOrderedVitalQueryWrapper);

        List<QuestionShortDTO> questionShortDTOList = Lists.newLinkedList();
        for (QuestionInfo questionInfo : vitals) {
            QuestionShortDTO questionShortDTO = new QuestionShortDTO();
            BeanUtil.copyProperties(questionInfo, questionShortDTO);
            questionShortDTOList.add(questionShortDTO);
        }
        return questionShortDTOList;
    }

    /**
     * 删除排序
     *
     * @param vitalId
     * @return
     */
    public Boolean deleteVitalOrder(Long vitalId) {
        UpdateWrapper<VitalOrder> vitalOrderUpdateWrapper = new UpdateWrapper<>();
        vitalOrderUpdateWrapper.eq("vital_id", vitalId).
                eq("is_deleted", IsDeleteEnum.N.getKey()).
                set("is_deleted", IsDeleteEnum.Y.getKey()).
                set("modifier", UserUtils.getCurrentPrincipleID()).
                set("gmt_modified", DateUtil.now());
        this.update(new VitalOrder(), vitalOrderUpdateWrapper);
        return true;
    }

    /**
     * 删除排序-批量
     *
     * @param vitalIds
     * @return
     */
    public Boolean deleteVitalOrders(Long[] vitalIds) {
        UpdateWrapper<VitalOrder> vitalOrderUpdateWrapper = new UpdateWrapper<>();
        vitalOrderUpdateWrapper.in("vital_id", vitalIds).
                eq("is_deleted", IsDeleteEnum.N.getKey()).
                set("is_deleted", IsDeleteEnum.Y.getKey()).
                set("modifier", UserUtils.getCurrentPrincipleID()).
                set("gmt_modified", DateUtil.now());
        this.update(new VitalOrder(), vitalOrderUpdateWrapper);
        return true;
    }
}