BoxHookService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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,$box) = $this->boxDetailInsert($data);
  40. if(!$status) return [false,$box];
  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. foreach ($data as $v){
  82. if(!isset($data['top_id'])) return [false,'top_id is not exist'];
  83. if(!isset($data['code'])) return [false,'code is not exist'];
  84. if(!isset($data['title'])) return [false,'title is not exist'];
  85. $insert[] = [
  86. 'order_no' => $order_no,
  87. 'out_order_no' => $out_order_no,
  88. 'top_id' => $v['top_id'],
  89. 'code' => $v['code'],
  90. 'title' => $v['title'],
  91. 'ext_1' => isset($data['ext_1']) ? $data['ext_1'] : '',
  92. 'ext_2' => isset($data['ext_2']) ? $data['ext_2'] : '',
  93. 'ext_3' => isset($data['ext_3']) ? $data['ext_3'] : '',
  94. 'ext_4' => isset($data['ext_4']) ? $data['ext_4'] : '',
  95. 'ext_5' => isset($data['ext_5']) ? $data['ext_5'] : '',
  96. ];
  97. }
  98. return [true,$insert];
  99. }
  100. /**
  101. * @return string
  102. */
  103. public function setOrderNo(){
  104. return date('YmdHis').rand(1000,9999);
  105. }
  106. }