package com.fs.hisStore.controller;

import cn.hutool.json.JSONUtil;
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.FsStoreProductRuleScrm;
import com.fs.hisStore.param.FsStoreProductRuleParam;
import com.fs.hisStore.service.IFsStoreProductRuleScrmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规则值(规格)Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/store/storeProductRule")
public class FsStoreProductRuleScrmController extends BaseController
{
    @Autowired
    private IFsStoreProductRuleScrmService fsStoreProductRuleService;

    /**
     * 查询商品规则值(规格)列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductRule:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProductRuleScrm fsStoreProductRule)
    {
        startPage();
        fsStoreProductRule.setIsDel(0);
        List<FsStoreProductRuleScrm> list = fsStoreProductRuleService.selectFsStoreProductRuleList(fsStoreProductRule);
        return getDataTable(list);
    }

    /**
     * 导出商品规则值(规格)列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductRule:export')")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreProductRuleScrm fsStoreProductRule)
    {
        List<FsStoreProductRuleScrm> list = fsStoreProductRuleService.selectFsStoreProductRuleList(fsStoreProductRule);
        ExcelUtil<FsStoreProductRuleScrm> util = new ExcelUtil<FsStoreProductRuleScrm>(FsStoreProductRuleScrm.class);
        return util.exportExcel(list, "storeProductRule");
    }

    /**
     * 获取商品规则值(规格)详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductRule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(fsStoreProductRuleService.selectFsStoreProductRuleById(id));
    }

    /**
     * 新增商品规则值(规格)
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductRule:add')")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreProductRuleParam fsStoreProductRule)
    {
        FsStoreProductRuleScrm rule=new FsStoreProductRuleScrm();
        rule.setRuleName(fsStoreProductRule.getRuleName());
        rule.setRuleValue(JSONUtil.toJsonStr(fsStoreProductRule.getRuleValue()));
        rule.setIsDel(0);
        return toAjax(fsStoreProductRuleService.insertFsStoreProductRule(rule));
    }

    /**
     * 修改商品规则值(规格)
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductRule:edit')")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreProductRuleParam fsStoreProductRule)
    {FsStoreProductRuleScrm rule=new FsStoreProductRuleScrm();
        rule
        .setId(fsStoreProductRule.getId());
        rule.setRuleName(fsStoreProductRule.getRuleName());
        rule.setRuleValue(JSONUtil.toJsonStr(fsStoreProductRule.getRuleValue()));
        rule.setIsDel(0);
        return toAjax(fsStoreProductRuleService.updateFsStoreProductRule(rule));
    }

    /**
     * 删除商品规则值(规格)
     */
    @PreAuthorize("@ss.hasPermi('store:storeProductRule:remove')")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(fsStoreProductRuleService.deleteFsStoreProductRuleByIds(ids));
    }


    @GetMapping("/getAllList")
    public R getAllList(FsStoreProductRuleScrm fsStoreProductRule)
    {
        fsStoreProductRule.setIsDel(0);
        List<FsStoreProductRuleScrm> list = fsStoreProductRuleService.selectFsStoreProductRuleList(fsStoreProductRule);
        return R.ok().put("data",list);
    }
}
