/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
 *
 */
package net.northking.cctp.attachment.db.impl;

import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.exception.InvalidException;
import net.northking.cctp.attachment.db.dao.AttachmentDao;
import net.northking.cctp.attachment.db.entity.Attachment;
import net.northking.cctp.attachment.db.service.AttachmentService;
import net.northking.cctp.common.db.BasicDao;
import net.northking.cctp.common.saas.db.TenantPaginationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 附件
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2022-03-08 10:31:06  <br>
 *
 * @author: maven-cctp-plugin  <br>
 * @since: 1.0 <br>
 */
@Service
@ConditionalOnMissingBean(name = "AttachmentServiceImpl")
public class AttachmentServiceImpl extends TenantPaginationService<Attachment> implements AttachmentService
{
  private static final Logger logger = LoggerFactory.getLogger(AttachmentServiceImpl.class);

  private final AttachmentDao attachmentDao;

  public AttachmentServiceImpl(AttachmentDao attachmentDao)
  {
    this.attachmentDao = attachmentDao;
  }

  public BasicDao<Attachment> getDao()
  {
    return attachmentDao;
  }

  public Attachment createEntity()
  {
    return new Attachment();
  }




  private static final Map<String, String> EXIST_TABLES = new HashMap<>();

  @Override
  public boolean existTable(String tenantId)
  {
    if (EXIST_TABLES.get(tenantId) != null)
    {
      return true;
    }
    String tableName = "attachment_" + tenantId;
    int exist = attachmentDao.existTable(tableName);

    return exist > 0;
  }

  @Override
  public void createNewTable(String tenantId)
  {
    attachmentDao.createNewTable(tenantId);
  }

  @Override
  public int insert(Attachment record)
  {
    return insertAttachment(record, false);
  }

  private int insertAttachment(Attachment record, boolean isAnonymous)
  {
    String tenantId = record.getTenantId();
    if (!existTable(tenantId))
    {
      createNewTable(tenantId);
    }
    EXIST_TABLES.put(tenantId, "");
    if (isAnonymous)
    {
      return attachmentDao.insert(record);
    } else
    {
      return super.insert(record);
    }
  }

  @Override
  public int insertByBus(Attachment record)
  {
    return insertAttachment(record, true);
  }

  @Override
  public void updateAllNewFile(Attachment update) {
    attachmentDao.updateAllNewFile(update);
  }

    @Override
    public List<Attachment> queryClearRecordScreenshot(List<String> businessCodes, LocalDate date) {
      String tableSuffix = getDefaultTableSuffix();
      return attachmentDao.queryClearRecordScreenshot(businessCodes, date, tableSuffix);
    }

    @Override
    public void updateByObjIds(List<String> objectIds) {
      if (CollectionUtils.isEmpty(objectIds)) {
        throw new InvalidException("关联对象Id集合不能为空");
      }
      String tableSuffix = getDefaultTableSuffix();
      attachmentDao.updateByObjIds(objectIds, tableSuffix);
    }


  @Override
  public List<Attachment> queryByObjId(String objId, LocalDate date) {
    String tableSuffix = getDefaultTableSuffix();
    return attachmentDao.queryByObjId(objId, date, tableSuffix);
  }

  @Override
  public List<Attachment> findByPrimaryKeys(List<String> ids, String tenantId) {
    String tableSuffix = getTableSuffix(tenantId);
    return attachmentDao.findByPrimaryKeys(ids, tableSuffix);
  }

  @Override
  public void clearSceneTaskAttachments(List<String> deleteIds) {
    attachmentDao.clearAttachmentsByIds(deleteIds, getDefaultTableSuffix());
  }

  @Override
  public void associatedFiles(String objectId, List<String> fileIds) {
    String tableSuffix = getDefaultTableSuffix();
    attachmentDao.associatedFiles(objectId, fileIds, tableSuffix);
  }

  @Override
  public List<Attachment> queryByOverTime(LocalDateTime overTime) {
    String tableSuffix = getDefaultTableSuffix();
    return attachmentDao.queryByOverTime(overTime, tableSuffix);
  }

    @Override
    public List<Attachment> queryByBusiness(LocalDate localDate) {
      String tableSuffix = getDefaultTableSuffix();
      return attachmentDao.queryByBusiness(localDate, tableSuffix);
    }

    @Override
  public Attachment findByPK(String tenantId, String pkId)
  {
    Attachment attachment = new Attachment();
    attachment.setTenantId(tenantId);
    attachment.setId(pkId);
    return attachmentDao.findByPK(attachment);
  }

  @Override
  public Attachment queryByStoragePath(String tenantId, String storagePath)
  {
    Attachment attachment = new Attachment();
    attachment.setTenantId(tenantId);
    attachment.setStoragePath(storagePath);
    return attachmentDao.queryByStoragePath(attachment);
  }


}

