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.common.utils.poi.ExcelUtil;
import com.fs.hisStore.domain.FsStoreActivityScrm;
import com.fs.hisStore.service.IFsStoreActivityScrmService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.hisStore.vo.FsStoreProductActivityListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动Controller
 *
 * @author fs
 * @date 2022-11-18
 */
@RestController
@RequestMapping("/store/storeActivity")
public class FsStoreActivityScrmController extends BaseController
{
    @Autowired
    private IFsStoreActivityScrmService fsStoreActivityService;
    @Autowired
    private IFsStoreProductScrmService productService;
    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreActivityScrm fsStoreActivity)
    {
        startPage();
        List<FsStoreActivityScrm> list = fsStoreActivityService.selectFsStoreActivityList(fsStoreActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreActivityScrm fsStoreActivity)
    {
        List<FsStoreActivityScrm> list = fsStoreActivityService.selectFsStoreActivityList(fsStoreActivity);
        ExcelUtil<FsStoreActivityScrm> util = new ExcelUtil<FsStoreActivityScrm>(FsStoreActivityScrm.class);
        return util.exportExcel(list, "storeActivity");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:query')")
    @GetMapping(value = "/{activityId}")
    public R getInfo(@PathVariable("activityId") Long activityId)
    {
        FsStoreActivityScrm activity=fsStoreActivityService.selectFsStoreActivityById(activityId);
        List<FsStoreProductActivityListVO> products =new ArrayList<>();
        if(activity.getProductIds()!=null){
            products=productService.selectFsStoreProductByIds(activity.getProductIds());
        }
        return R.ok().put("activity",activity).put("products",products);
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreActivityScrm fsStoreActivity)
    {
        return toAjax(fsStoreActivityService.insertFsStoreActivity(fsStoreActivity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreActivityScrm fsStoreActivity)
    {
        return toAjax(fsStoreActivityService.updateFsStoreActivity(fsStoreActivity));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable Long[] activityIds)
    {
        return toAjax(fsStoreActivityService.deleteFsStoreActivityByIds(activityIds));
    }
}
