package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.MallPermission;
import com.iformall.service.MallPermissionService;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenkx
 * @date 2018-01-05.
 */
@RestController
@RequestMapping("permission")
public class MallPermissionController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    MallPermissionService mallPermissionService;


    @GetMapping("alllist")
    @SystemControllerLog(description = "用户管理-权限")
    public ResultData alllist(String ava) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::alllist");
        MallPermission record = new MallPermission();
        record.setAvailable(ava);
        PageInfo<MallPermission> permissions = mallPermissionService.listAsPage(record, 1, 10000);
        return new ResultData(Result.SUCCESS, "查询成功", permissions.getList());
    }

    @PostMapping("add")
    @SystemControllerLog(description = "用户管理-权限添加")
    public ResultData createPermission(MallPermission mallPermission) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::createPermission");
        Assert.notNull(mallPermission.getName(), "用户名不能为空");
        mallPermissionService.saveOrUpdate(mallPermission);
        return new ResultData(mallPermission.getId());
    }

    @PostMapping("update")
    @SystemControllerLog(description = "用户管理-权限更新")
    public ResultData updatePermission(MallPermission sysPermission) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::updatePermission");
        mallPermissionService.saveOrUpdate(sysPermission);
        return new ResultData();
    }

    @GetMapping("/del")
    @SystemControllerLog(description = "用户管理-权限删除")
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::delete");
        mallPermissionService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @GetMapping("/findById")
    @SystemControllerLog(description = "用户管理-权限查找")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::findById");
        mallPermissionService.getById(id);
        return new ResultData(Result.SUCCESS, "成功", null);
    }


}
