BoxHookService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Service\Service;
  7. /**
  8. * 包装相关工厂模式
  9. * @package App\Models
  10. */
  11. class BoxHookService extends Service
  12. {
  13. protected static $instance;
  14. protected static $box_header;
  15. protected static $box_detail_header;
  16. public function __construct(){
  17. self::$box_header = Header_ext::where('type','box')->pluck('value','key')->toArray();
  18. self::$box_detail_header = Header_ext::where('type','box_detail')->pluck('value','key')->toArray();
  19. }
  20. public static function getInstance(): self
  21. {
  22. if (self::$instance == null) {
  23. self::$instance = new BoxHookService();
  24. }
  25. return self::$instance;
  26. }
  27. /**
  28. * 包装单新增
  29. * @param $data
  30. * @return array
  31. */
  32. public function boxInsert($data){
  33. $box = new Box();
  34. $data['order_no'] = $this->setOrderNo();
  35. if(!isset($data['out_order_no'])) return [false,'out_order_no is not exist'];
  36. if(!isset($data['top_id'])) return [false,'top_id is not exist'];
  37. if(!isset($data['num'])) return [false,'num is not exist'];
  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. /**
  46. * @param $box
  47. * @param $data
  48. * @return mixed
  49. */
  50. public function dealBox($box,$data){
  51. $box->order_no = $data['order_no'];
  52. $box->out_order_no = $data['out_order_no'];
  53. $box->top_id = $data['top_id'];
  54. $box->num = $data['num'];
  55. $box->ext_1 = isset($data['ext_1']) ? $data['ext_1'] : '';
  56. $box->ext_2 = isset($data['ext_2']) ? $data['ext_2'] : '';
  57. $box->ext_3 = isset($data['ext_3']) ? $data['ext_3'] : '';
  58. $box->ext_4 = isset($data['ext_4']) ? $data['ext_4'] : '';
  59. $box->ext_5 = isset($data['ext_5']) ? $data['ext_5'] : '';
  60. return [true,$box];
  61. }
  62. /**
  63. * 包装单详情新增
  64. * @param $data
  65. * @return array
  66. */
  67. public function boxDetailInsert($data){
  68. $order_no = $data['order_no'];
  69. $out_order_no = $data['out_order_no'];
  70. $box_detail = new BoxDetail(['channel'=>$order_no]);
  71. if(!isset($data['detail'])||empty($data['detail'])) return [true,''];
  72. $insert = $data['detail'];
  73. list($status,$insert) = $this->dealBoxDetail($insert,$order_no,$out_order_no);
  74. if(!$status) return [false,$insert];
  75. $box_detail->insert($insert);
  76. return [true,''];
  77. }
  78. /**
  79. * 包装单详情数据处理
  80. * @param $data
  81. * @return array
  82. */
  83. public function dealBoxDetail($data,$order_no,$out_order_no){
  84. $insert = [];
  85. $time = time();
  86. foreach ($data as $v){
  87. if(!isset($v['top_id'])) return [false,'top_id is not exist'];
  88. if(!isset($v['code'])) return [false,'code is not exist'];
  89. if(!isset($v['title'])) return [false,'title is not exist'];
  90. $insert[] = [
  91. 'order_no' => $order_no,
  92. 'out_order_no' => $out_order_no,
  93. 'top_id' => $v['top_id'],
  94. 'code' => $v['code'],
  95. 'title' => $v['title'],
  96. 'type' => isset($v['type'])?$v['type'] : 1,
  97. 'crt_time' => $time,
  98. 'upd_time' => $time,
  99. 'ext_1' => isset($v['ext_1']) ? $v['ext_1'] : '',
  100. 'ext_2' => isset($v['ext_2']) ? $v['ext_2'] : '',
  101. 'ext_3' => isset($v['ext_3']) ? $v['ext_3'] : '',
  102. 'ext_4' => isset($v['ext_4']) ? $v['ext_4'] : '',
  103. 'ext_5' => isset($v['ext_5']) ? $v['ext_5'] : '',
  104. ];
  105. }
  106. return [true,$insert];
  107. }
  108. /**
  109. * @return string
  110. */
  111. public function setOrderNo(){
  112. return date('YmdHis').rand(1000,9999);
  113. }
  114. /**
  115. * @param $data
  116. * @return array
  117. */
  118. public function boxDetail($data){
  119. $box = new Box();
  120. if(!isset($data['id'])) return [false,'id not found'];
  121. $list = $box->where('top_id',$data['id'])->get()->toArray();
  122. return [true,$list];
  123. }
  124. // /**
  125. // * @param $data
  126. // * @return array
  127. // */
  128. // public function boxDetail($data){
  129. // $order_no = $data['order_no'];
  130. //
  131. // $box = new BoxDetail(['channel'=>$order_no]);
  132. //
  133. // $list = $this->limit($box,'*',$data);
  134. //
  135. // return [true,$list];
  136. // }
  137. }