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.PolicyManagement;
import com.fkzy.warn.service.PolicyManagementService;
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("policyManagement/")
@Slf4j
public class PolicyManagementController {
    @Resource
    private PolicyManagementService policyManagementService ;

    @RequestMapping(value = "save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建用户")
    public ApiResponse save(@ApiParam("{\n" +
        "\"version\":\"政策版本\",\n" +
        "\"content\":\"政策内容（富文本）\",\n" +
        "\"contentType\":\"政策类型 0用户协议管理1隐私政策管理2关于我们管理\",\n" +
        "}") @RequestBody PolicyManagement entity, @RequestHeader String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return policyManagementService.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 PolicyManagement entity,@RequestHeader String ticket) {
        log.info("修改==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse response = new ApiResponse();
        try {
            return policyManagementService.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  policyManagementService.deleteModel(jsonObject.getJSONArray("ids").toJavaList(String.class));
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }

    /**
     * 通过ids删除
     *
     * @param
     * @return
     */
    @RequestMapping(value = "getPolicy", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "获取当前政策")
    public ApiResponse getPolicy(String contentType) {
        ApiResponse apiResponse = new ApiResponse();
        try {
            return policyManagementService.getPolicy(contentType);
        } catch (Exception e) {
            log.error("查询错误,errMsg==={}", e.getMessage());
            e.printStackTrace();
            apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }

        return apiResponse;
    }
    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @RequestMapping(value = "queryPage", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询")
    public ApiResponse queryPage(@ApiParam("{\n" +
        "\"contentType\":\"政策类型 0用户协议管理1隐私政策管理2关于我们管理\",\n" +
        "}") @RequestBody RequestParameter parameter) {
        ApiResponse apiResponse = new ApiResponse();
            try {
                apiResponse.setData(policyManagementService.queryPage(parameter));
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }

}
