package com.ruoyi.web.controller.jarvis;

import java.util.List;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jarvis.domain.FavoriteProduct;
import com.ruoyi.jarvis.service.IFavoriteProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 常用商品Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/jarvis/favoriteProduct")
public class FavoriteProductController extends BaseController
{
    @Autowired
    private IFavoriteProductService favoriteProductService;

    /**
     * 查询常用商品列表
     */
    @PreAuthorize("@ss.hasPermi('jarvis:favoriteProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(FavoriteProduct favoriteProduct)
    {
        startPage();
        List<FavoriteProduct> list = favoriteProductService.selectFavoriteProductList(favoriteProduct);
        return getDataTable(list);
    }

    /**
     * 导出常用商品列表
     */
    @PreAuthorize("@ss.hasPermi('jarvis:favoriteProduct:export')")
    @Log(title = "常用商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FavoriteProduct favoriteProduct)
    {
        List<FavoriteProduct> list = favoriteProductService.selectFavoriteProductList(favoriteProduct);
        ExcelUtil<FavoriteProduct> util = new ExcelUtil<FavoriteProduct>(FavoriteProduct.class);
        return util.exportExcel(list, "常用商品数据");
    }

    /**
     * 获取常用商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('jarvis:favoriteProduct:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(favoriteProductService.selectFavoriteProductById(id));
    }

    /**
     * 新增常用商品
     */
    @PreAuthorize("@ss.hasPermi('jarvis:favoriteProduct:add')")
    @Log(title = "常用商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FavoriteProduct favoriteProduct)
    {
        return toAjax(favoriteProductService.insertFavoriteProduct(favoriteProduct));
    }

    /**
     * 修改常用商品
     */
    @PreAuthorize("@ss.hasPermi('jarvis:favoriteProduct:edit')")
    @Log(title = "常用商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FavoriteProduct favoriteProduct)
    {
        return toAjax(favoriteProductService.updateFavoriteProduct(favoriteProduct));
    }

    /**
     * 删除常用商品
     */
    @PreAuthorize("@ss.hasPermi('jarvis:favoriteProduct:remove')")
    @Log(title = "常用商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(favoriteProductService.deleteFavoriteProductByIds(ids));
    }

    /**
     * 添加商品到常用列表
     */
    @PostMapping("/addToFavorites")
    public AjaxResult addToFavorites(@RequestBody FavoriteProduct favoriteProduct)
    {
        return toAjax(favoriteProductService.addToFavorites(favoriteProduct));
    }

    /**
     * 从常用列表移除商品
     */
    @DeleteMapping("/removeFromFavorites/{skuid}")
    public AjaxResult removeFromFavorites(@PathVariable String skuid)
    {
        return toAjax(favoriteProductService.removeFromFavorites(skuid));
    }

    /**
     * 更新置顶状态
     */
    @PutMapping("/updateTopStatus/{id}/{isTop}")
    public AjaxResult updateTopStatus(@PathVariable Long id, @PathVariable Integer isTop)
    {
        return toAjax(favoriteProductService.updateTopStatus(id, isTop));
    }

    /**
     * 批量更新置顶状态
     */
    @PutMapping("/batchUpdateTopStatus")
    public AjaxResult batchUpdateTopStatus(@RequestBody FavoriteProduct favoriteProduct)
    {
        // 这里需要从请求体中获取ids和isTop
        return toAjax(favoriteProductService.updateTopStatus(favoriteProduct.getId(), favoriteProduct.getIsTop()));
    }

    /**
     * 根据SKUID查询常用商品
     */
    @GetMapping("/getBySkuid/{skuid}")
    public AjaxResult getBySkuid(@PathVariable String skuid)
    {
        return success(favoriteProductService.selectFavoriteProductBySkuid(skuid));
    }

    /**
     * 查询用户的常用商品列表
     */
    @GetMapping("/userFavorites")
    public AjaxResult getUserFavorites()
    {
        Long userId = getUserId();
        List<FavoriteProduct> list = favoriteProductService.selectUserFavoriteProducts(userId);
        return success(list);
    }

    /**
     * 根据线报消息创建常用商品
     */
    @PostMapping("/createFromXbMessage")
    public AjaxResult createFromXbMessage(@RequestBody Object xbMessageItem)
    {
        return toAjax(favoriteProductService.createFromXbMessage(xbMessageItem));
    }

    /**
     * 快速发品（从常用商品）
     */
    @PostMapping("/quickPublish/{id}")
    public AjaxResult quickPublish(@PathVariable Long id, @RequestBody String appid)
    {
        Object result = favoriteProductService.quickPublishFromFavorite(id, appid);
        return success(result);
    }

    /**
     * 更新使用次数和最后使用时间
     */
    @PutMapping("/updateUseCount/{id}")
    public AjaxResult updateUseCount(@PathVariable Long id)
    {
        return toAjax(favoriteProductService.updateUseCountAndTime(id));
    }

    /**
     * 更新发品信息
     */
    @PreAuthorize("@ss.hasPermi('jarvis:favoriteProduct:edit')")
    @PutMapping("/updateProductInfo")
    public AjaxResult updateProductInfo(@RequestBody FavoriteProduct favoriteProduct)
    {
        return toAjax(favoriteProductService.updateProductInfo(favoriteProduct));
    }
}
