package com.starry.admin.modules.statistics.service.impl;

import com.starry.admin.modules.clerk.module.entity.PlayClerkLevelInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.personnel.module.entity.PlayPersonnelGroupInfoEntity;
import com.starry.admin.modules.statistics.module.vo.PlayClerkPerformanceInfoReturnVo;
import com.starry.admin.modules.statistics.service.IPlayClerkPerformanceService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author admin
 * @since 2024/6/15 下午3:29
 **/
@Service
public class PlayClerkPerformanceServiceImpl implements IPlayClerkPerformanceService {

    @Override
    public PlayClerkPerformanceInfoReturnVo getClerkPerformanceInfo(PlayClerkUserInfoEntity userInfo, List<PlayOrderInfoEntity> orderInfoEntities, List<PlayClerkLevelInfoEntity> clerkLevelInfoEntities, List<PlayPersonnelGroupInfoEntity> groupInfoEntities) {

        Set<String> customIds = new HashSet<>();
        int orderContinueNumber = 0;
        int orderRefundNumber = 0;
        int ordersExpiredNumber = 0;
        BigDecimal finalAmount = BigDecimal.ZERO;
        BigDecimal orderFirstAmount = BigDecimal.ZERO;
        BigDecimal orderTotalAmount = BigDecimal.ZERO;
        BigDecimal orderRewardAmount = BigDecimal.ZERO;
        BigDecimal orderRefundAmount = BigDecimal.ZERO;
        BigDecimal estimatedRevenue = BigDecimal.ZERO;
        for (PlayOrderInfoEntity orderInfoEntity : orderInfoEntities) {
            customIds.add(orderInfoEntity.getPurchaserBy());
            finalAmount = finalAmount.add(orderInfoEntity.getFinalAmount());
            if ("1".equals(orderInfoEntity.getFirstOrder())) {
                orderFirstAmount = orderFirstAmount.add(orderInfoEntity.getFinalAmount());
            } else {
                orderContinueNumber++;
                orderTotalAmount = orderTotalAmount.add(orderInfoEntity.getFinalAmount());
            }
            if ("2".equals(orderInfoEntity.getPlaceType())) {
                orderRewardAmount = orderRewardAmount.add(orderInfoEntity.getFinalAmount());
            }
            if ("1".equals(orderInfoEntity.getRefundType())) {
                orderRefundNumber++;
                orderRefundAmount = orderRefundAmount.add(orderInfoEntity.getRefundAmount());
            }
            if ("1".equals(orderInfoEntity.getOrdersExpiredState())) {
                ordersExpiredNumber++;
            }
        }
        PlayClerkPerformanceInfoReturnVo returnVo = new PlayClerkPerformanceInfoReturnVo();
        returnVo.setClerkId(userInfo.getId());
        returnVo.setClerkNickname(userInfo.getNickname());
        returnVo.setClerkSex(userInfo.getSex());
        for (PlayClerkLevelInfoEntity infoEntity : clerkLevelInfoEntities) {
            if (infoEntity.getId().equals(userInfo.getLevelId())) {
                returnVo.setLevelName(infoEntity.getName());
            }
        }
        for (PlayPersonnelGroupInfoEntity infoEntity : groupInfoEntities) {
            if (infoEntity.getId().equals(userInfo.getGroupId())) {
                returnVo.setGroupName(infoEntity.getGroupName());
            }
        }
        returnVo.setOrderNumber(orderInfoEntities.size());
        returnVo.setOrderContinueNumber(orderContinueNumber);
        returnVo.setOrderRefundNumber(orderRefundNumber);
        returnVo.setOrdersExpiredNumber(ordersExpiredNumber);
        returnVo.setFinalAmount(finalAmount);
        returnVo.setOrderFirstAmount(orderFirstAmount);
        returnVo.setOrderTotalAmount(orderTotalAmount);
        returnVo.setOrderRewardAmount(orderRewardAmount);
        returnVo.setOrderRefundAmount(orderRefundAmount);
        returnVo.setCustomNumber(customIds.size());
        returnVo.setEstimatedRevenue(estimatedRevenue);

        return returnVo;
    }
}
