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.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
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.service.IFsStoreProductAttrValueService;
import com.fs.store.service.IFsStoreProductService;
import com.fs.store.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.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 TokenService tokenService;
    @Autowired
    private IFsStoreProductService productService;
    /**
     * 查询商品组合套餐列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductPackage:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProductPackage 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(FsStoreProductPackage 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)
    {
        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=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 FsStoreProductPackage fsStoreProductPackage)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsStoreProductPackage.setCompanyId(loginUser.getCompany().getCompanyId());
        fsStoreProductPackage.setDeptId(loginUser.getUser().getDeptId());
        for(StoreOrderProductDTO dto:fsStoreProductPackage.getProductList()){
            FsStoreProductAttrValue 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 FsStoreProductPackage fsStoreProductPackage)
    {
        for(StoreOrderProductDTO dto:fsStoreProductPackage.getProductList()){
            FsStoreProductAttrValue 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));
    }
}
