package com.ssyf.module.custom.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.module.custom.domain.entity.CustomField;
import com.ssyf.module.custom.domain.query.CustomFieldQuery;
import com.ssyf.module.custom.domain.vo.CustomFieldVo;
import com.ssyf.module.custom.service.CustomFieldService;
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.CustomField模型开放接口
*
* @author: auth
* @date: 2022-12-12
*/
@Api(tags="资源-自定义表字段接口")
@RestController
@RequestMapping("/auth/customField")
public class CustomFieldAuthController {

    @Autowired
    private CustomFieldService baseService;

    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增单条数据")
    @PostMapping(value = "/addOne/{tableUid}")
    public Result<CustomFieldVo> addOne(@PathVariable String tableUid ,@Validated @RequestBody CustomFieldQuery query) {
        CustomField CustomField = this.baseService.addOne(tableUid ,query);
        return Result.success(BeanUtil.copyProperties(CustomField , CustomFieldVo.class));
    }

    @RequestSynchronization
    @ApiOperation(value = "新增数据", notes = "新增多条数据")
    @PostMapping(value = "/addBatch/{tableUid}")
    public Result<List<CustomFieldVo>> addBatch(@PathVariable String tableUid ,@Validated @RequestBody List<CustomFieldQuery> queries) {
        if(CollectionUtil.isEmpty(queries)){
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode() ,"新增数据不能为空");
        }
        List<CustomField> customFields = this.baseService.addBatch(tableUid ,queries);
        return Result.success(customFields);
    }

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

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

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryAll/{tableUid}")
    public Result<List<CustomFieldVo>> queryAll(@PathVariable String tableUid ,@Validated @RequestBody CustomFieldQuery query) {
        List<CustomField> menuCustomFieldList = this.baseService.getAll(tableUid ,query);
        List<CustomFieldVo> t = EntityUtil.copyProperties(menuCustomFieldList, CustomFieldVo.class);
        return Result.success(t);
    }

}
