/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . 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.BugTemplateStatusService;
import net.northking.cctp.dw.db.entity.BugTemplateStatus;

import net.northking.cctp.dw.dto.BugTemplateStatusAddDto;
import net.northking.cctp.dw.dto.BugTemplateStatusDetailDto;
import net.northking.cctp.dw.dto.BugTemplateStatusQueryDto;
import net.northking.cctp.dw.dto.BugTemplateStatusUpdateDto;
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 = "BugTemplateStatusApiServiceImpl")
public class BugTemplateStatusApiServiceImpl extends AbstractExcelService<BugTemplateStatus> implements BugTemplateStatusApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugTemplateStatusApiServiceImpl.class);

  @Autowired
  private BugTemplateStatusService bugTemplateStatusService;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("主键", "id", String.class);
    ExcelHeader projectId = new ExcelHeader("系统id", "projectId", String.class);
    ExcelHeader bugStatusCode = new ExcelHeader("缺陷状态码", "bugStatusCode", String.class);
    ExcelHeader bugStatus = new ExcelHeader("缺陷状态名", "bugStatus", String.class);
    ExcelHeader idx = new ExcelHeader("", "idx", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  /**
   * 查询缺陷终态
   *
   * @return
   */
  @Override
  public List<BugTemplateStatus> queryBugEndStatus() {
    BugTemplateStatus status = new BugTemplateStatus();
    status.setIsEndStatus(1);
    return bugTemplateStatusService.query(status);
  }

}
