package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.entity.App;
import com.zcxk.model.*;
import com.zcxk.service.AppService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 应用(App)控制层
 *
 * @author makejava
 * @since 2020-10-20 16:56:39
 */
@RestController
@RequestMapping("/app")
@Api(tags = "")
public class AppController {
    /**
     * 服务对象
     */
    @Autowired
    private AppService appService;

    /**
     * 通过主键查询单条数据
     *
     * @param app 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ApiOperation(value = "查询设施配置列表")
    @PreAuthorize("hasAuthority('sys:menu:edit')")
    public AjaxMessage<App> selectOne(
            @ApiParam(value = "设置配置", required = true) @RequestBody App app) {
        App result = appService.selectById(app.getId());
        LoginUser currentUser = UserUtil.getCurrentUser();
        //数据权限如此获取
        List<ProgramItem> programItemList = currentUser.getProgramItemList();
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 新增一条数据
     *
     * @param app 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "查询设施配置列表")
    public AjaxMessage<Integer> insert(@ApiParam(value = "设置配置", required = true) @RequestBody App app) {
        int result = appService.insert(app);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改一条数据
     *
     * @param app 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "查询设施配置列表")
    public AjaxMessage<Integer> update(@ApiParam(value = "设置配置", required = true) @RequestBody App app) {
        int result = appService.update(app);
        return new AjaxMessage<>(ResultStatus.OK, result);

    }

    /**
     * 删除一条数据
     *
     * @param app 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ApiOperation(value = "查询设施配置列表")
    public AjaxMessage<Integer> delete(@ApiParam(value = "设置配置", required = true) @RequestBody App app) {
        int result = appService.deleteById(app.getId());
        return new AjaxMessage<>(ResultStatus.OK, result);
    }


    /**
     * 分页查询
     *
     * @param pageNum  偏移
     * @param pageSize 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询设施配置列表")
    public AjaxMessage<Pagination<App>> selectPage(Integer pageNum, Integer pageSize) {
        App app = new App();
        IPage<App> iPage = new Page<>(pageNum, pageSize);
        iPage = appService.selectPage(app, iPage);
        Pagination<App> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

}