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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
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.GiftRelation;
import com.ssyf.module.robot.domain.query.GiftRelationQuery;
import com.ssyf.module.robot.domain.vo.GiftRelationVo;
import com.ssyf.module.robot.service.GiftRelationService;
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.GiftRelation模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="基础-礼物关联")
@RestController
@RequestMapping("/auth/GiftRelation")
public class GiftRelationAuthController {

    @Autowired
    private GiftRelationService baseService;

    @Log(title = "礼物关联-新增数据")
    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增数据")
    @PostMapping(value = "/addOne")
    public Result<GiftRelationVo> addOne(@Validated({ValidA.class}) @RequestBody GiftRelationQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR);
        }
        GiftRelation giftRelation = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(giftRelation , GiftRelationVo.class));
    }
    
    @Log(title = "礼物关联-批量新增数据")
    @RequestSynchronization
    @ApiOperation(value = "批量新增数据", notes = "批量新增数据")
    @PostMapping(value = "/addBatch")
    public Result<List<GiftRelationVo>> addBatch(@Validated({ValidA.class}) @RequestBody GiftRelationQuery  query) {
        if(CollectionUtil.isEmpty(query.getQueries())){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "批量新增数据不能为空");
        }
        List<GiftRelation> giftRelations = this.baseService.addBatch(query.getQueries());
        List<GiftRelationVo> result = EntityUtil.copyProperties(giftRelations, GiftRelationVo.class);
        return Result.success(result);
    }

    @Log(title = "礼物关联-更新数据")
    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "uid更新")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result updateByUid(@PathVariable String uid , @Validated @RequestBody GiftRelationQuery 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 GiftRelationQuery query){
        this.baseService.removeBatchByUids(query.getUids());
        return Result.success();
    }

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

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

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

}