/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
*
*/
package net.northking.cctp.testbug.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.testbug.db.service.BugStatisticsChartService;
import net.northking.cctp.testbug.db.entity.BugStatisticsChart;

import net.northking.cctp.testbug.dto.BugStatisticsChartAddDto;
import net.northking.cctp.testbug.dto.BugStatisticsChartDetailDto;
import net.northking.cctp.testbug.dto.BugStatisticsChartQueryDto;
import net.northking.cctp.testbug.dto.BugStatisticsChartUpdateDto;
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 = "BugStatisticsChartApiServiceImpl")
public class BugStatisticsChartApiServiceImpl extends AbstractExcelService<BugStatisticsChart> implements BugStatisticsChartApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugStatisticsChartApiServiceImpl.class);

  @Autowired
  private BugStatisticsChartService bugStatisticsChartService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader receiverName = new ExcelHeader("分配人", "receiverName", String.class);
    ExcelHeader moduleId = new ExcelHeader("模块", "moduleId", String.class);
    ExcelHeader type = new ExcelHeader("分类: 0分配人，1模块", "type", 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 createTime = new ExcelHeader("创建时间", "createTime", String.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public int deleteAll() {
    return this.bugStatisticsChartService.deleteAll();
  }

}
