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

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.db.BasicDao;
import net.northking.cctp.common.db.DefaultPagination;
import net.northking.cctp.common.db.OrderBy;
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.common.security.authentication.NKSecurityContext;
import net.northking.cctp.platform.db.entity.CltDictItem;
import net.northking.cctp.testbug.db.dao.BugManagementDao;
import net.northking.cctp.testbug.db.entity.BugManagement;
import net.northking.cctp.testbug.db.service.BugManagementService;
import net.northking.cctp.testbug.dto.*;
import net.northking.cctp.testbug.dto.bus.*;
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.List;


/**
 * 缺陷管理
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2021-10-08 15:56:45  <br>
 * @author:  maven-cctp-plugin  <br>
 * @since:  1.0 <br>
 */
@Service
@ConditionalOnMissingBean(name = "BugManagementServiceImpl")
public class BugManagementServiceImpl extends TenantEntityService<BugManagement> implements BugManagementService
{
private static final Logger logger = LoggerFactory.getLogger(BugManagementServiceImpl.class);

  private final BugManagementDao bugManagementDao;
  @Autowired
  private EntityNameCache entityNameCache;

  public BugManagementServiceImpl(BugManagementDao bugManagementDao) {this.bugManagementDao = bugManagementDao;}

  public BasicDao<BugManagement> getDao() {return bugManagementDao;}

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

  @Override
  public List<String> queryBugCreatedBy(List<String> projectList, String tenantId) {

    return bugManagementDao.queryBugCreatedBy(projectList, tenantId);
  }

  @Override
  public List<String> queryBugReceiver(List<String> projectList, String tenantId) {

    return bugManagementDao.queryBugReceiver(projectList, tenantId);
  }

  @Override
  public Pagination<BugManagementDetailDto> pagingQueryNew(QueryByPage<BugManagementQueryDto> queryByPage) {
    int pageNo = queryByPage.getPageNo();
    int pageSize = queryByPage.getPageSize();
    BugManagementQueryDto query = queryByPage.getQuery();
    OrderBy orderBy = queryByPage.toSqlOrderBy();
    query.setTenantId(NKSecurityContext.getTenantId());

    return this.query(query, orderBy, pageNo, pageSize, bugManagementDao);
  }

  @Override
  public List<BugManagement> queryBugInfo(BugManagementQueryDto dto) {
    return bugManagementDao.queryBugInfo(dto);
  }

  @Override
  public Integer synchronizationModule(SynModule data) {
    return bugManagementDao.synchronizationModule(data);
  }

  @Override
  public Integer synchronizationSystem(SynSystem data) {
    return bugManagementDao.synchronizationSystem(data);
  }

  @Override
  public List<BugManagementDetailDto> findByLinkId(String linkId) {
    return bugManagementDao.findByLinkId(linkId);
  }

  @Override
  public List<BugTendencyChartDetailDto> queryBugByStatus(String proId,List<String> statusList, String tenantId,String startTime,String endTime) {
    return bugManagementDao.queryBugByStatus(proId,statusList, tenantId,startTime,endTime);
  }

  @Override
  public DefaultPagination<BugManagementDetailDto> pagingQueryFilter(QueryByPage<BugManagementQueryDto> queryByPage) {
    int pageNo = queryByPage.getPageNo();
    int pageSize = queryByPage.getPageSize();
    BugManagementQueryDto query = queryByPage.getQuery();
    OrderBy orderBy = queryByPage.toSqlOrderBy();
    query.setTenantId(NKSecurityContext.getTenantId());
    if (pageNo <= 0)
    {
      pageNo = 1;
    }
    if (pageSize <= 0 || pageSize > 500)
    {
      pageSize = 500;
    }

    DefaultPagination<BugManagementDetailDto> pagination = new DefaultPagination<>();
    pagination.setPageSize(pageSize);
    pagination.setPageNo(pageNo);

    OrderBy sort = orderBy;
    if (sort == null || sort.isEmpty())
    {
      sort = defaultOrderBy();
    }

    if (sort != null && sort.isNotEmpty())
    {
      PageMethod.orderBy(sort.toString());
    }

    Page<BugManagementDetailDto> page = PageMethod.startPage(pageNo, pageSize);

    beforeQuery(query);
    List<BugManagementDetailDto> result;

    result = bugManagementDao.pagingQueryFilter(query);

    pagination.setRecords(result);
    pagination.setRecordCount(page.getTotal());
    return pagination;
  }

  @Override
  public List<BugStatisticsChartAddDto> queryBugByDicItems(List<CltDictItem> dictItems,String tenantId) {
    List<BugStatisticsChartAddDto> bugStatisticsChartAddDtos = bugManagementDao.queryBugByDicItems(dictItems, tenantId);
    return bugStatisticsChartAddDtos;
  }

  @Override
  public List<BugManagementDetailDto> pageQ(BugManagementQueryDto query) {
    return bugManagementDao.pageQ(query);
  }

  @Override
  public void add(BugManagement entity) {
    bugManagementDao.add(entity);
  }

  @Override
  public List<IsCommitBugDetailDto> findByLinkIdList(List<String> linkIdList) {
    return bugManagementDao.findByLinkIdList(linkIdList);
  }

  @Override
  public List<String> findTenantIdList() {
    return bugManagementDao.findTenantIdList();
  }

  @Override
  public List<BugStatisticsChartAddDto> queryBugByDicItems2(List<CltDictItem> parseArray, String tenantId, List<String> projectIdList, String recordType) {
    return bugManagementDao.queryBugByDicItems2(parseArray,tenantId,projectIdList,recordType);
  }

  @Override
  public DefaultPagination<BugHighDegreeChartDetailDto> pagingQueryHighDegree(QueryByPage<BugHighDegreeChartQueryDto> queryByPage) {
    int pageNo = queryByPage.getPageNo();
    int pageSize = queryByPage.getPageSize();
    BugHighDegreeChartQueryDto query = queryByPage.getQuery();
    OrderBy orderBy = queryByPage.toSqlOrderBy();
    if (pageNo <= 0)
    {
      pageNo = 1;
    }
    if (pageSize <= 0 || pageSize > 500)
    {
      pageSize = 500;
    }

    DefaultPagination<BugHighDegreeChartDetailDto> pagination = new DefaultPagination<>();
    pagination.setPageSize(pageSize);
    pagination.setPageNo(pageNo);

    OrderBy sort = orderBy;
    if (sort == null || sort.isEmpty())
    {
      sort = defaultOrderBy();
    }

    if (sort != null && sort.isNotEmpty())
    {
      PageMethod.orderBy(sort.toString());
    }

    Page<BugHighDegreeChartDetailDto> page = PageMethod.startPage(pageNo, pageSize);

    List<BugHighDegreeChartDetailDto> result;

    result = bugManagementDao.pagingQueryHighDegree(query);

    pagination.setRecords(result);
    pagination.setRecordCount(page.getTotal());
    return pagination;
  }
  @Override
  public Integer queryUserBugByProId(String projectId, String userId) {
    return bugManagementDao.queryUserBugByProId(projectId, userId);
  }
  @Override
  public CountBugDetailDto countBugAmount(String projectId,List<String> endStatusList) {
    return bugManagementDao.countBugAmount(projectId,endStatusList);
  }


}
