package com.willalp.web.controller.canteen;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
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.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;
import com.willalp.canteen.domain.HsCanteenMenuCustom;
import com.willalp.canteen.service.IHsCanteenMenuCustomService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

/**
 * 自由消费定制菜单Controller
 *
 * @author songyu
 * @date 2021-12-15
 */
@RestController
@RequestMapping("/canteen/custom")
public class HsCanteenMenuCustomController extends BaseController
{
    @Autowired
    private IHsCanteenMenuCustomService hsCanteenMenuCustomService;

    /**
     * 查询自由消费定制菜单列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:custom:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsCanteenMenuCustom hsCanteenMenuCustom)
    {
        startPage();
        List<HsCanteenMenuCustom> list = hsCanteenMenuCustomService.selectHsCanteenMenuCustomList(hsCanteenMenuCustom);
        return getDataTable(list);
    }

    /**
     * 导出自由消费定制菜单列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:custom:export')")
    @Log(title = "自由消费定制菜单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsCanteenMenuCustom hsCanteenMenuCustom)
    {
        List<HsCanteenMenuCustom> list = hsCanteenMenuCustomService.selectHsCanteenMenuCustomList(hsCanteenMenuCustom);
        ExcelUtil<HsCanteenMenuCustom> util = new ExcelUtil<HsCanteenMenuCustom>(HsCanteenMenuCustom.class);
        return util.exportExcel(list, "自由消费定制菜单数据");
    }

    /**
     * 获取自由消费定制菜单详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:custom:query')")
    @GetMapping(value = "/{customId}")
    public AjaxResult getInfo(@PathVariable("customId") Long customId)
    {
        return AjaxResult.success(hsCanteenMenuCustomService.getById(customId));
    }

    /**
     * 新增自由消费定制菜单
     */
    @PreAuthorize("@ss.hasPermi('canteen:custom:add')")
    @Log(title = "自由消费定制菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsCanteenMenuCustom hsCanteenMenuCustom)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsCanteenMenuCustom byId = hsCanteenMenuCustomService.getById(idStr);
            if (null == byId) {
                hsCanteenMenuCustom.setCustomId(idStr);
                break;
            }
        }
        return toAjax(hsCanteenMenuCustomService.save(hsCanteenMenuCustom));
    }

    /**
     * 修改自由消费定制菜单
     */
    @PreAuthorize("@ss.hasPermi('canteen:custom:edit')")
    @Log(title = "自由消费定制菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsCanteenMenuCustom hsCanteenMenuCustom)
    {
        return toAjax(hsCanteenMenuCustomService.updateById(hsCanteenMenuCustom));
    }

    /**
     * 删除自由消费定制菜单
     */
    @PreAuthorize("@ss.hasPermi('canteen:custom:remove')")
    @Log(title = "自由消费定制菜单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customIds}")
    public AjaxResult remove(@PathVariable Long[] customIds)
    {
        return toAjax(hsCanteenMenuCustomService.removeByIds(Arrays.asList(customIds)));
    }
}
