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

import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetVersionAddDto;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetVersionDetailDto;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetVersionQueryDto;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetVersionUpdateDto;
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.Date;
import java.util.List;

/**
 * 脚本输入项关联草稿表版本表服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

@Service
@ConditionalOnMissingBean(name = "AtuScriptInputSetVersionApiServiceImpl")
public class AtuScriptInputSetVersionApiServiceImpl extends AbstractExcelService<AtuScriptInputSetVersion> implements AtuScriptInputSetVersionApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AtuScriptInputSetVersionApiServiceImpl.class);

  @Autowired
  private AtuScriptInputSetVersionService atuScriptInputSetVersionService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("主键", "id", String.class);
    ExcelHeader scriptVersionId = new ExcelHeader("脚本版本表主键", "scriptVersionId", String.class);
    ExcelHeader scriptId = new ExcelHeader("脚本id", "scriptId", String.class);
    ExcelHeader envId = new ExcelHeader("环境id", "envId", String.class);
    ExcelHeader dataUrl = new ExcelHeader("输入项数据文件地址", "dataUrl", String.class);
    ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", Date.class);
    ExcelHeader createdBy = new ExcelHeader("创建人", "createdBy", String.class);
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

}
