Index.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\api\controller\inspection;
  3. use app\api\controller\inspection\Base;
  4. use app\common\Service\InspectService;
  5. use app\common\Enum\StatusEnum;
  6. /**
  7. * 示例接口
  8. */
  9. class Index extends Base
  10. {
  11. /**
  12. * 获取验货选项列表
  13. */
  14. public function getOptions()
  15. {
  16. // 查询验货类型的
  17. $inspectId= $this->request->param('inspection_type_id');
  18. if(intval($inspectId) <= 0){
  19. $this->error('验货类型ID不能为空');
  20. }
  21. $params = [
  22. 'status' => StatusEnum::ENABLED,
  23. 'inspection_type_id' => $inspectId,
  24. ];
  25. $options = InspectService::getInspectionItems($params);
  26. $this->success('获取成功', $options);
  27. }
  28. /**
  29. * 提交验货任务
  30. *
  31. * @ApiTitle (提交验货任务)
  32. * @ApiSummary (提交验货任务,包含验货结果和照片信息)
  33. * @ApiMethod (POST)
  34. * @ApiRoute (/api/inspection/index/submitTask)
  35. * @ApiParams (name="order_id", type="integer", required=true, description="订单ID")
  36. * @ApiParams (name="order_goods_id", type="integer", required=true, description="订单商品ID")
  37. * @ApiParams (name="inspector_id", type="integer", required=true, description="验货员ID")
  38. * @ApiParams (name="remark", type="string", required=false, description="备注信息")
  39. * @ApiParams (name="images", type="string", required=false, description="任务图片,多张用逗号分隔")
  40. * @ApiParams (name="start_time", type="integer", required=false, description="任务开始时间戳")
  41. * @ApiParams (name="end_time", type="integer", required=false, description="任务结束时间戳")
  42. * @ApiParams (name="task_id", type="integer", required=false, description="任务ID,更新时传入")
  43. * @ApiParams (name="results", type="array", required=true, description="验货结果数组")
  44. * @ApiParams (name="results.item_id", type="integer", required=true, description="检查项ID")
  45. * @ApiParams (name="results.name", type="string", required=false, description="检查项名称")
  46. * @ApiParams (name="results.is_qualified", type="integer", required=true, description="是否合格 0:否 1:是")
  47. * @ApiParams (name="results.remark", type="string", required=false, description="不合格原因")
  48. * @ApiParams (name="results.photos", type="array", required=false, description="照片数组")
  49. * @ApiParams (name="results.photos.photo_url", type="string", required=true, description="照片URL")
  50. * @ApiReturnParams (name="code", type="integer", required=true, sample="1")
  51. * @ApiReturnParams (name="msg", type="string", required=true, sample="验货任务提交成功")
  52. * @ApiReturnParams (name="data", type="object", description="返回数据")
  53. * @ApiReturn ({
  54. 'code':'1',
  55. 'msg':'验货任务提交成功',
  56. 'data': {
  57. 'task_id': 123
  58. }
  59. })
  60. */
  61. public function submitTask()
  62. {
  63. $params = $this->request->post();
  64. // 验证必要参数
  65. if (empty($params['order_id'])) {
  66. $this->error('订单ID不能为空');
  67. }
  68. if (empty($params['order_goods_id'])) {
  69. $this->error('订单商品ID不能为空');
  70. }
  71. $params['inspector_id'] = $this->auth->id;
  72. if (empty($params['results']) || !is_array($params['results'])) {
  73. $this->error('验货结果不能为空');
  74. }
  75. // 组装任务数据
  76. $taskData = [
  77. 'order_id' => $params['order_id'],
  78. 'order_goods_id' => $params['order_goods_id'],
  79. 'inspector_id' => $params['inspector_id'],
  80. 'remark' => $params['remark'] ?? '',
  81. 'images' => $params['images'] ?? '',
  82. 'start_time' => $params['start_time'] ?? time(),
  83. 'end_time' => $params['end_time'] ?? time(),
  84. 'task_status' => 1, // 进行中
  85. 'status' => 1 // 开启
  86. ];
  87. // 如果是更新任务,传入task_id
  88. if (!empty($params['task_id'])) {
  89. $taskData['task_id'] = $params['task_id'];
  90. }
  91. // 组装结果数据
  92. $resultData = [];
  93. foreach ($params['results'] as $result) {
  94. if (empty($result['item_id'])) {
  95. $this->error('检查项ID不能为空');
  96. }
  97. $resultItem = [
  98. 'item_id' => $result['item_id'],
  99. 'name' => $result['name'] ?? '',
  100. 'is_qualified' => $result['is_qualified'] ?? 1,
  101. 'remark' => $result['remark'] ?? '',
  102. 'photos' => $result['photos'] ?? []
  103. ];
  104. $resultData[] = $resultItem;
  105. }
  106. $inspectUid = $this->auth->id;
  107. // 调用服务提交验货任务
  108. list($code, $msg, $data) = InspectService::submitInspectionTask($taskData, $resultData,$inspectUid);
  109. if ($code == 1) {
  110. $this->success($msg, $data);
  111. } else {
  112. $this->error($msg);
  113. }
  114. }
  115. //
  116. }