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

import net.northking.cctp.scriptcase.dto.casedesign.MqSynchronizeTaskAddDto;
import net.northking.cctp.scriptcase.dto.casedesign.MqSynchronizeTaskDetailDto;
import net.northking.cctp.scriptcase.dto.casedesign.MqSynchronizeTaskQueryDto;
import net.northking.cctp.scriptcase.dto.casedesign.MqSynchronizeTaskUpdateDto;
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 = "MqSynchronizeTaskApiServiceImpl")
public class MqSynchronizeTaskApiServiceImpl extends AbstractExcelService<MqSynchronizeTask> implements MqSynchronizeTaskApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(MqSynchronizeTaskApiServiceImpl.class);

  @Autowired
  private MqSynchronizeTaskService mqSynchronizeTaskService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader taskInfo = new ExcelHeader("", "taskInfo", String.class);
    ExcelHeader type = new ExcelHeader("0-自动化用例，1-脚本，2-业务组件，3-脚本版本", "type", Integer.class);
    ExcelHeader createdTime = new ExcelHeader("", "createdTime", Date.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public List<MqSynchronizeTask> findByTypes(List<Integer> types) {
    return mqSynchronizeTaskService.findByTypes(types);
  }

}
