package com.fkzy.warn.controller;

import com.alibaba.fastjson2.JSONObject;
import com.fkzy.warn.model.Permission;
import com.fkzy.warn.service.PermissionService;
import com.fkzy.warn.common.constants.ResponseMsgConstants;
import com.fkzy.warn.common.request.RequestParameter;
import com.fkzy.warn.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zhangjing
 * @date 2024/05/21 11:22
 * @description
 */
@Api(tags = "数据权限管理")
@RestController
@RequestMapping("permission/")
@Slf4j
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @RequestMapping(value = "save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "新增")
    public ApiResponse save(@ApiParam("{\n" +
        "\"name\":\"名称\",\n" +
        "\"organization\":\"组织\",\n" +
        "}") @RequestBody Permission entity, @RequestHeader String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return permissionService.saveModel(entity,ticket);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "修改")
    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse edit(@ApiParam() @RequestBody Permission entity,@RequestHeader String ticket) {
        log.info("修改==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse response = new ApiResponse();
        try {
            return permissionService.updateModel(entity,ticket);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }
    /**
     * 通过ids删除
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "删除")
    @RequestMapping(value = "remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse remove(@ApiParam("{\"ids\":[\"1\",\"2\"]}") @RequestBody JSONObject jsonObject) {
        ApiResponse apiResponse = new ApiResponse();
            try {
                return  permissionService.deleteModel(jsonObject.getJSONArray("ids").toJavaList(String.class));
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }
    @RequestMapping(value = "queryPage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询")
    public ApiResponse queryPage(@ApiParam() @RequestBody RequestParameter parameter) {
        ApiResponse apiResponse = new ApiResponse();
            try {
                apiResponse.setData(permissionService.queryPage(parameter));
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }

}
