package com.red.circle.wallet.app.command.freight.query;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import com.red.circle.wallet.app.dto.clientobject.FreightSellerCO;
import com.red.circle.wallet.app.dto.cmd.FreightSellerQryCmd;
import com.red.circle.wallet.infra.database.rds.entity.freight.FreightSeller;
import com.red.circle.wallet.infra.database.rds.service.freight.FreightSellerService;
import java.math.RoundingMode;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 经销商卖家列表查询.
 *
 * @author zongpubin on 2023/7/18
 */
@Service
@RequiredArgsConstructor
public class UserFreightSellerInfoQryExe {

  private final UserProfileClient userProfileClient;
  private final FreightSellerService freightSellerService;

  public FreightSellerCO execute(FreightSellerQryCmd cmd) {

    FreightSeller freightSeller = freightSellerService.getById(cmd.getSellerId());
    UserProfileDTO userProfile = ResponseAssert.requiredSuccess(
        userProfileClient.getByUserId(freightSeller.getUserId())
    );

    if (Objects.nonNull(userProfile.getOwnSpecialId())) {
      userProfile.setAccount(userProfile.getOwnSpecialId().getAccount());
    }

    return new FreightSellerCO()
        .setId(freightSeller.getId())
        .setUserProfile(userProfile)
        .setEarnGold(freightSeller.getEarnGold())
        .setBalance(freightSeller.getEarnGold().subtract(freightSeller.getConsumptionGold())
            .setScale(2, RoundingMode.DOWN))
        .setCreateTime(freightSeller.getCreateTime());
  }
}

