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

import net.northking.cctp.dw.dto.*;
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 = "MtLeafDesignApiServiceImpl")
public class MtLeafDesignApiServiceImpl extends AbstractExcelService<MtLeafDesign> implements MtLeafDesignApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(MtLeafDesignApiServiceImpl.class);

  @Autowired
  private MtLeafDesignService mtLeafDesignService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", String.class);
    ExcelHeader design = new ExcelHeader("设计中", "design", Integer.class);
    ExcelHeader finish = new ExcelHeader("已完成", "finish", Integer.class);
    ExcelHeader treeName = new ExcelHeader("目录名称", "treeName", String.class);
    ExcelHeader proId = new ExcelHeader("系统id", "proId", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public List<MtLeafDesignDetailDto> listQuery(ChartDataGeneQueryDto chartDataGeneQueryDto) {
    return this.mtLeafDesignService.listQuery(chartDataGeneQueryDto);
  }

}
