package com.boman.web.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.boman.domain.dto.AjaxResult;
import com.boman.domain.dto.FormDataDto;
import com.boman.web.core.service.common.ICommonService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shiqian
 * @date 2021年04月15日 16:25
 **/
@RestController
@RequestMapping("common/")
public class CommonController {

    @Resource
    private ICommonService commonService;

    /**
     * 功能描述: 根据表名和id查找
     *
     * @param tableName tableName
     * @param id        id
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/tableName/{tableName}/id/{id}")
    public AjaxResult getById(@PathVariable("tableName") String tableName, @PathVariable("id") Long id) {
        return AjaxResult.success(commonService.getById(tableName, id));
    }

    /**
     * 功能描述: 根据表名和自定义字段查找
     *
     * @param dto dto
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping
    public AjaxResult getByMap(@RequestBody FormDataDto dto) {
        return AjaxResult.success(commonService.getByMap(dto.getTable(), dto.getFixedData()));
    }

    /**
     * 功能描述: 根据表名和自定义字段查找
     *
     * @param dto dto
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping("/one/map")
    public JSONObject getOneByMap(@RequestBody FormDataDto dto) {
        return commonService.getOneByMap(dto.getTable(), dto.getFixedData());
    }

    /**
     * 功能描述: getMaxId
     *
     * @param tableName tableName
     * @param pkName        pkName
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("tableName/{tableName}/pkName/{pkName}")
    public Long getMaxId(@PathVariable("tableName") String tableName, @PathVariable("pkName") String pkName) {
        return commonService.getMaxId(tableName, pkName);
    }

    /**
     * 功能描述: count
     *
     * @param dto dto
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping("count")
    public int count(@RequestBody FormDataDto dto) {
        return commonService.count(dto);
    }

    /**
     * 功能描述: 获取最新的一条数据 create_time desc
     *
     * @param tableName tableName
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("newest/{tableName}")
    public JSONObject getNewest(@PathVariable("tableName") String tableName) {
        return commonService.getNewest(tableName);
    }

}
