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

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
//import com.ruoyi.dashboard.domain.vo.BillCountVo;
//import com.ruoyi.ss.device.domain.enums.DeviceServiceMode;
//import com.ruoyi.ss.device.domain.vo.DeviceVO;
//import com.ruoyi.ss.device.service.DeviceAssembler;
//import com.ruoyi.ss.device.service.DeviceValidator;
//import com.ruoyi.ss.deviceSuit.domain.DeviceSuitQuery;
//import com.ruoyi.ss.deviceSuit.domain.DeviceSuitVO;
//import com.ruoyi.ss.deviceSuit.service.DeviceSuitService;
//import com.ruoyi.ss.store.domain.StoreQuery;
//import com.ruoyi.ss.store.domain.StoreVo;
//import com.ruoyi.ss.store.service.StoreService;
//import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
//import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillGroupBy;
//import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus;
//import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillType;
//import com.ruoyi.ss.transactionBill.service.TransactionBillService;
//import com.ruoyi.system.domain.enums.config.ConfigKey;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceAssembler;
import com.ruoyi.ss.device.service.DeviceValidator;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.room.service.IRoomService;
import com.ruoyi.ss.store.domain.StoreVO;
import com.ruoyi.ss.store.service.IStoreService;
import com.ruoyi.ss.store.service.impl.StoreServiceImpl;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.ruoyi.common.constant.ServiceConstants.DEVICE_PLACEMENT_STATUS_PLACED;
import static com.ruoyi.common.constant.ServiceConstants.DEVICE_STATUS_PLACEMENT_NOT_PLACED;

/**
 * @author qzz
 * 2024/4/13
 */
@Service
public class DeviceAssemblerImpl implements DeviceAssembler {

    @Autowired
    private IRoomService roomService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceValidator deviceValidator;

    @Autowired
    private IEquipmentService equipmentService;

    @Autowired
    private IStoreService storeService;

    /**
     * 拼接套餐列表
     *
     * @param list
     */
    @Override
    public void assembleSuitList(List<DeviceVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }
//        DeviceSuitQuery query = new DeviceSuitQuery();
//        query.setDeviceIds(CollectionUtils.map(list, DeviceVO::getDeviceId));
//        Map<Long, List<DeviceSuitVO>> group = deviceSuitService.selectDeviceSuitList(query)
//                .stream().collect(Collectors.groupingBy(DeviceSuitVO::getDeviceId));
//
//        for (DeviceVO device : list) {
//            List<DeviceSuitVO> deviceSuitList = group.get(device.getDeviceId());
//            if (deviceSuitList != null) {
//                deviceSuitList.sort(Comparator.comparing(o -> o.getSuit().getSort()));
//                device.setSuitList(deviceSuitList.stream().map(DeviceSuitVO::getSuit).collect(Collectors.toList()));
//            } else {
//                device.setSuitList(Collections.emptyList());
//            }
//        }
    }

//    /**
//     * 拼接店铺营业时间
//     *
//     * @param list
//     */
//    @Override
//    public void assembleBusinessTime(List<DeviceVO> list) {
//        if (CollectionUtils.isEmptyElement(list)) {
//            return;
//        }
//        StoreQuery query = new StoreQuery();
//        query.setStoreIds(list.stream().map(DeviceVO::getStoreId).filter(Objects::nonNull).distinct().collect(Collectors.toList()));
//        Map<Long, StoreVo> map = storeService.selectMap(query, StoreVo::getStoreId);
//
//        for (DeviceVO device : list) {
//            StoreVo store = device.getStoreId() == null ? null : map.get(device.getStoreId());
//            if (store != null) {
//                device.setStoreBusinessTimeStart(store.getBusinessTimeStart());
//                device.setStoreBusinessTimeEnd(store.getBusinessTimeEnd());
//            }
//        }
//
//    }

//    /**
//     * 拼接订单统计信息：订单数量、订单金额
//     *
//     * @param list
//     */
//    @Override
//    public void assembleOrderCountInfo(List<DeviceVO> list) {
//        if (CollectionUtils.isEmptyElement(list)) {
//            return;
//        }
//
//        TransactionBillQuery query = new TransactionBillQuery();
//        query.setGroupBy(TransactionBillGroupBy.device_id.name());
//        query.setDeviceIds(list.stream().map(DeviceVO::getDeviceId).collect(Collectors.toList()));
//        query.setType(TransactionBillType.RECHARGE.getType());
//        query.setStatus(TransactionBillStatus.SUCCESS.getStatus());
//        Map<Long, BillCountVo> map = transactionBillService.selectCountMap(query, BillCountVo::getDeviceId);
//
//        for (DeviceVO device : list) {
//            BillCountVo count = map.get(device.getDeviceId());
//            device.setOrderCount(count == null ? 0 : count.getCount());
//            device.setOrderAmount(count == null ? BigDecimal.ZERO : count.getRecharge());
//        }
//    }

    @Override
    public void assembleRealServiceRate(DeviceVO device) {
        if (device == null) {
            return;
        }
        this.assembleRealServiceRate(Collections.singletonList(device));
    }

    /**
     * 拼接真实的服务费率
     *
     * @param list
     */
    @Override
    public void assembleRealServiceRate(List<DeviceVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }

//        // 系统默认配置
//        BigDecimal sysServiceRate = sysConfigService.getBigDecimal(ConfigKey.SERVICE_FEE_RATE);
//
//        for (DeviceVO device : list) {
//            if (device == null) {
//                continue;
//            }
//            if (device.getServiceRate() != null) {
//                device.setRealServiceRate(device.getServiceRate());
//            } else if (device.getUserServiceRate() != null) {
//                device.setRealServiceRate(device.getUserServiceRate());
//            } else {
//                ServiceUtil.assertion(sysServiceRate == null, "系统服务费率未配置，请联系管理员");
//                device.setRealServiceRate(sysServiceRate);
//            }
//        }

    }

//    /**
//     * 拼接投放状态
//     */
//    @Override
//    public void assemblePlacementStatus(List<DeviceVO> list) {
//        for (DeviceVO device : list){
//            List<Long> roomIds = equipmentService.selectRoomIdByDeviceId(device.getDeviceId());
//            device.setPlacementStatus(roomIds.isEmpty() ? DEVICE_STATUS_PLACEMENT_NOT_PLACED : DEVICE_PLACEMENT_STATUS_PLACED);
//        }
//    }

//    /**
//     * 拼接投放状态
//     */
//    @Override
//    public void assemblePlacementStatus(DeviceVO device) {
//        List<Long> roomIds = equipmentService.selectRoomIdByDeviceId(device.getDeviceId());
//        device.setPlacementStatus(roomIds.isEmpty() ? DEVICE_STATUS_PLACEMENT_NOT_PLACED : DEVICE_PLACEMENT_STATUS_PLACED);
//    }


    /**
     * 拼接正在使用中的订单数量
     */
    @Override
    public void assembleUsingBillCount(List<DeviceVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }

//        TransactionBillQuery query = new TransactionBillQuery();
//        query.setIsUsing(true);
//        query.setType(TransactionBillType.RECHARGE.getType());
//        query.setDeviceIds(CollectionUtils.map(list, DeviceVO::getDeviceId));
//        List<JSONObject> counts = transactionBillService.selectCountGroupBy(query, "stb.device_id");
//
//        for (DeviceVO device : list) {
//            JSONObject count = counts.stream()
//                    .filter(item -> Objects.equals(item.getLong("key"), device.getDeviceId()))
//                    .findFirst().orElse(null);
//            if (count != null && count.getInteger("count") != null) {
//                device.setUsingBillCount(count.getInteger("count"));
//            } else {
//                device.setUsingBillCount(0);
//            }
//        }
    }

    @Override
    public void assembleAllowSwitch(List<DeviceVO> list, Long userId) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }

//        for (DeviceVO device : list) {
//            // 直营模式
//            if (DeviceServiceMode.DIRECT.getMode().equals(device.getServiceMode())) {
//                // 是商户
//                if (deviceValidator.isMch(device, userId)) {
//                    device.setAllowSwitch(true);
//                    continue;
//                }
//            }
//            // 代理模式
//            else if (DeviceServiceMode.AGENT.getMode().equals(device.getServiceMode())) {
//                // 是代理
//                if (deviceValidator.isAgent(device, userId)) {
//                    device.setAllowSwitch(true);
//                    continue;
//                }
//                // 是商户，并且代理允许
//                if (deviceValidator.isMch(device, userId) && device.getAgentAllowMchSwitch()) {
//                    device.setAllowSwitch(true);
//                    continue;
//                }
//            }
//            device.setAllowSwitch(false);
//        }
    }

    /**
     * 拼接设施对象
     */
    @Override
    public void assembleEquipment(List<DeviceVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }
        for (DeviceVO device : list){
            EquipmentVO equipmentVO = equipmentService.selectByDeviceId(device.getDeviceId());
            device.setEqu(equipmentVO);
        }
    }

    /**
     * 拼接设施对象
     */
    @Override
    public void assembleEquipment(DeviceVO device) {
        if (device == null) {
            return;
        }
        EquipmentVO equipmentVO = equipmentService.selectByDeviceId(device.getDeviceId());
        if (equipmentVO == null) {
            return;
        }
        RoomVO roomVO = roomService.selectERoomByRoomId(equipmentVO.getRoomId());
        if(Objects.nonNull(roomVO)){
            StoreVO storeVO = storeService.selectEStoreByStoreId(roomVO.getStoreId());
            if(Objects.nonNull(storeVO)){
                device.setStoreName(storeVO.getName());
                device.setStoreId(storeVO.getStoreId());
            }
            device.setRoom(roomVO);
        }
        device.setEqu(equipmentVO);
    }
}
