package com.starry.admin.modules.weichat.controller;


import com.starry.admin.modules.clerk.module.entity.PlayClerkCommodityEntity;
import com.starry.admin.modules.clerk.service.IPlayClerkCommodityService;
import com.starry.admin.modules.weichat.entity.PlayClerkCommodityTreeData;
import com.starry.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Slf4j
@RestController
@RequestMapping("/wx/commodity/")
public class WxCommodityController {
    @Resource
    private IPlayClerkCommodityService clerkCommodityService;

    @GetMapping("/getTree")
    public R getTree(@RequestParam("userId") String userId) {
        List<PlayClerkCommodityEntity> commodityEntities = clerkCommodityService.selectByUser(userId);
        Map<String, List<PlayClerkCommodityEntity>> item = commodityEntities.stream().collect(Collectors.groupingBy(PlayClerkCommodityEntity::getCommodityName));
        List<PlayClerkCommodityTreeData> result = new ArrayList<>();
        for (Map.Entry<String, List<PlayClerkCommodityEntity>> entry : item.entrySet()) {

            PlayClerkCommodityTreeData treeData = new PlayClerkCommodityTreeData(entry.getKey(),entry.getValue());
            result.add(treeData);
        }
        return R.ok(result);
    }
}
