package com.fs.store.controller;

import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fs.store.domain.FsStoreProduct;
import com.fs.store.domain.FsStoreProductAttrValue;
import com.fs.store.dto.StoreProductGroupDTO;
import com.fs.store.service.IFsStoreProductAttrValueService;
import com.fs.store.service.IFsStoreProductService;
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.FsStoreProductGroup;
import com.fs.store.service.IFsStoreProductGroupService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 商品组合Controller
 *
 * @author fs
 * @date 2022-11-23
 */
@RestController
@RequestMapping("/store/storeProductGroup")
public class FsStoreProductGroupController extends BaseController
{
    @Autowired
    private IFsStoreProductGroupService fsStoreProductGroupService;
    @Autowired
    private IFsStoreProductAttrValueService attrValueService;
    @Autowired
    private IFsStoreProductService storeProductService;
    /**
     * 查询商品组合列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductGroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProductGroup fsStoreProductGroup)
    {
        startPage();
        List<FsStoreProductGroup> list = fsStoreProductGroupService.selectFsStoreProductGroupList(fsStoreProductGroup);
        return getDataTable(list);
    }

    /**
     * 导出商品组合列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductGroup:export')")
    @Log(title = "商品组合", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreProductGroup fsStoreProductGroup)
    {
        List<FsStoreProductGroup> list = fsStoreProductGroupService.selectFsStoreProductGroupList(fsStoreProductGroup);
        ExcelUtil<FsStoreProductGroup> util = new ExcelUtil<FsStoreProductGroup>(FsStoreProductGroup.class);
        return util.exportExcel(list, "storeProductGroup");
    }

    /**
     * 获取商品组合详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductGroup:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId)
    {
        FsStoreProductGroup storeProductGroup=fsStoreProductGroupService.selectFsStoreProductGroupById(groupId);
        JSONArray jsonArray=JSONUtil.parseArray(storeProductGroup.getProducts());
        List<StoreProductGroupDTO> productGroupDTOS=JSONUtil.toList(jsonArray, StoreProductGroupDTO.class);
        for(StoreProductGroupDTO dto:productGroupDTOS){
            FsStoreProductAttrValue attrValue=attrValueService.selectFsStoreProductAttrValueById(dto.getId());
            if(attrValue!=null){
                FsStoreProduct product=storeProductService.selectFsStoreProductById(attrValue.getProductId());
                if(product!=null){
                    dto.setProductId(attrValue.getProductId());
                    dto.setId(dto.getId());
                    dto.setBarCode(attrValue.getBarCode());
                    dto.setPrice(attrValue.getPrice());
                    dto.setCount(dto.getCount());
                    dto.setSku(attrValue.getSku());
                    dto.setImage(attrValue.getImage());
                    dto.setProductName(product.getProductName());
                }

            }

        }
        storeProductGroup.setProductList(productGroupDTOS);
        return AjaxResult.success(storeProductGroup);
    }

    /**
     * 新增商品组合
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductGroup:add')")
    @Log(title = "商品组合", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreProductGroup fsStoreProductGroup)
    {
        String products=JSONUtil.toJsonStr(fsStoreProductGroup.getProductList());
        fsStoreProductGroup.setProducts(products);
        return toAjax(fsStoreProductGroupService.insertFsStoreProductGroup(fsStoreProductGroup));
    }

    /**
     * 修改商品组合
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductGroup:edit')")
    @Log(title = "商品组合", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreProductGroup fsStoreProductGroup)
    {
        String products=JSONUtil.toJsonStr(fsStoreProductGroup.getProductList());
        fsStoreProductGroup.setProducts(products);
        return toAjax(fsStoreProductGroupService.updateFsStoreProductGroup(fsStoreProductGroup));
    }

    /**
     * 删除商品组合
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductGroup:remove')")
    @Log(title = "商品组合", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        return toAjax(fsStoreProductGroupService.deleteFsStoreProductGroupByIds(groupIds));
    }
}
