package com.ruoyi.ss.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.device.domain.DeviceQuery;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.dividendDetail.domain.DividendDetailQuery;
import com.ruoyi.ss.dividendDetail.service.IDividendDetailService;
import com.ruoyi.ss.order.domain.OrderQuery;
import com.ruoyi.ss.order.service.IOrderService;
import com.ruoyi.ss.room.domain.RoomQuery;
import com.ruoyi.ss.room.service.IRoomService;
import com.ruoyi.ss.store.service.IStoreService;
import com.ruoyi.ss.user.domain.UserVO;
import com.ruoyi.ss.user.service.IUserAssembler;
import com.ruoyi.ss.user.service.IUserService;
import com.ruoyi.ss.withdraw.service.IWithdrawService;
import com.ruoyi.system.domain.vo.InCome;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import static com.ruoyi.common.constant.ServiceConstants.*;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class UserAssemblerImpl implements IUserAssembler {

    @Autowired
    private IStoreService storeService;

    @Autowired
    private IWithdrawService withdrawService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IDividendDetailService dividendDetailService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IUserService userService;

    /**
     * 拼接店铺数
     * @param list 用户列表
     */
    @Override
    public void assemblerStoreNum(List<UserVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }
        list.forEach(user -> {
            user.setStoreNum(storeService.selectCount(user.getUserId()));
        });
    }

    @Override
    public void assembleStatisticalData(UserVO user, LocalDate startDate, LocalDate endDate) {

        Long userId = user.getUserId();
        /** 店铺数*/
        user.setStoreNum(storeService.selectCount(userId));

        /** 房间数*/
        RoomQuery query = new RoomQuery();
        query.setMerchantId(userId);
        query.setType2(ROOM_TYPE2_PRIVATE_ROOM);
        user.setRoomNum(roomService.getCount(query));
        /** 设施数*/
        query.setType2(ROOM_TYPE2_LOBBY_DEVICE);
        user.setEquNum(roomService.getCount(query));

        /** 设备数*/
        DeviceQuery query1 = new DeviceQuery();
        query1.setMerchantId(userId);
        int i = deviceService.selectCount(query1);
        user.setDeviceNum(i);
        /** 总收入*/
        DividendDetailQuery dividendDetailQuery = new DividendDetailQuery();
        dividendDetailQuery.setStatus(DIVIDEND_STATUS_PAID);
        dividendDetailQuery.setPartnerId(userId);
        BigDecimal income = dividendDetailService.getIncome(dividendDetailQuery);
        user.setTotalIncome(income);

        /** 总消费*/
        OrderQuery orderQuery = new OrderQuery();
        orderQuery.setUserId(userId);
        BigDecimal orderFee = orderService.getOrderFee(orderQuery);
        user.setTotalConsume(orderFee);

        /** 总提现*/
        BigDecimal totalWithdrawAmount = withdrawService.cumulativeWithdrawalAmount(userId);
        if(ObjectUtil.isNull(totalWithdrawAmount)){
            totalWithdrawAmount = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
        }
        user.setTotalWithdrawAmount(totalWithdrawAmount);

        /** 可提现金额*/
        BigDecimal withdrawableAmount = userService.getWithdrawAbleAmount(user);
        user.setWithdrawalAmount(withdrawableAmount);

        // 日报表
        getDailyVoList(user, startDate, endDate);

    }

    private void getDailyVoList(UserVO user, LocalDate startDate, LocalDate endDate) {
        DividendDetailQuery query = new DividendDetailQuery();
        query.setStatus(DIVIDEND_STATUS_PAID);
        query.setPartnerId(user.getUserId());
        query.setStartTime(startDate);
        query.setEndTime(endDate);
        List<InCome> inComes = dividendDetailService.selectIncome(query);
        if (CollectionUtils.isEmptyElement(inComes)) {
            return;
        }
        if (startDate != null && endDate!= null) {
            inComes = CollectionUtils.fillVoids(inComes, InCome::getDay, (date) -> {
                InCome co = new InCome();
                co.setDay(date);
                co.setOrderIncome(BigDecimal.ZERO);
                co.setOrderNum(0);
                return co;
            }, startDate, endDate);
        }
        user.setInComeList(inComes);
    }


}
