package com.yiqi.api.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.StatusType;
import com.yiqi.core.domain.ClothType;
import com.yiqi.core.domain.vo.ClothItemPriceVO;
import com.yiqi.core.domain.vo.ClothTypePriceVO;
import com.yiqi.core.service.IClothItemService;
import com.yiqi.core.service.IClothTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiaohei
 * @date 2023/10/4
 */
@Api(tags = "衣服")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/cloth")
public class WeAppClothItemController extends BaseController {
    @Autowired
    private IClothTypeService clothTypeService;

    @Autowired
    private IClothItemService clothItemService;

    @ApiOperation("获取衣服价目表")
    @GetMapping(value = "getPrices")
    private R<List<ClothTypePriceVO>> getPrices(@RequestParam Long storeId) throws Exception{
        List<ClothType> clothTypes = clothTypeService.list(new QueryWrapper<ClothType>().lambda()
                .eq(ClothType::getDelFlag, DeleteStatus.OK.getCode())
                .eq(ClothType::getStatus, StatusType.OK.getCode()));

        Map<Long, List<ClothItemPriceVO>> priceMap = clothItemService.selectClothItemPriceList(storeId)
                .stream().collect(Collectors.groupingBy(ClothItemPriceVO::getTypeId));
        List<ClothTypePriceVO> clothTypePriceVOS = new ArrayList<>();
        for (ClothType clothType : clothTypes) {
            ClothTypePriceVO clothTypePriceVO = new ClothTypePriceVO();
            clothTypePriceVO.setId(clothType.getId());
            clothTypePriceVO.setName(clothType.getName());
            clothTypePriceVO.setClothItemPriceVOS(priceMap.get(clothType.getId()));
            clothTypePriceVOS.add(clothTypePriceVO);
        }
        return R.ok(clothTypePriceVOS);
    }
}
