OrderPack.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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'];
  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)) {
  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 static function checkUserServicePack($order_pack_id, $user_id, $product_type, $payment_amount)
  36. {
  37. $data = self::where('id', $order_pack_id)->where('user_id', $user_id)->first();
  38. if (empty($data)) {
  39. exit_out(null, 10001, '服务包不存在');
  40. }
  41. if ($data['end_time'] < time()) {
  42. exit_out(null, 10002, '服务包已过期');
  43. }
  44. if ($product_type == 1) {
  45. if ($data['phone_minutes'] < 10) {
  46. exit_out(null, 10003, '服务包的电话咨询时间不足');
  47. }
  48. }
  49. elseif ($product_type == 2) {
  50. if ($data['chat_num'] <= 0) {
  51. exit_out(null, 10004, '服务包的图文咨询次数不足');
  52. }
  53. }
  54. elseif ($product_type == 3) {
  55. if ($data['appoint_num'] <= 0) {
  56. exit_out(null, 10005, '服务包的门诊预约次数不足');
  57. }
  58. }
  59. elseif ($product_type == 4) {
  60. if ($data['vaccine_limit_amount'] < $payment_amount) {
  61. exit_out(null, 10006, '服务包疫苗金额不足');
  62. }
  63. }
  64. elseif ($product_type == 5) {
  65. if ($data['nurses_limit_amount'] < $payment_amount) {
  66. exit_out(null, 10007, '服务包儿保金额不足');
  67. }
  68. }
  69. return true;
  70. }
  71. public static function deductPackData($order_pack_id, $product_type, $payment_amount)
  72. {
  73. if ($product_type == 1) {
  74. self::where('id', $order_pack_id)->decrement('phone_minutes', 10);
  75. }
  76. elseif ($product_type == 2) {
  77. self::where('id', $order_pack_id)->decrement('chat_num');
  78. }
  79. elseif ($product_type == 3) {
  80. self::where('id', $order_pack_id)->decrement('appoint_num');
  81. }
  82. elseif ($product_type == 4) {
  83. self::where('id', $order_pack_id)->decrement('vaccine_limit_amount', $payment_amount);
  84. }
  85. elseif ($product_type == 5) {
  86. self::where('id', $order_pack_id)->decrement('nurses_limit_amount', $payment_amount);
  87. }
  88. return true;
  89. }
  90. }