| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 | <?phpnamespace App\Service\Transport;use App\Model\Box;use App\Model\BoxDetail;use App\Model\Header_ext;use App\Service\Service;/** * 发货相关工厂模式 * @package App\Models */class TransportHookService extends Service{    protected static $instance;    protected static $transport_header;    protected static $transport_detail_header;    public function __construct(){        self::$transport_header = Header_ext::where('type','box')->pluck('value','key')->toArray();        self::$transport_detail_header = Header_ext::where('type','box_detail')->pluck('value','key')->toArray();    }    public static function getInstance(): self    {        if (self::$instance == null) {            self::$instance = new HeaderWordHookService();        }        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,$msg) =  $this->boxDetailInsert($data);        if(!$status) return [false,$msg];        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 = [];        $time = time();        foreach ($data as $v){            if(!isset($v['top_id'])) return [false,'top_id is not exist'];            if(!isset($v['code'])) return [false,'code is not exist'];            if(!isset($v['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'],                'type' => isset($v['type'])?$v['type'] : 1,                'crt_time' => $time,                'upd_time' => $time,                'ext_1' => isset($v['ext_1']) ? $v['ext_1'] : '',                'ext_2' => isset($v['ext_2']) ? $v['ext_2'] : '',                'ext_3' => isset($v['ext_3']) ? $v['ext_3'] : '',                'ext_4' => isset($v['ext_4']) ? $v['ext_4'] : '',                'ext_5' => isset($v['ext_5']) ? $v['ext_5'] : '',            ];        }        return [true,$insert];    }    /**     * @return string     */    public function setOrderNo(){        return date('YmdHis').rand(1000,9999);    }    /**     * @param $data     * @return array     */    public function boxList($data){        $box = new Box();        $list = $this->limit($box,'*',$data);        return [true,$list];    }    /**     * @param $data     * @return array     */    public function boxDetail($data){        $order_no = $data['order_no'];        $box = new BoxDetail(['channel'=>$order_no]);        $list = $this->limit($box,'*',$data);        return [true,$list];    }}
 |