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

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ssyf.common.core.util.EntityUtil;
import com.ssyf.common.web.aop.RequestSynchronization;
import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.common.web.valid.ValidA;
import com.ssyf.module.robot.domain.entity.RRobotGroup;
import com.ssyf.module.robot.domain.query.RRobotGroupQuery;
import com.ssyf.module.robot.domain.vo.RRobotGroupVo;
import com.ssyf.module.robot.service.RRobotGroupService;
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;


/**
* com.ssyf.auth.entity.RRobotGroup模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="基础-机器人分组")
@RestController
@RequestMapping("/auth/RobotGroup")
public class RRobotGroupAuthController {

    @Autowired
    private RRobotGroupService baseService;

    @Log(title = "机器人分组-新增数据")
    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping(value = "/addOne")
    public Result<RRobotGroupVo> addOne(@Validated({ValidA.class}) @RequestBody RRobotGroupQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        RRobotGroup menuRRobotGroup = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(menuRRobotGroup , RRobotGroupVo.class));
    }

    @Log(title = "机器人分组-更新数据")
    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "uid更新")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result updateByUid(@PathVariable String uid , @Validated @RequestBody RRobotGroupQuery query){
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"更新数据不能为空");
        }
        this.baseService.updateByUid(uid ,query);
        return Result.success();
    }

    @Log(title = "机器人分组-删除数据")
    @RequestSynchronization
    @ApiOperation(value = "删除数据", notes = "uid删除")
    @PostMapping(value = "/removeByUid/{uid}")
    public Result removeByUid(@PathVariable String uid){
        this.baseService.removeByUid(uid);
        return Result.success();
    }

    @Log(title = "机器人分组-批量删除")
    @RequestSynchronization
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @PostMapping(value = "/removeBatchByUid")
    public Result removeBatchByUid(@Validated @RequestBody RRobotGroupQuery query){
        this.baseService.removeBatchByUids(query.getUids());
        return Result.success();
    }


    @ApiOperation(value = "/查询指定数据" ,notes = "uid查询")
    @PostMapping(value = "/getOneByUid/{uid}")
    public Result<RRobotGroupVo> getOneByUid(@PathVariable String uid) {
        return Result.success(BeanUtil.toBean(this.baseService.getOneByUid(uid) ,RRobotGroupVo.class));
    }

    @ApiOperation(value = "/查询分页数据", notes = "分页查询")
    @PostMapping(value = "/queryPage")
    public Result<PageInfo<RRobotGroupVo>> queryPage(@Validated @RequestBody RRobotGroupQuery query) {
        PageInfo<RRobotGroup> temp = this.baseService.getPage(query);
        return Result.success(PageEnhanceHelper.switchPageInfo(temp ,RRobotGroupVo.class));
    }

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryList")
    public Result<List<RRobotGroupVo>> queryList(@Validated @RequestBody RRobotGroupQuery query) {
        List<RRobotGroup> menuRRobotGroupList = this.baseService.getList(query);
        List<RRobotGroupVo> t = EntityUtil.copyProperties(menuRRobotGroupList, RRobotGroupVo.class);
        return Result.success(t);
    }
    @ApiOperation(value = "/查询总数")
    @PostMapping(value = "/count")
    public Result<Integer> count(@Validated @RequestBody RRobotGroupQuery query) {
        Integer count = this.baseService.countBy(query);
        return Result.success(count);
    }
}