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

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.service.BugTendencyChartTmpService;
import net.northking.cctp.dw.db.entity.BugTendencyChartTmp;

import net.northking.cctp.dw.dto.*;
import net.northking.cctp.dw.feign.ProjectsFeignClient;
import net.northking.cctp.dw.feign.StatusRoleDto;
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 = "BugTendencyChartTmpApiServiceImpl")
public class BugTendencyChartTmpApiServiceImpl extends AbstractExcelService<BugTendencyChartTmp> implements BugTendencyChartTmpApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugTendencyChartTmpApiServiceImpl.class);

  @Autowired
  private BugTendencyChartTmpService bugTendencyChartTmpService;
  @Autowired
  private ProjectsFeignClient projectsFeignClient;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    ExcelHeader projectId = new ExcelHeader("系统id", "projectId", String.class);
    ExcelHeader bugStatus = new ExcelHeader("缺陷状态", "bugStatus", String.class);
    ExcelHeader amount = new ExcelHeader("数量", "amount", Integer.class);
    ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public List<BugTendencyChartTmpDetailDto> listQuery(ChartDataGeneQueryDto chartDataGeneQueryDto) {
    //获取终态
    StatusRoleDto s=new StatusRoleDto();
    s.setProjectId(chartDataGeneQueryDto.getProId());
    s.setFinishedState(true);
    List<String> statusList=projectsFeignClient.queryBugEndStatus(s).getData();
    if(statusList==null || statusList.size()<1)return new ArrayList<>();
    return this.bugTendencyChartTmpService.listQuery(chartDataGeneQueryDto,statusList);
  }
}
