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

import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
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.BugGradeStatisticsService;
import net.northking.cctp.dw.db.entity.BugGradeStatistics;

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.Date;
import java.util.List;

/**
 * 缺陷严重程度统计图服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

@Service
@ConditionalOnMissingBean(name = "BugGradeStatisticsApiServiceImpl")
public class BugGradeStatisticsApiServiceImpl extends AbstractExcelService<BugGradeStatistics> implements BugGradeStatisticsApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugGradeStatisticsApiServiceImpl.class);

  @Autowired
  private BugGradeStatisticsService bugGradeStatisticsService;
  @Autowired
  private EntityNameCache entityNameCache;
  //= Excel 导入导出相关代码 start ==================//
  @Override
  public BasicService<BugGradeStatistics> getService()
  {
    return this.bugGradeStatisticsService;
  }

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader receiverName = new ExcelHeader("分配人", "receiverName", String.class);
    ExcelHeader loginNo = new ExcelHeader("登陆帐号", "loginNo", String.class);
    ExcelHeader moduleId = new ExcelHeader("模块", "moduleId", String.class);
    ExcelHeader type = new ExcelHeader("类型", "type", String.class);
    ExcelHeader a = new ExcelHeader("致命性错误-A类", "a", Integer.class);
    ExcelHeader b = new ExcelHeader("严重性错误-B类", "b", Integer.class);
    ExcelHeader c = new ExcelHeader("一般性错误-C类", "c", Integer.class);
    ExcelHeader d = new ExcelHeader("警告性错误-D类", "d", Integer.class);
    ExcelHeader e = new ExcelHeader("建议性错误-E类", "e", Integer.class);
    ExcelHeader projectId = new ExcelHeader("系统id", "projectId", String.class);
    ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", Date.class);
    ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

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

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

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

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

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

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

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

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

  @Override
  public List<BugGradeStatisticsDetailDto> listQuery(ChartDataGeneQueryDto chartDataGeneQueryDto) {
    List<BugGradeStatisticsDetailDto> page = this.bugGradeStatisticsService.listQuery(chartDataGeneQueryDto);

    if(null!=page&&page.size()!=0){
      page.forEach(bugGradeStatisticsAddDto->{
        if(null!=bugGradeStatisticsAddDto.getReceiverName()){
          bugGradeStatisticsAddDto.setReceiverName(entityNameCache.translateById(EntityNameCatalog.MONSTER_USER,bugGradeStatisticsAddDto.getReceiverName()));
        }
        bugGradeStatisticsAddDto.setLoginNo(entityNameCache.translateById(EntityNameCatalog.WITH_LOGIN_NO,bugGradeStatisticsAddDto.getLoginNo()));
      });
    }
    return page;
  }

}
