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

import net.northking.cctp.common.db.BasicService;
import net.northking.cctp.common.db.OrderBy;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.db.SqlOrderBy;
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.testbug.constants.TestBugConstants;
import net.northking.cctp.testbug.db.entity.BugRecord;
import net.northking.cctp.testbug.db.service.BugRecordService;
import net.northking.cctp.testbug.dto.BugRecordAddDto;
import net.northking.cctp.testbug.dto.BugRecordDetailDto;
import net.northking.cctp.testbug.dto.BugRecordQueryDto;
import net.northking.cctp.testbug.dto.BugRecordUpdateDto;
import net.northking.cctp.testbug.util.BeanHelper;
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 = "BugRecordApiServiceImpl")
public class BugRecordApiServiceImpl extends AbstractExcelService<BugRecord> implements BugRecordApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugRecordApiServiceImpl.class);

  @Autowired
  private BugRecordService bugRecordService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader recordId = new ExcelHeader("记录id", "recordId", String.class);
    ExcelHeader bugId = new ExcelHeader("缺陷id", "bugId", String.class);
    ExcelHeader recordField = new ExcelHeader("变更字段", "recordField", String.class);
    ExcelHeader recordValue = new ExcelHeader("变更值", "recordValue", String.class);
    ExcelHeader updateBy = new ExcelHeader("变更人", "updateBy", String.class);
    ExcelHeader updateTime = new ExcelHeader("变更时间", "updateTime", Date.class);
    ExcelHeader createBy = new ExcelHeader("创建人", "createBy", String.class);
    ExcelHeader createTime = new ExcelHeader("创建时间", "createTime", Date.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

  @Override
  @DatabaseWriteOnly
  public String add(BugRecordAddDto dto)
  {
    String uuid = UUIDUtil.create32UUID();
    BugRecord entity = new BugRecord();
    BeanUtils.copyProperties(dto, entity);
    entity.setRecordId(uuid);
    this.bugRecordService.insert(entity);
    return uuid;
  }

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

  @Override
  @DatabaseReadOnly
  public BugRecordDetailDto findByPK(String uuid)
  {
    BugRecord example = new BugRecord();
    example.setRecordId(uuid);
    List<BugRecord> entitys = this.bugRecordService.query(example);
    if (entitys.isEmpty())
    {
      return null;
    }

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

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

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

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

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

  /**
   * 根据bugId查询变更记录信息
   *
   * @param bugId
   * @return
   */
  @Override
  public List<BugRecordDetailDto> queryByBugId(String bugId) {

    BugRecord entity = bugRecordService.createEntity();
    entity.setBugId(bugId);
    OrderBy orderBy = new SqlOrderBy();
    orderBy.desc(TestBugConstants.CREATED_TIME);
    List<BugRecord> records = bugRecordService.query(entity, orderBy);

    return BeanHelper.copyWithCollection(records, BugRecordDetailDto.class);
  }

  /**
   * 根据bugIds查询变更记录信息
   *
   * @param tenantId
   * @return
   */
  @Override
  public List<BugRecordDetailDto> queryByTenantId(String tenantId) {

    BugRecord example = bugRecordService.createEntity();
    example.setTenantId(tenantId);
    SqlOrderBy sqlOrderBy = new SqlOrderBy();
    sqlOrderBy.desc(TestBugConstants.CREATED_TIME);
    List<BugRecord> query = bugRecordService.query(example,sqlOrderBy);

    return BeanHelper.copyWithCollection(query, BugRecordDetailDto.class);
  }

  /**
   * 查询缺陷变更记录
   *
   * @param bugIds
   * @return
   */
  @Override
  public List<BugRecordDetailDto> queryByBugIds(List<String> bugIds) {

    return bugRecordService.queryByBugIds(bugIds);
  }

  @Override
  public void pubAdd(BugRecord bugRecordAddDto) {
    bugRecordService.pubAdd(bugRecordAddDto);
  }

}
