package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.YpZdClass;
import cn.hnthyy.thmz.entity.his.zd.MzZdYpYsh;
import cn.hnthyy.thmz.service.his.mz.*;
import cn.hnthyy.thmz.service.his.zd.MzZdYpYshService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
public class YpZdClassController {
    @Autowired
    private YpZdClassService ypZdClassService;
    @Autowired
    private MzZdYpYshService mzZdYpYshService;


    /**
     * 查询所有药品分类
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpZdClasses", method = {RequestMethod.GET})
    public Map<String, Object> getYpZdClasses() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            //普通西药
            List<YpZdClass> ypZdClasses = ypZdClassService.queryYpZdClasses();
            List<Map<String, Object>> treeList = null;
            if (ypZdClasses == null || ypZdClasses.size() == 0) {
                log.info("未查询到药品分类");
            } else {
                treeList = getYpZdClassTreeList(ypZdClasses);
            }
            List<Map<String, Object>> fullTree = new ArrayList<>();
            if(treeList!=null){
                Map<String, Object> parentMap = new HashMap<>();
                fullTree.add(parentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                parentMap.put("isParent", true);
                parentMap.put("id", 1);
                parentMap.put("name", "西成药");
                parentMap.put("children", treeList);
            }
            //医材
            List<MzZdYpYsh> ycList = mzZdYpYshService.queryAllYcList();
            List<Map<String, Object>> ycTreeList = null;
            if (ycList == null || ycList.size() == 0) {
                log.info("未查询到医材分类");
            } else {
                ycTreeList = getCommonTreeList(ycList);
            }
            if(ycTreeList!=null){
                Map<String, Object> parentMap = new HashMap<>();
                fullTree.add(parentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                parentMap.put("isParent", true);
                parentMap.put("id", 91);
                parentMap.put("name", "医材");
                parentMap.put("children", ycTreeList);
            }
            //麻、精一
            List<MzZdYpYsh> mjyList = mzZdYpYshService.queryAllMjyList();
            List<Map<String, Object>> mjyTreeList = null;
            if (mjyList == null || mjyList.size() == 0) {
                log.info("未查询到麻、精一药品分类");
            } else {
                mjyTreeList = getCommonTreeList(mjyList);
            }
            if(mjyTreeList!=null){
                Map<String, Object> parentMap = new HashMap<>();
                fullTree.add(parentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                parentMap.put("isParent", true);
                parentMap.put("id", 111);
                parentMap.put("name", "麻、精一");
                parentMap.put("children", mjyTreeList);
            }
            //精二
            List<MzZdYpYsh> jeList = mzZdYpYshService.queryAllJeList();
            List<Map<String, Object>> jeTreeList = null;
            if (jeList == null || jeList.size() == 0) {
                log.info("未查询到精二药品分类");
            } else {
                jeTreeList = getCommonTreeList(jeList);
            }
            if(jeTreeList!=null){
                Map<String, Object> parentMap = new HashMap<>();
                fullTree.add(parentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                parentMap.put("isParent", true);
                parentMap.put("id", 131);
                parentMap.put("name", "精二");
                parentMap.put("children", jeTreeList);
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的分类成功");
            resultMap.put("data", fullTree);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有药品分类失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有药品分类失败,错误原因【" + e.getMessage() + "】");
            return resultMap;
        }
    }



    /**
     * 查询所有检验、检查的分类
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getJyJcZdClasses", method = {RequestMethod.GET})
    public Map<String, Object> getJyJcZdClasses() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> jyZdClasses = ypZdClassService.queryJyZdClasses();
            List<Map<String, Object>> jyTreeList = null;
            if (jyZdClasses == null || jyZdClasses.size() == 0) {
                log.info("未查询到检验分类");
            } else {
                jyTreeList = getJyZdClassTreeList(jyZdClasses);
            }

            List<Map<String, Object>> jcZdClasses = ypZdClassService.queryJcZdClasses();
            List<Map<String, Object>> jcTreeList = null;
            if (jcZdClasses == null || jcZdClasses.size() == 0) {
                log.info("未查询到检查分类");
            } else {
                jcTreeList = getJcZdClassTreeList(jcZdClasses);
            }
            List<Map<String, Object>> fullTree = new ArrayList<>();
            if(jyTreeList!=null){
                Map<String, Object> parentMap = new HashMap<>();
                fullTree.add(parentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                parentMap.put("isParent", true);
                parentMap.put("id", 2);
                parentMap.put("name", "检验项目");
                parentMap.put("children", jyTreeList);
            }
            if(jcTreeList!=null){
                Map<String, Object> parentMap = new HashMap<>();
                fullTree.add(parentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                parentMap.put("isParent", true);
                parentMap.put("id", 3);
                parentMap.put("name", "检查项目");
                parentMap.put("children", jcTreeList);
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询所有的分类成功");
            resultMap.put("data", fullTree);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询所有检验、检查的分类失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询所有检验、检查的分类失败,错误原因【" + e.getMessage() + "】");
            return resultMap;
        }
    }


//    /**
//     * 查询所有药品、检验、检查的分类
//     *
//     * @return
//     */
//    @UserLoginToken
//    @RequestMapping(value = "/getYpZdClasses", method = {RequestMethod.GET})
//    public Map<String, Object> getYpZdClasses() {
//        Map<String, Object> resultMap = new HashMap<>();
//        try {
//            List<YpZdClass> ypZdClasses = ypZdClassService.queryYpZdClasses();
//            List<Map<String, Object>> treeList = null;
//            if (ypZdClasses == null || ypZdClasses.size() == 0) {
//                log.info("未查询到药品分类");
//            } else {
//                treeList = getYpZdClassTreeList(ypZdClasses);
//            }
//
//            List<Map<String, Object>> jyZdClasses = ypZdClassService.queryJyZdClasses();
//            List<Map<String, Object>> jyTreeList = null;
//            if (jyZdClasses == null || jyZdClasses.size() == 0) {
//                log.info("未查询到检验分类");
//            } else {
//                jyTreeList = getJyZdClassTreeList(jyZdClasses);
//            }
//
//            List<Map<String, Object>> jcZdClasses = ypZdClassService.queryJcZdClasses();
//            List<Map<String, Object>> jcTreeList = null;
//            if (jcZdClasses == null || jcZdClasses.size() == 0) {
//                log.info("未查询到检查分类");
//            } else {
//                jcTreeList = getJcZdClassTreeList(jcZdClasses);
//            }
//            List<Map<String, Object>> fullTree = new ArrayList<>();
//            if(treeList!=null){
//                Map<String, Object> parentMap = new HashMap<>();
//                fullTree.add(parentMap);
//                //初始化时不知道自己有子节点，所以设置为非父节点
//                parentMap.put("isParent", true);
//                parentMap.put("id", 1);
//                parentMap.put("name", "西成药");
//                parentMap.put("children", treeList);
//            }
//            if(jyTreeList!=null){
//                Map<String, Object> parentMap = new HashMap<>();
//                fullTree.add(parentMap);
//                //初始化时不知道自己有子节点，所以设置为非父节点
//                parentMap.put("isParent", true);
//                parentMap.put("id", 2);
//                parentMap.put("name", "检验项目");
//                parentMap.put("children", jyTreeList);
//            }
//            if(jcTreeList!=null){
//                Map<String, Object> parentMap = new HashMap<>();
//                fullTree.add(parentMap);
//                //初始化时不知道自己有子节点，所以设置为非父节点
//                parentMap.put("isParent", true);
//                parentMap.put("id", 3);
//                parentMap.put("name", "检查项目");
//                parentMap.put("children", jcTreeList);
//            }
//            resultMap.put("code", 0);
//            resultMap.put("message", "查询所有的分类成功");
//            resultMap.put("data", fullTree);
//            return resultMap;
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("查询所有药品、检验、检查的分类失败，错误信息{}", e.getMessage());
//            resultMap.put("code", -1);
//            resultMap.put("message", "查询所有药品、检验、检查的分类失败,错误原因【" + e.getMessage() + "】");
//            return resultMap;
//        }
//    }

    /**
     * 获取药品字典树
     *
     * @param ypZdClasses
     * @return
     */
    private List<Map<String, Object>> getYpZdClassTreeList(List<YpZdClass> ypZdClasses) {
        List<Map<String, Object>> treeList = new ArrayList<>();
        //当前一级父节点
        Map<String, Object> grandParentMap = null;
        //当前二级父节点
        Map<String, Object> parentMap = null;
        for (YpZdClass ypZdClass : ypZdClasses) {
            if (ypZdClass.getCode().endsWith("0000")) {
                grandParentMap = new HashMap<>();
                treeList.add(grandParentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                grandParentMap.put("isParent", false);
                grandParentMap.put("id", ypZdClass.getCode());
                grandParentMap.put("name", ypZdClass.getName());
            } else if (ypZdClass.getCode().endsWith("00")) {
                //此时已经有子节点，设置成父节点
                grandParentMap.put("isParent", true);
                Map<String, Object> temMap = new HashMap<>();
                //初始化时不知道自己有子节点，所以设置为非父节点
                temMap.put("isParent", false);
                temMap.put("id", ypZdClass.getCode());
                temMap.put("name", ypZdClass.getName());
                List<Map<String, Object>> children = (List<Map<String, Object>>) grandParentMap.get("children");
                if (children == null) {
                    children = new ArrayList<>();
                    grandParentMap.put("children", children);
                }
                children.add(temMap);
                parentMap = temMap;
            } else {
                parentMap.put("isParent", true);
                Map<String, Object> temMap = new HashMap<>();
                //初始化时不知道自己有子节点，所以设置为非父节点
                temMap.put("isParent", false);
                temMap.put("id", ypZdClass.getCode());
                temMap.put("name", ypZdClass.getName());
                List<Map<String, Object>> children = (List<Map<String, Object>>) parentMap.get("children");
                if (children == null) {
                    children = new ArrayList<>();
                    parentMap.put("children", children);
                }
                children.add(temMap);
            }
        }
        return treeList;
    }


    /**
     * 获取通用字典树
     *
     * @param ycList
     * @return
     */
    private List<Map<String, Object>> getCommonTreeList(List<MzZdYpYsh> ycList) {
        List<Map<String, Object>> treeList = new ArrayList<>();
        Set<String> set = new HashSet<>();
        for (MzZdYpYsh mzZdYpYsh : ycList) {
            if(set.contains(mzZdYpYsh.getCode())){
                continue;
            }
            Map<String, Object> temMap = new HashMap<>();
            //初始化时不知道自己有子节点，所以设置为非父节点
            temMap.put("isParent", false);
            temMap.put("id", mzZdYpYsh.getCode());
            temMap.put("name", mzZdYpYsh.getName());
            treeList.add(temMap);
            set.add(mzZdYpYsh.getCode());
        }
        return treeList;
    }


    /**
     * 获取所有检验分类字典树
     *
     * @param jyZdClasses
     * @return
     */
    private List<Map<String, Object>> getJyZdClassTreeList(List<Map<String, Object>> jyZdClasses) {
        List<Map<String, Object>> treeList = new ArrayList<>();
        //父节点
        Map<String, Object> parentMap = null;
        //上一条数据的父编码
        String prevDlCode = null;
        for (Map<String, Object> jyZdClass : jyZdClasses) {
            String xlCode = (String) jyZdClass.get("xl_code");
            if(StringUtils.isBlank(xlCode)){
                continue;
            }
            String dlCode = (String) jyZdClass.get("dl_code");
            String dlName = (String) jyZdClass.get("dl_name");
            String xlName = (String) jyZdClass.get("xl_name");
            if(prevDlCode==null || !prevDlCode.equals(dlCode)){
                parentMap = new HashMap<>();
                treeList.add(parentMap);
                parentMap.put("isParent", true);
                parentMap.put("id", dlCode);
                parentMap.put("name", dlName);
                List<Map<String, Object>> children = new ArrayList<>();
                parentMap.put("children", children);
                Map<String, Object> temMap = new HashMap<>();
                //初始化时不知道自己有子节点，所以设置为非父节点
                temMap.put("isParent", false);
                temMap.put("id", xlCode);
                temMap.put("name", xlName);
                children.add(temMap);
            }else {
                List<Map<String, Object>> children = (List<Map<String, Object>>) parentMap.get("children");
                Map<String, Object> temMap = new HashMap<>();
                //初始化时不知道自己有子节点，所以设置为非父节点
                temMap.put("isParent", false);
                temMap.put("id", xlCode);
                temMap.put("name", xlName);
                children.add(temMap);
            }
            prevDlCode=dlCode;
        }
        return treeList;
    }

    /**
     * 获取所有检查分类字典树
     *
     * @param jyZcClasses
     * @return
     */
    private List<Map<String, Object>> getJcZdClassTreeList(List<Map<String, Object>> jyZcClasses) {
        List<Map<String, Object>> treeList = new ArrayList<>();
        //当前一级父节点
        Map<String, Object> grandParentMap = null;
        //当前二级父节点
        Map<String, Object> parentMap = null;
        //当前数据的祖父编码
        String currentGrandParentDlCode = null;
        //当前数据的父编码
        String currentParentDlCode = null;
        for (Map<String, Object> jcZdClass : jyZcClasses) {
            String dlCode = (String) jcZdClass.get("dl_code");
            if(StringUtils.isBlank(dlCode)){
                continue;
            }
            String dlName = (String) jcZdClass.get("dl_name");
            String xlCode = (String) jcZdClass.get("xl_code");
            String xlName = (String) jcZdClass.get("xl_name");
            //父编码是2位数 说明是一级树
            if(dlCode.length()==2 && !dlCode.equals(currentGrandParentDlCode)){
                grandParentMap = new HashMap<>();
                treeList.add(grandParentMap);
                //初始化时不知道自己有子节点，所以设置为非父节点
                grandParentMap.put("isParent", false);
                grandParentMap.put("id", dlCode);
                grandParentMap.put("name", dlName);
                currentGrandParentDlCode=dlCode;
                if(StringUtils.isNotBlank(xlCode)){
                    grandParentMap.put("isParent", true);
                    List<Map<String, Object>> children = new ArrayList<>();
                    grandParentMap.put("children", children);
                    Map<String, Object> temMap = new HashMap<>();
                    //初始化时不知道自己有子节点，所以设置为非父节点
                    temMap.put("isParent", false);
                    temMap.put("id", xlCode);
                    temMap.put("name", xlName);
                    children.add(temMap);
                }
                //以当前的祖父节点的编码做为编码起始，说明示二级菜单
            }else if(dlCode.startsWith(currentGrandParentDlCode)){
                //此时已经有子节点，设置成父节点
                grandParentMap.put("isParent", true);
                List<Map<String, Object>> children = (List<Map<String, Object>>) grandParentMap.get("children");
                if (children == null) {
                    children = new ArrayList<>();
                    grandParentMap.put("children", children);
                }
                if(parentMap==null || !currentParentDlCode.equals(dlCode)){
                    parentMap=new HashMap<>();
                    //初始化时不知道自己有子节点，所以设置为非父节点
                    parentMap.put("isParent", false);
                    parentMap.put("id", dlCode);
                    parentMap.put("name", dlName.substring(dlName.indexOf(" ")+1));
                    children.add(parentMap);
                    currentParentDlCode=dlCode;
                }
                if(StringUtils.isNotBlank(xlCode)){
                    parentMap.put("isParent", true);
                    List<Map<String, Object>> grandchildren = (List<Map<String, Object>>) parentMap.get("children");
                    if (grandchildren == null) {
                        grandchildren = new ArrayList<>();
                        parentMap.put("children", grandchildren);
                    }
                    parentMap.put("children", grandchildren);
                    Map<String, Object> temMap = new HashMap<>();
                    //初始化时不知道自己有子节点，所以设置为非父节点
                    temMap.put("isParent", false);
                    temMap.put("id", xlCode);
                    temMap.put("name", xlName);
                    grandchildren.add(temMap);
                }
            }
        }
        return treeList;
    }

    /**
     * 根据code模糊查询该药品分类的子类
     * @param code
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getYpZdClassTreeListByCode",method = {RequestMethod.GET})
    public Map<String, Object> getYpZdClassTreeListByCode(@RequestParam("code") String code) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            String reCode = new StringBuffer(code).reverse().toString();
            int i = Integer.parseInt(reCode);
            if (i == 0) {
                code = "%0000";//查询一级节点
            } else if (i < 100) {
                code = code.substring(0, 2) + "%00";//查询二级节点
            } else if (i < 10000) {
                code = code.substring(0, 4) + "%";//查询三级节点
            }
            List<YpZdClass> ypZdClasses = ypZdClassService.queryYpZdClassesByCode(code);
            List<Map<String,Object>> maps = new ArrayList<>();
            for (int j = 0; j < ypZdClasses.size(); j++) {//将多查询的自身节点删除
                Map<String,Object> map = new HashMap<>();
                YpZdClass ypZdClass = ypZdClasses.get(j);
                if( i == 0){
                    //查询是否为父节点
                    String prCode = ypZdClass.getCode().substring(0, 2) + "%";
                    map.put("isParent",ypZdClassService.queryIsParent(prCode));
                } else if (i > 0 && i < 100) {
                    if (ypZdClass.getCode().substring(2, 4).equals("00")) {
                        ypZdClasses.remove(ypZdClass);
                        j--;
                        continue;
                    }
                    //查询是否为父节点
                    String prCode = ypZdClass.getCode().substring(0, 4) + "%";
                    map.put("isParent",ypZdClassService.queryIsParent(prCode));
                } else if (i > 0 && i < 10000) {
                    if (ypZdClass.getCode().substring(4, 6) .equals("00")) {
                        ypZdClasses.remove(ypZdClass);
                        j--;
                        continue;
                    }
                    map.put("isParent",0);//三级节点为子节点
                }
                map.put("code",ypZdClass.getCode());
                map.put("text",ypZdClass.getName());
                maps.add(map);
            }
            resultMap.put("code", 0);
            resultMap.put("data", maps);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询该药品分类的子类失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询该药品分类的子类失败,错误原因【" + e.getMessage() + "】");
            return resultMap;
        }
    }
}
