package com.fkzy.warn.controller;

import com.alibaba.fastjson.JSONObject;
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.Contract;
import com.fkzy.warn.service.ContractService;
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("contract/")
@Slf4j
public class ContractController {
    @Resource
    private ContractService contractService ;

    @RequestMapping(value = "save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "新增")
    public ApiResponse save(@ApiParam("{\n" +
        "\"userId\":\"关联用户\",\n" +
        "\"signingDate\":\"合同签订日期\",\n" +
        "\"contractAmount\":\"合同金额\",\n" +
        "}") @RequestBody Contract entity, @RequestHeader String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return contractService.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 Contract entity,@RequestHeader String ticket) {
        log.info("修改==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse response = new ApiResponse();
        try {
            return contractService.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  contractService.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(contractService.queryPage(parameter));
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }

}
