package ink.wgink.module.form.controller.api.docno;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.form.enums.docno.DocNoTypeEnum;
import ink.wgink.module.form.pojo.bos.docno.DocNoBO;
import ink.wgink.module.form.pojo.dtos.docno.DocNoDTO;
import ink.wgink.module.form.pojo.vos.docno.DocNoVO;
import ink.wgink.module.form.service.docno.IDocNoService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName: DocNoController
 * @Description: 文号
 * @Author: CodeFactory
 * @Date: 2022-06-24 15:06:48
 * @Version: 3.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "文号接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/doc-no")
public class DocNoController extends DefaultBaseController {

    @Autowired
    private IDocNoService docNoService;

    @ApiOperation(value = "新增文号", notes = "新增文号接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody DocNoVO docNoVO) {
        checkField(docNoVO);
        docNoService.save(docNoVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除文号", notes = "删除文号接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult remove(@PathVariable("ids") String ids) {
        docNoService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "修改文号", notes = "修改文号接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docNoId", value = "文号ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{docNoId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("docNoId") String docNoId, @RequestBody DocNoVO docNoVO) {
        checkField(docNoVO);
        docNoService.update(docNoId, docNoVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "文号详情", notes = "文号详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docNoId", value = "文号ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{docNoId}")
    public DocNoDTO get(@PathVariable("docNoId") String docNoId) {
        return docNoService.get(docNoId);
    }

    @ApiOperation(value = "文号列表", notes = "文号列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<DocNoDTO> list() {
        Map<String, Object> params = requestParams();
        return docNoService.list(params);
    }

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

    @ApiOperation(value = "文号统计", notes = "文号统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    SuccessResultData<Integer> count() {
        Map<String, Object> params = requestParams();
        return new SuccessResultData<>(docNoService.count(params));
    }

    @ApiOperation(value = "获取最新文号", notes = "获取最新文号接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docNoId", value = "文号ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-latest-doc-no/{docNoId}/{docNoUsedId}")
    public SuccessResultData<String> getLatestDocNo(@PathVariable("docNoId") String docNoId, @PathVariable("docNoUsedId") String docNoUsedId) {
        DocNoBO latestNewDocNoBO = docNoService.getLatestNewDocNoBO(docNoId, docNoUsedId);
        String latestDocNo;
        if (latestNewDocNoBO == null) {
            latestDocNo = "";
        } else {
            latestDocNo = latestNewDocNoBO.getDocNoFull();
        }
        return new SuccessResultData<>(latestDocNo);
    }

    /**
     * 验证模板
     *
     * @param docNoVO
     */
    private void checkField(DocNoVO docNoVO) {
        if (StringUtils.equals(docNoVO.getType(), DocNoTypeEnum.MANUAL.getValue())) {
            return;
        }
        if (StringUtils.isBlank(docNoVO.getTemplate())) {
            throw new ParamsException("模板不能为空");
        }
    }

}