/*
* 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.service.MtCaseExecChartService;
import net.northking.cctp.dw.db.entity.MtCaseExecChart;

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

  @Autowired
  private MtCaseExecChartService mtCaseExecChartService;
  @Autowired
  private EntityNameCache entityNameCache;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader createdBy = new ExcelHeader("创建人", "createdBy", String.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", String.class);
    ExcelHeader noExec = new ExcelHeader("未执行", "noExec", Integer.class);
    ExcelHeader invalid = new ExcelHeader("无效", "invalid", Integer.class);
    ExcelHeader fail = new ExcelHeader("失败", "fail", Integer.class);
    ExcelHeader block = new ExcelHeader("阻塞", "block", Integer.class);
    ExcelHeader hang = new ExcelHeader("挂起", "hang", Integer.class);
    ExcelHeader incomplete = new ExcelHeader("未完成", "incomplete", Integer.class);
    ExcelHeader pass = new ExcelHeader("通过", "pass", Integer.class);
    ExcelHeader projectId = new ExcelHeader("", "projectId", String.class);
    ExcelHeader statisticsType = new ExcelHeader("类型", "statisticsType", Integer.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public List<MtCaseExecChartDetailDto> listQuery(ChartDataGeneQueryDto chartDataGeneQueryDto) {
    List<MtCaseExecChartDetailDto> page = this.mtCaseExecChartService.listQuery(chartDataGeneQueryDto);

    if(null!=page && page.size()!=0) {
      page.forEach(mtCaseExecChartAddDto -> {
        mtCaseExecChartAddDto.setCreatedBy(entityNameCache.translateById(EntityNameCatalog.WITH_LOGIN_NO, mtCaseExecChartAddDto.getCreatedBy()));
      });
    }
    return page;
  }

}
