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

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.northking.cctp.common.db.BasicDao;
import net.northking.cctp.common.db.DefaultPagination;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.saas.db.TenantEntityService;
import net.northking.cctp.scriptcase.db.dao.AppDao;
import net.northking.cctp.scriptcase.db.entity.App;
import net.northking.cctp.scriptcase.db.service.AppService;
import net.northking.cctp.scriptcase.dto.app.AppCreator;
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.feign.DeviceFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 应用表
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2022-10-13 10:00:44  <br>
 * @author:  maven-cctp-plugin  <br>
 * @since:  1.0 <br>
 */
@Service
@ConditionalOnMissingBean(name = "AppServiceImpl")
public class AppServiceImpl extends TenantEntityService<App> implements AppService
{
private static final Logger logger = LoggerFactory.getLogger(AppServiceImpl.class);

  private final AppDao appDao;

  public AppServiceImpl(AppDao appDao) {this.appDao = appDao;}

  public BasicDao<App> getDao() {return appDao;}

  public App createEntity() { return new App();}

  @Autowired
  private DeviceFeignClient deviceFeignClient;





  @Override
  public List<AppCreator> queryCreator(AppQueryDto query) {
    return appDao.queryCreator(query);
  }


  @Override
  public List<App> queryAppList(App app) {
    List<App> appList = this.appDao.queryAppList(app);
    return appList;
  }

  @Override
  public List<App> checkAppAlive(List<String> ids) {
    List<App> appList = new ArrayList<>();
    if (ids == null || ids.size() <= 0){
      return appList;
    }
    return this.appDao.checkAppAlive(ids);
  }

  @Override
  public Pagination<App> PagingQueryGroupByPackage(QueryByPage<AppVersionQueryDto> queryByPage) {
    int pageNo = queryByPage.getPageNo();
    int pageSize = queryByPage.getPageSize();
    DefaultPagination<App> pagination = new DefaultPagination();
    if (pageNo <= 0) {
      pageNo = 1;
    }
    if (pageSize <= 0 || pageSize > 500) {
      pageSize = 500;
    }

    Page<App> page = PageHelper.startPage(pageNo, pageSize);
    appDao.PagingQueryGroupByPackage(queryByPage.getQuery());

    pagination.setRecordCount(page.getTotal());
    pagination.setRecords(page.getResult());
    pagination.setPageSize(pageSize);
    pagination.setPageNo(pageNo);
    return pagination;
  }

    @Override
    public List<String> selectCreatorByProject(String projectId) {
        return this.appDao.selectCreatorByProject(projectId);
    }

    private String[] getPackageList(String result, String platform) {
    String[] packageList = null;
    if ("1".equalsIgnoreCase(platform)) {

    } else {
      packageList = result.split("package:");
    }
    return packageList;
  }

  @Override
  public Pagination<AppDetailDto> myPagingQuery(QueryByPage<AppQueryDto> queryByPage) {
    int pageNo = queryByPage.getPageNo();
    int pageSize = queryByPage.getPageSize();
    DefaultPagination<AppDetailDto> pagination = new DefaultPagination();
    if (pageNo <= 0) {
      pageNo = 1;
    }
    if (pageSize <= 0 || pageSize > 500) {
      pageSize = 500;
    }

    Page<AppDetailDto> page = PageHelper.startPage(pageNo, pageSize);
    appDao.myPagingQuery(queryByPage.getQuery());

    pagination.setRecordCount(page.getTotal());
    pagination.setRecords(page.getResult());
    pagination.setPageSize(pageSize);
    pagination.setPageNo(pageNo);
    return pagination;
  }
}

