BoxHookService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $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. return [true,$box];
  63. }
  64. /**
  65. * 包装单详情新增
  66. * @param $data
  67. * @return array
  68. */
  69. public function boxDetailInsert($data){
  70. $order_no = $data['order_no'];
  71. $out_order_no = $data['out_order_no'];
  72. $box_detail = new BoxDetail(['channel'=>$order_no]);
  73. if(!isset($data['detail'])||empty($data['detail'])) return [true,''];
  74. $insert = $data['detail'];
  75. list($status,$insert) = $this->dealBoxDetail($insert,$order_no,$out_order_no);
  76. if(!$status) return [false,$insert];
  77. $box_detail->insert($insert);
  78. return [true,''];
  79. }
  80. /**
  81. * 包装单详情数据处理
  82. * @param $data
  83. * @return array
  84. */
  85. public function dealBoxDetail($data,$order_no,$out_order_no){
  86. $insert = [];
  87. $time = time();
  88. foreach ($data as $v){
  89. if(!isset($v['top_id'])) return [false,'top_id is not exist'];
  90. // if(!isset($v['code'])) return [false,'code is not exist'];
  91. // if(!isset($v['title'])) return [false,'title is not exist'];
  92. // if(!isset($v['type'])) return [false,'type is not exist'];
  93. if(!isset($v['num'])) return [false,'type is not exist'];
  94. $insert[] = [
  95. 'order_no' => $order_no,
  96. 'out_order_no' => $out_order_no,
  97. 'top_id' => $v['top_id'],
  98. 'code' => '',
  99. 'title' => '',
  100. 'num' => $v['num'],
  101. 'type' => isset($v['type'])?$v['type'] : 1,
  102. 'crt_time' => $time,
  103. 'upd_time' => $time,
  104. 'ext_1' => isset($v['ext_1']) ? $v['ext_1'] : '',
  105. 'ext_2' => isset($v['ext_2']) ? $v['ext_2'] : '',
  106. 'ext_3' => isset($v['ext_3']) ? $v['ext_3'] : '',
  107. 'ext_4' => isset($v['ext_4']) ? $v['ext_4'] : '',
  108. 'ext_5' => isset($v['ext_5']) ? $v['ext_5'] : '',
  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. $order_no = $data['order_no'];
  125. $box = new BoxDetail(['channel'=>$order_no]);
  126. if(!isset($data['id'])) return [false,'id not found'];
  127. $list = $box->where('top_id',$data['id'])->get()->toArray();
  128. return [true,$list];
  129. }
  130. // /**
  131. // * @param $data
  132. // * @return array
  133. // */
  134. // public function boxDetail($data){
  135. // $order_no = $data['order_no'];
  136. //
  137. // $box = new BoxDetail(['channel'=>$order_no]);
  138. //
  139. // $list = $this->limit($box,'*',$data);
  140. //
  141. // return [true,$list];
  142. // }
  143. }