package com.fs.his.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.his.domain.FsIntegralGoods;
import com.fs.his.service.IFsIntegralGoodsService;
import com.fs.his.utils.RedisCacheUtil;
import com.fs.his.vo.FsIntegralGoodsChooseVO;
import com.fs.his.vo.FsIntegralGoodsListVO;
import com.fs.his.vo.FsStoreProductExcelVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 积分商品Controller
 *
 * @author fs
 * @date 2023-11-02
 */
@RestController
@RequestMapping("/his/integralGoods")
public class FsIntegralGoodsController extends BaseController
{
    @Autowired
    private IFsIntegralGoodsService fsIntegralGoodsService;
    @Autowired
    RedisCacheUtil redisCacheUtil;
    /**
     * 查询积分商品列表
     */
    @PreAuthorize("@ss.hasPermi('his:integralGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsIntegralGoods fsIntegralGoods)
    {
        startPage();
        List<FsIntegralGoodsListVO> list = fsIntegralGoodsService.selectFsIntegralGoodsListVO(fsIntegralGoods);
        return getDataTable(list);
    }

    /**
     * 导出积分商品列表
     */
    @PreAuthorize("@ss.hasPermi('his:integralGoods:export')")
    @Log(title = "积分商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsIntegralGoods fsIntegralGoods)
    {
        List<FsIntegralGoods> list = fsIntegralGoodsService.selectFsIntegralGoodsList(fsIntegralGoods);
        ExcelUtil<FsIntegralGoods> util = new ExcelUtil<FsIntegralGoods>(FsIntegralGoods.class);
        return util.exportExcel(list, "积分商品数据");
    }

    /**
     * 获取积分商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:integralGoods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return AjaxResult.success(fsIntegralGoodsService.selectFsIntegralGoodsByGoodsId(goodsId));
    }


    @Log(title = "商品导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        redisCacheUtil.delRedisKey("getIntegralGoodsList");
        redisCacheUtil.delRedisKey("getIntegralGoodsById");
        ExcelUtil<FsIntegralGoods> util = new ExcelUtil<>(FsIntegralGoods.class);
        List<FsIntegralGoods> list = util.importExcel(file.getInputStream());
        String message = fsIntegralGoodsService.importIntegralGoodsService(list);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<FsIntegralGoods> util = new ExcelUtil<>(FsIntegralGoods.class);
        return util.importTemplateExcel("商品数据");
    }


    /**
     * 新增积分商品
     */
    @PreAuthorize("@ss.hasPermi('his:integralGoods:add')")
    @Log(title = "积分商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsIntegralGoods fsIntegralGoods)
    {
        redisCacheUtil.delRedisKey("getIntegralGoodsList");
        redisCacheUtil.delRedisKey("getIntegralGoodsById");
        return toAjax(fsIntegralGoodsService.insertFsIntegralGoods(fsIntegralGoods));
    }

    /**
     * 修改积分商品
     */
    @PreAuthorize("@ss.hasPermi('his:integralGoods:edit')")
    @Log(title = "积分商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsIntegralGoods fsIntegralGoods)
    {

        redisCacheUtil.delRedisKey("getIntegralGoodsList");
        redisCacheUtil.delSpringCacheKey("getIntegralGoodsById", fsIntegralGoods.getGoodsId());
        redisCacheUtil.delRedisKey("getIntegralGoodsById");
        return toAjax(fsIntegralGoodsService.updateFsIntegralGoods(fsIntegralGoods));
    }

    /**
     * 删除积分商品
     */
    @PreAuthorize("@ss.hasPermi('his:integralGoods:remove')")
    @Log(title = "积分商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        redisCacheUtil.delRedisKey("getIntegralGoodsList");
        redisCacheUtil.delRedisKey("getIntegralGoodsById");
        return toAjax(fsIntegralGoodsService.deleteFsIntegralGoodsByGoodsIds(goodsIds));
    }

    @GetMapping("/getChooseIntegralGoodsList")
    public R getChooseIntegralGoodsList(@RequestParam(required = false) String goodsName,
                                        @RequestParam(required = false) Integer goodsType,
                                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Map<String,Object> params = new HashMap<>();
        params.put("goodsName", goodsName);
        params.put("goodsType", goodsType);

        PageHelper.startPage(pageNum, pageSize);
        List<FsIntegralGoodsChooseVO> list = fsIntegralGoodsService.getChooseIntegralGoodsListByMap(params);
        return R.ok().put("data", new PageInfo<>(list));
    }
}
