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

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysRoleService;
import com.th.demo.tools.JSONTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import com.th.demo.mapping.system.SysRoleMapper;
import com.th.demo.model.system.SysRole;


@Service("SysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;
    int num = 0;
    String result = Type.FAIL;

    @Override
    public  String query(int page, int rows,  String query,String userId, String belongId){
        PageHelper.startPage(page, rows);
        List<SysRole> list = sysRoleMapper.select( query, belongId);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }


    @Override
    public   String save(String json, String userId, String belongId){
        SysRole sysRole = JSON.parseObject(json, SysRole.class);
        if (sysRole.getId() == null || sysRole.getId().equals("")) {
            sysRole.setAddId(userId);
            sysRole.setAddTime(new Date());
            sysRole.setBelongId(belongId);
            num = sysRoleMapper.insert(sysRole);
            result = JSONTools.toString(num);
        } else {
            sysRole.setModifyId(userId);
            sysRole.setModifyTime(new Date());
            num = sysRoleMapper.updateByPrimaryKey(sysRole);
            result = JSONTools.toString(num);
        }
        return result;
    }

    @Override
    public String remove(String id, String userId) {
        num = sysRoleMapper.deleteByPrimaryKey(id);
        result = JSONTools.toString(num);
        return result;
    }

    @Override
    public String queryUserRole(String selectUserId, String userId, String belongId) {
        List<SysRole> list = sysRoleMapper.selectUserRoleByUserid(selectUserId,belongId);
        result = JSONTools.toString(list);
        return result;
    }

    @Override
    public String querySystemRole(String selectUserId, String userId, String belongId) {
        List<SysRole> list = sysRoleMapper.selectSystemRoleByUserid(selectUserId,belongId);
        result = JSONTools.toString(list);
        return result;
    }

}
