package cn.com.tenlion.configtable.controller.app.api.configtableoperation;

import cn.com.tenlion.configtable.pojo.dtos.configtableoperation.ConfigTableOperationDTO;
import cn.com.tenlion.configtable.pojo.dtos.configtableoperation.ConfigTableOperationGroupDTO;
import cn.com.tenlion.configtable.service.configtableoperation.IConfigTableOperationService;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
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;

/**
 * @ClassName: ConfigTableOperationAppController
 * @Description:
 * @Author: CodeFactory
 * @Date: 2021-06-26 13:49:46
 * @Version: 3.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "接口")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/configtableoperation")
public class ConfigTableOperationAppController extends DefaultBaseController {

    @Autowired
    private IConfigTableOperationService iConfigTableOperationService;

    @ApiOperation(value = "字段列表", notes = "字段列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listfield" + ISystemConstant.APP_RELEASE_SUFFIX + "/{configTableId}")
    public List<ConfigTableOperationDTO> listConfig(@RequestHeader("token") String token, @PathVariable("configTableId") String configTableId) {
        return iConfigTableOperationService.listFieldConfig(token, configTableId);
    }

    @ApiOperation(value = "动态表单列表数据(默认使用token查询,可传入userId查询)", notes = "动态表单列表数据列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list" + ISystemConstant.APP_RELEASE_SUFFIX + "/{configTableId}")
    public List<ConfigTableOperationGroupDTO> list(@RequestHeader("token") String token, @PathVariable("configTableId") String configTableId) {
        Map<String, Object> params = requestParams();
        return iConfigTableOperationService.listMyData(token, configTableId, params);
    }

    @ApiOperation(value = "详情(通过ID)", notes = "详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header"),
            @ApiImplicitParam(name = "configColumnId", value = "ID", paramType = "path"),
            @ApiImplicitParam(name = "uid", value = "ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get" + ISystemConstant.APP_RELEASE_SUFFIX + "/{configTableId}/{uid}")
    public ConfigTableOperationGroupDTO get(@RequestHeader("token") String token, @PathVariable("configTableId") String configTableId, @PathVariable("uid") String uid) {
        return iConfigTableOperationService.getData(token, configTableId, uid);
    }

    @ApiOperation(value = "新增或修改动态表单数据", notes = "新增或修改动态表单数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save" + ISystemConstant.APP_RELEASE_SUFFIX + "/{configTableId}")
    public SuccessResultData<String> saveData(@RequestHeader("token") String token, @PathVariable("configTableId") String configTableId, @RequestBody Map<String, Object> params) {
        String uid = iConfigTableOperationService.saveDataById(token, configTableId, params);
        return new SuccessResultData(uid);
    }

    @ApiOperation(value = "删除动态表单数据", notes = "删除动态表单数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("delete" + ISystemConstant.APP_RELEASE_SUFFIX + "/{configTableId}/{uid}")
    public SuccessResult saveData(@RequestHeader("token") String token, @PathVariable("configTableId") String configTableId, @PathVariable("uid") String uid) {
        iConfigTableOperationService.deleteDataById(token, configTableId, uid);
        return new SuccessResult();
    }

}