/*
* 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.MtCaseDesignChartService;
import net.northking.cctp.dw.db.entity.MtCaseDesignChart;

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 = "MtCaseDesignChartApiServiceImpl")
public class MtCaseDesignChartApiServiceImpl extends AbstractExcelService<MtCaseDesignChart> implements MtCaseDesignChartApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(MtCaseDesignChartApiServiceImpl.class);

  @Autowired
  private MtCaseDesignChartService mtCaseDesignChartService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader processCaseDesignAmount = new ExcelHeader("", "processCaseDesignAmount", Integer.class);
    ExcelHeader pageCaseDesignAmount = new ExcelHeader("", "pageCaseDesignAmount", Integer.class);
    ExcelHeader deviceCaseDesignAmount = new ExcelHeader("", "deviceCaseDesignAmount", Integer.class);
    ExcelHeader financeCaseDesignAmount = new ExcelHeader("", "financeCaseDesignAmount", Integer.class);
    ExcelHeader businessCaseDesignAmount = new ExcelHeader("", "businessCaseDesignAmount", Integer.class);
    ExcelHeader runBatchCaseDesignAmount = new ExcelHeader("", "runBatchCaseDesignAmount", Integer.class);
    ExcelHeader specialCaseDesignAmount = new ExcelHeader("", "specialCaseDesignAmount", Integer.class);
    ExcelHeader otherCaseDesignAmount = new ExcelHeader("", "otherCaseDesignAmount", Integer.class);
    ExcelHeader noBusinessCaseDesignAmount = new ExcelHeader("", "noBusinessCaseDesignAmount", Integer.class);
    ExcelHeader formCaseDesignAmount = new ExcelHeader("", "formCaseDesignAmount", Integer.class);
    ExcelHeader formCheckCaseDesignAmount = new ExcelHeader("", "formCheckCaseDesignAmount", Integer.class);
    ExcelHeader interfaceCaseDesignAmount = new ExcelHeader("", "interfaceCaseDesignAmount", Integer.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    ExcelHeader createBy = new ExcelHeader("", "createBy", String.class);
    ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", String.class);
    ExcelHeader projectId = new ExcelHeader("", "projectId", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public List<MtCaseDesignChartDetailDto> listQuery(ChartDataGeneQueryDto chartDataGeneQueryDto) {
    return this.mtCaseDesignChartService.listQuery(chartDataGeneQueryDto);
  }

}
