package com.hhh.cloud.framework.example.client.controller;

import com.alibaba.fastjson.JSONObject;
import com.hhh.cloud.framework.example.client.entity.SysClient;
import com.hhh.cloud.framework.example.client.service.ISysClientService;
import com.hhh.cloud.framework.megrez.utils.UserContextHolder;
import com.hhh.cloud.framework.megrez.utils.model.UserJwt;
import com.hhh.cloud.framework.util.enumeration.CodeEnum;
import com.hhh.cloud.framework.util.exception.BusinessException;
import com.hhh.cloud.framework.util.function.JSONFuncs;
import com.hhh.cloud.framework.util.result.Result;
import com.hhh.cloud.framework.util.result.TableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@RestController
@RequestMapping("example")
public class ExampleController {

    @Autowired
    private ISysClientService iSysClientService;

    /**
     * 查询列表（service层使用QueryWrapper或LambdaQueryWrapper）
     * @param json 查询条件
     * @return TableResult
     */
    @PostMapping("listClient")
    public TableResult listClient(@RequestBody JSONObject json) {
//        UserJwt user = UserContextHolder.get().getUserJwt();
        SysClient search = json.toJavaObject(SysClient.class);
        int page = json.containsKey("page") ? json.getInteger("page") : 1;
        int limit = json.containsKey("limit") ? json.getInteger("limit") : 15;
        return iSysClientService.listClient(search, page, limit);
    }

    /**
     * 查询列表（自定义sql）
     * @param json 查询条件
     * @return TableResult
     */
    @PostMapping("listClient2")
    public TableResult listClient2(@RequestBody JSONObject json) {
        UserJwt user = UserContextHolder.get().getUserJwt();
        SysClient search = json.toJavaObject(SysClient.class);
        int page = json.containsKey("page") ? json.getInteger("page") : 1;
        int limit = json.containsKey("limit") ? json.getInteger("limit") : 15;
        return iSysClientService.listClient2(search, page, limit);
    }

    /**
     * 新增功能API，包含赋值，逻辑操作交由service层，如果引用了megrez-spring-boot-starter，出错会有AOP自动处理
     * @param json 数据
     * @return Result
     * @throws Exception e
     */
    @PostMapping("addClient")
    public Result<SysClient> addClient(@RequestBody JSONObject json) throws Exception {
        JSONFuncs.checkEmpty(json, "clientIp", "客户端ip不能为空", "clientName", "客户端名称不能为空");
        UserJwt user = UserContextHolder.get().getUserJwt();
        SysClient client = json.toJavaObject(SysClient.class);
        client.setClientKey("dasdasdasds");
        client.setSecret("seeee");
        client.setCreatedDate(LocalDate.now());
        iSysClientService.save(client);
        return new Result<>(CodeEnum.SUCCESS.get(), client, "保存成功");
    }

    /**
     * 删除功能API，逻辑操作交由service层，如果引用了megrez-spring-boot-starter，出错会有AOP自动处理
     * @param json json
     * @return Result
     * @throws Exception e
     */
    @PostMapping("deleteClient")
    public Result<Boolean> deleteClient(@RequestBody JSONObject json) throws Exception {
        JSONFuncs.checkEmpty(json, "clientKey", "删除条件不足");
        return new Result<>(CodeEnum.SUCCESS.get(), iSysClientService.removeById(json.getString("clientKey")), "删除操作");
    }

    /**
     * 业务错误抛错样例
     * @param json json
     * @return Result
     */
    @PostMapping("errorTest")
    public Result<Boolean> errorTest(@RequestBody JSONObject json) {
        throw new BusinessException("测试错误");
    }
}
