/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
*
*/
package net.northking.cctp.device.api.engineReleases.service;

import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
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.security.authentication.NKSecurityContext;
import net.northking.cctp.common.util.UUIDUtil;

import net.northking.cctp.device.db.service.CdEngineReleasesService;
import net.northking.cctp.device.db.entity.CdEngineReleases;

import net.northking.cctp.device.dto.engineReleases.CdEngineReleasesAddDto;
import net.northking.cctp.device.dto.engineReleases.CdEngineReleasesDetailDto;
import net.northking.cctp.device.dto.engineReleases.CdEngineReleasesQueryDto;
import net.northking.cctp.device.dto.engineReleases.CdEngineReleasesUpdateDto;
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 = "CdEngineReleasesApiServiceImpl")
public class CdEngineReleasesApiServiceImpl extends AbstractExcelService<CdEngineReleases> implements CdEngineReleasesApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(CdEngineReleasesApiServiceImpl.class);

  @Autowired
  private CdEngineReleasesService cdEngineReleasesService;

  @Autowired
  private EntityNameCache entityNameCache;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("主键id", "id", String.class);
    ExcelHeader packageName = new ExcelHeader("更新包名称", "packageName", String.class);
    ExcelHeader releaseVersion = new ExcelHeader("发行版本号", "releaseVersion", String.class);
    ExcelHeader releaseTitle = new ExcelHeader("发行标题", "releaseTitle", String.class);
    ExcelHeader releaseNote = new ExcelHeader("发行内容", "releaseNote", String.class);
    ExcelHeader releaseBy = new ExcelHeader("发行人", "releaseBy", String.class);
    ExcelHeader releaseTime = new ExcelHeader("发行时间", "releaseTime", Date.class);
    return new ExcelHeader[]{};
  }

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

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

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

  //= 增删改查 相关代码 start ==================//
  @Override
  @DatabaseReadOnly
  public Pagination<CdEngineReleasesDetailDto> pagingQuery(QueryByPage<CdEngineReleasesQueryDto> queryByPage)
  {
    Pagination<CdEngineReleasesDetailDto> pagination = this.cdEngineReleasesService.pagingQuery(queryByPage);
    for (CdEngineReleasesDetailDto record : pagination.getRecords()) {
      String releaseBy = entityNameCache.translateById(EntityNameCatalog.MONSTER_USER, record.getReleaseBy());
      record.setReleaseBy(releaseBy);
    }
    return pagination;
  }

  @Override
  @DatabaseWriteOnly
  public String add(CdEngineReleasesAddDto dto)
  {
    String uuid = UUIDUtil.create32UUID();
    CdEngineReleases entity = new CdEngineReleases();
    BeanUtils.copyProperties(dto, entity);
    entity.setId(uuid);
    entity.setReleaseBy(NKSecurityContext.getUserId());
    entity.setReleaseTime(new Date());
    this.cdEngineReleasesService.insert(entity);
    return uuid;
  }

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

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

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

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

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

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

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

  @Override
  public CdEngineReleases getLatestRelease() {
    CdEngineReleases release = this.cdEngineReleasesService.findPackage();
    return release;
  }

}
