BoxHookService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Service\Box;
  3. use App\Model\Box;
  4. use App\Model\BoxDetail;
  5. use App\Model\Header_ext;
  6. use App\Model\Orders;
  7. use App\Model\OrdersProduct;
  8. use App\Service\Service;
  9. /**
  10. * 包装相关工厂模式
  11. * @package App\Models
  12. */
  13. class BoxHookService extends Service
  14. {
  15. protected static $instance;
  16. protected static $box_header;
  17. protected static $box_detail_header;
  18. public function __construct(){
  19. self::$box_header = Header_ext::where('type','box')->pluck('value','key')->toArray();
  20. self::$box_detail_header = Header_ext::where('type','box_detail')->pluck('value','key')->toArray();
  21. }
  22. public static function getInstance(): self
  23. {
  24. if (self::$instance == null) {
  25. self::$instance = new BoxHookService();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 包装单新增
  31. * @param $data
  32. * @return array
  33. */
  34. public function boxInsert($data){
  35. $box = new Box();
  36. if(!isset($data['order_no'])||empty($data['order_no'])) $data['order_no'] = $this->setOrderNo();
  37. if(!isset($data['out_order_no'])) return [false,'out_order_no不存在!'];
  38. list($status,$box) = $this->dealBox($box,$data);
  39. if(!$status) return [false,$box];
  40. $box->save();
  41. list($status,$msg) = $this->boxDetailInsert($data);
  42. if(!$status) return [false,$msg];
  43. return [true,$box];
  44. }
  45. public function dealBox($box,$data){
  46. $box->order_no = $data['order_no'];
  47. $box->out_order_no = $data['out_order_no'];
  48. // $box->top_id = $data['top_id'];
  49. // $box->num = $data['num'];
  50. $box->ext_1 = isset($data['ext_1']) ? $data['ext_1'] : '';
  51. $box->ext_2 = isset($data['ext_2']) ? $data['ext_2'] : '';
  52. $box->ext_3 = isset($data['ext_3']) ? $data['ext_3'] : '';
  53. $box->ext_4 = isset($data['ext_4']) ? $data['ext_4'] : '';
  54. $box->ext_5 = isset($data['ext_5']) ? $data['ext_5'] : '';
  55. $box->top_order_no = $data['top_order_no'];
  56. $box->shipment_order_no = isset($data['shipment_order_no'])? $data['shipment_order_no'] : '';
  57. return [true,$box];
  58. }
  59. /**
  60. * 包装单详情新增
  61. * @param $data
  62. * @return array
  63. */
  64. public function boxDetailInsert($data){
  65. $order_no = $data['order_no'];
  66. $out_order_no = $data['out_order_no'];
  67. $box_detail = new BoxDetail(['channel'=>$order_no]);
  68. if(!isset($data['detail'])||empty($data['detail'])) return [true,''];
  69. $insert = $data['detail'];
  70. $top_order_no = $data['top_order_no'];
  71. list($status,$insert) = $this->dealBoxDetail($insert,$order_no,$out_order_no,$top_order_no);
  72. if(!$status) return [false,$insert];
  73. $box_detail->insert($insert);
  74. return [true,''];
  75. }
  76. /**
  77. * 包装单详情数据处理
  78. * @param $data
  79. * @return array
  80. */
  81. public function dealBoxDetail($data,$order_no,$out_order_no,$top_order_no){
  82. $insert = [];
  83. $time = time();
  84. foreach ($data as $v){
  85. if(!isset($v['top_id'])) return [false,'top_id不存在!'];
  86. if(!isset($v['num'])) return [false,'数量不存在!'];
  87. $insert[] = [
  88. 'order_no' => $order_no,
  89. 'out_order_no' => $out_order_no,
  90. 'top_id' => $v['top_id'],
  91. 'orders_product_id' => $v['orders_product_id'] ?? 0,
  92. 'code' => '',
  93. 'title' => '',
  94. 'num' => $v['num'],
  95. 'price' => $v['price'],
  96. 'type' => isset($v['type'])?$v['type'] : 1,
  97. 'crt_time' => $time,
  98. 'upd_time' => $time,
  99. 'top_order_no' => $top_order_no,
  100. 'box_type' => $v['box_type'],
  101. 'ext_1' => isset($v['ext_1']) ? $v['ext_1'] : '',
  102. 'ext_2' => isset($v['ext_2']) ? $v['ext_2'] : '',
  103. 'ext_3' => isset($v['ext_3']) ? $v['ext_3'] : '',
  104. 'ext_4' => isset($v['ext_4']) ? $v['ext_4'] : '',
  105. 'ext_5' => isset($v['ext_5']) ? $v['ext_5'] : '',
  106. 'ext_8' => isset($v['ext_8']) ? $v['ext_8'] : '',
  107. 'team_id' => isset($v['team_id']) ? $v['team_id'] : '',
  108. 'shipment_order_no' => isset($v['shipment_order_no']) ? $v['shipment_order_no'] : '',
  109. ];
  110. }
  111. return [true,$insert];
  112. }
  113. /**
  114. * @return string
  115. */
  116. public function setOrderNo(){
  117. return date('YmdHis').rand(1000,9999);
  118. }
  119. /**
  120. * @param $data
  121. * @return array
  122. */
  123. public function boxDetail($data){
  124. // var_dump($data);
  125. if(isset($data['top_order_no'])) $top_order_no = $data['top_order_no'];
  126. else $top_order_no = Box::where('order_no',$data['order_no'])->where('del_time',0)->value('top_order_no');
  127. $box = new BoxDetail(['channel'=>$top_order_no]);
  128. // $list = [];
  129. $box = $box->where('del_time',0);
  130. if(isset($data['id'])) {
  131. $box = $box->where('top_id',$data['id']);
  132. }
  133. if(isset($data['order_no'])){
  134. $box = $box->where('order_no',$data['order_no']);
  135. }
  136. // var_dump($box->toSql());
  137. $box = $box->get()->toArray();
  138. // var_dump($box);die;
  139. return [true,$box];
  140. }
  141. /**
  142. * @param $order_no
  143. * @return array
  144. */
  145. public function delBox($order_no){
  146. $box = Box::where('del_time',0)->where('order_no',$order_no)->first();
  147. if(empty($box)) return [];
  148. $boxDetail = new BoxDetail(['channel' => $box->top_order_no]);
  149. $list = $boxDetail->where('order_no', $order_no)
  150. ->where('del_time',0)
  151. ->select('id','top_id','orders_product_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type')
  152. ->get()->toArray();
  153. $boxDetail->where('order_no',$order_no)
  154. ->where('del_time',0)
  155. ->update(['del_time' => time()]);
  156. $box->del_time = time();
  157. $box->save();
  158. return $list;
  159. }
  160. // /**
  161. // * @param $data
  162. // * @return array
  163. // */
  164. // public function boxDetail($data){
  165. // $order_no = $data['order_no'];
  166. //
  167. // $box = new BoxDetail(['channel'=>$order_no]);
  168. //
  169. // $list = $this->limit($box,'*',$data);
  170. //
  171. // return [true,$list];
  172. // }
  173. }