package com.red.circle.other.infra.database.rds.service.family.impl;

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.other.infra.database.rds.dao.family.FamilyMemberTotalExpDAO;
import com.red.circle.other.infra.database.rds.entity.family.FamilyMemberTotalExp;
import com.red.circle.other.infra.database.rds.service.family.FamilyMemberTotalExpService;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service;

/**
 * 家族成员总榜 服务实现类
 */
@Service
public class FamilyMemberTotalExpServiceImpl extends
    BaseServiceImpl<FamilyMemberTotalExpDAO, FamilyMemberTotalExp> implements FamilyMemberTotalExpService {

  @Override
  public void incrMemberTotalExp(String sysOrigin, Long familyId, Long memberId, Long exp) {
    if (StringUtils.isBlank(sysOrigin) || Objects.isNull(familyId) || Objects.isNull(memberId) || exp <= 0) {
      return;
    }

    FamilyMemberTotalExp existsMember = query()
        .select(FamilyMemberTotalExp::getId)
        .eq(FamilyMemberTotalExp::getFamilyId, familyId)
        .eq(FamilyMemberTotalExp::getFamilyMemberId, memberId)
        .eq(FamilyMemberTotalExp::getSysOrigin, sysOrigin)
        .last(PageConstant.LIMIT_ONE)
        .getOne();

    if (Objects.isNull(existsMember) || Objects.isNull(existsMember.getId())) {
      try {
        super.save(new FamilyMemberTotalExp()
            .setSysOrigin(sysOrigin)
            .setFamilyId(familyId)
            .setFamilyMemberId(memberId)
            .setExp(exp)
        );
      } catch (Exception e) {
        update()
            .setSql("exp=exp+" + exp)
            .eq(FamilyMemberTotalExp::getFamilyId, familyId)
            .eq(FamilyMemberTotalExp::getFamilyMemberId, memberId)
            .eq(FamilyMemberTotalExp::getSysOrigin, sysOrigin)
            .execute();
      }
      return;
    }

    update()
        .setSql("exp=exp+" + exp)
        .eq(FamilyMemberTotalExp::getId, existsMember.getId())
        .execute();
  }

  @Override
  public List<FamilyMemberTotalExp> listMemberTotalByFamilyId(Long familyId) {
    return Optional.ofNullable(
            query()
                .eq(FamilyMemberTotalExp::getFamilyId, familyId)
                .orderByDesc(FamilyMemberTotalExp::getExp)
                .list())
        .orElse(CollectionUtils.newArrayList());
  }

  @Override
  public FamilyMemberTotalExp getMemberTotalExp(Long familyId, Long memberId) {
    return query()
        .eq(FamilyMemberTotalExp::getFamilyId, familyId)
        .eq(FamilyMemberTotalExp::getFamilyMemberId, memberId)
        .last(PageConstant.LIMIT_ONE)
        .getOne();
  }

  @Override
  public void deleteByFamilyId(Long familyId) {
    if (Objects.isNull(familyId)) {
      return;
    }
    delete().eq(FamilyMemberTotalExp::getFamilyId, familyId).execute();
  }
  
  @Override
  public void deleteByMemberId(Long familyId, Long memberId) {
    if (Objects.isNull(familyId) || Objects.isNull(memberId)) {
      return;
    }
    delete()
        .eq(FamilyMemberTotalExp::getFamilyId, familyId)
        .eq(FamilyMemberTotalExp::getFamilyMemberId, memberId)
        .execute();
  }
}
