package com.ssyf.module.custom.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.module.custom.domain.entity.CustomTable;
import com.ssyf.module.custom.domain.query.CustomTableQuery;
import com.ssyf.module.custom.domain.vo.CustomTableVo;
import com.ssyf.module.custom.service.CustomTableService;
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.CustomTable模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="资源-自定义表接口")
@RestController
@RequestMapping("/auth/customTable")
public class CustomTableAuthController {

    @Autowired
    private CustomTableService baseService;

    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增单条数据")
    @PostMapping(value = "/addOne")
    public Result<CustomTableVo> addOne(@Validated @RequestBody CustomTableQuery query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"新增数据不能为空");
        }
        CustomTable customTable = this.baseService.addOne(query);
        return Result.success(BeanUtil.copyProperties(customTable , CustomTableVo.class));
    }

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

    @RequestSynchronization
    @ApiOperation(value = "删除数据", notes = "uid删除")
    @PostMapping(value = "/removeByUid/{uid}")
    public Result removeByUid(@PathVariable String uid){
        this.baseService.removeByUid(uid);
        return Result.success();
    }

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

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

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryAll")
    public Result<List<CustomTableVo>> queryAll(@Validated @RequestBody CustomTableQuery query) {
        List<CustomTable> menuCustomTableList = this.baseService.getAll(query);
        List<CustomTableVo> t = EntityUtil.copyProperties(menuCustomTableList, CustomTableVo.class);
        return Result.success(t);
    }

}
