package easydo.technology.controller;

import easydo.technology.model.vo.MaterialRequisitionVO;
import easydo.technology.service.MaterialRequisitionService;
import easydo.technology.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 领料单控制器
 */
@RestController
@RequestMapping("/materialRequisition")
public class MaterialRequisitionController {

    @Resource
    private MaterialRequisitionService materialRequisitionService;

    /**
     * 分页查询领料单
     */
    @RequestMapping(value = "/getPage")
    public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
        // 校验用户登录状态
        Long userId = SecurityUtils.getCurrentUserId();
        Map<String, Object> result = materialRequisitionService.getPage(map);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 新增领料单
     */
    @RequestMapping(value = "/save")
    public Object save(@RequestBody MaterialRequisitionVO model) throws Exception {
        Long userId = SecurityUtils.getCurrentUserId();
        model.setCreateId(userId);
        return new ResponseEntity<>(materialRequisitionService.save(model), HttpStatus.OK);
    }

    /**
     * 修改领料单
     */
    @RequestMapping(value = "/update")
    public Object update(@RequestBody MaterialRequisitionVO model) throws Exception {
        Long userId = SecurityUtils.getCurrentUserId();
        model.setUpdateId(userId);
        return new ResponseEntity<>(materialRequisitionService.update(model), HttpStatus.OK);
    }

    /**
     * 删除领料单
     */
    @RequestMapping(value = "/remove")
    public Object remove(@RequestBody MaterialRequisitionVO model) throws Exception {
        Long userId = SecurityUtils.getCurrentUserId();
        return new ResponseEntity<>(materialRequisitionService.remove(model), HttpStatus.OK);
    }
}
