package com.fs.his.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;

import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.domain.FsStoreProduct;
import com.fs.his.domain.FsStoreProductAttrValue;
import com.fs.his.domain.FsStoreProductPackage;
import com.fs.his.dto.StoreOrderProductDTO;
import com.fs.his.dto.StorePackageProductDTO;
import com.fs.his.param.FsStoreProductPackageModifyParam;
import com.fs.his.service.IFsStoreProductAttrValueService;
import com.fs.his.service.IFsStoreProductPackageService;
import com.fs.his.service.IFsStoreProductService;
import com.fs.his.vo.FsStoreProductPacketExportVO;
import com.fs.his.vo.FsStoreProductPacketVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 商品组合套餐Controller
 *
 * @author fs
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/his/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.selectFsStoreProductByProductId(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.selectFsStoreProductByProductId(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));

        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));
    }
}
