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

import net.northking.cctp.dw.dto.BugLifeCycleChartAddDto;
import net.northking.cctp.dw.dto.BugLifeCycleChartDetailDto;
import net.northking.cctp.dw.dto.BugLifeCycleChartQueryDto;
import net.northking.cctp.dw.dto.BugLifeCycleChartUpdateDto;
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 = "BugLifeCycleChartApiServiceImpl")
public class BugLifeCycleChartApiServiceImpl extends AbstractExcelService<BugLifeCycleChart> implements BugLifeCycleChartApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugLifeCycleChartApiServiceImpl.class);

  @Autowired
  private BugLifeCycleChartService bugLifeCycleChartService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("主键", "id", String.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    ExcelHeader lifeTime = new ExcelHeader("生命周期", "lifeTime", String.class);
    ExcelHeader amount = new ExcelHeader("数量", "amount", Integer.class);
    ExcelHeader projectId = new ExcelHeader("", "projectId", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public List<BugLifeCycleChartDetailDto> listQuery(String proId,String tenantId) {
    return bugLifeCycleChartService.listQuery(proId,tenantId);
  }

}
