StoreCouponUser.php 11 KB

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