package com.ssyf.module.robot.controller.auth;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.module.robot.domain.entity.ChatConfig;
import com.ssyf.module.robot.domain.query.ChatConfigQuery;
import com.ssyf.module.robot.domain.vo.ChatConfigVo;
import com.ssyf.module.robot.service.ChatConfigService;
import com.ssyf.plugs.logger.Log;
import com.ssyf.plugs.mysql.helper.PageEnhanceHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天配置开放接口
 *
 * @author: auth
 * @date: 2026-01-31
 */
@Api(tags="基础-聊天配置")
@RestController
@RequestMapping("/auth/ChatConfig")
public class ChatConfigAuthController {

    @Autowired
    private ChatConfigService baseService;

    /**
     * 新增聊天配置
     */
    @Log(title = "聊天配置-新增数据")
    @ApiOperation("新增聊天配置")
    @PostMapping("/addOne")
    public Result<ChatConfigVo> addOne(@Validated @RequestBody ChatConfigQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        ChatConfig entity = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(entity, ChatConfigVo.class));
    }

    /**
     * 根据UUID删除聊天配置
     */
    @Log(title = "聊天配置-删除数据")
    @ApiOperation("根据UUID删除聊天配置")
    @PostMapping("/removeByUid/{uuid}")
    public Result removeByUid(@PathVariable String uuid) {
        this.baseService.removeByUid(uuid);
        return Result.success();
    }

    /**
     * 批量删除聊天配置
     */
    @Log(title = "聊天配置-批量删除")
    @ApiOperation("批量删除聊天配置")
    @PostMapping("/removeBatchByUids")
    public Result removeBatchByUids(@RequestBody List<String> uuids) {
        this.baseService.removeBatchByUids(uuids);
        return Result.success();
    }

    /**
     * 根据UUID更新聊天配置
     */
    @Log(title = "聊天配置-更新数据")
    @ApiOperation("根据UUID更新聊天配置")
    @PostMapping("/updateByUid")
    public Result updateByUid(@Validated @RequestBody ChatConfigQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "更新数据不能为空");
        }
        this.baseService.updateByUid(query.getUuid(), query);
        return Result.success();
    }

    /**
     * 根据UUID获取聊天配置
     */
    @ApiOperation("根据UUID获取聊天配置")
    @GetMapping("/getOneByUid/{uuid}")
    public Result<ChatConfigVo> getOneByUid(@PathVariable String uuid) {
        return Result.success(this.baseService.getOneByUid(uuid));
    }

    /**
     * 分页查询聊天配置
     */
    @ApiOperation("分页查询聊天配置")
    @PostMapping("/queryPage")
    public Result<IPage<ChatConfigVo>> queryPage(@Validated @RequestBody ChatConfigQuery query) {
        PageInfo<ChatConfig> temp = this.baseService.getPage(query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp, ChatConfigVo.class));
    }

    /**
     * 查询聊天配置列表
     */
    @ApiOperation("查询聊天配置列表")
    @PostMapping("/queryList")
    public Result<List<ChatConfigVo>> queryList(@Validated @RequestBody ChatConfigQuery query) {
        PageInfo<ChatConfig> temp = this.baseService.getPage(query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp, ChatConfigVo.class));
    }

    /**
     * 统计聊天配置数量
     */
    @ApiOperation("统计聊天配置数量")
    @PostMapping("/count")
    public Result<Long> count(@Validated @RequestBody ChatConfigQuery query) {
        Integer result = baseService.countBy(query);
        return Result.success(result);
    }
}