package com.fkzy.warn.controller;

import com.alibaba.fastjson.JSONObject;
import com.fkzy.warn.service.AuthorizationService;
import com.fkzy.warn.common.constants.ResponseMsgConstants;
import com.fkzy.warn.common.request.RequestParameter;
import com.fkzy.warn.common.response.ApiResponse;
import com.fkzy.warn.model.Authorization;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhangjing
 * @date 2024/05/21 11:22
 * @description
 */
@Api(tags = "用户授权")
@RestController
@RequestMapping("authorization/")
@Slf4j
public class AuthorizationController {
    @Resource
    private AuthorizationService authorizationService ;


    @RequestMapping(value = "save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建授权信息")
    public ApiResponse save(@ApiParam("{\n" +
        "\"startDate\":\"授权开始日期\",\n" +
        "\"endDate\":\"授权截止日期\",\n" +
        "\"fileId\":\"授权书id\",\n" +
        "\"email\":\"邮箱\"\n" +
        "\"queryCount\":\"风险查询次数\"\n" +
        "}") @RequestBody Authorization entity, String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return authorizationService.saveModel(entity,ticket);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }
    @RequestMapping(value = "applyFor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "申请")
    public ApiResponse applyFor(@ApiParam("{\n" +
        "\"authorizationId\":\"授权信息id\",\n" +
        "\"id\":\"申请的id\",\n" +
        "}") @RequestBody JSONObject jsonObject, String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return authorizationService.applyFor(jsonObject,ticket);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }
    @RequestMapping(value = "approve", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "审批")
    public ApiResponse approve(@ApiParam("{\n" +
        "\"content\":\"审批内容\",\n" +
        "\"statusCode\":\"1通过2拒绝\",\n" +
        "}") @RequestBody JSONObject jsonObject, String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return authorizationService.approve(jsonObject,ticket);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }

    @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(authorizationService.queryPage(parameter));
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }

}
