StoreRechargeCard.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\services\UtilService;
  4. use crmeb\traits\ModelTrait;
  5. use crmeb\basic\BaseModel;
  6. /**
  7. * TODO 充值卡Model
  8. * Class StoreRechargeCard
  9. * @package app\models\store
  10. */
  11. class StoreRechargeCard extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'store_recharge_card';
  23. use ModelTrait;
  24. public static function merSet($mer_id, $alias = '')
  25. {
  26. return $mer_id ? self::where($alias ? $alias . '.mer_id' : 'mer_id', $mer_id) : new self;
  27. }
  28. protected function getAddTimeAttr($value)
  29. {
  30. if ($value) return date('Y-m-d H:i:s', $value);
  31. return '';
  32. }
  33. /**
  34. * 充值卡列表
  35. * @param $where
  36. * @return array
  37. */
  38. public static function systemPage($where)
  39. {
  40. $model = self::setWhere($where);
  41. $count = $model->count();
  42. $list = $model->page((int)$where['page'], (int)$where['limit'])
  43. ->select()
  44. ->each(function ($item) {
  45. $item['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $item['id']])->order('role asc, level asc')->select();
  46. });
  47. return compact('count', 'list');
  48. }
  49. /**
  50. * 设置充值卡 where 条件
  51. * @param $where
  52. * @param null $model
  53. * @return mixed
  54. */
  55. public static function setWhere($where, $model = null)
  56. {
  57. $model = $model === null ? new self() : $model;
  58. $model = $model->where('mer_id', $where['mer_id']);
  59. if ($where['is_brokerage'] != '') $model = $model->where('is_brokerage', $where['is_brokerage']);
  60. if (isset($where['status']) && $where['status'] != '') $model = $model->where('status', $where['status']);
  61. if (isset($where['card_name']) && $where['card_name'] != '') $model = $model->where('id|title', 'LIKE', "%$where[card_name]%");
  62. return $model->order('id desc')->where('is_del', 0);
  63. }
  64. /**
  65. * 详情
  66. */
  67. public static function getOne($id)
  68. {
  69. $info = self::where('is_del', 0)->find($id);
  70. if ($info) {
  71. if ($info['start_time'])
  72. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  73. if ($info['end_time'])
  74. $end_time = date('Y-m-d H:i:s', $info['end_time']);
  75. if (isset($start_time) && isset($end_time))
  76. $info['section_time'] = [$start_time, $end_time];
  77. else
  78. $info['section_time'] = [];
  79. unset($info['start_time'], $info['end_time']);
  80. }
  81. if ($info['poster'])
  82. $info['poster'] = json_decode($info['poster'], true);
  83. else
  84. $info['poster'] = [];
  85. $info['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $id])->order('role asc, level asc')->select();
  86. return $info;
  87. }
  88. /**
  89. * 获取充值卡数据
  90. * @param int $page
  91. * @param int $limit
  92. * @return mixed
  93. */
  94. public static function getAll($mer_id = '')
  95. {
  96. $model = self::merSet($mer_id);
  97. $model = $model->where('status', 1);
  98. $model = $model->where('is_del', 0);
  99. $model = $model->where('start_time', '<', time());
  100. $model = $model->where('end_time', '>', time());
  101. return $model->select()->each(function ($item) {
  102. $item['self_brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $item['id'], 'role' => 1])->find();
  103. $item['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $item['id'], 'role' => 2])->order('level asc')->select();
  104. });
  105. }
  106. /**
  107. * 获取一条充值卡数据
  108. * @param $id
  109. * @return mixed
  110. */
  111. public static function getRechargeCardOne($id)
  112. {
  113. $model = new self();
  114. $model = $model->where('id', $id);
  115. $model = $model->where('status', 1);
  116. $model = $model->where('is_del', 0);
  117. $model = $model->where('start_time', '<', time());
  118. $model = $model->where('end_time', '>', time());
  119. $info = $model->find();
  120. if ($info['poster'])
  121. $info['poster'] = json_decode($info['poster'], true);
  122. else
  123. $info['poster'] = [];
  124. if (!empty($info['price'])) $info['price'] = floatval($info['price']);
  125. if (!empty($info['give_money'])) $info['give_money'] = floatval($info['give_money']);
  126. $info['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $id])->order('role asc, level asc')->select();
  127. if (isset($info['id'])) {
  128. return $info;
  129. } else {
  130. return [];
  131. }
  132. }
  133. }