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

/**
 * 食材入库Controller
 *
 * @author songyu
 * @date 2021-11-26
 */
@RestController
@RequestMapping("/canteen/scrk")
public class HsCanteenScrkController extends BaseController
{
    @Autowired
    private IHsCanteenScrkService hsCanteenScrkService;

    /**
     * 查询食材入库列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:scrk:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsCanteenScrk hsCanteenScrk)
    {
        startPage();
        List<HsCanteenScrk> list = hsCanteenScrkService.selectHsCanteenScrkList(hsCanteenScrk);
        return getDataTable(list);
    }

    /**
     * 导出食材入库列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:scrk:export')")
    @Log(title = "食材入库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsCanteenScrk hsCanteenScrk)
    {
        List<HsCanteenScrk> list = hsCanteenScrkService.selectHsCanteenScrkList(hsCanteenScrk);
        ExcelUtil<HsCanteenScrk> util = new ExcelUtil<HsCanteenScrk>(HsCanteenScrk.class);
        return util.exportExcel(list, "食材入库数据");
    }

    /**
     * 获取食材入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:scrk:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") String recordId)
    {
        return AjaxResult.success(hsCanteenScrkService.getById(recordId));
    }

    /**
     * 新增食材入库
     */
    @PreAuthorize("@ss.hasPermi('canteen:scrk:add')")
    @Log(title = "食材入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsCanteenScrk hsCanteenScrk)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsCanteenScrk byId = hsCanteenScrkService.getById(idStr);
            if (null == byId) {
                hsCanteenScrk.setRecordId(idStr);
                break;
            }
        }
        return toAjax(hsCanteenScrkService.save(hsCanteenScrk));
    }

    /**
     * 修改食材入库
     */
    @PreAuthorize("@ss.hasPermi('canteen:scrk:edit')")
    @Log(title = "食材入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsCanteenScrk hsCanteenScrk)
    { ;
        return toAjax(hsCanteenScrkService.updateById(hsCanteenScrk));
    }

    /**
     * 删除食材入库
     */
    @PreAuthorize("@ss.hasPermi('canteen:scrk:remove')")
    @Log(title = "食材入库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable String[] recordIds)
    {
        return toAjax(hsCanteenScrkService.removeByIds(Arrays.asList(recordIds)));
    }

    @GetMapping("/getScList")
    public AjaxResult getScList(String jgid){
        return AjaxResult.success(hsCanteenScrkService.getScList(jgid));
    }
}
