Coupon.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace addon\mall\app\model\coupon;
  12. use addon\mall\app\dict\coupon\CouponDict;
  13. use app\model\site\Site;
  14. use core\base\BaseModel;
  15. use think\contract\Jsonable;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\db\Query;
  20. use think\model\relation\HasMany;
  21. use think\model\relation\hasOne;
  22. /**
  23. * 优惠券模型
  24. */
  25. class Coupon extends BaseModel
  26. {
  27. /**
  28. * 数据表主键
  29. * @var string
  30. */
  31. protected $pk = 'id';
  32. /**
  33. * 模型名称
  34. * @var string
  35. */
  36. protected $name = 'mall_coupon';
  37. protected $type = [
  38. 'start_time' => 'timestamp',
  39. 'end_time' => 'timestamp',
  40. 'valid_start_time' => 'timestamp',
  41. 'valid_end_time' => 'timestamp',
  42. ];
  43. // 设置json类型字段
  44. protected $json = [ 'join_site_ids' ];
  45. // 设置JSON数据返回数组
  46. protected $jsonAssoc = true;
  47. /**
  48. * 优惠券商品项
  49. * @return HasMany
  50. */
  51. public function goods()
  52. {
  53. return $this->hasMany(CouponGoods::class, 'coupon_id', 'id');
  54. }
  55. /**
  56. * 关联站点表
  57. * @return hasOne
  58. */
  59. public function site()
  60. {
  61. return $this->hasOne(Site::class, 'site_id', 'site_id');
  62. }
  63. /**
  64. * 关联商品项
  65. * @param $value
  66. * @param $data
  67. * @return int|string
  68. * @throws DataNotFoundException
  69. * @throws DbException
  70. * @throws ModelNotFoundException
  71. */
  72. // public function getGoodsDataAttr($value, $data)
  73. // {
  74. // $coupon_id = $data['id'] ?? 0;
  75. // $list = [];
  76. // if($coupon_id > 0) {
  77. // $coupon_id = $data['id'];
  78. // $list = (new CouponGoods())->where([['coupon_id', '=', $coupon_id]])->select();
  79. // }
  80. // return $list;
  81. // }
  82. public function getCouponPriceAttr($value, $data)
  83. {
  84. if (empty($data['price']))
  85. {
  86. return 0;
  87. }
  88. return rtrim(rtrim($data['price'], '0'), '.');
  89. }
  90. public function getCouponMinPriceAttr($value, $data)
  91. {
  92. if (empty($data['min_condition_money']))
  93. {
  94. return 0;
  95. }
  96. return rtrim(rtrim($data['min_condition_money'], '0'), '.');
  97. }
  98. /**
  99. * 搜索器:标题
  100. * @param $value
  101. * @param $data
  102. */
  103. public function searchTitleAttr($query, $value, $data)
  104. {
  105. if ($value) {
  106. $query->where('title', 'like', '%'.$value.'%');
  107. }
  108. }
  109. /**
  110. * 搜索器:状态
  111. * @param $value
  112. * @param $data
  113. */
  114. public function searchStatusAttr($query, $value, $data)
  115. {
  116. if ($value != '') {
  117. $query->where('status', '=', $value);
  118. }
  119. }
  120. /**
  121. * 搜索器:优惠券类型
  122. * @param $value
  123. * @param $data
  124. */
  125. public function searchTypeAttr($query, $value, $data)
  126. {
  127. if ($value) {
  128. $query->where('type', '=', $value);
  129. }
  130. }
  131. /**
  132. * 搜索器:领取方式
  133. * @param $value
  134. * @param $data
  135. */
  136. public function searchReceiveTypeAttr($query, $value, $data)
  137. {
  138. if ($value) {
  139. $query->where('receive_type', '=', $value);
  140. }
  141. }
  142. /**
  143. * 搜索器:站点id
  144. * @param $value
  145. * @param $data
  146. */
  147. public function searchSiteIdAttr($query, $value, $data)
  148. {
  149. if ($value != '') {
  150. if (is_array($value)) {
  151. $query->where('site_id', 'in', $value);
  152. } else {
  153. $query->where('site_id', '=', $value);
  154. }
  155. }
  156. }
  157. /**
  158. * 类型
  159. * @param $value
  160. * @param $data
  161. * @return string
  162. */
  163. public function getTypeNameAttr($value, $data)
  164. {
  165. if (empty($data['type']))
  166. return '';
  167. return CouponDict::getType()[$data['type']] ?? '';
  168. }
  169. /**
  170. * 领取类型
  171. * @param $value
  172. * @param $data
  173. * @return string
  174. */
  175. public function getReceiveTypeNameAttr($value, $data)
  176. {
  177. if (empty($data['receive_type']))
  178. return '';
  179. return CouponDict::getReceiveType()[$data['receive_type']] ?? '';
  180. }
  181. /**
  182. * 优惠券状态
  183. * @param $value
  184. * @param $data
  185. * @return string
  186. */
  187. public function getStatusNameAttr($value, $data)
  188. {
  189. return CouponDict::getStatus()[$data['status']] ?? '';
  190. }
  191. /**
  192. * 活动开始时间搜索器
  193. * @param Query $query
  194. * @param $value
  195. * @param $data
  196. */
  197. public function searchStartTimeAttr(Query $query, $value, $data)
  198. {
  199. $start_time = empty($value[0]) ? 0 : strtotime($value[0]);
  200. $end_time = empty($value[1]) ? 0 : strtotime($value[1]);
  201. if ($start_time > 0 && $end_time > 0) {
  202. $query->whereBetweenTime('start_time', $start_time, $end_time);
  203. } elseif ($start_time > 0 && $end_time == 0) {
  204. $query->where([['start_time', '>=', $start_time]]);
  205. } elseif ($start_time == 0 && $end_time > 0) {
  206. $query->where([['start_time', '<=', $end_time]]);
  207. }
  208. }
  209. /**
  210. * 活动结束时间搜索器
  211. * @param Query $query
  212. * @param $value
  213. * @param $data
  214. */
  215. public function searchEndTimeAttr(Query $query, $value, $data)
  216. {
  217. $start_time = empty($value[0]) ? 0 : strtotime($value[0]);
  218. $end_time = empty($value[1]) ? 0 : strtotime($value[1]);
  219. if ($start_time > 0 && $end_time > 0) {
  220. $query->whereBetweenTime('end_time', $start_time, $end_time);
  221. } elseif ($start_time > 0 && $end_time == 0) {
  222. $query->where([['end_time', '>=', $start_time]]);
  223. } elseif ($start_time == 0 && $end_time > 0) {
  224. $query->where([['end_time', '<=', $end_time]]);
  225. }
  226. }
  227. /**
  228. * 领取生效时间范围搜索器(查询生效时间范围内的优惠券)
  229. * @param Query $query
  230. * @param $value
  231. * @param $data
  232. */
  233. public function searchReceiveTimeAttr(Query $query, $value, $data)
  234. {
  235. $now_time = time();
  236. $time_where = [
  237. [
  238. [ 'start_time', '<=', $now_time ],
  239. [ 'end_time', '>=', $now_time ],
  240. ],
  241. [
  242. [ 'start_time', '=', 0 ],
  243. [ 'end_time', '=', 0 ],
  244. ]
  245. ];
  246. $query->where(function ($query) use ($time_where){
  247. $query->whereOr($time_where);
  248. });
  249. }
  250. }