package com.fs.app.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.store.domain.FsStoreProduct;
import com.fs.store.domain.FsStoreProductAttrValue;
import com.fs.store.domain.FsStoreProductPackage;
import com.fs.store.dto.StoreOrderProductDTO;
import com.fs.store.dto.StorePackageProductDTO;
import com.fs.store.param.FsStoreProductPackageQueryParam;
import com.fs.store.service.IFsStoreProductAttrValueService;
import com.fs.store.service.IFsStoreProductPackageService;
import com.fs.store.service.IFsStoreProductService;
import com.fs.store.vo.FsStoreProductPacketVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Api("套餐")
@RestController
@RequestMapping(value="/app/storeProductPackage")
public class StoreProductPackageController extends  AppBaseController {

    @Autowired
    private IFsStoreProductPackageService productPackageService;
    @Autowired
    private IFsStoreProductAttrValueService attrValueService;
    @Autowired
    private IFsStoreProductService storeProductService;
    @Login
    @ApiOperation("获取套餐列表")
    @GetMapping("/getStoreProductPackage")
    public R getStoreProductPackage(FsStoreProductPackageQueryParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<FsStoreProductPacketVO> list=productPackageService.selectFsStoreProductPackageListQueryVO(param);
        PageInfo<FsStoreProductPacketVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("获取套餐详情")
    @GetMapping("/getStoreProductPackageDetails")
    public R getStoreProductPackageDetails(@RequestParam("packageId") Long packageId, HttpServletRequest request){
        FsStoreProductPackage storeProductPackage=productPackageService.selectFsStoreProductPackageById(packageId);
        List<StoreOrderProductDTO> productList=new ArrayList<>();
        JSONArray jsonArray= JSONUtil.parseArray(storeProductPackage.getProducts());
        List<StorePackageProductDTO> goodsList=JSONUtil.toList(jsonArray, StorePackageProductDTO.class);
        for(StorePackageProductDTO dto:goodsList){
            StoreOrderProductDTO productDTO=new StoreOrderProductDTO();
            FsStoreProductAttrValue attrValue=attrValueService.selectFsStoreProductAttrValueById(dto.getId());
            if(attrValue!=null){
                FsStoreProduct product=storeProductService.selectFsStoreProductById(attrValue.getProductId());
                if(product!=null){
                    productDTO.setProductId(attrValue.getProductId());
                    productDTO.setId(dto.getId());
                    productDTO.setBarCode(attrValue.getBarCode());
                    productDTO.setPrice(attrValue.getPrice());
                    productDTO.setCount(dto.getCount());
                    productDTO.setSku(attrValue.getSku());
                    productDTO.setImage(attrValue.getImage());
                    productDTO.setProductName(product.getProductName());
                    productList.add(productDTO);
                }

            }

        }
        storeProductPackage.setProductList(productList);
        return R.ok().put("data",storeProductPackage);
    }

}
