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.HsCanteenOrderDetail;
import com.willalp.canteen.service.IHsCanteenOrderDetailService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

/**
 * 食堂订单明细Controller
 *
 * @author songyu
 * @date 2021-12-03
 */
@RestController
@RequestMapping("/canteen/detail")
public class HsCanteenOrderDetailController extends BaseController
{
    @Autowired
    private IHsCanteenOrderDetailService hsCanteenOrderDetailService;

    /**
     * 查询食堂订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsCanteenOrderDetail hsCanteenOrderDetail)
    {
        startPage();
        List<HsCanteenOrderDetail> list = hsCanteenOrderDetailService.selectHsCanteenOrderDetailList(hsCanteenOrderDetail);
        return getDataTable(list);
    }

    /**
     * 导出食堂订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:detail:export')")
    @Log(title = "食堂订单明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsCanteenOrderDetail hsCanteenOrderDetail)
    {
        List<HsCanteenOrderDetail> list = hsCanteenOrderDetailService.selectHsCanteenOrderDetailList(hsCanteenOrderDetail);
        ExcelUtil<HsCanteenOrderDetail> util = new ExcelUtil<HsCanteenOrderDetail>(HsCanteenOrderDetail.class);
        return util.exportExcel(list, "食堂订单明细数据");
    }

    /**
     * 获取食堂订单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:detail:query')")
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") String detailId)
    {
        return AjaxResult.success(hsCanteenOrderDetailService.getById(detailId));
    }

    /**
     * 新增食堂订单明细
     */
    @PreAuthorize("@ss.hasPermi('canteen:detail:add')")
    @Log(title = "食堂订单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsCanteenOrderDetail hsCanteenOrderDetail)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsCanteenOrderDetail byId = hsCanteenOrderDetailService.getById(idStr);
            if (null == byId) {
                hsCanteenOrderDetail.setDetailId(idStr);
                break;
            }
        }
        return toAjax(hsCanteenOrderDetailService.save(hsCanteenOrderDetail));
    }

    /**
     * 修改食堂订单明细
     */
    @PreAuthorize("@ss.hasPermi('canteen:detail:edit')")
    @Log(title = "食堂订单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsCanteenOrderDetail hsCanteenOrderDetail)
    {
        return toAjax(hsCanteenOrderDetailService.updateById(hsCanteenOrderDetail));
    }

    /**
     * 删除食堂订单明细
     */
    @PreAuthorize("@ss.hasPermi('canteen:detail:remove')")
    @Log(title = "食堂订单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable String[] detailIds)
    {
        return toAjax(hsCanteenOrderDetailService.removeByIds(Arrays.asList(detailIds)));
    }
}
