123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace app\common\model;
- use app\admin\model\Admin;
- use app\admin\model\MobileOrderAdmin;
- use think\db\Query;
- use think\Model;
- /**
- * 配置模型
- * @method static static payed()
- */
- class MobileOrder extends Model
- {
- const STATUS_WAIT_SEND=10;
- public static $status=[
- 0=>'待付款',
- 10=>'已付款',
- 15=>'可发货',
- 17=>'有号码未发货',
- 20=>'待开卡',
- 25=>'已完成',
- 30=>'申请退款',
- 50=>'已关闭',
- 60=>'无号码',
- 70=>'换卡',
- 80=>'争议单',
- 90=>'已退款',
- ];
- public static $payTypes=[
- 1=>'微信',
- 2=>'支付宝',
- 3=>'京东',
- ];
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- public function mobile(){
- return $this->belongsTo(Mobile::class);
- }
- public function info(){
- return $this->hasOne(MobileOrderInfo::class);
- }
- public function operation(){
- return $this->hasMany(MobileOrderOperation::class)->order('mobile_order_operation.id','desc');
- }
- public function admin(){
- return $this->hasMany(MobileOrderAdmin::class);
- }
- public function proxy(){
- $columns=Admin::getTableInfo()['fields'];
- unset($columns['password']);
- return $this->belongsTo(Admin::class)->field($columns);
- }
- public function getAddressAttr($a,$b){
- return Area::getNameString($b['city']).$a;
- }
- protected static function init()
- {
- self::beforeInsert(function (self $mobileOrder){
- $mobileOrder['order_no']=order_no();
- $mobileOrder['expire_time']=time()+86400;
- $mobileOrder['s_id']=$mobileOrder['mobile']['s_id'];
- $mobileOrder['brand']=$mobileOrder['mobile']['brand'];
- });
- self::afterInsert(function (self $mobileOrder){
- $mobileOrder->info()->save([
- 'mobile'=>$mobileOrder['mobile'],
- 'info'=>$mobileOrder['mobile']['info'],
- ]);
- });
- self::beforeUpdate(function (self $mobileOrder){
- $data=$mobileOrder->getChangedData();
- #已支付
- if(isset($data['status']) && $data['status']==self::STATUS_WAIT_SEND){
- SysConfig::set('mo_ordered_num',SysConfig::look('mo_ordered_num',0)+1);
- Mobile::whenOrderPayed($mobileOrder->mobile);
- }
- });
- }
- public function scopeExpired(Query $query){
- $query->where('status',0)->where('expire_time','<',time());
- }
- public function cancel(){
- if($this['pay_time'] || $this['pay_time']!=0){
- return;
- }
- $this['status']=50;
- $this->save();
- }
- public function continuePay(){
- if($this['expire_time']<time()){
- throw_user('订单已过期');
- }
- if($this['status']!=0){
- throw_user('订单状态有误');
- }
- if($this['type']!=1){
- throw_user('该产品无需支付');
- }
- }
- public function scopePayed(Query $query){
- $query->where('status','>=',self::STATUS_WAIT_SEND);
- }
- public function originData(){
- return $this->origin;
- }
- }
|