/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
*
*/
package net.northking.cctp.scriptcase.api.app;

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.hzbank.testteam.autotest.dependencies.authDependency.constants.AuthDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.constants.BaseDefineDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.northking.cctp.common.db.DefaultPagination;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.dto.AdvancedQueryOptions;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.scriptcase.api.app.service.AppApiService;
import net.northking.cctp.scriptcase.dto.app.AppDetailDto;
import net.northking.cctp.scriptcase.dto.app.AppQueryDto;
import net.northking.cctp.scriptcase.dto.app.AppVersionQueryDto;
import net.northking.cctp.scriptcase.dto.app.AtuAppVersionDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "应用表")
@RequestMapping("/v1")
@RestController
public class AppQueryCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AppQueryCtrl.class);

  @Autowired
  private AppApiService apiService;

  /**
   * 分页查询 应用表 列表
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：应用表")
  @RequiredPermissions("app")
  @PostMapping(value = "/paging/query/Apps",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<AppDetailDto>> pagingQuery(
      @RequestBody @Validated QueryByPage<AppQueryDto> queryByPage)
  {
    logger.debug("查询应用表开始---------");
    ResultWrapper<Pagination<AppDetailDto>> wrapper = new ResultWrapper<>();
    if (StringUtils.isEmpty(queryByPage.getQuery().getIsQueryAll()) || "0".equals(queryByPage.getQuery().getIsQueryAll())) {
      if (CollectionUtils.isEmpty(queryByPage.getQuery().getOwnerIdList())) {
        return wrapper.success(new DefaultPagination<>());
      }
    }
    if ("1".equals(queryByPage.getQuery().getIsQueryAll())) {
      queryByPage.getQuery().setOwnerIdList(null);
    }
    if (queryByPage.getOrderByList().isEmpty()) {
      queryByPage.addQueryOrderByDesc("updated_time");
    }
    Pagination<AppDetailDto> page = apiService.pagingQuery(queryByPage);
    logger.debug("查询应用表结束---------");
    return wrapper.success(page);
  }

  @ApiOperation(value = "分页查询app版本详情")
  @PostMapping(value = "/paging/query/appVersions")
  public ResultWrapper<Pagination<AtuAppVersionDto>> pagingQueryAppVersions(@RequestBody QueryByPage<AppVersionQueryDto> page){
    ResultWrapper<Pagination<AtuAppVersionDto>> wrapper = new ResultWrapper<>();
    Pagination<AtuAppVersionDto> result = this.apiService.pagingQueryAppVersions(page);
    return wrapper.success(result);
  }

    @ApiOperation(value = "获取高级查询相关配置数据:创建人，更新人，责任人")
    @GetMapping(value = "/app/advancedQueryOptions")
    public ResponseVO<AdvancedQueryOptions> advancedQueryOptions(
            @ApiParam(value = "系统ID", required = true, example = "36602691803153920")
            @RequestHeader(AuthDependencyConstants.REQUEST_HEADER_PROJECT_ID) String projectId) {
      logger.debug("start advancedQueryOptions");
      ResponseVO<AdvancedQueryOptions> responseVO = new ResponseVO<>();
      AdvancedQueryOptions result = apiService.advancedQueryOptions(projectId);
      responseVO.setCode(BaseDefineDependencyConstants.SYSTEM_RESPONSE_CODE_SUCCESS);
      responseVO.setData(result);
      responseVO.setMessage("获取成功");
      logger.debug("end advancedQueryOptions");
      return responseVO;
    }
}


