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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.mapping.system.SysRoleMenuMapper;
import com.th.demo.model.system.SysRoleMenu;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysRoleMenuService;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("SysRoleMenuService")
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    public SysRoleMenuMapper sysRoleMenuMapper;
    int num = 0;
    String result=Type.FAIL;
    Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();//gson json的三方框架 数据传递格式

    @Override
    public int add(SysRoleMenu roleMenu) {
        num = 0;
        num = sysRoleMenuMapper.insert(roleMenu);
        return num;
    }

    @Override
    public String queryByRoleId(String roleId,String userId,String belongId) {
        List<SysRoleMenu> list = sysRoleMenuMapper.selectByRoleId(roleId);
        String result = "";
        for (int i = 0; i < list.size(); i++) {
            if (!result.equals("")) {
                result += ",";
            }
            result += list.get(i).getMenuId();
        }
        return result;
    }
    @Override
    public String save(String roleId,
                       String checkedMenuIdLeaf,
                       String checkedMenuId,
                       String halfCheckMenuId,
                       String userId) {
        num = sysRoleMenuMapper.deleteByRoleId(roleId);
        if(!checkedMenuId.equals("")) {
            String[] strCheckedMenuId = checkedMenuId.split(",");
            for (int i = 0; i < strCheckedMenuId.length; i++) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setMenuId(strCheckedMenuId[i]);
                roleMenu.setRoleId(roleId);
                roleMenu.setAddId(userId);
                roleMenu.setAddTime(new Date());
                roleMenu.setCancelFlag("0");
                roleMenu.setAttr2("0");
                num += sysRoleMenuMapper.insert(roleMenu);
            }
        }
        if(!halfCheckMenuId.equals("")) {
            String[] strHalfCheckMenuId = halfCheckMenuId.split(",");
            for (int i = 0; i < strHalfCheckMenuId.length; i++) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setMenuId(strHalfCheckMenuId[i]);
                roleMenu.setRoleId(roleId);
                roleMenu.setAddId(userId);
                roleMenu.setAddTime(new Date());
                roleMenu.setCancelFlag("0");
                roleMenu.setAttr2("1");
                num += sysRoleMenuMapper.insert(roleMenu);
            }
        }
        if(!checkedMenuIdLeaf.equals("")) {
            String[] strCheckedMenuIdLeaf = checkedMenuIdLeaf.split(",");
            for (int i = 0; i < strCheckedMenuIdLeaf.length; i++) {
                SysRoleMenu roleMenu = sysRoleMenuMapper.selectByRoleIdMenuId(roleId, strCheckedMenuIdLeaf[i]);
                roleMenu.setAttr2("2");
                num += sysRoleMenuMapper.updateByPrimaryKey(roleMenu);
            }
        }



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

    @Override
    public String queryByRoleIdOnlyLeaf(String roleId) {
        List<SysRoleMenu> list = sysRoleMenuMapper.queryByRoleIdOnlyLeaf(roleId);
        String result = "";
        for (int i = 0; i < list.size(); i++) {
            if (!result.equals("")) {
                result += ",";
            }
            result += list.get(i).getMenuId();
        }
        return result;
    }
}
