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

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ssyf.common.core.domain.query.PageQuery;
import com.ssyf.common.core.util.EntityUtil;
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 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;
import java.util.Map;


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

    @Autowired
    private CustomDataService baseService;

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

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

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

    @ApiOperation(value = "/查询分页数据", notes = "分页查询")
    @PostMapping(value = "/queryPageMap/{tableUid}")
    public Result<PageInfo<Map<String ,Object>>> queryPage(@PathVariable String tableUid , @Validated @RequestBody Map<String ,Object> query) {
        PageQuery pageQuery = new PageQuery();
        if(query.containsKey("pageNum")){
            pageQuery.setPageNum((int)query.get("pageNum"));
        }
        if(query.containsKey("pageSize")){
            pageQuery.setPageSize((int)query.get("pageSize"));
        }
        PageInfo<Map<String ,Object>> temp = this.baseService.getPage(tableUid,query ,pageQuery);
        return Result.success(temp);
    }

    @ApiOperation(value = "/查询所有数据")
    @PostMapping(value = "/queryAllMap/{tableUid}")
    public Result<List<Map<String ,Object>>> queryAll(@PathVariable String tableUid ,@Validated @RequestBody Map<String ,Object> query) {
        List<Map<String ,Object>> list = this.baseService.getAll(tableUid ,query);
        return Result.success(list);
    }

}
