package com.fs.store.controller;

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

import com.fs.common.core.domain.R;
import com.fs.store.service.IFsStoreProductService;
import com.fs.store.vo.FsStoreProductActivityListVO;
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.FsStoreActivity;
import com.fs.store.service.IFsStoreActivityService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

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

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

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:query')")
    @GetMapping(value = "/{activityId}")
    public R getInfo(@PathVariable("activityId") Long activityId)
    {
        FsStoreActivity 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 FsStoreActivity fsStoreActivity)
    {
        return toAjax(fsStoreActivityService.insertFsStoreActivity(fsStoreActivity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('store:storeActivity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreActivity 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));
    }
}
