/*
* Copyright (c) ${Company} Corporation ${Year} . All rights reserved.
*
*/
package ${genPackage}.${javaClassRef.getClassName()};

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "${javaClassRef.getComment()}")
@RequestMapping("/v1")
@RestController
public class ${javaClassRef.getClassName()}QueryCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(${javaClassRef.getClassName()}QueryCtrl.class);

  @Autowired
  private ${javaClassRef.getClassName()}ApiService apiService;

  /**
   * 分页查询
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：${javaClassRef.getComment()}")
  @PostMapping(value = "/paging/query/${javaClassRef.getClassName()}s",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<${javaClassRef.getClassName()}DetailDto>> pagingQuery(
      @RequestBody @Validated QueryByPage<${javaClassRef.getClassName()}QueryDto> queryByPage)
  {
    ResultWrapper<Pagination<${javaClassRef.getClassName()}DetailDto>> wrapper = new ResultWrapper<>();
    Pagination<${javaClassRef.getClassName()}DetailDto> page = apiService.pagingQuery(queryByPage);

    return wrapper.success(page);
  }

  @ApiOperation(value = "导出Excel：${javaClassRef.getComment()}")
  @PostMapping(value = "/excel/export/${javaClassRef.getClassName()}s",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void exportExcel(
      @RequestBody @Validated QueryByPage<${javaClassRef.getClassName()}QueryDto> queryByPage,
      HttpServletResponse response)
  {
    // 定义导出的Excel在客户端浏览器上提示下载的文件名，不写后缀
    String fileName = "CCTP-${javaClassRef.getComment()}";
    apiService.exportExcel(response, queryByPage, fileName);
  }

  @ApiOperation(value = "下载Excel模板：${javaClassRef.getComment()}")
  @PostMapping(value = "/excel/template/${javaClassRef.getClassName()}s",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void downloadTemplate(HttpServletResponse response)
  {
    String fileName = "CCTP模板-${javaClassRef.getComment()}";
    apiService.downloadTemplate(response, fileName);
  }

  @ApiOperation(value = "导入Excel：${javaClassRef.getComment()}")
  @PostMapping(value = "/excel/import/${javaClassRef.getClassName()}s",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<Integer> importExcel(
      @RequestParam("file") MultipartFile file)
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    int count = apiService.importExcel(file);

    return wrapper.success(count);
  }


}


