package cn.iocoder.yudao.module.member.controller.app.storeshoppingcart;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.storeshoppingcart.vo.StoreShoppingCartPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storeshoppingcart.vo.StoreShoppingCartRespVO;
import cn.iocoder.yudao.module.member.controller.admin.storeshoppingcart.vo.StoreShoppingCartSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoods.StoreGoodsDO;
import cn.iocoder.yudao.module.member.dal.dataobject.storeshoppingcart.StoreShoppingCartDO;
import cn.iocoder.yudao.module.member.service.storegoods.StoreGoodsService;
import cn.iocoder.yudao.module.member.service.storeshoppingcart.StoreShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
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 javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP超市 - 小程序商品购物车")
@RestController
@RequestMapping("/member/store-shopping-cart")
@Validated
public class AppStoreShoppingCartController {

    @Resource
    private StoreShoppingCartService storeShoppingCartService;

    @Resource
    private StoreGoodsService storeGoodsService;

    @PostMapping("/create")
    @Operation(summary = "创建小程序商品购物车")
    public CommonResult<Long> createStoreShoppingCart(@Valid @RequestBody StoreShoppingCartSaveReqVO createReqVO) {
        return success(storeShoppingCartService.createStoreShoppingCart(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序商品购物车")
    public CommonResult<Boolean> updateStoreShoppingCart(@Valid @RequestBody StoreShoppingCartSaveReqVO updateReqVO) {
        storeShoppingCartService.updateStoreShoppingCart(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序商品购物车")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteStoreShoppingCart(@RequestParam("id") Long id) {
        storeShoppingCartService.deleteStoreShoppingCart(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序商品购物车")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<StoreShoppingCartRespVO> getStoreShoppingCart(@RequestParam("id") Long id) {
        StoreShoppingCartDO storeShoppingCart = storeShoppingCartService.getStoreShoppingCart(id);
        return success(BeanUtils.toBean(storeShoppingCart, StoreShoppingCartRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小程序商品购物车分页")
    public CommonResult<PageResult<StoreShoppingCartRespVO>> getStoreShoppingCartPage(@Valid StoreShoppingCartPageReqVO pageReqVO) {
        PageResult<StoreShoppingCartDO> pageResult = storeShoppingCartService.getStoreShoppingCartPage(pageReqVO);
        PageResult<StoreShoppingCartRespVO> bean = BeanUtils.toBean(pageResult, StoreShoppingCartRespVO.class);
        List<StoreShoppingCartRespVO> list = bean.getList();
        List<Integer> goodsIds = list.stream().map(StoreShoppingCartRespVO::getGoodsId).collect(Collectors.toList());
        List<StoreGoodsDO> all = storeGoodsService.getAll(goodsIds);
        Map<Integer, StoreGoodsDO> map = all.stream().collect(Collectors.toMap(StoreGoodsDO::getGoodsId, a -> a));
        for (StoreShoppingCartRespVO vo : list) {
            vo.setGoodsInfo(map.get(vo.getGoodsId()));
        }
        return success(bean);
    }

    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除小程序商品购物车")
    public CommonResult<Boolean> deleteStoreShoppingCart(List<Long> ids) {
        storeShoppingCartService.deleteBatch(ids);
        return success(true);
    }

}