package easydo.technology.controller;

import easydo.technology.model.ProductOrder;
import easydo.technology.service.ProductOrderService;
import easydo.technology.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {

    @Resource
    private ProductOrderService productOrderService;

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

    /**
     * 保存工单
     */
    @PostMapping("/save")
    public Object save(@RequestBody ProductOrder model) throws Exception {
        Long userId = SecurityUtils.getCurrentUserId();
        model.setCreateId(userId);
        return new ResponseEntity<>(productOrderService.save(model), HttpStatus.OK);
    }

    /**
     * 更新工单
     */
    @PostMapping("/update")
    public Object update(@RequestBody ProductOrder model) throws Exception {
        Long userId = SecurityUtils.getCurrentUserId();
        model.setUpdateId(userId);
        return new ResponseEntity<>(productOrderService.update(model), HttpStatus.OK);
    }

    /**
     * 删除工单
     */
    @PostMapping("/remove")
    public Object remove(@RequestBody ProductOrder model) throws Exception {
        // 校验用户登录状态
        Long userId = SecurityUtils.getCurrentUserId();
        
        return new ResponseEntity<>(productOrderService.remove(model), HttpStatus.OK);
    }
}
