BoxHookService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. list($status,$box) = $this->dealBox($box,$data);
  38. if(!$status) return [false,$box];
  39. $box->save();
  40. list($status,$msg) = $this->boxDetailInsert($data);
  41. if(!$status) return [false,$msg];
  42. return [true,$box];
  43. }
  44. /**
  45. * @param $box
  46. * @param $data
  47. * @return mixed
  48. */
  49. public function dealBox($box,$data){
  50. $box->order_no = $data['order_no'];
  51. $box->out_order_no = $data['out_order_no'];
  52. $box->top_id = $data['top_id'];
  53. $box->ext_1 = isset($data['ext_1']) ? $data['ext_1'] : '';
  54. $box->ext_2 = isset($data['ext_2']) ? $data['ext_2'] : '';
  55. $box->ext_3 = isset($data['ext_3']) ? $data['ext_3'] : '';
  56. $box->ext_4 = isset($data['ext_4']) ? $data['ext_4'] : '';
  57. $box->ext_5 = isset($data['ext_5']) ? $data['ext_5'] : '';
  58. return [true,$box];
  59. }
  60. /**
  61. * 包装单详情新增
  62. * @param $data
  63. * @return array
  64. */
  65. public function boxDetailInsert($data){
  66. $order_no = $data['order_no'];
  67. $out_order_no = $data['out_order_no'];
  68. $box_detail = new BoxDetail(['channel'=>$order_no]);
  69. if(!isset($data['detail'])) return [false,'detail is not exist'];
  70. $insert = $data['detail'];
  71. list($status,$insert) = $this->dealBoxDetail($insert,$order_no,$out_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){
  82. $insert = [];
  83. $time = time();
  84. foreach ($data as $v){
  85. if(!isset($v['top_id'])) return [false,'top_id is not exist'];
  86. if(!isset($v['code'])) return [false,'code is not exist'];
  87. if(!isset($v['title'])) return [false,'title is not exist'];
  88. $insert[] = [
  89. 'order_no' => $order_no,
  90. 'out_order_no' => $out_order_no,
  91. 'top_id' => $v['top_id'],
  92. 'code' => $v['code'],
  93. 'title' => $v['title'],
  94. 'type' => isset($v['type'])?$v['type'] : 1,
  95. 'crt_time' => $time,
  96. 'upd_time' => $time,
  97. 'ext_1' => isset($v['ext_1']) ? $v['ext_1'] : '',
  98. 'ext_2' => isset($v['ext_2']) ? $v['ext_2'] : '',
  99. 'ext_3' => isset($v['ext_3']) ? $v['ext_3'] : '',
  100. 'ext_4' => isset($v['ext_4']) ? $v['ext_4'] : '',
  101. 'ext_5' => isset($v['ext_5']) ? $v['ext_5'] : '',
  102. ];
  103. }
  104. return [true,$insert];
  105. }
  106. /**
  107. * @return string
  108. */
  109. public function setOrderNo(){
  110. return date('YmdHis').rand(1000,9999);
  111. }
  112. /**
  113. * @param $data
  114. * @return array
  115. */
  116. public function boxList($data){
  117. $box = new Box();
  118. if(!isset($data['id'])) return [false,'id not found'];
  119. $list = $box->where('top_id',$data['id'])->get()->toArray();
  120. return [true,$list];
  121. }
  122. /**
  123. * @param $data
  124. * @return array
  125. */
  126. public function boxDetail($data){
  127. $order_no = $data['order_no'];
  128. $box = new BoxDetail(['channel'=>$order_no]);
  129. $list = $this->limit($box,'*',$data);
  130. return [true,$list];
  131. }
  132. }