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.model.User;
import com.fkzy.warn.service.UserService;
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.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("account/")
@Slf4j
public class AccountController {
    @Resource
    private UserService userService ;

    @RequestMapping(value = "sendCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "发送验证码")
    public ApiResponse sendCode(@ApiParam("{\n" +
        "\"phoneNumber\":\"电话号码\",\n" +
        "}") @RequestBody JSONObject jsonObject) {
        ApiResponse response = new ApiResponse();
        try {
            return userService.sendCode(jsonObject);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }
    @RequestMapping(value = "login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "登录")
    public ApiResponse login(@ApiParam("{\n" +
        "\"loginType\":\"登录类型：1.账户密码登录，2：手机短信验证码登录\",\n" +
        "\"phoneNumber\":\"电话号码\",\n" +
        "\"password\":\"密码密文\",\n" +
        "\"code\":\"随机验证码\"\n" +
        "}") @RequestBody JSONObject jsonObject) {
        ApiResponse response = new ApiResponse();
        try {
            return userService.login(jsonObject);
        } catch (Exception e) {
            response.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return response;
    }

    @RequestMapping(value = "createUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建用户")
    public ApiResponse createUser(@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.createUser(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.queryPage(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 = "loginOut", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    @ApiOperation(value = "退出系统")
//    public ApiResponse logOut(@ApiParam("{\n"
//        + "  \"ticket\":\"ticket\",\n"
//        + "}") @RequestBody JSONObject object) {
//        ApiResponse response = new ApiResponse();
//        String ticket = object.getString("ticket");
//        if (StringUtils.isBlank(ticket)) {
//            response.recordError("ticket不能为空");
//            return response;
//        }
//        //调用全局登出
//        response = accountService.logout(ticket);
//        return response;
//    }
}
