123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace app\common\model;
- use app\admin\model\Admin;
- use app\admin\model\MobileOrderAdmin;
- use app\common\library\MobileConstant;
- use app\common\service\Refund;
- use app\common\service\SmsSend;
- use think\Db;
- use think\db\Query;
- use think\Model;
- /**
- * 配置模型
- * @method static static filterSaled()
- * @method static static expired()
- * @method static static filterFlow()
- * @method $this filterShow($type)
- * @method $this waitPay()
- */
- class MobileOrder extends Model
- {
- use TraitModel;
- const STATUS_WAIT_SEND=10;
- const STATUS_CAN_SEND=15;
- const STATUS_REFUND=30;
- const STATUS_REFUNDED=90;
- public static $status=[
- 0=>'待付款',
- self::STATUS_WAIT_SEND=>'已付款',
- self::STATUS_CAN_SEND=>'待发货',
- 17=>'有号码未发货',
- 20=>'已发货待激活',
- 25=>'已完成',
- self::STATUS_REFUND=>'申请退款',
- 50=>'已关闭',
- 60=>'无号码',
- 70=>'换卡',
- 80=>'争议单',
- self::STATUS_REFUNDED=>'已退款',
- ];
- public static $refundAllowStatus=[
- self::STATUS_WAIT_SEND,
- 15,
- 17,
- 20,
- 60,
- 70,
- ];
- public static $flowStatus=[
- 0=>'待付款',//待付款
- 10=>'已付款未处理',//已付款未处理
- 15=>'待处理',//待处理
- 20=>'已提交运营商',//已提交运营商
- 23=>'已发货',//已发货
- 25=>'已完结',//已激活/已完结
- 50=>'已关闭',//已激活/已完结
- 110=>'审核失败',//审核失败
- ];
- public static $frontStatus=[
- 0,
- self::STATUS_WAIT_SEND,
- self::STATUS_CAN_SEND,
- 17,20,25,30,90,50,
- ];
- public static $payTypes=[
- 1=>'微信',
- 2=>'支付宝',
- 3=>'京东',
- ];
- protected $hidden=[
- 'status_bak',
- ];
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- public function mobile(){
- return $this->belongsTo(Mobile::class);
- }
- public function info(){
- return $this->hasOne(MobileOrderInfo::class);
- }
- public function mind(){
- return $this->hasOne(MobileOrderMind::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 status(){
- return $this->hasMany(MobileOrderStatus::class);
- }
- public function refundLog(){
- return $this->hasMany(MobileOrderRefundLog::class);
- }
- public function rules(){
- return $this->hasMany(MobileOrderRules::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;
- }
- public function setCityAttr($a){
- if(is_array($a)){
- return implode(',',$a);
- }
- return $a;
- }
- public function saveRules(){
- if($this['type']==1) {
- $mobile=$this['mobile'];
- $filters=MobileConstant::getFilters();
- $rules=[];
- foreach ($filters as $rule=>$filter){
- $fit=false;
- foreach ($filter as $column){
- if($mobile[$column]){
- $fit=true;
- break;
- }
- }
- if($fit){
- $rules[]=[
- 'rule'=>$rule
- ];
- }
- }
- $this->rules()->saveAll($rules);
- }
- }
- protected static function init()
- {
- self::beforeInsert(function (self $mobileOrder){
- $mobileOrder['order_no']=order_no();
- $mobileOrder['expire_time']=time()+86400;
- $mobileOrder['brand']=$mobileOrder['mobile']['brand'];
- #计算盈利
- $mobileOrder['amount_profit']=bcsub($mobileOrder['amount'],$mobileOrder['amount_di']);
- });
- self::afterInsert(function (self $mobileOrder){
- $mobileOrder->info()->save([
- 'mobile'=>$mobileOrder['mobile'],
- 'info'=>$mobileOrder['mobile']['info'],
- ]);
- #保存规律
- $mobileOrder->saveRules();
- });
- self::beforeUpdate(function (self $mobileOrder){
- $data=$mobileOrder->getChangedData();
- #记录status
- if(isset($data['status'])){
- $mobileOrder->status()->save([
- 'status'=>$mobileOrder->origin['status'],
- ]);
- }
- #物流信息
- if(!empty($data['trans_id'])){
- $mobileOrder['trans_name']=LogisticsCompany::getNameById($data['trans_id']);
- }
- if(isset($data['trans_id']) && !$data['trans_id']){
- $mobileOrder['trans_name']=null;
- }
- #主播
- });
- }
- public function sendNotifyToUser(){
- $mobileOrder=$this;
- if($mobileOrder['trans_name'] && $mobileOrder['trans_no']) {
- SmsSend::orderSend($mobileOrder['phone'], $mobileOrder['trans_name'], $mobileOrder['trans_no']);
- }
- }
- public function scopeExpired(Query $query){
- $query->where('status',0)->where('expire_time','<',time())->where('amount','>',0);
- }
- public function scopeFilterFlow(Query $query){
- $query->where('type',2);
- }
- 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('该产品无需支付');
- }
- if(!$this['mobile']){
- throw_user('手机号不存在或已下架');
- }
- $this['mobile']->shouldBuy();
- }
- public function paySuccessCallback($payment=null,$data=[],$type='wechat'){
- $mobileOrder=$this;
- if(!empty($mobileOrder['pay_time'])){
- throw_user("手机订单已支付");
- }
- if($mobileOrder['type']==1) {
- $mobileOrder['status'] = MobileOrder::STATUS_CAN_SEND;
- }else{
- $mobileOrder['status'] = 10;
- }
- if($payment) {
- $payConfig=get_addon_config('epay');
- $mobileOrder['pay_time'] = $payment['pay_time'];
- if ($type == 'wechat') {
- $mobileOrder['pay_type'] = 1;
- $mobileOrder['pay_no'] = $data['transaction_id'];
- $mobileOrder['pay_mid_wechat']=$payConfig['wechat']['mch_id'];
- } elseif ($type == 'alipay') {
- $mobileOrder['pay_type'] = 2;
- $mobileOrder['pay_no'] = $data['trade_no'];
- $mobileOrder['pay_mid_alipay']=$payConfig['alipay']['app_id'];
- }
- $mobileOrder['payment_id'] = $payment['id'];
- SmsSend::orderPayed($mobileOrder['phone']);
- }else{
- $mobileOrder['pay_time']=time();
- }
- if (!$mobileOrder->save()) {
- throw_user("订单保存失败");
- }
- Mobile::whenOrderPayed($mobileOrder->mobile);
- }
- public function originData(){
- return $this->origin;
- }
- public function checkAllowRefund(){
- if(!in_array($this['status'],self::$refundAllowStatus)){
- throw_user('当前状态不允许退款');
- }
- }
- public function makeUserRefund($reason){
- $this['status_bak']=$this['status'];
- $this['status']=self::STATUS_REFUND;
- $this['refund_reason']=$reason;
- $this->save();
- }
- public static function flowStatus(){
- $s=self::$flowStatus;
- return $s;
- }
- public function frontStatus(){
- return self::$frontStatus;
- }
- public function scopeFilterShow(Query $query,$type){
- if ($type==1){
- $query->whereIn('mobile_order.status',$this->frontStatus());
- }else{
- $query->whereIn('mobile_order.status',array_keys(self::flowStatus()));
- }
- }
- public function makeRefund($from,$user,$data){
- $model=$this;
- $model['amount_refund'] = $data['amount'];
- $model['refund_no'] = session_create_id();
- $model['refund_reason']=$data['refund_reason'];
- $statusBak=$model['status_bak'];
- $model['status_bak']=$model['status'];
- if ($data['pass']) {
- if ($data['amount'] > $model['amount']) {
- throw_user('退款金额不能大于付款金额');
- }
- Refund::setType($model)->refund();
- $model['status'] = self::STATUS_REFUNDED;
- SmsSend::orderRefund($model['phone']);
- #退款盈利哦
- $model['amount_profit']=bcsub($model['amount'],$model['amount_refund']);
- }else{
- $model['amount_refund']=0;
- $model['status'] = $statusBak;
- }
- $model->save();
- $model->refundLog()->save(MobileOrderRefundLog::withRefund($from,$user,$data));
- }
- public function flowOrderSubmit(){
- $this['status']=0;
- }
- public function scopeWaitPay(Query $query){
- $query->where('status',0);
- }
- #销售金额
- public function scopeFilterSaled(Query $query){
- $query->whereNotIn('mobile_order.status',[0,50,90]);
- }
- #发货
- public function dealSend($trans_no,$trans_id){
- $this['trans_no']=$trans_no;
- $this['trans_id']=$trans_id;
- if($this['type']==1){
- $this['status']=20;
- }else{
- $this['status']=23;
- }
- $this->save();
- $this->sendNotifyToUser();
- }
- }
|