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

import net.northking.cctp.dw.dto.atu.*;
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.*;
import java.util.stream.Collectors;

/**
 * 业务组件发布同步表服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

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

  @Autowired
  private DwReBusComponentService dwReBusComponentService;
  @Autowired
  private EntityNameCache entityNameCache;

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

  @Override
  public ExcelHeader[] excelHeaders()
  {
    ExcelHeader id = new ExcelHeader("", "id", String.class);
    ExcelHeader comName = new ExcelHeader("", "comName", String.class);
    ExcelHeader comType = new ExcelHeader("", "comType", Integer.class);
    ExcelHeader tenantId = new ExcelHeader("", "tenantId", String.class);
    ExcelHeader projectId = new ExcelHeader("", "projectId", String.class);
    ExcelHeader createdBy = new ExcelHeader("", "createdBy", String.class);
    ExcelHeader createdTime = new ExcelHeader("", "createdTime", Date.class);
    ExcelHeader isDeleted = new ExcelHeader("", "isDeleted", Integer.class);
    // TODO 定义表头及顺序
    return new ExcelHeader[]{};
  }

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

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

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

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

  @Override
  @DatabaseWriteOnly
  public String add(DwReBusComponent dto)
  {
    String uuid = dto.getId();
    this.dwReBusComponentService.insert(dto);
    return uuid;
  }

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

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

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

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

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

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

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

  @Override
  public DwAtuDetailTmpDto countByGroup(DwReBusComponentQueryDto query) {
    DwAtuDetailTmpDto dwAtuDetailTmpDto=new DwAtuDetailTmpDto();
    List<DwReBusComponentDetailDto> dwReBusComponentDetailDtos = dwReBusComponentService.countByGroup(query);
    //翻译中文名
    for (DwReBusComponentDetailDto dwReBusComponentDetailDto: dwReBusComponentDetailDtos) {
      dwReBusComponentDetailDto.setCreatedBy(entityNameCache.translateById(EntityNameCatalog.MONSTER_USER,dwReBusComponentDetailDto.getCreatedBy()));
    }
    //获取类型
    if (dwReBusComponentDetailDtos.size()!=0){
      List<Integer> collect = dwReBusComponentDetailDtos.stream().map(DwReBusComponentDetailDto::getType).distinct().sorted().collect(Collectors.toList());
      //封装对象
      List<String> createdBys = dwReBusComponentDetailDtos.stream().map(DwReBusComponentDetailDto::getCreatedBy).distinct().collect(Collectors.toList());
      Map<String, List<DwReBusComponentDetailDto>> collect1 = dwReBusComponentDetailDtos.stream().collect(Collectors.groupingBy(DwReBusComponentDetailDto::getCreatedBy));
      for (String createdBy:createdBys){
        List<DwReBusComponentDetailDto> dwAtuCaseDesignDetailDtos1 = collect1.get(createdBy);
        List<Integer> collect2 = dwAtuCaseDesignDetailDtos1.stream().map(DwReBusComponentDetailDto::getType).collect(Collectors.toList());
        List<Integer> integers = collect.stream().filter(c -> !collect2.contains(c)).collect(Collectors.toList());
        if (integers.size()!=0){
          DwReBusComponentDetailDto dwAtuCaseDesignDetailDto=null;
          for (Integer type:integers){
            dwAtuCaseDesignDetailDto=new DwReBusComponentDetailDto();
            dwAtuCaseDesignDetailDto.setCreatedBy(createdBy);
            dwAtuCaseDesignDetailDto.setType(type);
            dwAtuCaseDesignDetailDto.setMount(0);
            dwReBusComponentDetailDtos.add(dwAtuCaseDesignDetailDto);
          }
        }
      }
      dwAtuDetailTmpDto.setDwReBusComponentDetailDtos(dwReBusComponentDetailDtos.stream().sorted(Comparator.comparing(DwReBusComponentDetailDto::getType)).collect(Collectors.toList()));
      dwAtuDetailTmpDto.setType(collect);
    }
    return dwAtuDetailTmpDto;
  }

}
