package com.fs.app.controller.store;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.core.domain.R;
import com.fs.hisStore.domain.FsStoreProductAttrValueScrm;
import com.fs.hisStore.domain.FsStoreProductPackageScrm;
import com.fs.hisStore.domain.FsStoreProductScrm;
import com.fs.hisStore.dto.StoreOrderProductDTO;
import com.fs.hisStore.dto.StorePackageProductDTO;
import com.fs.hisStore.param.FsStoreProductPackageQueryParam;
import com.fs.hisStore.service.IFsStoreProductAttrValueScrmService;
import com.fs.hisStore.service.IFsStoreProductPackageScrmService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.hisStore.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="/store/app/storeProductPackage")
public class StoreProductPackageScrmController extends AppBaseController {

    @Autowired
    private IFsStoreProductPackageScrmService productPackageService;
    @Autowired
    private IFsStoreProductAttrValueScrmService attrValueService;
    @Autowired
    private IFsStoreProductScrmService storeProductService;
    @Login
    @ApiOperation("获取套餐列表")
    @GetMapping("/getStoreProductPackage")
    public R getStoreProductPackage(FsStoreProductPackageQueryParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        param.setStatus(1);
        List<FsStoreProductPacketVO> list=productPackageService.selectFsStoreProductPackageListQueryVO(param);
        PageInfo<FsStoreProductPacketVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @ApiOperation("获取套餐详情")
    @GetMapping("/getStoreProductPackageDetails")
    public R getStoreProductPackageDetails(@RequestParam("packageId") Long packageId, HttpServletRequest request){
        FsStoreProductPackageScrm 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();
            FsStoreProductAttrValueScrm attrValue=attrValueService.selectFsStoreProductAttrValueById(dto.getId());
            if(attrValue!=null){
                FsStoreProductScrm product=storeProductService.selectFsStoreProductByIdAudit(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);
    }

}
