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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
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;

/**
 * @author admin
 */
@Api(tags = "微信商品接口", description = "微信端商品相关接口")
@Slf4j
@RestController
@RequestMapping("/wx/commodity/")
public class WxCommodityController {
    @Resource
    private IPlayClerkCommodityService clerkCommodityService;

    @ApiOperation(value = "获取商品树", notes = "根据用户ID获取商品树形结构")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkCommodityTreeData.class, responseContainer = "List")})
    @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);
    }
}
