package com.fs.hisStore.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.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.hisStore.domain.FsStoreProductScrm;
import com.fs.hisStore.domain.FsStoreProductAttrValueScrm;
import com.fs.hisStore.dto.StoreOrderProductDTO;
import com.fs.hisStore.dto.StorePackageProductDTO;
import com.fs.hisStore.service.IFsStoreProductAttrValueScrmService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.hisStore.vo.FsStoreProductPacketVO;
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.hisStore.domain.FsStoreProductPackageScrm;
import com.fs.hisStore.service.IFsStoreProductPackageScrmService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 商品组合套餐Controller
 *
 * @author fs
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/store/store/storeProductPackage")
public class FsStoreProductPackageScrmController extends BaseController
{
    @Autowired
    private IFsStoreProductPackageScrmService fsStoreProductPackageService;
    @Autowired
    private IFsStoreProductAttrValueScrmService attrValueService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IFsStoreProductScrmService productService;
    /**
     * 查询商品组合套餐列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProductPackageScrm fsStoreProductPackage)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreProductPackage.setCompanyId(loginUser.getCompany().getCompanyId());
        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(FsStoreProductPackageScrm fsStoreProductPackage)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreProductPackage.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FsStoreProductPacketVO> list = fsStoreProductPackageService.selectFsStoreProductPackageListVO(fsStoreProductPackage);
        ExcelUtil<FsStoreProductPacketVO> util = new ExcelUtil<FsStoreProductPacketVO>(FsStoreProductPacketVO.class);
        return util.exportExcel(list, "storeProductPackage");
    }

    /**
     * 获取商品组合套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:query')")
    @GetMapping(value = "/{packageId}")
    public R getInfo(@PathVariable("packageId") Long packageId)
    {
        FsStoreProductPackageScrm 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();
            FsStoreProductAttrValueScrm attrValue=attrValueService.selectFsStoreProductAttrValueById(dto.getId());
            if(attrValue!=null){
                FsStoreProductScrm product=productService.selectFsStoreProductById(attrValue.getProductId());
                if(product!=null){
                    productDTO.setProductId(product.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);
                }
            }
        }
        return R.ok().put("productPckage",productPackage).put("productList",productList);
    }

    /**
     * 新增商品组合套餐
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:add')")
    @Log(title = "商品组合套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreProductPackageScrm fsStoreProductPackage)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreProductPackage.setCompanyId(loginUser.getCompany().getCompanyId());
        fsStoreProductPackage.setDeptId(loginUser.getUser().getDeptId());
        for(StoreOrderProductDTO dto:fsStoreProductPackage.getProductList()){
            FsStoreProductAttrValueScrm attrValue=attrValueService.selectFsStoreProductAttrValueById(dto.getId());
            if(attrValue!=null){
                dto.setBarCode(attrValue.getBarCode());
            }
        }
        fsStoreProductPackage.setProducts(JSONUtil.toJsonStr(fsStoreProductPackage.getProductList()));

        return toAjax(fsStoreProductPackageService.insertFsStoreProductPackage(fsStoreProductPackage));
    }

    /**
     * 修改商品组合套餐
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:edit')")
    @Log(title = "商品组合套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreProductPackageScrm fsStoreProductPackage)
    {
        for(StoreOrderProductDTO dto:fsStoreProductPackage.getProductList()){
            FsStoreProductAttrValueScrm attrValue=attrValueService.selectFsStoreProductAttrValueById(dto.getId());
            if(attrValue!=null){
                dto.setBarCode(attrValue.getBarCode());
            }
        }
        fsStoreProductPackage.setProducts(JSONUtil.toJsonStr(fsStoreProductPackage.getProductList()));

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