package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.QcModeAllDTO;
import com.diagbot.dto.QcModeDTO;
import com.diagbot.entity.QcMode;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.QcModeService;
import com.diagbot.service.impl.QcModeServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description:
 * @author: zhoutg
 * @time: 2020/4/12 11:39
 */
@Component
public class QcModeFacade extends QcModeServiceImpl {

    @Autowired
    QcModeService qcModeService;

    public List<QcModeDTO> getMenu(Map<String, Object> pageData) {
        List<QcMode> qcModeList = this.list(new QueryWrapper<QcMode>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .orderByAsc("parent_id", "order_no")
        );

        for (int i = 0; i < qcModeList.size(); i++) {
            String key = qcModeList.get(i).getName();
            if (!pageData.containsKey(key) && !"病程信息".equals(key)) {
                qcModeList.remove(i--);
            }
        }
        Map<Long, List<QcMode>> map = EntityUtil.makeEntityListMap(qcModeList, "parentId");

        // 获取根节点
        List<QcMode> root = map.get(-1L);
        List<QcModeDTO> res = BeanUtil.listCopyTo(root, QcModeDTO.class);

        for (int i = 0; i < res.size(); i++) {
            QcModeDTO qcModeDTO = res.get(i);
            if (map.containsKey(qcModeDTO.getId())) {
                qcModeDTO.setSonMode(BeanUtil.listCopyTo(map.get(qcModeDTO.getId()), QcModeDTO.class));
            }
            // 如果病程信息下无内容，则删除
            if ("病程信息".equals(qcModeDTO.getName()) && ListUtil.isEmpty(qcModeDTO.getSonMode())) {
                res.remove(i--);
            }
        }
        return res;
    }


    public List<QcModeAllDTO> getModeAll() {
        List<QcMode> data = qcModeService.list();
        List<QcModeAllDTO> dataNew = new ArrayList<QcModeAllDTO>();
        dataNew = BeanUtil.listCopyTo(data, QcModeAllDTO.class);
        return dataNew;

    }
}
