package com.red.circle.other.infra.database.mongo.service.sys.impl;


import com.red.circle.other.infra.database.mongo.entity.sys.CustomerServiceConfig;
import com.red.circle.other.infra.database.mongo.service.sys.CustomerServiceConfigService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.ZonedId;
import com.red.circle.tool.core.text.StringUtils;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * 客服配置.
 *
 * @author pengshigang on 2022/10/08
 */
@Service
@RequiredArgsConstructor
public class CustomerServiceConfigServiceImpl implements CustomerServiceConfigService {

  private final MongoTemplate mongoTemplate;

  @Override
  public void add(CustomerServiceConfig config) {

    config.setTimeSlot("," + config.getTimeSlot() + ",");
    config.setRegions("," + config.getRegions() + ",");
    mongoTemplate.save(config);
  }

  @Override
  public void updateById(CustomerServiceConfig config) {

    if (Objects.isNull(config.getId())) {
      return;
    }

    Update update = new Update().set("updateTime", config.getUpdateTime());

    if (Objects.nonNull(config.getUserId())) {
      update.set("userId", config.getUserId());
    }
    if (StringUtils.isNotBlank(config.getTimeSlot())) {
      update.set("timeSlot", "," + config.getTimeSlot() + ",");
    }
    if (StringUtils.isNotBlank(config.getRegions())) {
      update.set("regions", "," + config.getRegions() + ",");
    }
    if (StringUtils.isNotBlank(config.getLanguage())) {
      update.set("language", config.getLanguage());
    }
    mongoTemplate.updateFirst(
        Query.query(Criteria.where("id").is(config.getId())),
        update, CustomerServiceConfig.class);
  }

  @Override
  public void deleteById(Long id) {

    mongoTemplate.remove(Query.query(Criteria.where("id").is(id)), CustomerServiceConfig.class);
  }

  @Override
  public List<CustomerServiceConfig> list(String sysOrigin, String region) {

    Criteria criteria = Criteria.where("sysOrigin").is(sysOrigin);

    if (StringUtils.isNotBlank(region)) {
      criteria.and("regions").regex(".*?" + region + ".*");
    }
    List<CustomerServiceConfig> list = mongoTemplate.find(Query.query(criteria)
            .with(Sort.by(Sort.Order.desc("createTime")))
        , CustomerServiceConfig.class);

    if (CollectionUtils.isNotEmpty(list)) {
      list.forEach(config -> config
          .setTimeSlot(config.getTimeSlot().substring(1, config.getTimeSlot().length() - 1)));
    }

    return list;

  }

  @Override
  public CustomerServiceConfig getByTimeSlot(String sysOrigin, String region) {

    // 获得当前上海时间段; 如: ,13,

    String hour = "," + ZonedDateTime.now(ZonedId.ASIA_SHANGHAI.getZonedId()).getHour() + ",";

    Criteria criteria = Criteria.where("sysOrigin").is(sysOrigin);

    if (StringUtils.isNotBlank(region)) {
      criteria.and("regions").regex(".*?" + region + ".*");
    }

    criteria.and("timeSlot").regex(".*?" + hour + ".*", "i");

    List<CustomerServiceConfig> configList = mongoTemplate.find(Query.query(criteria)
            .with(Sort.by(Sort.Order.desc("createTime")))
        , CustomerServiceConfig.class);

//    if (CollectionUtils.isEmpty(configList)) {
//      // 查询通用语言
//      configList = mongoTemplate.find(Query.query(Criteria.where("sysOrigin").is(sysOrigin)
//              .and("language").is("COMMON")
//              .and("timeSlot").regex(".*?" + hour + ".*", "i"))
//              .with(Sort.by(Sort.Order.desc("createTime")))
//          , CustomerServiceConfig.class);
//
//    }

    if (CollectionUtils.isEmpty(configList)) {
      return null;
    }

    return configList.get(0);

  }
}
