package cn.feast.coding.service.system;

import cn.feast.coding.mapper.system.PermissionMapper;
import cn.feast.coding.model.generator.Page;
import cn.feast.coding.model.system.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By FeastCoding.
 */
@Service
public class PermissionService {

    @Autowired
    private PermissionMapper mapper;

    public Integer save(Permission permission) {
        return mapper.save(permission);
    }

    public void update(Permission permission) {
        mapper.update(permission);
    }

    public void deleteById(Integer id) {
        mapper.deleteById(id);
    }

    public int queryByCount(Permission permission) {
        return mapper.queryByCount(permission);
    }

    public List<Permission> queryByList(Page page, Permission permission) {
        String pageQueryCondition = " limit " + page.getStart() + " , " + page.getMaxRows();
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("name", permission.getName());
        paramsMap.put("type", permission.getType());
        paramsMap.put("url", permission.getUrl());
        paramsMap.put("permission", permission.getPermission());
        paramsMap.put("parentid", permission.getParentid());
        paramsMap.put("pageQueryCondition", pageQueryCondition);
        return mapper.queryByList(paramsMap);
    }

    public List<Permission> queryByPermission(Permission permission) {
        return mapper.queryByPermission(permission);
    }

    public Permission queryById(Integer id) {
        return mapper.queryById(id);
    }

    public List<Permission> queryByMenuPermission() {
        Permission permission = new Permission();
        permission.setType(1);
        return mapper.queryByPermission(permission);
    }

    public List<Permission> queryBySubPermission() {
        Permission permission = new Permission();
        permission.setType(2);
        return mapper.queryByPermission(permission);
    }
}
