package com.fkzy.warn.controller;

import com.alibaba.fastjson.JSONObject;
import com.fkzy.warn.model.Report;
import com.fkzy.warn.model.params.GenerateReportParams;
import com.fkzy.warn.service.ReportService;
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("report/")
@Slf4j
public class ReportController {
    @Resource
    private ReportService reportService ;

    @RequestMapping(value = "save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "新增")
    public ApiResponse save(@ApiParam("{\n" +
        "\"companyName\":\"企业名称\",\n" +
        "\"creditCode\":\"统一社会信用代码\",\n" +
        "\"legalPerson\":\"企业法人\",\n" +
        "\"address\":\"地址\"\n" +
        "\"authorizationId\":\"授权书id\"\n" +
        "}") @RequestBody Report entity, @RequestHeader String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return reportService.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 Report entity,@RequestHeader String ticket) {
        log.info("修改==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse response = new ApiResponse();
        try {
            return reportService.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  reportService.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(reportService.queryPage(parameter));
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }

    @PostMapping(value = "generateReport")
    @ApiOperation(value = "生成报告")
    public ApiResponse generateReport(@ApiParam("{\n" +
        "\"id\":\"报告id\",\n" +
        "}") @RequestBody GenerateReportParams params, @RequestHeader String ticket) {
        ApiResponse apiResponse = new ApiResponse();
        try {
            apiResponse.setData(reportService.generateReport(params,ticket));
            apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
        } catch (Exception e) {
            apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return apiResponse;
    }

}
