package com.fkzy.warn.controller;

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.User;
import com.fkzy.warn.service.UserService;
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;
import java.util.Objects;

/**
 * @author zhangjing
 * @date 2024/05/21 11:22
 * @description
 */
@Api(tags = "试用授权")
@RestController
@RequestMapping("trial/")
@Slf4j
public class TrialController {
    @Resource
    private UserService userService ;




    @RequestMapping(value = "createTrial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建试用账号")
    public ApiResponse createTrial(@ApiParam("{\n" +
        "\"companyName\":\"企业名称\",\n" +
        "\"creditCode\":\"统一社会信用代码\",\n" +
        "\"phoneNumber\":\"手机号\",\n" +
        "\"email\":\"邮箱\"\n" +
        "\"province\":\"所在省\"\n" +
        "\"city\":\"所在市\"\n" +
        "\"district\":\"所在区\"\n" +
        "\"detailedAddress\":\"详细地址\"\n" +
        "\"businessPerson\":\"商务负责人\"\n" +
        "\"remarks\":\"备注\"\n" +
        "}") @RequestBody User entity, String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return userService.createTrial(entity,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) {
        log.info("分页查询 ==== 参数{" + parameter.toString() + "}");
        ApiResponse apiResponse = new ApiResponse();
        if (!Objects.isNull(parameter) && !Objects.isNull(parameter.getParameter())) {
            try {
                apiResponse.setData(userService.queryPageTrial(parameter));
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                log.error("查询错误,errMsg==={}", e.getMessage());
                e.printStackTrace();
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        } else {
            apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return apiResponse;
    }
    @RequestMapping(value = "setTrial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "配置试用账号")
    public ApiResponse setTrial(@ApiParam("{\n" +
        "\"id\":\"id\",\n" +
        "\"trialStatus\":\"1试用中2冻结\",\n" +
        "\"trialDay\":\"试用天数\",\n" +
        "}") @RequestBody User entity, String ticket) {
        ApiResponse response = new ApiResponse();
        try {
            return userService.setTrial(entity,ticket);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }
}
