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

import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
import net.northking.cctp.common.db.BasicService;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.db.annotation.DatabaseReadOnly;
import net.northking.cctp.common.db.annotation.DatabaseWriteOnly;
import net.northking.cctp.common.db.excel.AbstractExcelService;
import net.northking.cctp.common.db.excel.ExcelHeader;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.util.UUIDUtil;

import net.northking.cctp.dw.db.entity.BugStatusStatistics;
import net.northking.cctp.dw.db.service.BugOverallTendencyChartService;
import net.northking.cctp.dw.db.entity.BugOverallTendencyChart;

import net.northking.cctp.dw.dto.BugOverallTendencyChartAddDto;
import net.northking.cctp.dw.dto.BugOverallTendencyChartDetailDto;
import net.northking.cctp.dw.dto.BugOverallTendencyChartQueryDto;
import net.northking.cctp.dw.dto.BugOverallTendencyChartUpdateDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
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;

/**
 * 跨系统缺陷趋势图服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

@Service
@ConditionalOnMissingBean(name = "BugOverallTendencyChartApiServiceImpl")
public class BugOverallTendencyChartApiServiceImpl extends AbstractExcelService<BugOverallTendencyChart> implements BugOverallTendencyChartApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugOverallTendencyChartApiServiceImpl.class);

  @Autowired
  private BugOverallTendencyChartService bugOverallTendencyChartService;

  @Autowired
  private EntityNameCache entityNameCache;

  //= Excel 导入导出相关代码 start ==================//
  @Override
  public BasicService<BugOverallTendencyChart> getService()
  {
    return this.bugOverallTendencyChartService;
  }

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", String.class);
    ExcelHeader unsolved = new ExcelHeader("未解决", "unsolved", Integer.class);
    ExcelHeader solved = new ExcelHeader("已解决", "solved", Integer.class);
    ExcelHeader all = new ExcelHeader("所有", "all", Integer.class);
    ExcelHeader proId = new ExcelHeader("系统id", "proId", String.class);
    ExcelHeader recordType = new ExcelHeader("记录类型：0跨系统的，1系统级的", "recordType", Integer.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

  /**
   * Excel导入，入库之前的处理
   * @param records 从Excel读取到的记录
   * @return 待入库的记录
   */
  @Override
  protected List<BugOverallTendencyChart> beforeInsertBatch(List<BugOverallTendencyChart> records)
  {
    for (BugOverallTendencyChart record : records)
    {
      record.setId(UUIDUtil.create32UUID());
    }
    return records;
  }

  /**
   * 写入Excel之前的处理<br>
   * 数据的特殊处理在此处加工处理
   *
   * @param record 来自数据库记录
   * @return 待写入Excel的记录
   */
  @Override
  protected BugOverallTendencyChart beforeWriteExcel(BugOverallTendencyChart record)
  {
    return record;
  }

  //= Excel 导入导出相关代码 end =======================//

  //= 增删改查 相关代码 start ==================//
  @Override
  @DatabaseReadOnly
  public Pagination<BugOverallTendencyChartDetailDto> pagingQuery(QueryByPage<BugOverallTendencyChartQueryDto> queryByPage)
  {
    return this.bugOverallTendencyChartService.pagingQuery(queryByPage);
  }

  @Override
  @DatabaseWriteOnly
  public String add(BugOverallTendencyChartAddDto dto)
  {
    String uuid = UUIDUtil.create32UUID();
    BugOverallTendencyChart entity = new BugOverallTendencyChart();
    BeanUtils.copyProperties(dto, entity);
    entity.setId(uuid);
    this.bugOverallTendencyChartService.insert(entity);
    return uuid;
  }

  @Override
  @DatabaseWriteOnly
  public Integer updateByPK(BugOverallTendencyChartUpdateDto dto) throws IllegalAccessException
  {
    BugOverallTendencyChart entity = new BugOverallTendencyChart();
    BeanUtils.copyProperties(dto, entity);
    return this.bugOverallTendencyChartService.updateByPrimaryKey(entity);
  }

  @Override
  @DatabaseReadOnly
  public BugOverallTendencyChartDetailDto findByPK(String uuid)
  {
    BugOverallTendencyChart example = new BugOverallTendencyChart();
    example.setId(uuid);
    List<BugOverallTendencyChart> entitys = this.bugOverallTendencyChartService.query(example);
    if (entitys.isEmpty())
    {
      return null;
    }

    BugOverallTendencyChartDetailDto dto = new BugOverallTendencyChartDetailDto();
    BeanUtils.copyProperties(entitys.get(0), dto);
    return dto;
  }

  @Override
  @DatabaseWriteOnly
  public Integer deleteByPK(String uuid)
  {
    BugOverallTendencyChart entity = new BugOverallTendencyChart();
    entity.setId(uuid);
    return this.bugOverallTendencyChartService.deleteByExample(entity);
  }

  @Override
  public List<String> addBatch(List<BugOverallTendencyChartAddDto> batch)
  {
    List<String> result = new ArrayList<>();
    for (BugOverallTendencyChartAddDto dto : batch)
    {
      result.add(this.add(dto));
    }
    return result;
  }

  @Override
  public Integer updateBatch(List<BugOverallTendencyChartUpdateDto> batch) throws IllegalAccessException
  {
    int sum = 0;
    for (BugOverallTendencyChartUpdateDto dto : batch)
    {
      sum = sum + this.updateByPK(dto);
    }
    return sum;
  }

  @Override
  public Integer deleteBatch(List<Object> batch)
  {
    return this.bugOverallTendencyChartService.deleteByPrimaryKeys(batch);
  }

  @Override
  public List<BugOverallTendencyChartDetailDto> listQuery(BugOverallTendencyChartQueryDto bugOverallTendencyChartQueryDto) {
    return bugOverallTendencyChartService.listQuery(bugOverallTendencyChartQueryDto);
  }

  @Override
  public List<BugOverallTendencyChartDetailDto> queryUnsolvedBugDataByProjectId(String projectId) {
    return bugOverallTendencyChartService.queryUnsolvedBugDataByProjectId(projectId);
  }

  @Override
  public List<BugStatusStatistics> queryAllUnsolvedBugDataSortedByTenantId(String tenantId,List<String> projectIds) {
    List<BugStatusStatistics> bugStatusStatistics = bugOverallTendencyChartService.queryAllUnsolvedBugDataSortedByTenantId(tenantId,projectIds);
    if (null==bugStatusStatistics||bugStatusStatistics.size()==0)return new ArrayList<>();
    for (BugStatusStatistics bugStatusStatistics1:bugStatusStatistics){
      bugStatusStatistics1.setProjectId(entityNameCache.translateById(EntityNameCatalog.MONSTER_SYSTEM,bugStatusStatistics1.getProjectId()));
    }
    return bugStatusStatistics;
  }

}
