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.NewsManage;
import com.fkzy.warn.service.NewsManageService;
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("newsManage/")
@Slf4j
public class NewsManageController {
    @Resource
    private NewsManageService newsManageService ;

    @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" +
        "\"authorizationId\":\"内容简述\"\n" +
        "\"authorizationId\":\"缩略图 URL\"\n" +
        "\"authorizationId\":\"内容详情\"\n" +
        "}") @RequestBody NewsManage entity, @RequestHeader String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return newsManageService.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 NewsManage entity,@RequestHeader String ticket) {
        log.info("修改==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse response = new ApiResponse();
        try {
            return newsManageService.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  newsManageService.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(newsManageService.queryPage(parameter));
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        return apiResponse;
    }

    /**
     * 发布
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "发布")
    @RequestMapping(value = "publish", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse publish(@ApiParam("{\n" +
        "\"id\":\"id\",\n" +
        "\"status\":\"0待发布1发布\",\n" +
        "}") @RequestBody NewsManage entity,@RequestHeader String ticket) {
        log.info("修改==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse response = new ApiResponse();
        try {
            return newsManageService.publish(entity,ticket);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }
}
