package com.cm.common.controller.apis.templaterecord;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.component.SecurityComponent;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.CurrentUserIdInfoDTO;
import com.cm.common.pojo.dtos.templaterecord.TemplateRecordDTO;
import com.cm.common.pojo.vos.templaterecord.TemplateRecordVO;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.service.templaterecord.ITemplateRecordService;
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: TemplateRecordController
 * @Description: 模板构建记录
 * @Author: WenG
 * @Date: 2020-05-07 16:50
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "模板构建记录接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/templaterecord")
public class TemplateRecordController extends AbstractController {

    @Autowired
    private ITemplateRecordService templateRecordService;
    @Autowired
    private SecurityComponent securityComponent;

    @ApiOperation(value = "新增模板构建记录", notes = "新增模板构建记录接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savetemplaterecord")
    @CheckRequestBodyAnnotation
    public SuccessResult saveTemplateRecord(@RequestBody TemplateRecordVO templateRecordVO) throws Exception {
        return templateRecordService.saveTemplateRecord(templateRecordVO);
    }

    @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("removetemplaterecord/{ids}")
    public SuccessResult removeTemplateRecord(@PathVariable("ids") String ids) throws RemoveException {
        return templateRecordService.removeTemplateRecord(ids);
    }

    @ApiOperation(value = "修改模板构建记录", notes = "修改模板构建记录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateRecordId", value = "模板构建记录ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatetemplaterecord/{templateRecordId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateTemplateRecord(@PathVariable("templateRecordId") String templateRecordId, @RequestBody TemplateRecordVO templateRecordVO) throws Exception {
        return templateRecordService.updateTemplateRecord(templateRecordId, templateRecordVO);
    }

    @ApiOperation(value = "模板构建记录详情(通过ID)", notes = "模板构建记录详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateRecordId", value = "模板构建记录ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("gettemplaterecordbyid/{templateRecordId}")
    public TemplateRecordDTO getTemplateRecordById(@PathVariable("templateRecordId") String templateRecordId) throws SearchException {
        return templateRecordService.getTemplateRecordById(templateRecordId);
    }

    @ApiOperation(value = "模板构建记录列表", notes = "模板构建记录列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listtemplaterecord")
    public List<TemplateRecordDTO> listTemplateRecord() throws SearchException {
        Map<String, Object> params = requestParams();
        return templateRecordService.listTemplateRecord(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("listpagetemplaterecord")
    public SuccessResultList<List<TemplateRecordDTO>> listPageTemplateRecord(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return templateRecordService.listPageTemplateRecord(page);
    }

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

}