OrderPack.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-10-2
  6. * Time: 下午10:57
  7. */
  8. namespace App\Models;
  9. class OrderPack extends BaseModel
  10. {
  11. protected $appends = ['label_texts', 'usable_status'];
  12. protected $casts = [
  13. 'label' => 'json',
  14. 'team_id' => 'json'
  15. ];
  16. public function order()
  17. {
  18. return $this->belongsTo(Order::class);
  19. }
  20. public function teams(){
  21. return $this->hasOne(Team::class,'id','team_id');
  22. }
  23. public function user()
  24. {
  25. return $this->belongsTo(User::class);
  26. }
  27. public function getLabelTextsAttribute()
  28. {
  29. $data = [];
  30. if (!empty($this->label) && is_array($this->label)) {
  31. $map = config('config.pack_label_map');
  32. foreach ($this->label as $k => $v) {
  33. $data[$k]['name'] = $map[$v]??'';
  34. }
  35. }
  36. return $data;
  37. }
  38. public function getUsableStatusAttribute()
  39. {
  40. if ($this->phone_minutes == 0 && $this->chat_num == 0 && $this->appoint_num == 0 && $this->vaccine_limit_amount == 0 && $this->nurses_limit_amount == 0) {
  41. return 0;
  42. }
  43. return 1;
  44. }
  45. public static function checkUserServicePack($order_pack_id, $user_id, $product_type)
  46. {
  47. $data = self::where('id', $order_pack_id)->where('user_id', $user_id)->first();
  48. if (empty($data)) {
  49. exit_out(null, 10001, '服务包不存在');
  50. }
  51. if ($data['end_time'] < time()) {
  52. exit_out(null, 10002, '服务包已过期');
  53. }
  54. if ($product_type == 1) {
  55. if ($data['phone_minutes'] <= 0) {
  56. exit_out(null, 10003, '服务包的电话咨询次数不足');
  57. }
  58. }
  59. elseif ($product_type == 2) {
  60. if ($data['chat_num'] <= 0) {
  61. exit_out(null, 10004, '服务包的图文咨询次数不足');
  62. }
  63. }
  64. elseif ($product_type == 3) {
  65. if ($data['appoint_num'] <= 0) {
  66. exit_out(null, 10005, '服务包的门诊预约次数不足');
  67. }
  68. }
  69. elseif ($product_type == 4) {
  70. if ($data['vaccine_limit_amount'] <= 0) {
  71. exit_out(null, 10006, '服务包疫苗次数不足');
  72. }
  73. }
  74. elseif ($product_type == 5) {
  75. if ($data['nurses_limit_amount'] <= 0) {
  76. exit_out(null, 10007, '服务包儿保次数不足');
  77. }
  78. }
  79. return true;
  80. }
  81. public static function deductPackData($order_pack_id, $product_type)
  82. {
  83. if ($product_type == 1) {
  84. self::where('id', $order_pack_id)->decrement('phone_minutes');
  85. }
  86. elseif ($product_type == 2) {
  87. self::where('id', $order_pack_id)->decrement('chat_num');
  88. }
  89. elseif ($product_type == 3) {
  90. self::where('id', $order_pack_id)->decrement('appoint_num');
  91. }
  92. elseif ($product_type == 4) {
  93. self::where('id', $order_pack_id)->decrement('vaccine_limit_amount');
  94. }
  95. elseif ($product_type == 5) {
  96. self::where('id', $order_pack_id)->decrement('nurses_limit_amount');
  97. }
  98. return true;
  99. }
  100. public function orderpatients(){
  101. return $this->hasOne(OrderPatient::class,'order_id','order_id');
  102. }
  103. public function users(){
  104. return $this->hasOne(User::class,'id','user_id');
  105. }
  106. public function servicepacks(){
  107. return $this->hasOne(ServicePack::class,'id','service_pack_id');
  108. }
  109. }