Coupon.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\model;
  20. use app\common\enum\CouponEnum;
  21. use app\common\service\TimeService;
  22. use think\model\concern\SoftDelete;
  23. class Coupon extends BaseModel
  24. {
  25. use SoftDelete;
  26. protected $deleteTime = 'delete_time';
  27. function getUseGoodsIdsAttr($fieldValue, $data)
  28. {
  29. return $fieldValue ? explode(',', $fieldValue) : [];
  30. }
  31. function setUseGoodsIdsAttr($fieldValue, $data)
  32. {
  33. return is_array($fieldValue) ? implode(',', $fieldValue) : ((string) $fieldValue);
  34. }
  35. function getUseGoodsCategoryIdsAttr($fieldValue, $data)
  36. {
  37. return $fieldValue ? explode(',', $fieldValue) : [];
  38. }
  39. function setUseGoodsCategoryIdsAttr($fieldValue, $data)
  40. {
  41. return is_array($fieldValue) ? implode(',', $fieldValue) : ((string) $fieldValue);
  42. }
  43. function getGoodsAttr($fieldValue, $data)
  44. {
  45. if (! in_array($this->use_goods_type, [ CouponEnum::USE_GOODS_TYPE_ALLOW, CouponEnum::USE_GOODS_TYPE_BAN ]) ) {
  46. return [];
  47. }
  48. return Goods::where('id', 'in', $this->use_goods_ids)
  49. ->field('id,code,name,image,min_price,max_price')
  50. ->select()->toArray();
  51. }
  52. function getGoodsCategoryAttr()
  53. {
  54. if ($this->use_goods_type != CouponEnum::USE_GOODS_TYPE_CATEGORY) {
  55. return [];
  56. }
  57. return GoodsCategory::where('id', 'in', $this->use_goods_category_ids)
  58. ->field('id,name,image')
  59. ->select()->toArray();
  60. }
  61. /**
  62. * @notes 商品使用类型
  63. * @param $fieldValue
  64. * @param $data
  65. * @return string
  66. * @author lbzy
  67. * @datetime 2023-11-06 10:16:02
  68. */
  69. function getUseTypeAttr($fieldValue, $data)
  70. {
  71. return CouponEnum::getUseGoodsTypeDesc($data['use_goods_type']);
  72. }
  73. /**
  74. * @notes tips
  75. * @param $fieldValue
  76. * @param $data
  77. * @return string
  78. * @author lbzy
  79. * @datetime 2023-11-07 18:23:44
  80. */
  81. function getTipsAttr($fieldValue, $data)
  82. {
  83. switch ($this->use_goods_type) {
  84. case CouponEnum::USE_GOODS_TYPE_NOT:
  85. $tips = '';
  86. break;
  87. case CouponEnum::USE_GOODS_TYPE_ALLOW:
  88. $goodsName = (new Goods())->whereIn('id', $this->use_goods_ids)->column('name');
  89. $tips = '仅限 ' . implode('、', $goodsName) . ' 商品可用';
  90. break;
  91. case CouponEnum::USE_GOODS_TYPE_BAN:
  92. $goodsName = (new Goods())->whereIn('id', $this->use_goods_ids)->column('name');
  93. $tips = '限制 ' . implode('、', $goodsName) . ' 商品不可用';
  94. break;
  95. case CouponEnum::USE_GOODS_TYPE_CATEGORY:
  96. $goodsName = (new GoodsCategory())->whereIn('id', $this->use_goods_category_ids)->column('name');
  97. $tips = '限制 ' . implode('、', $goodsName) . ' 分类可用';
  98. break;
  99. }
  100. return $tips ?? '';
  101. }
  102. /**
  103. * @notes status_desc 状态
  104. * @param $fieldValue
  105. * @param $data
  106. * @return string
  107. * @author lbzy
  108. * @datetime 2023-11-06 10:16:52
  109. */
  110. function getStatusDescAttr($fieldValue, $data)
  111. {
  112. return CouponEnum::getCouponStatusDesc($this->status);
  113. }
  114. /**
  115. * @notes status_text 状态
  116. * @param $fieldValue
  117. * @param $data
  118. * @return string
  119. * @author lbzy
  120. * @datetime 2023-11-06 10:16:52
  121. */
  122. function getStatusTextAttr($fieldValue, $data)
  123. {
  124. return CouponEnum::getCouponStatusDesc($this->status);
  125. }
  126. /**
  127. * @notes condition 使用条件
  128. * @param $fieldValue
  129. * @param $data
  130. * @return string
  131. * @author lbzy
  132. * @datetime 2023-11-06 10:23:58
  133. */
  134. function getconditionAttr($fieldValue, $data)
  135. {
  136. if (CouponEnum::CONDITION_TYPE_NOT == $this->condition_type) {
  137. return '无金额门槛';
  138. }
  139. return '满' . $this->condition_money . '使用';
  140. }
  141. /**
  142. * @notes discount_content 优惠信息
  143. * @param $fieldValue
  144. * @param $data
  145. * @return string
  146. * @author lbzy
  147. * @datetime 2023-11-06 10:28:01
  148. */
  149. function getDiscountContentAttr($fieldValue, $data)
  150. {
  151. switch ($this->condition_type) {
  152. case CouponEnum::CONDITION_TYPE_NOT:
  153. return '无金额门槛';
  154. case CouponEnum::CONDITION_TYPE_FULL:
  155. return '满' . $this->condition_money . '使用';
  156. case CouponEnum::CONDITION_TYPE_DISCOUNT:
  157. return '满' . $this->condition_money . '打' . $this->discount_ratio . '折';
  158. default:
  159. return '';
  160. }
  161. }
  162. /**
  163. * @notes use_time_text
  164. * @return string|void
  165. * @author lbzy
  166. * @datetime 2023-11-06 10:39:41
  167. */
  168. function getUseTimeTextAttr($fieldValue, $data)
  169. {
  170. switch ($this->use_time_type) {
  171. case CouponEnum::USE_TIME_TYPE_FIXED:
  172. $start = date('Y-m-d H:i:s', $this->use_time_start);
  173. $end = date('Y-m-d H:i:s', $this->use_time_end);
  174. return $start . ' ~ ' . $end;
  175. case CouponEnum::USE_TIME_TYPE_TODAY:
  176. return '领取当日起' . $this->use_time . '天内可用';
  177. case CouponEnum::USE_TIME_TYPE_TOMORROW:
  178. return '领取次日起' . $this->use_time . '天内可用';
  179. }
  180. }
  181. /**
  182. * @notes use_time_text2 显示日期
  183. * @return string|void
  184. * @author lbzy
  185. * @datetime 2023-11-06 10:39:41
  186. */
  187. function getUseTimeText2Attr($fieldValue, $data)
  188. {
  189. switch ($this->use_time_type) {
  190. case CouponEnum::USE_TIME_TYPE_FIXED:
  191. $start = date('Y-m-d', $this->use_time_start);
  192. $end = date('Y-m-d', $this->use_time_end);
  193. return $start . ' ~ ' . $end;
  194. case CouponEnum::USE_TIME_TYPE_TODAY:
  195. return '领取当日起' . $this->use_time . '天内可用';
  196. case CouponEnum::USE_TIME_TYPE_TOMORROW:
  197. return '领取次日起' . $this->use_time . '天内可用';
  198. }
  199. }
  200. /**
  201. * @notes receive_number 领取数量
  202. * @param $fieldValue
  203. * @param $data
  204. * @return int
  205. * @author lbzy
  206. * @datetime 2023-11-06 10:49:57
  207. */
  208. function getReceiveNumberAttr($fieldValue, $data)
  209. {
  210. return CouponList::where('coupon_id', $data['id'])->count();
  211. }
  212. /**
  213. * @notes use_number 使用数量
  214. * @param $fieldValue
  215. * @param $data
  216. * @return int
  217. * @author lbzy
  218. * @datetime 2023-11-06 10:49:57
  219. */
  220. function getUseNumberAttr($fieldValue, $data)
  221. {
  222. return CouponList::where('coupon_id', $data['id'])->where('status', 1)->count();
  223. }
  224. /**
  225. * @notes surplus_number 限制
  226. * @param $fieldValue
  227. * @param $data
  228. * @return mixed|string
  229. * @author lbzy
  230. * @datetime 2023-11-06 10:50:30
  231. */
  232. function getSurplusNumberAttr($fieldValue, $data)
  233. {
  234. return $this->send_total_type == CouponEnum::SEND_TOTAL_TYPE_NOT
  235. ? '不限制'
  236. : $this->send_total - $this->getAttr('receive_number');
  237. }
  238. /**
  239. * @notes send_total_text
  240. * @param $fieldValue
  241. * @param $data
  242. * @return mixed|string
  243. * @author lbzy
  244. * @datetime 2023-11-06 10:54:59
  245. */
  246. function getSendTotalTextAttr($fieldValue,$data)
  247. {
  248. return $this->send_total_type == CouponEnum::SEND_TOTAL_TYPE_NOT ? '不限量' : $this->send_total;
  249. }
  250. /**
  251. * @notes is_available api模块用户是否可用
  252. * @return int
  253. * @author lbzy
  254. * @datetime 2023-11-06 14:16:56
  255. */
  256. function getIsAvailableAttr($fieldValue,$data)
  257. {
  258. switch ($data['get_num_type']) {
  259. case CouponEnum::GET_NUM_TYPE_NOT:
  260. return 0;
  261. case CouponEnum::GET_NUM_TYPE_LIMIT:
  262. $total = (new CouponList())
  263. ->where(['coupon_id' => $data['id']])
  264. ->where(['user_id' => request()->userId ])
  265. ->count();
  266. return $total >= $data['get_num'] ? 1 : 0;
  267. case CouponEnum::GET_NUM_TYPE_DAY:
  268. $total = (new CouponList())
  269. ->where(['coupon_id' => $data['id']])
  270. ->where(['user_id' => request()->userId ])
  271. ->where('create_time', '>=', TimeService::today()[0])
  272. ->where('create_time', '<=', TimeService::today()[1])
  273. ->count();
  274. return $total >= $data['get_num'] ? 1 : 0;
  275. }
  276. return 0;
  277. }
  278. }