package com.fs.store.controller.store;

import com.fs.store.utils.UserUtil;
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.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.domain.FsMaterial;
import com.fs.his.domain.StoreLoginUser;
import com.fs.his.service.IFsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 素材库Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/material")
public class FsMaterialController extends BaseController
{
    @Autowired
    private IFsMaterialService fsMaterialService;

    /**
     * 查询素材库列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FsMaterial fsMaterial)
    {
        StoreLoginUser loginUser = UserUtil.getLoginUser();
        fsMaterial.setStoreId(loginUser.getFsStore().getStoreId());
        startPage();
        List<FsMaterial> list = fsMaterialService.selectFsMaterialList(fsMaterial);
        return getDataTable(list);
    }

    /**
     * 导出素材库列表
     */
    @Log(title = "素材库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsMaterial fsMaterial)
    {

        List<FsMaterial> list = fsMaterialService.selectFsMaterialList(fsMaterial);
        ExcelUtil<FsMaterial> util = new ExcelUtil<FsMaterial>(FsMaterial.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 获取素材库详细信息
     */
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return AjaxResult.success(fsMaterialService.selectFsMaterialByMaterialId(materialId));
    }

    /**
     * 新增素材库
     */
    @Log(title = "素材库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsMaterial fsMaterial)
    {
        StoreLoginUser loginUser = UserUtil.getLoginUser();
        fsMaterial.setStoreId(loginUser.getFsStore().getStoreId());
        return toAjax(fsMaterialService.insertFsMaterial(fsMaterial));
    }

    /**
     * 修改素材库
     */
    @Log(title = "素材库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsMaterial fsMaterial)
    {
        return toAjax(fsMaterialService.updateFsMaterial(fsMaterial));
    }

    /**
     * 删除素材库
     */
    @Log(title = "素材库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds)
    {
        return toAjax(fsMaterialService.deleteFsMaterialByMaterialIds(materialIds));
    }
}
