123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace App\Service\Box;
- use App\Model\Box;
- use App\Model\BoxDetail;
- use App\Model\Header_ext;
- use App\Service\Service;
- /**
- * 包装相关工厂模式
- * @package App\Models
- */
- class BoxHookService extends Service
- {
- protected static $instance;
- protected static $box_header;
- protected static $box_detail_header;
- public function __construct(){
- self::$box_header = Header_ext::where('type','box')->pluck('value','key')->toArray();
- self::$box_detail_header = Header_ext::where('type','box_detail')->pluck('value','key')->toArray();
- }
- public static function getInstance(): self
- {
- if (self::$instance == null) {
- self::$instance = new BoxHookService();
- }
- return self::$instance;
- }
- /**
- * 包装单新增
- * @param $data
- * @return array
- */
- public function boxInsert($data){
- $box = new Box();
- $data['order_no'] = $this->setOrderNo();
- if(!isset($data['out_order_no'])) return [false,'out_order_no is not exist'];
- list($status,$box) = $this->dealBox($box,$data);
- if(!$status) return [false,$box];
- $box->save();
- list($status,$box) = $this->boxDetailInsert($data);
- if(!$status) return [false,$box];
- return [true,$box];
- }
- /**
- * @param $box
- * @param $data
- * @return mixed
- */
- public function dealBox($box,$data){
- $box->order_no = $data['order_no'];
- $box->out_order_no = $data['out_order_no'];
- $box->ext_1 = isset($data['ext_1']) ? $data['ext_1'] : '';
- $box->ext_2 = isset($data['ext_2']) ? $data['ext_2'] : '';
- $box->ext_3 = isset($data['ext_3']) ? $data['ext_3'] : '';
- $box->ext_4 = isset($data['ext_4']) ? $data['ext_4'] : '';
- $box->ext_5 = isset($data['ext_5']) ? $data['ext_5'] : '';
- return [true,$box];
- }
- /**
- * 包装单详情新增
- * @param $data
- * @return array
- */
- public function boxDetailInsert($data){
- $order_no = $data['order_no'];
- $out_order_no = $data['out_order_no'];
- $box_detail = new BoxDetail(['channel'=>$order_no]);
- if(!isset($data['detail'])) return [false,'detail is not exist'];
- $insert = $data['detail'];
- list($status,$insert) = $this->dealBoxDetail($insert,$order_no,$out_order_no);
- if(!$status) return [false,$insert];
- $box_detail->insert($insert);
- return [true,''];
- }
- /**
- * 包装单详情数据处理
- * @param $data
- * @return array
- */
- public function dealBoxDetail($data,$order_no,$out_order_no){
- $insert = [];
- foreach ($data as $v){
- if(!isset($data['top_id'])) return [false,'top_id is not exist'];
- if(!isset($data['code'])) return [false,'code is not exist'];
- if(!isset($data['title'])) return [false,'title is not exist'];
- $insert[] = [
- 'order_no' => $order_no,
- 'out_order_no' => $out_order_no,
- 'top_id' => $v['top_id'],
- 'code' => $v['code'],
- 'title' => $v['title'],
- 'ext_1' => isset($data['ext_1']) ? $data['ext_1'] : '',
- 'ext_2' => isset($data['ext_2']) ? $data['ext_2'] : '',
- 'ext_3' => isset($data['ext_3']) ? $data['ext_3'] : '',
- 'ext_4' => isset($data['ext_4']) ? $data['ext_4'] : '',
- 'ext_5' => isset($data['ext_5']) ? $data['ext_5'] : '',
- ];
- }
- return [true,$insert];
- }
- /**
- * @return string
- */
- public function setOrderNo(){
- return date('YmdHis').rand(1000,9999);
- }
- }
|