package com.fs.hisStore.controller;

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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.hisStore.domain.FsStoreUserEndCategoryScrm;
import com.fs.hisStore.dto.FsStoreProductSortItemDTO;
import com.fs.hisStore.service.IFsStoreUserEndCategoryScrmService;
import com.fs.hisStore.vo.FsStoreUserEndCategoryProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品用户分端类管理（金刚区/瀑布流）
 * 支持：分类名称模糊搜索、新增/编辑/删除、排序（用户端最多展示8个，超出不展示）、金刚区需icon/瀑布流不需、状态显示隐藏、一页10条分页、排序相同按创建时间倒序
 */
@RestController
@RequestMapping("/store/store/userEndCategory")
public class FsStoreUserEndCategoryScrmController extends BaseController {

    @Autowired
    private IFsStoreUserEndCategoryScrmService userEndCategoryService;

    /** 分页列表，一页10条，排序值相同按创建时间倒序 */
    @PreAuthorize("@ss.hasPermi('store:userEndCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreUserEndCategoryScrm query) {
        startPage();
        List<FsStoreUserEndCategoryScrm> list = userEndCategoryService.selectListPage(query);
        return getDataTable(list);
    }

    /** 添加/编辑商品时拉取用户端分类（金刚区、瀑布流各最多8条由前端截取） */
    @GetMapping("/listForProduct")
    public R listForProduct(@RequestParam(required = false) Long storeId) {
        List<FsStoreUserEndCategoryScrm> list = userEndCategoryService.selectListForProduct(storeId);
        return R.ok().put("data", list);
    }

    /** 按用户端分类ID分页查询关联商品（去重商品ID分页；返回商品ID、名称、售价、原价、销量及产品标签列表） */
    @PreAuthorize("@ss.hasPermi('store:userEndCategory:query')")
    @GetMapping("/products")
    public TableDataInfo listProductsByCategoryId(
            @RequestParam Long id,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        startPage();
        List<FsStoreUserEndCategoryProductVO> list = userEndCategoryService.listProductsByCategoryId(id,keyword);
        return getDataTable(list);
    }

    /** 保存关联商品在当前页的排序（写入 fs_store_product_user_end_category.sort） */
    @PreAuthorize("@ss.hasPermi('store:userEndCategory:edit')")
    @Log(title = "用户分端类关联商品排序", businessType = BusinessType.UPDATE)
    @PutMapping("/products/sort")
    public AjaxResult saveCategoryProductsSort(@RequestParam Long id,
                                               @RequestBody List<FsStoreProductSortItemDTO> items) {
        return toAjax(userEndCategoryService.saveProductsSort(id, items));
    }

    @PreAuthorize("@ss.hasPermi('store:userEndCategory:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(userEndCategoryService.selectById(id));
    }

    @PreAuthorize("@ss.hasPermi('store:userEndCategory:add')")
    @Log(title = "用户分端类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreUserEndCategoryScrm entity) {
        return toAjax(userEndCategoryService.insert(entity));
    }

    @PreAuthorize("@ss.hasPermi('store:userEndCategory:edit')")
    @Log(title = "用户分端类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreUserEndCategoryScrm entity) {
        return toAjax(userEndCategoryService.update(entity));
    }

    @PreAuthorize("@ss.hasPermi('store:userEndCategory:remove')")
    @Log(title = "用户分端类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userEndCategoryService.deleteByIds(ids));
    }
}
