StoreCouponUser.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\traits\ModelTrait;
  5. /**
  6. * TODO 优惠券发放Model
  7. * Class StoreCouponUser
  8. * @package app\models\store
  9. */
  10. class StoreCouponUser extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'store_coupon_user';
  22. protected $type = [
  23. 'coupon_price' => 'float',
  24. 'use_min_price' => 'float',
  25. ];
  26. protected $hidden = [
  27. 'uid'
  28. ];
  29. use ModelTrait;
  30. /**
  31. * TODO 获取用户优惠券(全部)
  32. * @param $uid
  33. * @return mixed
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. * @throws \think\exception\DbException
  37. */
  38. public static function getUserAllCoupon($uid)
  39. {
  40. self::checkInvalidCoupon();
  41. $couponList = self::where('uid', $uid)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  42. return self::tidyCouponList($couponList);
  43. }
  44. /**
  45. * 获取用户优惠券(未使用)
  46. * @return \think\response\Json
  47. */
  48. public static function getUserValidCoupon($uid)
  49. {
  50. self::checkInvalidCoupon();
  51. $couponList = self::where('uid', $uid)->where('status', 0)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  52. return self::tidyCouponList($couponList);
  53. }
  54. /**
  55. * 获取用户优惠券(已使用)
  56. * @return \think\response\Json
  57. */
  58. public static function getUserAlreadyUsedCoupon($uid)
  59. {
  60. self::checkInvalidCoupon();
  61. $couponList = self::where('uid', $uid)->where('status', 1)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  62. return self::tidyCouponList($couponList);
  63. }
  64. /**
  65. * 获取用户优惠券(已过期)
  66. * @return \think\response\Json
  67. */
  68. public static function getUserBeOverdueCoupon($uid)
  69. {
  70. self::checkInvalidCoupon();
  71. $couponList = self::where('uid', $uid)->where('status', 2)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  72. return self::tidyCouponList($couponList);
  73. }
  74. public static function beUsableCoupon($uid, $price)
  75. {
  76. return self::where('uid', $uid)->where('is_fail', 0)->where('status', 0)->where('use_min_price', '<=', $price)->find();
  77. }
  78. /**
  79. * @param $uid
  80. * @param $cartGroup
  81. * @param $price
  82. * @return array
  83. */
  84. public static function getUsableCouponList($uid, $cartGroup, $price)
  85. {
  86. $model = new self();
  87. $list = [];
  88. $catePrice = [];
  89. foreach ($cartGroup['valid'] as $value) {
  90. if (!empty($value['seckill_id']) || !empty($value['bargain_id']) || !empty($value['combination_id'])) continue;
  91. $value['cate_id'] = StoreProduct::where('id', $value['product_id'])->value('cate_id');
  92. if (!isset($catePrice[$value['cate_id']])) $catePrice[$value['cate_id']] = 0;
  93. $catePrice[$value['cate_id']] = bcadd(bcmul($value['truePrice'], $value['cart_num'], 2), $catePrice[$value['cate_id']], 2);
  94. }
  95. // var_dump($cartGroup['valid']);die;
  96. foreach ($cartGroup['valid'] as $value) {
  97. $lst1[] = $model->alias('a')
  98. ->join('store_coupon b', 'b.id=a.cid')
  99. ->where('a.uid', $uid)
  100. ->where('a.is_fail', 0)
  101. ->where('a.status', 0)
  102. ->where('a.use_min_price', '<=', bcmul($value['truePrice'], $value['cart_num'], 2))
  103. ->whereFindinSet('b.product_id', $value['product_id'])
  104. ->where('b.type', 2)
  105. ->field('a.*,b.type')
  106. ->order('a.coupon_price', 'DESC')
  107. ->select()
  108. ->hidden(['status', 'is_fail'])
  109. ->toArray();
  110. }
  111. foreach ($catePrice as $cateIds => $_price) {
  112. $cateId = explode(',', $cateIds);
  113. foreach ($cateId as $value) {
  114. $temp[] = StoreCategory::where('id', $value)->value('pid');
  115. }
  116. $cateId = array_merge($cateId, $temp);
  117. $cateId = array_filter(array_unique($cateId));
  118. foreach ($cateId as $value) {
  119. $lst2[] = $model->alias('a')
  120. ->join('store_coupon b', 'b.id=a.cid')
  121. ->where('a.uid', $uid)
  122. ->where('a.is_fail', 0)
  123. ->where('a.status', 0)
  124. ->where('a.use_min_price', '<=', $_price)
  125. ->whereFindinSet('b.category_id', $value)
  126. ->where('b.type', 1)
  127. ->field('a.*,b.type')
  128. ->order('a.coupon_price', 'DESC')
  129. ->select()
  130. ->hidden(['status', 'is_fail'])
  131. ->toArray();
  132. }
  133. }
  134. if (isset($lst1)) {
  135. foreach ($lst1 as $value) {
  136. if ($value) {
  137. foreach ($value as $v) {
  138. if ($v) {
  139. $list[] = $v;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. if (isset($lst2)) {
  146. foreach ($lst2 as $value) {
  147. if ($value) {
  148. foreach ($value as $v) {
  149. if ($v) {
  150. $list[] = $v;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. $lst3 = $model->alias('a')
  157. ->join('store_coupon b', 'b.id=a.cid')
  158. ->where('a.uid', $uid)
  159. ->where('a.is_fail', 0)
  160. ->where('a.status', 0)
  161. ->where('a.use_min_price', '<=', $price)
  162. ->where('b.type', 0)
  163. ->field('a.*,b.type')
  164. ->order('a.coupon_price', 'DESC')
  165. ->select()
  166. ->hidden(['status', 'is_fail'])
  167. ->toArray();
  168. $list = array_merge($list, $lst3);
  169. $list = array_unique_fb($list);
  170. foreach ($list as &$item) {
  171. $item['add_time'] = date('Y/m/d', $item['add_time']);
  172. $item['end_time'] = date('Y/m/d', $item['end_time']);
  173. $item['title'] = $item['coupon_title'];
  174. }
  175. return $list;
  176. }
  177. /**
  178. * 获取用户可以使用的优惠券
  179. * @param $uid
  180. * @param $cartId
  181. * @param $price
  182. * @return false|\PDOStatement|string|\think\Collection
  183. */
  184. public static function beUsableCouponList($uid, $cartId, $price = 0)
  185. {
  186. $cartGroup = StoreCart::getUserProductCartList($uid, $cartId, 1);
  187. return self::getUsableCouponList($uid, $cartGroup, $price);
  188. }
  189. public static function validAddressWhere($model = null, $prefix = '')
  190. {
  191. self::checkInvalidCoupon();
  192. if ($prefix) $prefix .= '.';
  193. $model = self::getSelfModel($model);
  194. return $model->where("{$prefix}is_fail", 0)->where("{$prefix}status", 0);
  195. }
  196. public static function checkInvalidCoupon()
  197. {
  198. self::where('end_time', '<', time())->where('status', 0)->update(['status' => 2]);
  199. }
  200. public static function tidyCouponList($couponList)
  201. {
  202. $time = time();
  203. foreach ($couponList as $k => $coupon) {
  204. $coupon['_add_time'] = date('Y/m/d', $coupon['add_time']);
  205. $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
  206. $coupon['use_min_price'] = number_format($coupon['use_min_price'], 2);
  207. $coupon['coupon_price'] = number_format($coupon['coupon_price'], 2);
  208. if ($coupon['is_fail']) {
  209. $coupon['_type'] = 0;
  210. $coupon['_msg'] = '已失效';
  211. } else if ($coupon['status'] == 1) {
  212. $coupon['_type'] = 0;
  213. $coupon['_msg'] = '已使用';
  214. } else if ($coupon['status'] == 2) {
  215. $coupon['_type'] = 0;
  216. $coupon['_msg'] = '已过期';
  217. } else if ($coupon['add_time'] > $time || $coupon['end_time'] < $time) {
  218. $coupon['_type'] = 0;
  219. $coupon['_msg'] = '已过期';
  220. } else {
  221. if ($coupon['add_time'] + 3600 * 24 > $time) {
  222. $coupon['_type'] = 2;
  223. $coupon['_msg'] = '可使用';
  224. } else {
  225. $coupon['_type'] = 1;
  226. $coupon['_msg'] = '可使用';
  227. }
  228. }
  229. $fn = StoreCoupon::get($coupon['cid']);
  230. $coupon['applicable_type'] = 0;
  231. if ($fn['product_id']) {
  232. $coupon['applicable_type'] = 2;
  233. } elseif ($fn['category_id']) {
  234. $coupon['applicable_type'] = 1;
  235. }
  236. $couponList[$k] = $coupon;
  237. }
  238. return $couponList;
  239. }
  240. public static function getUserValidCouponCount($uid)
  241. {
  242. self::checkInvalidCoupon();
  243. return self::where('uid', $uid)->where('status', 0)->order('is_fail ASC,status ASC,add_time DESC')->count();
  244. }
  245. public static function useCoupon($id)
  246. {
  247. return self::where('id', $id)->update(['status' => 1, 'use_time' => time()]);
  248. }
  249. public static function addUserCoupon($uid, $cid, $type = 'get')
  250. {
  251. $couponInfo = StoreCoupon::find($cid);
  252. if (!$couponInfo) return self::setErrorInfo('优惠劵不存在!');
  253. $data = [];
  254. $data['cid'] = $couponInfo['id'];
  255. $data['uid'] = $uid;
  256. $data['coupon_title'] = $couponInfo['title'];
  257. $data['coupon_price'] = $couponInfo['coupon_price'];
  258. $data['use_min_price'] = $couponInfo['use_min_price'];
  259. $data['add_time'] = time();
  260. $data['end_time'] = $data['add_time'] + $couponInfo['coupon_time'] * 86400;
  261. $data['type'] = $type;
  262. return self::create($data);
  263. }
  264. }