OrderPack.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 user()
  21. {
  22. return $this->belongsTo(User::class);
  23. }
  24. public function getLabelTextsAttribute()
  25. {
  26. $data = [];
  27. if (!empty($this->label) && is_array($this->label)) {
  28. $map = config('config.pack_label_map');
  29. foreach ($this->label as $k => $v) {
  30. $data[$k]['name'] = $map[$v]??'';
  31. }
  32. }
  33. return $data;
  34. }
  35. public function getUsableStatusAttribute()
  36. {
  37. if ($this->phone_minutes == 0 && $this->chat_num == 0 && $this->appoint_num == 0 && $this->vaccine_limit_amount == 0 && $this->nurses_limit_amount == 0) {
  38. return 0;
  39. }
  40. return 1;
  41. }
  42. public static function checkUserServicePack($order_pack_id, $user_id, $product_type)
  43. {
  44. $data = self::where('id', $order_pack_id)->where('user_id', $user_id)->first();
  45. if (empty($data)) {
  46. exit_out(null, 10001, '服务包不存在');
  47. }
  48. if ($data['end_time'] < time()) {
  49. exit_out(null, 10002, '服务包已过期');
  50. }
  51. if ($product_type == 1) {
  52. if ($data['phone_minutes'] <= 0) {
  53. exit_out(null, 10003, '服务包的电话咨询次数不足');
  54. }
  55. }
  56. elseif ($product_type == 2) {
  57. if ($data['chat_num'] <= 0) {
  58. exit_out(null, 10004, '服务包的图文咨询次数不足');
  59. }
  60. }
  61. elseif ($product_type == 3) {
  62. if ($data['appoint_num'] <= 0) {
  63. exit_out(null, 10005, '服务包的门诊预约次数不足');
  64. }
  65. }
  66. elseif ($product_type == 4) {
  67. if ($data['vaccine_limit_amount'] <= 0) {
  68. exit_out(null, 10006, '服务包疫苗次数不足');
  69. }
  70. }
  71. elseif ($product_type == 5) {
  72. if ($data['nurses_limit_amount'] <= 0) {
  73. exit_out(null, 10007, '服务包儿保次数不足');
  74. }
  75. }
  76. return true;
  77. }
  78. public static function deductPackData($order_pack_id, $product_type)
  79. {
  80. if ($product_type == 1) {
  81. self::where('id', $order_pack_id)->decrement('phone_minutes');
  82. }
  83. elseif ($product_type == 2) {
  84. self::where('id', $order_pack_id)->decrement('chat_num');
  85. }
  86. elseif ($product_type == 3) {
  87. self::where('id', $order_pack_id)->decrement('appoint_num');
  88. }
  89. elseif ($product_type == 4) {
  90. self::where('id', $order_pack_id)->decrement('vaccine_limit_amount');
  91. }
  92. elseif ($product_type == 5) {
  93. self::where('id', $order_pack_id)->decrement('nurses_limit_amount');
  94. }
  95. return true;
  96. }
  97. public function orderpatients(){
  98. return $this->hasOne(OrderPatient::class,'order_id','order_id');
  99. }
  100. public function users(){
  101. return $this->hasOne(User::class,'id','user_id');
  102. }
  103. public function servicepacks(){
  104. return $this->hasOne(ServicePack::class,'id','service_pack_id');
  105. }
  106. }