package com.njcn.serviceimpl.configuration;


import com.njcn.enums.DicDataTypeEnum;
import com.njcn.mapper.configuration.SysMapper;
import com.njcn.pojo.configuration.SysSet;
import com.njcn.pojo.data.FunctionPloyEntity;
import com.njcn.pojo.data.MenuEntity;
import com.njcn.pojo.data.TimerManage;
import com.njcn.service.configuration.SysService;
import com.njcn.shiro.token.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.List;

@Service
public class SysServiceImpl implements SysService {
    @Autowired
    private SysMapper sysm;

    @SuppressWarnings("unchecked")
    @Override
    public List<List> getSysList() {
        List<List> sysList = new ArrayList<List>();
        List<SysSet> sys = sysm.selectAll();
        if (sys.isEmpty()) {
            return sysList;
        } else {
            for (SysSet sy : sys) {
                List<String> temp = new ArrayList<String>();
                temp.add(sy.getSysSetIndex());
                temp.add(sy.getLimitDate().toString());
                temp.add(sy.getLimitTimes().toString());
                temp.add(sy.getLimitNumber().toString());
                temp.add(String.valueOf(sy.getLimitIdle()));
                temp.add(String.valueOf(sy.getLimitLogout()));
                temp.add(sy.getCasualUserTime().toString());
                temp.add(sy.getLogOutTime().toString());
                temp.add(sy.getLogSize().toString());
                temp.add(sy.getLockTime().toString());
                sysList.add(temp);
            }
        }
        return sysList;
    }

    @Override
    public int update(SysSet set) {
        return sysm.update(set);
    }

    @Override
    public List<MenuEntity> listMenu() {
        String systype = TokenManager.getSysType();
        return sysm.listMenu(systype);
    }

    @Override
    public List<MenuEntity> parentMenuList() {
        String systype = TokenManager.getSysType();
        return sysm.parentMenulist(systype);
    }

    @Override
    public List<MenuEntity> menuTypeList() {
        return sysm.menuTypeList(DicDataTypeEnum.menuTypeEnum.getName());
    }

    @Override
    public void insertData(MenuEntity menuEntity, String userIndex, String sysType) {
        sysm.insertData(menuEntity, userIndex, sysType);
    }

    @Override
    public void updateData(MenuEntity menuEntity, String userIndex) {
        sysm.updateData(menuEntity, userIndex);
    }

    @Override
    public void deleteMenu(String id, String userIndex, Date time) {
        sysm.deleteMenu(id, userIndex, time);
    }

    @Override
    public List<FunctionPloyEntity> menuPloyList(String sysType) {
        return sysm.menuPloyList(sysType);
    }

    @Override
    public List<MenuEntity> roleList() {
        List<MenuEntity> list = sysm.roleList();

        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            MenuEntity menuEntity = (MenuEntity) iterator.next();
            if (menuEntity.getName().equals("system") || menuEntity.getName().equals("audit") ||
                    menuEntity.getName().equals("check") || menuEntity.getName().equals("root") || menuEntity.getName().equals("casual")) {
                iterator.remove();
            }
        }

        return list;
    }

    @Override
    public void insertRoleMenuData(String uuid, String roleId, String name, List<String> list, String des, Date date, String userIndex, String sysType) {
        sysm.insertRoleMenuData(roleId, list, uuid);
        sysm.insertFunctionRoleData(uuid, roleId, name, des, date, userIndex, sysType);
    }

    @Override
    public void updateRoleMenuData(String id, String roleId, String name, List<String> list, String des, Date date, String userIndex) {
        //先删除pqs_rolefunction中已有的策略
        sysm.deleteRoleMenuData(roleId, id);
        sysm.insertRoleMenuData(roleId, list, id);
        sysm.updateRoleMenuData(id, name, des, date, userIndex);
    }

    @Override
    public void deleteFunctionRoleData(String id, Date date, String userIndex, String roleId) {
        sysm.deleteRoleMenuData(roleId, id);
        sysm.deleteFunctionRoleData(id, date, userIndex);
    }

    @Override
    public List<String> roleStateList(String sysType) {
        return sysm.roleStateList(sysType);
    }

    @Override
    public List<String> getRoleFunction(String roleId, String id) {
        return sysm.getRoleFunction(roleId, id);
    }

    @Override
    public List<MenuEntity> getListMenu(List<String> list, String sysType) {
        return sysm.getListMenu(list, sysType);
    }

    @Override
    public List<TimerManage> getTimerManage() {
        return sysm.getTimerManage();
    }

    @Override
    public Integer setTimerState(String guid, Integer state) {
        return sysm.setTimerState(guid, state);
    }

    @Override
    public String getTimerManageByGuid(String guid) {
        return sysm.getTimerManageByGuid(guid);
    }
}
