package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.TbVipCode;
import com.ruoyi.mall.service.ITbVipCodeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员码Controller
 *
 * @author lsm
 * @date 2023-12-22
 */
@RestController
@RequestMapping("/mall/vipCode")
public class TbVipCodeController extends BaseController
{
    @Autowired
    private ITbVipCodeService tbVipCodeService;

    /**
     * 查询会员码列表
     */
    @PreAuthorize("@ss.hasPermi('mall:vipCode:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbVipCode tbVipCode)
    {
        startPage();
        List<TbVipCode> list = tbVipCodeService.selectTbVipCodeList(tbVipCode);
        return getDataTable(list);
    }

    /**
     * 导出会员码列表
     */
    @PreAuthorize("@ss.hasPermi('mall:vipCode:export')")
    @Log(title = "会员码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbVipCode tbVipCode)
    {
        List<TbVipCode> list = tbVipCodeService.selectTbVipCodeList(tbVipCode);
        ExcelUtil<TbVipCode> util = new ExcelUtil<TbVipCode>(TbVipCode.class);
        util.exportExcel(response, list, "会员码数据");
    }

    /**
     * 获取会员码详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:vipCode:query')")
    @GetMapping(value = "/{codeId}")
    public AjaxResult getInfo(@PathVariable("codeId") Long codeId)
    {
        return AjaxResult.success(tbVipCodeService.selectTbVipCodeByCodeId(codeId));
    }

    /**
     * 新增会员码
     */
    @PreAuthorize("@ss.hasPermi('mall:vipCode:add')")
    @Log(title = "会员码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbVipCode tbVipCode)
    {
        return toAjax(tbVipCodeService.insertTbVipCode(tbVipCode));
    }

    /**
     * 修改会员码
     */
    @PreAuthorize("@ss.hasPermi('mall:vipCode:edit')")
    @Log(title = "会员码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbVipCode tbVipCode)
    {
        return toAjax(tbVipCodeService.updateTbVipCode(tbVipCode));
    }

    /**
     * 删除会员码
     */
    @PreAuthorize("@ss.hasPermi('mall:vipCode:remove')")
    @Log(title = "会员码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{codeIds}")
    public AjaxResult remove(@PathVariable Long[] codeIds)
    {
        return toAjax(tbVipCodeService.deleteTbVipCodeByCodeIds(codeIds));
    }


    /**
     * 批量创建会员码
     * @param map 数量
     * @return 结果
     */
    @PostMapping("/createVipCode")
    public AjaxResult createVipCode(@RequestBody Map<String, Object> map){
        Integer num = MapUtils.getInteger(map, "num");
        if (ObjectUtils.isEmpty(num)){
            return AjaxResult.error("缺少num");
        }

        return AjaxResult.toAjax(tbVipCodeService.createVipCode(num));
    }
}
