package com.fs.store.controller;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fs.common.core.domain.R;
import com.fs.course.domain.FsUserTalent;
import com.fs.store.domain.FsStoreProduct;
import com.fs.store.domain.FsStoreProductAttrValue;
import com.fs.store.dto.StoreOrderProductDTO;
import com.fs.store.dto.StorePackageProductDTO;

import com.fs.store.param.FsStoreProductPackageModifyParam;
import com.fs.store.service.IFsStoreProductAttrValueService;
import com.fs.store.service.IFsStoreProductService;
import com.fs.store.vo.FsStoreProductPacketExportVO;
import com.fs.store.vo.FsStoreProductPacketVO;
import org.springframework.beans.BeanUtils;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.store.domain.FsStoreProductPackage;
import com.fs.store.service.IFsStoreProductPackageService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 商品组合套餐Controller
 *
 * @author fs
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/store/storeProductPackage")
public class FsStoreProductPackageController extends BaseController
{
    @Autowired
    private IFsStoreProductPackageService fsStoreProductPackageService;
    @Autowired
    private IFsStoreProductAttrValueService attrValueService;
    @Autowired
    private IFsStoreProductService storeProductService;
    /**
     * 查询商品组合套餐列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProductPackage fsStoreProductPackage)
    {
        startPage();
        List<FsStoreProductPacketVO> list = fsStoreProductPackageService.selectFsStoreProductPackageListVO(fsStoreProductPackage);
        return getDataTable(list);
    }

    /**
     * 导出商品组合套餐列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:export')")
    @Log(title = "商品组合套餐", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreProductPackage fsStoreProductPackage)
    {
        List<FsStoreProductPacketVO> list = fsStoreProductPackageService.selectFsStoreProductPackageListVO(fsStoreProductPackage);
        List<FsStoreProductPacketExportVO> sorList=new ArrayList<>();

        for(FsStoreProductPacketVO productPackage:list){
            //List<StoreOrderProductDTO> productList=new ArrayList<>();
            JSONArray jsonArray=JSONUtil.parseArray(productPackage.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){
                        FsStoreProductPacketExportVO packetExportVO= new FsStoreProductPacketExportVO();
                        BeanUtils.copyProperties(productPackage,packetExportVO);
                        packetExportVO.setProductId(attrValue.getProductId());
                        packetExportVO.setId(dto.getId());
                        packetExportVO.setBarCode(attrValue.getBarCode());
                        packetExportVO.setPrice(attrValue.getPrice());
                        packetExportVO.setCount(dto.getCount());
                        packetExportVO.setSku(attrValue.getSku());
                        packetExportVO.setImage(attrValue.getImage());
                        packetExportVO.setProductName(product.getProductName());
                        sorList.add(packetExportVO);
                        //productList.add(productDTO);

                    }
                }
            }

        }
        ExcelUtil<FsStoreProductPacketExportVO> util = new ExcelUtil<FsStoreProductPacketExportVO>(FsStoreProductPacketExportVO.class);
        return util.exportExcel(sorList, "storeProductPackage");
    }

    /**
     * 获取商品组合套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:query')")
    @GetMapping(value = "/{packageId}")
    public AjaxResult getInfo(@PathVariable("packageId") Long packageId)
    {
        FsStoreProductPackage productPackage=fsStoreProductPackageService.selectFsStoreProductPackageById(packageId);
        List<StoreOrderProductDTO> productList=new ArrayList<>();
        JSONArray jsonArray=JSONUtil.parseArray(productPackage.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);
                }
            }
        }
        productPackage.setProductList(productList);
        return AjaxResult.success(productPackage);
    }

    /**
     * 批量修改商品组合套餐
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:edit')")
    @Log(title = "商品组合套餐", businessType = BusinessType.UPDATE)
    @PostMapping("/modifyMore")
    public AjaxResult modifyMore(@RequestBody FsStoreProductPackageModifyParam param) {
        return toAjax(fsStoreProductPackageService.updateFsStoreProductPackages(param.getPackageIds(),param.getStatus()));
    }

    /**
     * 新增商品组合套餐
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:add')")
    @Log(title = "商品组合套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreProductPackage fsStoreProductPackage)
    {
        JSONArray jsonArray=JSONUtil.parseArray(fsStoreProductPackage.getProductList());
        List<StorePackageProductDTO> goodsList=JSONUtil.toList(jsonArray, StorePackageProductDTO.class);
        fsStoreProductPackage.setProducts(JSONUtil.toJsonStr(goodsList));
        fsStoreProductPackage.setCompanyId(0l);
        return toAjax(fsStoreProductPackageService.insertFsStoreProductPackage(fsStoreProductPackage));
    }

    /**
     * 修改商品组合套餐
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:edit')")
    @Log(title = "商品组合套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreProductPackage fsStoreProductPackage)
    {
        JSONArray jsonArray=JSONUtil.parseArray(fsStoreProductPackage.getProductList());
        List<StorePackageProductDTO> goodsList=JSONUtil.toList(jsonArray, StorePackageProductDTO.class);
        fsStoreProductPackage.setProducts(JSONUtil.toJsonStr(goodsList));
        return toAjax(fsStoreProductPackageService.updateFsStoreProductPackage(fsStoreProductPackage));
    }

    /**
     * 删除商品组合套餐
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:remove')")
    @Log(title = "商品组合套餐", businessType = BusinessType.DELETE)
	@DeleteMapping("/{packageIds}")
    public AjaxResult remove(@PathVariable Long[] packageIds)
    {
        return toAjax(fsStoreProductPackageService.deleteFsStoreProductPackageByIds(packageIds));
    }


    @GetMapping("/listBySearch")
    public R listBySearCh(FsStoreProductPackage productPackage)
    {
        List<FsStoreProduct> products = new ArrayList<>();
        List<FsStoreProductPackage> list = fsStoreProductPackageService.selectFsStoreProductPackageList(productPackage);

        for (FsStoreProductPackage fsStoreProductPackage : list){
            FsStoreProduct product = new FsStoreProduct();
            product.setProductId(fsStoreProductPackage.getPackageId());
            product.setProductName(fsStoreProductPackage.getTitle());
            products.add(product);
        }
        return R.ok().put("data",products);
    }
}
