package com.yihu.jw.base.service.menu;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.base.dao.article.KnowledgeArticleDictDao;
import com.yihu.jw.base.dao.article.KnowledgeArticleMenuDao;
import com.yihu.jw.base.dao.menu.BaseLinkDictDao;
import com.yihu.jw.base.dao.menu.BaseMenuDictDao;
import com.yihu.jw.base.dao.menu.BaseMenuShowDao;
import com.yihu.jw.entity.base.menu.BaseLinkDictDO;
import com.yihu.jw.entity.base.menu.BaseMenuDictDO;
import com.yihu.jw.entity.base.menu.BaseMenuShowDO;

import com.yihu.jw.entity.hospital.article.KnowledgeArticleDictDO;
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;

import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;

import com.yihu.jw.restmodel.web.ListEnvelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.StringUtil;
import com.yihu.jw.utils.hibernate.HibenateUtils;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class BaseMenuManageService {
    @Autowired
    private BaseMenuDictDao baseMenuDictDao;
    @Autowired
    private BaseMenuShowDao baseMenuShowDao;
    @Autowired
    private BaseLinkDictDao baseLinkDictDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private KnowledgeArticleMenuDao knowledgeArticleMenuDao;
    @Autowired
    private KnowledgeArticleDictDao knowledgeArticleDictDao;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;
    //删除菜单字典
    public void deletMenuDict(String id) throws  Exception{
        BaseMenuDictDO baseMenuDictDO= baseMenuDictDao.findOne(id);
        if (baseMenuDictDO!=null){
            if ("0".equalsIgnoreCase(baseMenuDictDO.getParentId())){
                 List<BaseMenuDictDO> childList = baseMenuDictDao.findByParentId(baseMenuDictDO.getId());
                 if (childList!=null&&childList.size()>0){
                     throw  new Exception("存在子菜单不允许删除");
                 }else {
                     baseMenuDictDO.setIsDel("0");
                 }
            }else {
                if (1==baseMenuDictDO.getStatus()){
                    throw  new Exception("生效的子菜单不允许删除");
                }else{
                    baseMenuDictDO.setIsDel("0");
                }
            }
            baseMenuDictDao.save(baseMenuDictDO);
        }
    }
    //菜单词典查询
    public List<Map<String,Object>> findMenuDictByKey(String parentId, String name,Integer status){
        String sql = "select t.id as \"id\", " +
                " t.parent_id as \"parentId\", " +
                " t.name as \"name\", " +
                " t.menu_sort as \"menuSort\", " +
                " t.icon as \"icon\", " +
                " t.url as \"url\", " +
                " t.is_show as \"isShow\", " +
                " t.status as \"status\", " +
                " t.create_time as \"createTime\", " +
                " t.remark as \"remark\", " +
                " t.function_type as \"functionType\", " +
                " t.menu_level as \"menuLevel\", " +
                " t.menu_location as \"menuLocation\", " +
                " t.bg_img as \"bgImg\", " +
                " t.menu_title as \"menuTitle\", " +
                " t.describtion as \"describtion\", " +
                " t.menu_img as \"menuImg\"  " +
                "from base_menu_dict t  where 1=1 and t.is_del ='1' ";
        String sqlParent ="select t.id as \"id\", " +
                " t.parent_id as \"parentId\", " +
                " t.name as \"name\", " +
                " t.menu_sort as \"menuSort\", " +
                " t.icon as \"icon\", " +
                " t.url as \"url\", " +
                " t.is_show as \"isShow\", " +
                " t.status as \"status\", " +
                " t.create_time as \"createTime\", " +
                " t.remark as \"remark\", " +
                " t.function_type as \"functionType\", " +
                " t.menu_level as \"menuLevel\", " +
                " t.bg_img as \"bgImg\", " +
                " t.menu_title as \"menuTitle\", " +
                " t.describtion as \"describtion\", " +
                " t.menu_location as \"menuLocation\", " +
                " t.menu_img as \"menuImg\"  " +
                "from base_menu_dict t   where 1=1 and t.is_del ='1' ";
        if (StringUtils.isNoneBlank(parentId)){
            sql+=" and t.parent_id = '"+parentId+"'";
            sqlParent+=" and t.id = '"+parentId+"'";
        }else {
            sqlParent+=" and t.parent_id = '0'";
        }
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.name like '%"+name+"%'";
            sqlParent+=" and t.name like '%"+name+"%'";
        }
        if (status!=null){
            sql += " and t.status = '"+status+"' ";
            sqlParent+=" and t.status = '"+status+"' " ;
        }

        sql+=" order by t.parent_id asc ,t.menu_sort asc ";
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql);
        List<Map<String,Object>> listParent = hibenateUtils.createSQLQuery(sqlParent);
        List<WlyyHospitalSysDictDO> menuLocation = wlyyHospitalSysDictDao.findByDictName("menuLocation");
        List<WlyyHospitalSysDictDO> menuFunction = wlyyHospitalSysDictDao.findByDictName("menuFunction");
        List<WlyyHospitalSysDictDO> effect = wlyyHospitalSysDictDao.findByDictName("isEffect");
        for (Map<String,Object> map:listParent){
            List<Map<String,Object>> childList = new ArrayList<>();
            Integer articleParentNum= knowledgeArticleDictDao.getCountByCategoryFirst(map.get("id").toString());
            map.put("articleNum",articleParentNum);
            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:effect){
                if (map.get("status").toString().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                    map.put("statusName",wlyyHospitalSysDictDO.getDictValue());
                    map.put("isShow",map.get("status").toString().equalsIgnoreCase("1")?"是":"否");
                }
            }
            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:menuLocation){
                if (map.get("menuLocation").toString().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                    map.put("menuLocationName",wlyyHospitalSysDictDO.getDictValue());
                }
            }
            for (Map<String,Object> mapchild:list){
                if (mapchild.get("parentId").toString().equalsIgnoreCase(map.get("id").toString())){
                    List<String> list1 = new ArrayList<>();
                    list1.add(mapchild.get("id").toString());
                   Integer articleChildNum= knowledgeArticleDictDao.getCountByCategorySecond(list1);
                    mapchild.put("articleNum",articleChildNum);
                    for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:effect){
                        if (mapchild.get("status").toString().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                            mapchild.put("statusName",wlyyHospitalSysDictDO.getDictValue());
                            mapchild.put("isShow",mapchild.get("status").toString().equalsIgnoreCase("1")?"是":"否");
                        }
                    }
                    for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:menuFunction){
                        if (mapchild.get("functionType").toString().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                            mapchild.put("functionTypeName",wlyyHospitalSysDictDO.getDictValue());
                        }
                    }
                    childList.add(mapchild);
                }
            }
            map.put("childList",childList);
        }

        return listParent;
    }
    //查询单挑菜单字典
    public BaseMenuDictDO findOneMenuDict(String id){
        BaseMenuDictDO baseMenuDictDO= baseMenuDictDao.findOne(id);
        if (baseMenuDictDO!=null){
            WlyyHospitalSysDictDO effect = wlyyHospitalSysDictDao.findOneByDictNameAndDictCode("isEffect",baseMenuDictDO.getStatus().toString());
            baseMenuDictDO.setShowName(baseMenuDictDO.getStatus().equals("1")?"是":"否");
            baseMenuDictDO.setStatusName(effect.getDictValue());
            if (!"0".equalsIgnoreCase(baseMenuDictDO.getParentId())){
                BaseMenuDictDO parentDo= baseMenuDictDao.findOne(baseMenuDictDO.getParentId());
                if (parentDo!=null){
                    baseMenuDictDO.setParentName(parentDo.getName());
                }
                WlyyHospitalSysDictDO menuFunction = wlyyHospitalSysDictDao.findOneByDictNameAndDictCode("menuFunction",baseMenuDictDO.getFunctionType());
                baseMenuDictDO.setFunctionName(menuFunction.getDictValue());
            }else {
                WlyyHospitalSysDictDO menuLocation = wlyyHospitalSysDictDao.findOneByDictNameAndDictCode("menuLocation",baseMenuDictDO.getMenuLocation());
                if (menuLocation.getDictValue() == null){
                    baseMenuDictDO.setLocaTionName("无");
                }else {
                    baseMenuDictDO.setLocaTionName(menuLocation.getDictValue());
                }
            }
        }
        return baseMenuDictDO;
    }
    /**
     * 二级菜单下移
     * @param
     * @return
     * @throws Exception
     */
    public BaseMenuDictDO downMenu(String id) throws Exception{
        BaseMenuDictDO baseMenuDictDO= baseMenuDictDao.findOne(id);
        int maxSort = baseMenuDictDao.getMaxSort(baseMenuDictDO.getParentId());
        int sort = 0;
        if(null!=baseMenuDictDO){
            sort = baseMenuDictDO.getMenuSort();
        }
        if (maxSort==sort){
            throw new Exception("不能下移");
        }
        String sql = "select t.id AS \"id\" from base_menu_dict t where t.is_del='1' and t.menu_sort > "+sort+" and " +
                " t.parent_id='"+baseMenuDictDO.getParentId()+"' order by t.menu_sort asc ";
        System.out.println(sql);
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseMenuDictDO upPrevious = new BaseMenuDictDO();
        if (list.size()>0){
            String prviousBannerId = list.get(0).get("id").toString();
            upPrevious =baseMenuDictDao.findOne(prviousBannerId);
        }

        //交换sort值
        baseMenuDictDO.setMenuSort(upPrevious.getMenuSort());
        upPrevious.setMenuSort(sort);
        baseMenuDictDao.save(baseMenuDictDO);
        baseMenuDictDao.save(upPrevious);
        return baseMenuDictDO;
    }
    /**
     * 二级菜单上移
     * * @param
     * @return
     * @throws Exception
     */
    public BaseMenuDictDO upMenu(String id) throws Exception{
        //当前
        BaseMenuDictDO baseMenuDictDO = baseMenuDictDao.findOne(id);
        int minSort = baseMenuDictDao.getMinSort(baseMenuDictDO.getParentId());
        int sort = 0;
        if(null!=baseMenuDictDO){
            sort = baseMenuDictDO.getMenuSort();
        }
        if (minSort==sort){
            throw new Exception("不能上移");
        }
        String sql = " select id AS \"id\" from base_menu_dict where  is_del='1' and menu_sort < "+sort+" and" +
                " parent_id='"+baseMenuDictDO.getParentId()+"'order by menu_sort desc ";
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseMenuDictDO downPrevious = new BaseMenuDictDO();
        if (list.size()>0){
            String prviousBannerId = list.get(0).get("id").toString();
            downPrevious =baseMenuDictDao.findOne(prviousBannerId);
        }
        //获取的下一条b
        //交换sort值
        baseMenuDictDO.setMenuSort(downPrevious.getMenuSort());
        downPrevious.setMenuSort(sort);
        baseMenuDictDao.save(baseMenuDictDO);
        baseMenuDictDao.save(downPrevious);
        return baseMenuDictDO;
    }
    /**
     * 设置生效和失效
     * @param id
     * @param status
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(String id,Integer status){
        baseMenuDictDao.updateStatus(id,status);
    }
    //新增修改菜单
    public BaseMenuDictDO createOrUpdateMenu(String json) throws  Exception{
        BaseMenuDictDO menuDO = objectMapper.readValue(json,BaseMenuDictDO.class);
        if (StringUtils.isNoneBlank(menuDO.getId())){
            BaseMenuDictDO menuOld = baseMenuDictDao.findOne(menuDO.getId());
            if (menuOld!=null){
                menuOld.setParentId(menuDO.getParentId());
                menuOld.setName(menuDO.getName());
                menuOld.setMenuLocation(menuDO.getMenuLocation());
                menuOld.setFunctionType(menuDO.getFunctionType());
                //menuOld.setIsShow(menuDO.getIsShow());
                menuOld.setMenuImg(menuDO.getMenuImg());
                menuOld.setDescribtion(menuDO.getDescribtion());
                menuOld.setMenuTitle(menuDO.getMenuTitle());
                menuOld.setBgImg(menuDO.getBgImg());
                menuOld.setUpdateTime(new Date());
                menuOld.setIsDel("1");
                menuDO = baseMenuDictDao.save(menuOld);
            }
        }else {
            if (!"0".equalsIgnoreCase(menuDO.getParentId())){
                Integer maxSort=baseMenuDictDao.getMaxSort(menuDO.getParentId());
                menuDO.setMenuSort((maxSort==null?0:maxSort)+1);
                menuDO.setIsDel("1");
                menuDO.setIsShow(menuDO.getStatus()==1?"1":"0");
            }else{
                menuDO.setIsDel("1");
                menuDO.setIsShow(menuDO.getStatus()==1?"1":"0");
                menuDO.setMenuSort(1);
            }
            menuDO = baseMenuDictDao.save(menuDO);
        }
        return menuDO;
    }
    //新增修改友情链接
    public BaseLinkDictDO createOrUpdateLink(String json) throws  Exception{
        BaseLinkDictDO linkDictDO = objectMapper.readValue(json,BaseLinkDictDO.class);
        Integer maxSort=baseLinkDictDao.getMaxSort();
        if (StringUtils.isNoneBlank(linkDictDO.getId())){
            BaseLinkDictDO baseLinkDictDO=baseLinkDictDao.findOne(linkDictDO.getId());
            if (baseLinkDictDO!=null){
                baseLinkDictDO.setName(linkDictDO.getName());
                baseLinkDictDO.setLinkUrl(linkDictDO.getLinkUrl());
                //baseLinkDictDO.setLinkSort((maxSort==null?0:maxSort)+1);
                baseLinkDictDO.setIsShow(linkDictDO.getIsShow());
                baseLinkDictDO.setStatus(linkDictDO.getStatus());
                baseLinkDictDO.setIsDel("1");
                linkDictDO =  baseLinkDictDao.save(baseLinkDictDO);
            }else {
                throw new  NoSuchElementException();
            }
        }else {
            linkDictDO.setLinkSort((maxSort==null?0:maxSort)+1);
            linkDictDO.setIsDel("1");
            linkDictDO =  baseLinkDictDao.save(linkDictDO);
        }
        return linkDictDO;
    }
    public List<Map<String,Object>> findLinkDict(String name,String status){
        String sql="select t.id as \"id\"," +
                " t.name as \"name\"," +
                " t.link_url as \"linkUrl\"," +
                " t.is_show as \"isShow\"," +
                " t.status as \"status\"," +
                " t.link_sort as \"linkSort\"," +
                " t.create_time as \"createTime\"," +
                " t.is_del as \"isDel\" " +
                " from base_link_dict t where 1=1 and t.is_del='1'";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.name like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(status)){
            sql+=" and t.status = '"+status+"'";
        }
        sql+=" order by t.link_sort asc";
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql);
        List<WlyyHospitalSysDictDO> effect = wlyyHospitalSysDictDao.findByDictName("isEffect");
        for (Map<String,Object> map:list){
            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:effect){
                if (map.get("status").toString().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                    map.put("statusName",wlyyHospitalSysDictDO.getDictValue());
                    map.put("isShow",map.get("status").toString().equalsIgnoreCase("1")?"是":"否");
                }
            }

        }
        return list;

    }
    public BaseLinkDictDO findOneLinkDict(String id){
        BaseLinkDictDO baseLinkDictDO= baseLinkDictDao.findOne(id);
        if (baseLinkDictDO!=null){
            WlyyHospitalSysDictDO effect = wlyyHospitalSysDictDao.findOneByDictNameAndDictCode("isEffect",baseLinkDictDO.getStatus());
            baseLinkDictDO.setShowName(baseLinkDictDO.getStatus().equalsIgnoreCase("1")?"是":"否");
            baseLinkDictDO.setStatusName(effect.getDictValue());
        }
        return baseLinkDictDO;
    }

    /**
     * 友情链接下移
     * @param
     * @return
     * @throws Exception
     */
    public BaseLinkDictDO downlink(String id) throws Exception{
        BaseLinkDictDO baseLinkDictDO= baseLinkDictDao.findOne(id);
        int maxSort = baseLinkDictDao.getMaxSort();
        int sort = 0;
        if(null!=baseLinkDictDO){
            sort = baseLinkDictDO.getLinkSort();
        }
        if (maxSort==sort){
            throw new Exception("不能下移");
        }
        String sql = "select t.id AS \"id\" from base_link_dict t where t.is_del='1' and  t.link_sort > "+sort+" order by t.link_sort asc ";
        System.out.println(sql);
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseLinkDictDO upPrevious = new BaseLinkDictDO();
        if (list.size()>0){
            String prviousBannerId = list.get(0).get("id").toString();
            upPrevious =baseLinkDictDao.findOne(prviousBannerId);
        }

        //交换sort值
        baseLinkDictDO.setLinkSort(upPrevious.getLinkSort());
        upPrevious.setLinkSort(sort);
        baseLinkDictDao.save(baseLinkDictDO);
        baseLinkDictDao.save(upPrevious);
        return baseLinkDictDO;
    }
    /**
     * 友情链接上移
     * * @param
     * @return
     * @throws Exception
     */
    public BaseLinkDictDO upLink(String id) throws Exception{
        //当前
        BaseLinkDictDO baseLinkDictDO = baseLinkDictDao.findOne(id);
        int minSort = baseLinkDictDao.getMinSort();
        int sort = 0;
        if(null!=baseLinkDictDO){
            sort = baseLinkDictDO.getLinkSort();
        }
        if (minSort==sort){
            throw new Exception("不能上移");
        }
        String sql = " select id AS \"id\" from base_link_dict where is_del='1' and link_sort < "+sort+" order by link_sort desc ";
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseLinkDictDO downPrevious = new BaseLinkDictDO();
        if (list.size()>0){
            String prviousBannerId = list.get(0).get("id").toString();
            downPrevious =baseLinkDictDao.findOne(prviousBannerId);
        }
        //获取的下一条b
        //交换sort值
        baseLinkDictDO.setLinkSort(downPrevious.getLinkSort());
        downPrevious.setLinkSort(sort);
        baseLinkDictDao.save(baseLinkDictDO);
        baseLinkDictDao.save(downPrevious);
        return baseLinkDictDO;
    }

    /**
     * 设置生效和失效
     * @param id
     * @param status
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateLinkStatus(String id,String status){
        baseLinkDictDao.updateStatus(id,status);
    }
    /**
     * 设置生效和失效
     * @param id
     * @param status
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateLinkShow(String id,String status){
        baseLinkDictDao.updateShow(id,status);
    }
    /**
     * 友情链接删除
     * @param id
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateLinkDel(String id){
        baseLinkDictDao.updateDel(id,"0");
    }
    /**
     * 移除首页菜单
     * @param menuId
     *
     */
    public BaseMenuShowDO removeMenu(String menuId){
        BaseMenuShowDO baseMenuShowDO = baseMenuShowDao.findOne(menuId);
        baseMenuShowDO.setIsDel("0");
        baseMenuShowDao.save(baseMenuShowDO);
        return baseMenuShowDO;
    }


    public  List<Map<String,Object>> findMenuShow(){
        String modelSql = "select t.id as \"id\"," +
                " t.model_id as \"modelId\", " +
                " t.model_name as \"modelName\" from base_menu_show t where t.is_del ='1'" +
                " group by  t.model_id, t.model_name" ;
        List<Map<String,Object>> listModel=hibenateUtils.createSQLQuery(modelSql);
        String sql =" select t.id as \"id\", " +
                " t.model_id as \"modelId\", " +
                " t.model_name as \"modelName\", " +
                " t.menu_id as \"menuId\", " +
                " m.name as \"menuName\", " +
                " t.is_del as \"isDel\"," +
                " m.status as \"status\"," +
                " t.create_time as \"createTime\"," +
                " t.menu_sort as \"menuSort\"," +
                " t.style_code as \"styleCode\"," +
                " m.bg_img as \"bgImg\"," +
                " m.menu_title as \"menuTitle\"," +
                " m.describtion as \"describtion\"," +
                " t.style_name as \"styleName\", " +
                " m.parent_id as \"parentId\" " +
                " from base_menu_show t left join " +
                " base_menu_dict m on t.menu_id= m.id" +
                " where t.is_del ='1' order by t.model_id asc ,t.menu_sort asc ";
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql);
        for (Map<String,Object> map:list){
            if (map.get("parentId")!=null){
                String parentId = map.get("parentId").toString();
                BaseMenuDictDO parentDo= baseMenuDictDao.findOne(parentId);
                if (parentDo!=null){
                    map.put("parentName",parentDo.getName());
                }
            }
        }

        String sqlLink =" select t.id as \"id\", " +
                " t.model_id as \"modelId\", " +
                " t.model_name as \"modelName\", " +
                " t.menu_id as \"menuId\", " +
                " m.name as \"menuName\", " +
                " t.is_del as \"isDel\"," +
                " t.create_time as \"createTime\"," +
                " t.menu_sort as \"menuSort\"," +
                " m.link_url as \"linkUrl\"," +
                " t.style_code as \"styleCode\"," +
                " m.status as \"status\"," +
                " t.style_name as \"styleName\" " +
                " from base_menu_show t left join " +
                " base_link_dict m on t.menu_id= m.id" +
                " where t.is_del ='1' and t.model_id='03' order by t.menu_sort asc ";
        List<Map<String,Object>> listLink=hibenateUtils.createSQLQuery(sqlLink);
        for (Map<String,Object> map:listModel){
            List<Map<String,Object>> child = new ArrayList<>();
            if("03".equalsIgnoreCase(map.get("modelId").toString())){
                map.put("childList",listLink);
                map.put("childListNum",listLink==null?0:listLink.size());
            }else {
                for (Map<String,Object> childMap:list){
                    if (childMap.get("modelId").toString().equalsIgnoreCase(map.get("modelId").toString())){
                        child.add(childMap);
                    }
                }
                map.put("childList",child);
            }
        }

        return listModel;
    }


    /**
     * 首页菜单展示下移
     * @param
     * @return
     * @throws Exception
     */
    public BaseMenuShowDO downMenuShow(String id) throws Exception{
        BaseMenuShowDO baseMenuShowDO= baseMenuShowDao.findOne(id);
        int maxSort = baseMenuShowDao.getMaxSort(baseMenuShowDO.getModelId());
        int sort = 0;
        if(null!=baseMenuShowDO){
            sort = baseMenuShowDO.getMenuSort();
        }
        if (maxSort==sort){
            throw new Exception("不能下移");
        }
        String sql = "select t.id AS \"id\" from base_menu_show t where t.is_del='1' and t.menu_sort > "+sort+" " +
                " and t.model_id='"+baseMenuShowDO.getModelId()+"'order by t.menu_sort asc ";
        System.out.println(sql);
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseMenuShowDO upPrevious = new BaseMenuShowDO();
        if (list.size()>0){
            String prviousBannerId = list.get(0).get("id").toString();
            upPrevious =baseMenuShowDao.findOne(prviousBannerId);
        }

        //交换sort值
        baseMenuShowDO.setMenuSort(upPrevious.getMenuSort());
        upPrevious.setMenuSort(sort);
        baseMenuShowDao.save(baseMenuShowDO);
        baseMenuShowDao.save(upPrevious);
        return baseMenuShowDO;
    }
    /**
     * 首页菜单展示上移
     * * @param
     * @return
     * @throws Exception
     */
    public BaseMenuShowDO upMenuShow(String id) throws Exception{
        //当前
        BaseMenuShowDO baseMenuShowDO = baseMenuShowDao.findOne(id);
        int minSort = baseMenuShowDao.getMinSort(baseMenuShowDO.getModelId());
        int sort = 0;
        if(null!=baseMenuShowDO){
            sort = baseMenuShowDO.getMenuSort();
        }
        if (minSort==sort){
            throw new Exception("不能上移");
        }
        String sql = " select id AS \"id\" from base_menu_show where is_del='1' and menu_sort < "+sort+" " +
                " and model_id ='"+baseMenuShowDO.getModelId()+"'order by menu_sort desc ";
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,1,1);
        BaseMenuShowDO downPrevious = new BaseMenuShowDO();
        if (list.size()>0){
            String prviousBannerId = list.get(0).get("id").toString();
            downPrevious =baseMenuShowDao.findOne(prviousBannerId);
        }
        //获取的下一条b
        //交换sort值
        baseMenuShowDO.setMenuSort(downPrevious.getMenuSort());
        downPrevious.setMenuSort(sort);
        baseMenuShowDao.save(baseMenuShowDO);
        baseMenuShowDao.save(downPrevious);
        return baseMenuShowDO;
    }
    public void saveMenuShow(String json) throws  Exception{
        List<BaseMenuShowDO> menuShowDO = JSON.parseArray(json,BaseMenuShowDO.class);
        List<WlyyHospitalSysDictDO> list = wlyyHospitalSysDictDao.findByDictName("menuStyle");
        int i =1;
        for (BaseMenuShowDO baseMenuShowDO:menuShowDO){
            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:list){
                if (StringUtils.isNoneBlank(baseMenuShowDO.getStyleCode())){
                    if (baseMenuShowDO.getStyleCode().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                        baseMenuShowDO.setStyleName(wlyyHospitalSysDictDO.getDictValue());
                    }
                }
            }
            String modelSql = "select t.id as \"id\"," +
                    " t.model_id as \"modelId\", max(t.menu_sort) as \"maxSort\"" +
                    " from base_menu_show t where t.is_del ='1'" +"and t.model_id='"+baseMenuShowDO.getModelId()+"'"+
                    " group by  t.model_id " ;
            List<Map<String,Object>> listModel=hibenateUtils.createSQLQuery(modelSql);
            if (listModel!=null&&listModel.size()>0){
                baseMenuShowDO.setMenuSort((listModel.get(0).get("maxSort")==null?0:Integer.valueOf(listModel.get(0).get("maxSort").toString()))+i);
            }else {
                baseMenuShowDO.setMenuSort(1);
            }
            baseMenuShowDO.setIsDel("1");
            baseMenuShowDO.setCreateTime(new Date());
            i++;
        }
        baseMenuShowDao.save(menuShowDO);
    }
    public List<Map<String,Object>> findByParentId(String parentId,Integer status){
        String sql = "select t.id as \"id\", " +
                " t.parent_id as \"parentId\", " +
                " t.name as \"name\", " +
                " t.menu_sort as \"menuSort\", " +
                " t.icon as \"icon\", " +
                " t.url as \"url\", " +
                " t.is_show as \"isShow\", " +
                " t.status as \"status\", " +
                " t.create_time as \"createTime\", " +
                " t.remark as \"remark\", " +
                " t.function_type as \"functionType\", " +
                " t.menu_level as \"menuLevel\", " +
                " t.menu_location as \"menuLocation\", " +
                " t.bg_img as \"bgImg\", " +
                " t.menu_title as \"menuTitle\", " +
                " t.describtion as \"describtion\", " +
                " t.menu_img as \"menuImg\"  " +
                "from base_menu_dict t  where 1=1 and t.is_del ='1' ";
        if (StringUtils.isNoneBlank(parentId)){
            sql+=" and t.parent_id='"+parentId+"' ";
        }
        if (status!=null){
            sql+=" and t.status="+status+"";
        }
        sql+=" order by t.menu_sort asc";
        System.out.print("sql"+sql);
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql);
        return list;
    }

    public MixEnvelop findArticleByMenuId(String menuId,Integer page,Integer pageSize){
        Pageable pageRequest = new PageRequest(page-1,pageSize);
        List<String> menuIds = new ArrayList<>();
        if (StringUtils.isNoneBlank(menuId)){
            String str[]= menuId.split(",");
            for (int i=0;i<str.length;i++){
                menuIds.add(str[i]);
            }
        }

        System.out.print("menuId"+menuId);
        List<KnowledgeArticleDictDO> list=knowledgeArticleDictDao.findByCategorySecondAndPage(menuIds,pageRequest);
        Integer count = knowledgeArticleDictDao.getCountByCategorySecond(menuIds);
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setTotalCount(count);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        return mixEnvelop;

    }


    //菜单词典查询
    public List<Map<String,Object>> findMenuDictParent(String parentId, String name){
        String sqlParent ="select t.id as \"id\", " +
                " t.parent_id as \"parentId\", " +
                " t.name as \"name\", " +
                " t.menu_sort as \"menuSort\", " +
                " t.icon as \"icon\", " +
                " t.url as \"url\", " +
                " t.is_show as \"isShow\", " +
                " t.status as \"status\", " +
                " t.create_time as \"createTime\", " +
                " t.remark as \"remark\", " +
                " t.function_type as \"functionType\", " +
                " t.menu_level as \"menuLevel\", " +
                " t.menu_location as \"menuLocation\", " +
                " t.bg_img as \"bgImg\", " +
                " t.menu_title as \"menuTitle\", " +
                " t.describtion as \"describtion\", " +
                " t.menu_img as \"menuImg\"  " +
                "from base_menu_dict t   where 1=1 and t.is_del ='1' and t.status =1";
        if (StringUtils.isNoneBlank(parentId)){
            sqlParent+=" and t.id = '"+parentId+"'";
        }else {
            sqlParent+=" and t.parent_id = '0'";
        }
        if (StringUtils.isNoneBlank(name)){
            sqlParent+=" and t.name like '%"+name+"%'";
        }
        List<Map<String,Object>> listParent = hibenateUtils.createSQLQuery(sqlParent);
        List<WlyyHospitalSysDictDO> menuLocation = wlyyHospitalSysDictDao.findByDictName("menuLocation");
        List<WlyyHospitalSysDictDO> effect = wlyyHospitalSysDictDao.findByDictName("isEffect");
        for (Map<String,Object> map:listParent){
            Integer articleParentNum= knowledgeArticleDictDao.getCountByCategoryFirst(map.get("id").toString());
            map.put("articleNum",articleParentNum);
            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:effect){
                if (map.get("status").toString().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                    map.put("statusName",wlyyHospitalSysDictDO.getDictValue());
                    map.put("isShow",map.get("status").toString().equalsIgnoreCase("1")?"是":"否");
                }
            }
            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:menuLocation){
                if (map.get("menuLocation").toString().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                    map.put("menuLocationName",wlyyHospitalSysDictDO.getDictValue());
                }
            }

        }

        return listParent;
    }
}
