/*
* Copyright (c) ${Company} Corporation ${Year} . All rights reserved.
*
*/
package ${genPackage}.${javaClassRef.getClassName()};

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.EntityUtils;
import net.northking.cctp.common.util.UUIDUtil;

import ${servicePackage}.${javaClassRef.getClassName()}Service;
import ${entityPackage}.${javaClassRef.getClassName()};

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;

/**
 * ${javaClassRef.getComment()}服务实现类
 *
 * @author ${Author}
 * @since ${Version}
 */

@Service
@ConditionalOnMissingBean(name = "${javaClassRef.getClassName()}ApiServiceImpl")
public class ${javaClassRef.getClassName()}ApiServiceImpl extends AbstractExcelService<${javaClassRef.getClassName()}> implements ${javaClassRef.getClassName()}ApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(${javaClassRef.getClassName()}ApiServiceImpl.class);

  @Autowired
  private ${javaClassRef.getClassName()}Service ${javaClassRef.getRefName()}Service;

  //= Excel 导入导出相关代码 start ==================//
  @Override
  public BasicService<${javaClassRef.getClassName()}> getService()
  {
    return this.${javaClassRef.getRefName()}Service;
  }

  @Override
  public ExcelHeader[] excelHeaders()
  {
#foreach( ${attrRef} in ${javaClassRef.getAttributes()} )
    ExcelHeader ${attrRef.getRefName()} = new ExcelHeader("${attrRef.getComment()}", "${attrRef.getRefName()}", ${attrRef.getJavaTypeName()}.class);
#end
    // TODO 定义表头及顺序
//    return new ExcelHeader[]{header...};
    return new ExcelHeader[]{};
  }

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

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

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

  //= 增删改查 相关代码 start ==================//
  @Override
  @DatabaseReadOnly
  public Pagination<${javaClassRef.getClassName()}DetailDto> pagingQuery(QueryByPage<${javaClassRef.getClassName()}QueryDto> queryByPage)
  {
    return this.${javaClassRef.getRefName()}Service.pagingQuery(queryByPage);
  }

  @Override
  @DatabaseWriteOnly
  public String add(${javaClassRef.getClassName()}AddDto dto)
  {
    String uuid = UUIDUtil.create32UUID();
    ${javaClassRef.getClassName()} entity = new ${javaClassRef.getClassName()}();
    BeanUtils.copyProperties(dto, entity);
    entity.setId(uuid);
    this.${javaClassRef.getRefName()}Service.insert(entity);
    return uuid;
  }

  @Override
  @DatabaseWriteOnly
  public Integer updateByPK(${javaClassRef.getClassName()}UpdateDto dto) throws IllegalAccessException
  {
    ${javaClassRef.getClassName()} entity = new ${javaClassRef.getClassName()}();
    BeanUtils.copyProperties(dto, entity);
    return this.${javaClassRef.getRefName()}Service.updateByPrimaryKey(entity);
  }

  @Override
  @DatabaseReadOnly
  public ${javaClassRef.getClassName()}DetailDto findByPK(String uuid)
  {
    ${javaClassRef.getClassName()} example = new ${javaClassRef.getClassName()}();
    example.setId(uuid);
    List<${javaClassRef.getClassName()}> entitys = this.${javaClassRef.getRefName()}Service.query(example);
    if (entitys.isEmpty())
    {
      return null;
    }

    ${javaClassRef.getClassName()}DetailDto dto = new ${javaClassRef.getClassName()}DetailDto();
    BeanUtils.copyProperties(entitys.get(0), dto);
    return dto;
  }

  @Override
  @DatabaseWriteOnly
  public Integer deleteByPK(String uuid)
  {
    ${javaClassRef.getClassName()} entity = new ${javaClassRef.getClassName()}();
    entity.setId(uuid);
    return this.${javaClassRef.getRefName()}Service.deleteByExample(entity);
  }

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

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

  @Override
  public Integer deleteBatch(List<Object> batch)
  {
    return this.${javaClassRef.getRefName()}Service.deleteByPrimaryKeys(batch);
  }

}
