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

import net.northking.cctp.dw.dto.atu.DwAtuScriptVersionDetailDto;
import net.northking.cctp.dw.dto.atu.DwAtuScriptVersionQueryDto;
import net.northking.cctp.dw.dto.atu.DwAtuScriptVersionUpdateDto;
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;

/**
 * dw脚本版本同步表服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

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

  @Autowired
  private DwAtuScriptVersionService dwAtuScriptVersionService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader scriptId = new ExcelHeader("", "scriptId", String.class);
    ExcelHeader versionId = new ExcelHeader("", "versionId", Integer.class);
    ExcelHeader createdBy = new ExcelHeader("", "createdBy", String.class);
    ExcelHeader createdTime = new ExcelHeader("", "createdTime", Date.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

  @Override
  @DatabaseWriteOnly
  public String add(DwAtuScriptVersion dto)
  {
    String uuid = dto.getScriptId()+":"+dto.getVersionId();
    this.dwAtuScriptVersionService.insert(dto);
    return uuid;
  }

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

  @Override
  @DatabaseReadOnly
  public DwAtuScriptVersionDetailDto findByPK(String uuid)
  {
    DwAtuScriptVersion example = new DwAtuScriptVersion();
    List<DwAtuScriptVersion> entitys = this.dwAtuScriptVersionService.query(example);
    if (entitys.isEmpty())
    {
      return null;
    }

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

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

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

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

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

  @Override
  public Integer deleteByExamples(List<DwAtuScriptVersion> dwAtuScriptVersionDeletes) {
    Integer count=0;
    for (DwAtuScriptVersion dwAtuScriptVersion:dwAtuScriptVersionDeletes) {
      count+=dwAtuScriptVersionService.deleteByExample(dwAtuScriptVersion);
    }
    return count;
  }


}
