package com.cm.common.plugin.controller.app.apis.dynamic;

import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.plugin.service.dynamic.IDynamicDataService;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DynamicDataController
 * @Description: 动态数据
 * @Author: WangGeng
 * @Date: 2019/12/4 15:23
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_ROUTE_PREFIX + "动态数据接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/dynamicdata")
public class DynamicDataAppController extends AbstractController {

    @Autowired
    private IDynamicDataService dynamicDataService;

    @ApiOperation(value = "保存动态数据", notes = "保存动态数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savedynamicdata/{tableName}")
    public SuccessResult saveDynamicData(@RequestHeader("token") String token, @PathVariable("tableName") String tableName, @RequestBody Map<String, Object> params) {
        return dynamicDataService.saveDynamicData(token, tableName, params);
    }

    @ApiOperation(value = "删除动态数据", notes = "删除动态数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path"),
            @ApiImplicitParam(name = "ids", value = "数据ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removedynamicdata/{tableName}/{ids}")
    public SuccessResult removeDynamicData(@RequestHeader("token") String token, @PathVariable("tableName") String tableName, @PathVariable("ids") String ids) {
        return dynamicDataService.removeDynamicData(token, tableName, ids);
    }

    @ApiOperation(value = "修改动态数据", notes = "修改动态数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatedynamicdata/{tableName}/{id}")
    public SuccessResult updateDynamicData(@RequestHeader("token") String token, @PathVariable("tableName") String tableName, @PathVariable("id") String id, @RequestBody Map<String, Object> params) {
        return dynamicDataService.updateDynamicData(token, tableName, id, params);
    }

    @ApiOperation(value = " 修改动态数据（链接）", notes = " 修改动态数据（链接）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatedynamicjoindata/{tableName}/{joinKey}/{joinId}")
    public SuccessResult updateDynamicJoinData(@RequestHeader("token") String token, @PathVariable("tableName") String tableName, @PathVariable("joinKey") String joinKey, @PathVariable("joinId") String joinId, @RequestBody Map<String, Object> params) {
        return dynamicDataService.updateDynamicJoinData(token, tableName, joinKey, joinId, params);
    }

    @ApiOperation(value = "动态数据列表", notes = "动态数据列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listdynamicdata/{tableName}")
    public List<Map<String, Object>> listDynamicData(@PathVariable("tableName") String tableName) {
        Map<String, Object> params = getParams();
        return dynamicDataService.listDynamicData(tableName, params);
    }

    @ApiOperation(value = "分页动态数据列表", notes = "分页动态数据列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagedynamicdata/{tableName}")
    public SuccessResultList<List<Map<String, Object>>> listPageDynamicData(@PathVariable("tableName") String tableName, ListPage listPage) {
        Map<String, Object> params = getParams();
        listPage.setParams(params);
        return dynamicDataService.listPageDynamicData(tableName, listPage);
    }

    @ApiOperation(value = "动态数据详情", notes = "动态数据详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getdynamicdata/{tableName}/{id}")
    public Map<String, Object> getDynamicData(@PathVariable("tableName") String tableName, @PathVariable("id") String id) {
        return dynamicDataService.getDynamicData(tableName, id);
    }

    @ApiOperation(value = "动态数据（链接）详情", notes = "动态数据（链接）详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path"),
            @ApiImplicitParam(name = "joinKey", value = "链接键名", paramType = "path"),
            @ApiImplicitParam(name = "joinId", value = "链接键值", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getdynamicjoindata/{tableName}/{joinKey}/{joinId}")
    public Map<String, Object> getDynamicJoinData(@PathVariable("tableName") String tableName, @PathVariable("joinKey") String joinKey, @PathVariable("joinId") String joinId) {
        return dynamicDataService.getDynamicJoinData(tableName, joinKey, joinId);
    }

}
