package com.th.demo.service.impl.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.th.demo.mapping.system.SysMenuMapper;
import com.th.demo.model.system.SysMenu;
import com.th.demo.model.system.TreeNode;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysMenuService;
import com.th.demo.tools.JSONTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("SysMenuService")
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    SysMenuMapper sysMenuMapper;
    Gson gson = new Gson();
    String result = Type.FAIL;
    int num = 0;

    @Override
    public List<SysMenu> queryByUserId(String userId) {


        List<SysMenu> list = new ArrayList<SysMenu>();

        if (userId.equals("1")) {

            list = sysMenuMapper.selectAll();
        } else// pid
        {
            list = sysMenuMapper.selectByUserId(userId);
        }
        return list;
    }

    @Override
    public List<SysMenu> queryPidMenu(String menuPid) {
        List<SysMenu> list = sysMenuMapper.selectByMenuPid(menuPid);
        return list;

    }

    @Override
    public List<SysMenu> queryRoleMenu(String roleId) {
        List<SysMenu> list = sysMenuMapper.selectByRoleId(roleId);
        return list;
    }


    @Override
    public List<SysMenu> queryMainMenu() {
        List<SysMenu> list = sysMenuMapper.selectMainMenu();
        return list;
    }

    @Override
    public String query(int page, int rows, String pid, String userId) {
        PageHelper.startPage(page, rows);
        List<SysMenu> list = new ArrayList<SysMenu>();
        list = sysMenuMapper.selectByMenuPid(pid);
        PageInfo<SysMenu> pageInfo = new PageInfo<>(list);
        result = gson.toJson(pageInfo);
        return result;
    }

    @Override
    public String queryAppMenu(String userId) {
        try {
            List<SysMenu> list = sysMenuMapper.selectAppMenuByUserId(userId);
            result = gson.toJson(list);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return result;
        }
    }


    @Override
    public String save(String id, String menuPid, String menuCode, String menuName, String menuOpenBs, String menuBsIcon, String menuShow, String userId) {


        SysMenu sysMenu;
        if (id == null || id.equals("")) {


            sysMenu = new SysMenu();
            sysMenu.setId(id);
            sysMenu.setMenuPid(menuPid);
            sysMenu.setMenuCode(menuCode);
            sysMenu.setMenuName(menuName);
            sysMenu.setMenuBsIcon(menuBsIcon);
            sysMenu.setMenuOpenBs(menuOpenBs);
            sysMenu.setMenuShow(Short.parseShort(menuShow));

            if(menuPid.equals("0")){
                sysMenu.setAttr1("0");
            }else {
                sysMenu.setAttr1(menuCode.substring(0,3));
            }
            sysMenu.setCancelFlag("0");
            sysMenu.setAddId(userId);
            sysMenu.setAddTime(new Date());
          num =   sysMenuMapper.insert(sysMenu);
        } else {
            sysMenu = sysMenuMapper.selectByPrimaryKey(id);
            sysMenu.setId(id);
            sysMenu.setMenuPid(menuPid);
            sysMenu.setMenuCode(menuCode);
            sysMenu.setMenuName(menuName);
            sysMenu.setMenuBsIcon(menuBsIcon);
            sysMenu.setMenuOpenBs(menuOpenBs);
            sysMenu.setMenuShow(Short.parseShort(menuShow));
            if(menuPid.equals("0")){
                sysMenu.setAttr1("0");
            }else {
                sysMenu.setAttr1(menuCode.substring(0,3));
            }
            sysMenu.setModifyId(userId);
            sysMenu.setModifyTime(new Date());
            num = sysMenuMapper.updateByPrimaryKey(sysMenu);
        }


        result = Tools.moreThanZero(num);
        return result;
    }

    @Override
    public String remove(String id, String cancelId) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        sysMenu.setCancelId(cancelId);
        sysMenu.setCancelTime(new Date());
        sysMenu.setCancelFlag("9");
        num = sysMenuMapper.deleteByUpdate9(sysMenu);
        result = Tools.moreThanZero(num);
        return result;
    }

    @Override
    public String queryByMenuCode(String code) {
        SysMenu sysMenu = sysMenuMapper.selectByMenuCode(code);
        return JSONTools.toString(sysMenu);
    }

    @Override
    public String  queryByUserIdForRoleMenu(String userId) {

        List<SysMenu> list = new ArrayList<SysMenu>();

        if (userId.equals("1")) {
            list = sysMenuMapper.selectAll();
        } else// pid
        {
            list = sysMenuMapper.selectByUserIdForRoleMenu(userId);
        }
        List<TreeNode> tree = Tools.getTree(list,"0");
        return JSONTools.toString(tree);
    }

}
