BoxHookService.php 4.2 KB

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