package cn.com.tenlion.freemarker.controller.apis.templateconfig;

import cn.com.tenlion.freemarker.pojo.dtos.templateconfig.TemplateConfigDTO;
import cn.com.tenlion.freemarker.pojo.vos.templateconfig.TemplateConfigContentVO;
import cn.com.tenlion.freemarker.pojo.vos.templateconfig.TemplateConfigVO;
import cn.com.tenlion.freemarker.service.templateconfig.ITemplateConfigService;
import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.common.component.SecurityComponent;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.CurrentUserIdInfoDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.AbstractController;

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

/**
 * @ClassName: TemplateConfigController
 * @Description: 模板表
 * @Author: WenG
 * @Date: 2020-05-07 16:44
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "模板表接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/templateconfig")
public class TemplateConfigController extends DefaultBaseController {

    @Autowired
    private ITemplateConfigService templateConfigService;
    @Autowired
    private SecurityComponent securityComponent;

    @ApiOperation(value = "新增模板表", notes = "新增模板表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savetemplateconfig")
    @CheckRequestBodyAnnotation
    public SuccessResult saveTemplateConfig(@RequestBody TemplateConfigVO templateConfigVO) throws Exception {
        return templateConfigService.saveTemplateConfig(templateConfigVO);
    }

    @ApiOperation(value = "删除模板表(id列表)", notes = "删除模板表(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removetemplateconfig/{ids}")
    public SuccessResult removeTemplateConfig(@PathVariable("ids") String ids) throws RemoveException {
        return templateConfigService.removeTemplateConfig(ids);
    }


    @ApiOperation(value = "修改模板内容", notes = "修改模板内容接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateConfigId", value = "模板表ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatetemplateconfigcontent/{templateConfigId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateTemplateConfigContent(@PathVariable("templateConfigId") String templateConfigId, @RequestBody TemplateConfigContentVO templateConfigContentVO) throws Exception {
        return templateConfigService.updateTemplateConfigContent(templateConfigId, templateConfigContentVO.getTemplateContent());
    }

    @ApiOperation(value = "修改模板表", notes = "修改模板表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateConfigId", value = "模板表ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatetemplateconfig/{templateConfigId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateTemplateConfig(@PathVariable("templateConfigId") String templateConfigId, @RequestBody TemplateConfigVO templateConfigVO) throws Exception {
        return templateConfigService.updateTemplateConfig(templateConfigId, templateConfigVO);
    }

    @ApiOperation(value = "模板表详情(通过ID)", notes = "模板表详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateConfigId", value = "模板表ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("gettemplateconfigbyid/{templateConfigId}")
    public TemplateConfigDTO getTemplateConfigById(@PathVariable("templateConfigId") String templateConfigId) throws SearchException {
        return templateConfigService.getTemplateConfigById(templateConfigId);
    }

    @ApiOperation(value = "模板表列表", notes = "模板表列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listtemplateconfig")
    public List<TemplateConfigDTO> listTemplateConfig() throws SearchException {
        Map<String, Object> params = requestParams();
        return templateConfigService.listTemplateConfig(params);
    }

    @ApiOperation(value = "模板表分页列表", notes = "模板表分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "form", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "form", dataType = "Integer", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "form", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagetemplateconfig")
    public SuccessResultList<List<TemplateConfigDTO>> listPageTemplateConfig(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return templateConfigService.listPageTemplateConfig(page);
    }

    @ApiOperation(value = "当前用户id信息", notes = "当前用户id信息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getcurrentuseridinfo")
    public CurrentUserIdInfoDTO getCurrentUserIdInfo() {
        return securityComponent.getCurrentUserIdInfo();
    }

}