UserTax.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class UserTax extends Model
  5. {
  6. protected $autoWriteTimestamp=true;
  7. protected $updateTime=null;
  8. protected $type=[
  9. 'tax'=>'json',
  10. ];
  11. public static function fromOrder(Orders $orders){
  12. if($orders['tax']){
  13. $has=self::where('user_id',$orders['user_id'])->where('tax',json_encode($orders['tax'],JSON_UNESCAPED_UNICODE))->find();
  14. if(!$has) {
  15. self::create([
  16. 'user_id' => $orders['user_id'],
  17. 'tax' => $orders['tax'],
  18. 'order_id'=>$orders->id,
  19. ]);
  20. }
  21. }
  22. }
  23. public function orders(){
  24. return $this->belongsTo(Orders::class,'order_id');
  25. }
  26. protected static function init()
  27. {
  28. self::afterWrite(function (self $tax){
  29. if(!empty($tax['is_default'])){
  30. self::where('user_id',$tax['user_id'])->where('id','<>',$tax['id'])->update(['is_default'=>0]);
  31. }
  32. });
  33. }
  34. }