package com.qmth.distributed.print.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.dto.TemplateDto;
import com.qmth.distributed.print.business.entity.BasicTemplate;
import com.qmth.distributed.print.business.service.BasicTemplateService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * <p>
 * 通用模板 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "通用模板Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.basic}/template")
@Validated
public class BasicTemplateController {

    @Autowired
    private BasicTemplateService basicTemplateService;

    /**
     * 查询
     *
     * @param enable
     * @param type
     * @param name
     * @param startTime
     * @param endTime
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "enable", required = false) Boolean enable,
                       @RequestParam(value = "type", required = false) String type,
                       @RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "startTime", required = false) Long startTime,
                       @RequestParam(value = "endTime", required = false) Long endTime,
                       @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<TemplateDto> templateDtoIPage = basicTemplateService.list(enable, type, name, startTime, endTime, pageNumber, pageSize);
        return ResultUtil.ok(templateDtoIPage);
    }


    /**
     * 根据ID查询单个信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询单个信息")
    @RequestMapping(value = "/get_one", method = RequestMethod.POST)
    public Result getOne(@RequestParam("id") Long id) {
        BasicTemplate basicTemplate = basicTemplateService.getOne(id);
        return ResultUtil.ok(basicTemplate);
    }

    /**
     * 新增/修改
     *
     * @param template
     * @return
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody BasicTemplate template) throws Exception {
        boolean isSuccess = basicTemplateService.saveTemplate(template);
        return ResultUtil.ok(isSuccess);
    }

    /**
     * 启用/禁用
     *
     * @param template
     * @return
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enable(@RequestBody BasicTemplate template) {
        boolean isSuccess = basicTemplateService.enable(template);
        return ResultUtil.ok(isSuccess);
    }

    /**
     * 读取模板内容
     *
     * @param attachmentId
     * @return
     */
    @ApiOperation(value = "读取模板内容")
    @RequestMapping(value = "/read_content", method = RequestMethod.POST)
    public Result enable(@RequestParam("attachmentId") Long attachmentId) {
        Object content = basicTemplateService.readContent(attachmentId);
        return ResultUtil.ok(content);
    }
}

