package com.palmnest.service.dinner.manager.impl;

import com.palmnest.dao.dinner.OrderDinnerDao;
import com.palmnest.dao.dinner.OrderDinnerSetDao;
import com.palmnest.model.dinner.OrderDinnerSet;
import com.palmnest.service.dinner.manager.OrderDinnerSetManager;
import com.palmnest.service.impl.GenericManagerImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("orderDinnerSetManager")
public class OrderDinnerSetManagerImpl extends GenericManagerImpl<OrderDinnerSet, Integer> implements OrderDinnerSetManager {
    private OrderDinnerSetDao orderDinnerSetDao;

    @Autowired
    public void setOrderDinnerSetDao(OrderDinnerSetDao orderDinnerSetDao) {
        this.orderDinnerSetDao = orderDinnerSetDao;
        this.dao = orderDinnerSetDao;
    }

    @Override
    public OrderDinnerSet findByOrgId(Integer orgId) {
        return orderDinnerSetDao.findByOrgId(orgId);
    }

    @Override
    public OrderDinnerSet findByUserId(Integer userId) {
        return orderDinnerSetDao.findByUserId(userId);
    }

    @Override
    public String getOrgListByOrgIds(String orgIds) {
        return null;
    }

    @Override
    public Map<Integer, String> findUserByOrgIds(String orgIds,String openId) {
        List<Map<String, Object>> list = orderDinnerSetDao.findUserByOrgIds(orgIds,openId);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        Map<Integer, String> map = new HashMap<>();
        for(int i = 0 ; i < list.size() ; i++){
            Map<String, Object> resMap = list.get(i);
            Integer orgId = Integer.parseInt(resMap.get("orgId").toString());
            Integer userId = Integer.parseInt(resMap.get("userId").toString());
            if(map.containsKey(orgId)){
                String userIds = map.get(orgId);
                userIds = userIds +"," + userId;
                map.put(orgId,userIds);
            }else{
                map.put(orgId, userId.toString());
            }
        }
        return map;
    }
}
