BoxHookService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 is not exist'];
  38. // if(!isset($data['top_id'])) return [false,'top_id is not exist'];
  39. // if(!isset($data['num'])) return [false,'num is not exist'];
  40. list($status,$box) = $this->dealBox($box,$data);
  41. if(!$status) return [false,$box];
  42. $box->save();
  43. list($status,$msg) = $this->boxDetailInsert($data);
  44. if(!$status) return [false,$msg];
  45. return [true,$box];
  46. }
  47. /**
  48. * @param $box
  49. * @param $data
  50. * @return mixed
  51. */
  52. public function dealBox($box,$data){
  53. $box->order_no = $data['order_no'];
  54. $box->out_order_no = $data['out_order_no'];
  55. // $box->top_id = $data['top_id'];
  56. // $box->num = $data['num'];
  57. $box->ext_1 = isset($data['ext_1']) ? $data['ext_1'] : '';
  58. $box->ext_2 = isset($data['ext_2']) ? $data['ext_2'] : '';
  59. $box->ext_3 = isset($data['ext_3']) ? $data['ext_3'] : '';
  60. $box->ext_4 = isset($data['ext_4']) ? $data['ext_4'] : '';
  61. $box->ext_5 = isset($data['ext_5']) ? $data['ext_5'] : '';
  62. $box->top_order_no = $data['top_order_no'];
  63. $box->shipment_order_no = isset($data['shipment_order_no'])? $data['shipment_order_no'] : '';
  64. return [true,$box];
  65. }
  66. /**
  67. * 包装单详情新增
  68. * @param $data
  69. * @return array
  70. */
  71. public function boxDetailInsert($data){
  72. $order_no = $data['order_no'];
  73. $out_order_no = $data['out_order_no'];
  74. $box_detail = new BoxDetail(['channel'=>$order_no]);
  75. if(!isset($data['detail'])||empty($data['detail'])) return [true,''];
  76. $insert = $data['detail'];
  77. $top_order_no = $data['top_order_no'];
  78. list($status,$insert) = $this->dealBoxDetail($insert,$order_no,$out_order_no,$top_order_no);
  79. if(!$status) return [false,$insert];
  80. $box_detail->insert($insert);
  81. return [true,''];
  82. }
  83. /**
  84. * 包装单详情数据处理
  85. * @param $data
  86. * @return array
  87. */
  88. public function dealBoxDetail($data,$order_no,$out_order_no,$top_order_no){
  89. $insert = [];
  90. $time = time();
  91. foreach ($data as $v){
  92. if(!isset($v['top_id'])) return [false,'top_id is not exist'];
  93. // if(!isset($v['code'])) return [false,'code is not exist'];
  94. // if(!isset($v['title'])) return [false,'title is not exist'];
  95. // if(!isset($v['type'])) return [false,'type is not exist'];
  96. if(!isset($v['num'])) return [false,'type is not exist'];
  97. $insert[] = [
  98. 'order_no' => $order_no,
  99. 'out_order_no' => $out_order_no,
  100. 'top_id' => $v['top_id'],
  101. 'code' => '',
  102. 'title' => '',
  103. 'num' => $v['num'],
  104. 'type' => isset($v['type'])?$v['type'] : 1,
  105. 'crt_time' => $time,
  106. 'upd_time' => $time,
  107. 'top_order_no' => $top_order_no,
  108. 'box_type' => $v['box_type'],
  109. 'ext_1' => isset($v['ext_1']) ? $v['ext_1'] : '',
  110. 'ext_2' => isset($v['ext_2']) ? $v['ext_2'] : '',
  111. 'ext_3' => isset($v['ext_3']) ? $v['ext_3'] : '',
  112. 'ext_4' => isset($v['ext_4']) ? $v['ext_4'] : '',
  113. 'ext_5' => isset($v['ext_5']) ? $v['ext_5'] : '',
  114. 'team_id' => isset($v['team_id']) ? $v['team_id'] : '',
  115. 'shipment_order_no' => isset($v['shipment_order_no']) ? $v['shipment_order_no'] : '',
  116. ];
  117. }
  118. return [true,$insert];
  119. }
  120. /**
  121. * @return string
  122. */
  123. public function setOrderNo(){
  124. return date('YmdHis').rand(1000,9999);
  125. }
  126. /**
  127. * @param $data
  128. * @return array
  129. */
  130. public function boxDetail($data){
  131. // var_dump($data);
  132. if(isset($data['top_order_no'])) $top_order_no = $data['top_order_no'];
  133. else $top_order_no = Box::where('order_no',$data['order_no'])->where('del_time',0)->value('top_order_no');
  134. $box = new BoxDetail(['channel'=>$top_order_no]);
  135. // $list = [];
  136. $box = $box->where('del_time',0);
  137. if(isset($data['id'])) {
  138. $box = $box->where('top_id',$data['id']);
  139. }
  140. if(isset($data['order_no'])){
  141. $box = $box->where('order_no',$data['order_no']);
  142. }
  143. // var_dump($box->toSql());
  144. $box = $box->get()->toArray();
  145. // var_dump($box);die;
  146. return [true,$box];
  147. }
  148. /**
  149. * @param $order_no
  150. * @return array
  151. */
  152. public function delBox($order_no){
  153. $box = Box::where('del_time',0)->where('order_no',$order_no)->first();
  154. if(empty($box)) return [];
  155. $boxDetail = new BoxDetail(['channel'=>$box->top_order_no]);
  156. // $list = [];
  157. $list = $boxDetail->where('order_no',$order_no)->where('del_time',0)->select('id','top_id','num','ext_1','ext_2','ext_3','ext_4','ext_5','out_order_no','box_type')->get()->toArray();
  158. $boxDetail->where('order_no',$order_no)->where('del_time',0)->update([
  159. 'del_time' => time()
  160. ]);
  161. $box->del_time = time();
  162. $box->save();
  163. return $list;
  164. }
  165. // /**
  166. // * @param $data
  167. // * @return array
  168. // */
  169. // public function boxDetail($data){
  170. // $order_no = $data['order_no'];
  171. //
  172. // $box = new BoxDetail(['channel'=>$order_no]);
  173. //
  174. // $list = $this->limit($box,'*',$data);
  175. //
  176. // return [true,$list];
  177. // }
  178. }