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

import cn.hutool.core.bean.BeanUtil;
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.CustomData;
import com.ssyf.module.custom.domain.query.CustomDataQuery;
import com.ssyf.module.custom.domain.vo.CustomDataVo;
import com.ssyf.module.custom.service.CustomDataService;
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.Map;


/**
* com.ssyf.auth.entity.CustomData模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="开发-自定义表接口")
@RestController
@RequestMapping("/auth/customData")
public class CustomDataAuthController {

    @Autowired
    private CustomDataService baseService;

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

    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增单条数据")
    @PostMapping(value = "/addOneMap/{tableUid}")
    public Result addOneMap(@PathVariable String tableUid,@Validated @RequestBody Map<String ,Object> query) {
        if(BeanUtil.isEmpty(query)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"新增数据不能为空");
        }
        Map<String ,Object> map = this.baseService.addOne(tableUid ,query);
        return Result.success(map);
    }

    @RequestSynchronization
    @ApiOperation(value = "更新数据", notes = "uid更新")
    @PostMapping(value = "/updateByUid/{uid}")
    public Result updateByUid(@PathVariable String uid , @Validated @RequestBody CustomDataQuery 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 = "/updateByUidMap/{uid}")
    public Result updateByUidMap(@PathVariable String uid , @Validated @RequestBody Map<String ,Object> 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();
    }
}
