package com.jkcredit.invoice.controller.param;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.annotation.annotationdes.AuthenticationInterceptor;
import com.jkcredit.invoice.model.entity.manager.Param;
import com.jkcredit.invoice.service.manager.ParamService;
import com.jkcredit.invoice.util.RespR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: sunzhaoning
 * @create: 2019-05-29 09:14
 * @version: V1.0
 **/
@Api(tags = "参数管理")
@RestController
@RequestMapping(value = {"/param"})
public class ParamController {


    @Autowired
    private ParamService paramService;


    /**
     * 分页查询参数
     *
     * @param page  参数集
     * @param param 查询参数列表
     * @return 用户集合
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询参数管理", notes = "分页查询参数管理")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR getParamPage(Page page, Param param) {
        return new RespR<>(paramService.getParamWithRolePage(page, param));
    }


    /**
     * 添加参数
     *
     * @param param 参数信息
     * @return success/false
     */
    @PostMapping
    @ApiOperation(value = "新增参数详细信息", notes = "新增参数详细信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR param(@RequestBody @Validated Param param) {
        Param param1 = paramService.getParamsByParamName(param.getParamName());
        if (param1 != null) {
            return new RespR<>(false, "参数名称已存在,请重新维护！");
        }
        return new RespR<>(paramService.saveParam(param));
    }


    /**
     * 更新参数信息
     *
     * @param param 参数信息
     * @return R
     */
    @PostMapping("/updateParam")
    @ApiOperation(value = "更新参数信息", notes = "更新参数信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR updateUser(@RequestBody @Validated Param param) {
        return new RespR<>(paramService.updateParam(param));
    }

}
