package easydo.technology.controller;

import easydo.technology.model.vo.DispatchVO;
import easydo.technology.service.ProductOrderDispatchService;
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("/productOrderDispatch")
public class ProductOrderDispatchController {

    @Resource
    private ProductOrderDispatchService productOrderDispatchService;

    /**
     * 分页查询派工记录
     */
    @RequestMapping(value = "/getPage")
    public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
        Map<String, Object> result = productOrderDispatchService.getPage(map);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 工单派工
     */
    @RequestMapping(value = "/dispatch")
    public Object dispatch(@RequestBody DispatchVO vo) throws Exception {
        // 校验用户登录状态
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            return new ResponseEntity<>("用户请先登录", HttpStatus.UNAUTHORIZED);
        }

        String result = productOrderDispatchService.dispatch(vo);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 查询工单派工统计
     */
    @RequestMapping(value = "/getDispatchSummary")
    public Object getDispatchSummary(@RequestBody Map<String, Object> map) throws Exception {
        String orderId = (String) map.get("orderId");
        if (orderId == null || orderId.trim().isEmpty()) {
            return new ResponseEntity<>("orderId 不能为空", HttpStatus.BAD_REQUEST);
        }
        Object result = productOrderDispatchService.getDispatchSummary(orderId);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
