StoreCouponUser.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. use app\models\wechat\WechatUser as UserModel;
  11. use app\models\user\User;
  12. /**
  13. * TODO 优惠券发放Model
  14. * Class StoreCouponUser
  15. * @package app\models\store
  16. */
  17. class StoreCouponUser extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'store_coupon_user';
  29. protected $type = [
  30. 'coupon_price' => 'float',
  31. 'use_min_price' => 'float',
  32. ];
  33. protected $hidden = [
  34. 'uid'
  35. ];
  36. use ModelTrait;
  37. /**
  38. * TODO 获取用户优惠券(全部)
  39. * @param $uid
  40. * @return mixed
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public static function getUserAllCoupon($uid, $mer_id = '')
  46. {
  47. self::checkInvalidCoupon();
  48. $couponList = self::merSet($mer_id)->where('uid', $uid)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  49. return self::tidyCouponList($couponList);
  50. }
  51. /**
  52. * 获取用户优惠券(未使用)
  53. * @return \think\response\Json
  54. */
  55. public static function getUserValidCoupon($uid, $mer_id = '')
  56. {
  57. self::checkInvalidCoupon();
  58. $couponList = self::merSet($mer_id)->where('uid', $uid)->where('status', 0)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  59. return self::tidyCouponList($couponList);
  60. }
  61. /**
  62. * 获取用户优惠券(已使用)
  63. * @return \think\response\Json
  64. */
  65. public static function getUserAlreadyUsedCoupon($uid, $mer_id = '')
  66. {
  67. self::checkInvalidCoupon();
  68. $couponList = self::merSet($mer_id)->where('uid', $uid)->where('status', 1)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  69. return self::tidyCouponList($couponList);
  70. }
  71. /**
  72. * 获取用户优惠券(已过期)
  73. * @return \think\response\Json
  74. */
  75. public static function getUserBeOverdueCoupon($uid, $mer_id = '')
  76. {
  77. self::checkInvalidCoupon();
  78. $couponList = self::merSet($mer_id)->where('uid', $uid)->where('status', 2)->order('is_fail ASC,status ASC,add_time DESC')->select()->toArray();
  79. return self::tidyCouponList($couponList);
  80. }
  81. public static function beUsableCoupon($uid, $price)
  82. {
  83. return self::where('uid', $uid)->where('is_fail', 0)->where('status', 0)->where('use_min_price', '<=', $price)->find();
  84. }
  85. /**
  86. * 获取用户可以使用的优惠券
  87. * @param $uid
  88. * @param $price
  89. * @return false|\PDOStatement|string|\think\Collection
  90. */
  91. // public static function beUsableCouponList($uid, $price = 0)
  92. // {
  93. // $list = self::where('uid', $uid)->where('is_fail', 0)->where('status', 0)->where('use_min_price', '<=', $price)->order('coupon_price', 'DESC')->select();
  94. // $list = count($list) ? $list->hidden(['type', 'status', 'is_fail'])->toArray() : [];
  95. // foreach ($list as &$item) {
  96. // $item['add_time'] = date('Y/m/d', $item['add_time']);
  97. // $item['end_time'] = date('Y/m/d', $item['end_time']);
  98. // }
  99. // return $list;
  100. // }
  101. public static function beUsableCouponList($uid, $cartId, $price = 0, $mer_id = '')
  102. {
  103. $cartGroup = StoreCart::getUserProductCartList($uid, $cartId, 1, $mer_id);
  104. return self::getUsableCouponList($uid, $cartGroup, $price, $mer_id);
  105. }
  106. public static function validAddressWhere($model = null, $prefix = '')
  107. {
  108. self::checkInvalidCoupon();
  109. if ($prefix) $prefix .= '.';
  110. $model = self::getSelfModel($model);
  111. return $model->where("{$prefix}is_fail", 0)->where("{$prefix}status", 0);
  112. }
  113. public static function checkInvalidCoupon()
  114. {
  115. self::where('end_time', '<', time())->where('status', 0)->update(['status' => 2]);
  116. }
  117. public static function tidyCouponList($couponList)
  118. {
  119. $time = time();
  120. foreach ($couponList as $k => $coupon) {
  121. $coupon['_add_time'] = date('Y/m/d', $coupon['add_time']);
  122. $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
  123. $coupon['use_min_price'] = number_format($coupon['use_min_price'], 2);
  124. $coupon['coupon_price'] = number_format($coupon['coupon_price'], 2);
  125. if ($coupon['is_fail']) {
  126. $coupon['_type'] = 0;
  127. $coupon['_msg'] = '已失效';
  128. } else if ($coupon['status'] == 1) {
  129. $coupon['_type'] = 0;
  130. $coupon['_msg'] = '已使用';
  131. } else if ($coupon['status'] == 2) {
  132. $coupon['_type'] = 0;
  133. $coupon['_msg'] = '已过期';
  134. } else if ($coupon['add_time'] > $time || $coupon['end_time'] < $time) {
  135. $coupon['_type'] = 0;
  136. $coupon['_msg'] = '已过期';
  137. } else {
  138. if ($coupon['add_time'] + 3600 * 24 > $time) {
  139. $coupon['_type'] = 2;
  140. $coupon['_msg'] = '可使用';
  141. } else {
  142. $coupon['_type'] = 1;
  143. $coupon['_msg'] = '可使用';
  144. }
  145. }
  146. $couponList[$k] = $coupon;
  147. }
  148. return $couponList;
  149. }
  150. public static function getUserValidCouponCount($uid, $mer_id = '')
  151. {
  152. self::checkInvalidCoupon();
  153. return self::merSet($mer_id)->where('uid', $uid)->where('status', 0)->order('is_fail ASC,status ASC,add_time DESC')->count();
  154. }
  155. public static function useCoupon($id)
  156. {
  157. return self::where('id', $id)->update(['status' => 1, 'use_time' => time()]);
  158. }
  159. public static function addUserCoupon($uid, $cid, $type = 'get', $mer_id = '')
  160. {
  161. $couponInfo = StoreCoupon::find($cid);
  162. if (!$couponInfo) return self::setErrorInfo('优惠劵不存在!');
  163. $data = [];
  164. $data['cid'] = $couponInfo['id'];
  165. $data['uid'] = $uid;
  166. $data['coupon_title'] = $couponInfo['title'];
  167. $data['coupon_price'] = $couponInfo['coupon_price'];
  168. $data['use_min_price'] = $couponInfo['use_min_price'];
  169. $data['add_time'] = time();
  170. $data['end_time'] = $data['add_time'] + $couponInfo['coupon_time'] * 86400;
  171. $data['type'] = $type;
  172. $data['mer_id'] = $mer_id;
  173. return self::create($data);
  174. }
  175. //获取个人优惠券列表
  176. public static function getOneCouponsList($where)
  177. {
  178. $list = self::where(['uid' => $where['id']])->page((int)$where['page'], (int)$where['limit'])->select();
  179. $data['count'] = self::where(['uid' => $where['id']])->count();
  180. $data['list'] = self::tidyCouponList($list);
  181. return $data;
  182. }
  183. /**
  184. * @param $where
  185. * @return array
  186. */
  187. public static function systemPage($where)
  188. {
  189. $model = new self;
  190. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  191. // if ($where['is_fail'] != '') $model = $model->where('status', $where['is_fail']);
  192. if ($where['coupon_title'] != '') $model = $model->where('coupon_title', 'LIKE', '%'.$where['coupon_title'].'%');
  193. if ($where['nickname'] != '') {
  194. $uid = User::merSet($where['mer_id'])->where('nickname', 'LIKE', '%'.$where['nickname'].'%')->column('uid', 'uid');
  195. $model = $model->where('uid', 'IN', implode(',', $uid));
  196. };
  197. if ($where['mer_id'] != '') $model = $model->where('mer_id', $where['mer_id']);
  198. // $model = $model->where('is_del',0);
  199. $count = $model->count();
  200. $model = $model->order('id desc');
  201. $list = $model->page((int)$where['page'], (int)$where['limit'])
  202. ->select()
  203. ->each(function ($item) {
  204. $item['nickname'] = UserModel::where('uid', $item['uid'])->value('nickname');
  205. $item['type'] = $item['type'] == 'send' ? '后台发放' : '手动领取';
  206. switch ($item['status']) {
  207. case 0:
  208. $item['status'] = '未使用';
  209. break;
  210. case 1:
  211. $item['status'] = '已使用';
  212. break;
  213. case 2:
  214. $item['status'] = '已过期';
  215. break;
  216. }
  217. });
  218. return compact('count', 'list');
  219. }
  220. /**
  221. * 给用户发放优惠券
  222. * @param $coupon
  223. * @param $user
  224. * @return int|string
  225. */
  226. public static function setCoupon($coupon, $user, $mer_id)
  227. {
  228. $data = array();
  229. foreach ($user as $k => $v) {
  230. $data[$k]['cid'] = $coupon['id'];
  231. $data[$k]['uid'] = $v;
  232. $data[$k]['coupon_title'] = $coupon['title'];
  233. $data[$k]['coupon_price'] = $coupon['coupon_price'];
  234. $data[$k]['use_min_price'] = $coupon['use_min_price'];
  235. $data[$k]['add_time'] = time();
  236. $data[$k]['end_time'] = $data[$k]['add_time'] + $coupon['coupon_time'] * 86400;
  237. $data[$k]['mer_id'] = $mer_id;
  238. }
  239. $data_num = array_chunk($data, 30);
  240. self::beginTrans();
  241. $res = true;
  242. foreach ($data_num as $k => $v) {
  243. $res = $res && self::insertAll($v);
  244. }
  245. self::checkTrans($res);
  246. return $res;
  247. }
  248. /**
  249. * @param $uid
  250. * @param $cartGroup
  251. * @param $price
  252. * @return array
  253. */
  254. // public static function getUsableCouponList($uid, $cartGroup, $price)
  255. // {
  256. // $model = new self();
  257. // $list = [];
  258. // $catePrice = [];
  259. // $product_all = [];
  260. // $product_ids = array_unique(array_column($cartGroup['valid'], 'product_id'));
  261. // if (!empty($product_ids)) {
  262. // $product_all = StoreProduct::field('id,cate_id')->where('id', 'in', $product_ids)->select()->toArray();
  263. // if (!empty($product_all)) $product_all = array_combine(array_column($product_all, 'id'), $product_all);
  264. // }
  265. // foreach ($cartGroup['valid'] as $value) {
  266. // if (!empty($value['seckill_id']) || !empty($value['bargain_id']) || !empty($value['combination_id'])) continue;
  267. // $cate_id = $product_all[$value['product_id']]['cate_id'];
  268. // if (!isset($catePrice[$cate_id])) $catePrice[$cate_id] = 0;
  269. // $catePrice[$cate_id] = bcadd(bcmul($value['truePrice'], $value['cart_num'], 2), $catePrice[$cate_id], 2);
  270. // }
  271. //// var_dump($cartGroup['valid']);die;
  272. // foreach ($cartGroup['valid'] as $value) {
  273. // $lst1[] = $model->alias('a')
  274. // ->join('store_coupon b', 'b.id=a.cid')
  275. // ->where('a.uid', $uid)
  276. // ->where('a.is_fail', 0)
  277. // ->where('a.status', 0)
  278. // ->where('a.use_min_price', '<=', bcmul($value['truePrice'], $value['cart_num'], 2))
  279. // ->whereFindinSet('b.product_id', $value['product_id'])
  280. // ->where('b.type', 2)
  281. // ->field('a.*,b.type')
  282. // ->order('a.coupon_price', 'DESC')
  283. // ->select()
  284. // ->hidden(['status', 'is_fail'])
  285. // ->toArray();
  286. // }
  287. // $lst2 = [];
  288. // foreach ($catePrice as $cateIds => $_price) {
  289. // $cateId = explode(',', $cateIds);
  290. // $cate_pid_arr = StoreCategory::field('pid')->where('id', 'in', $cateId)->select()->toArray();
  291. // $cate_pids = [];
  292. // if (!empty($cate_pid_arr)) {
  293. // $cate_pids = array_column($cate_pid_arr, 'pid');
  294. // }
  295. // $cateId = array_merge($cateId, $cate_pids);
  296. // $cateId = array_unique($cateId);
  297. // $where = [];
  298. // foreach ($cateId as $cate_id) {
  299. //// $where ['b.category_id'] = ['exp','FIND_IN_SET('.$cate_id.', b.category_id)'];
  300. // $where [] = 'FIND_IN_SET(' . $cate_id . ', b.category_id)';
  301. // }
  302. // $coupon = $model->alias('a')
  303. // ->join('store_coupon b', 'b.id=a.cid')
  304. // ->where('a.uid', $uid)
  305. // ->where('a.is_fail', 0)
  306. // ->where('a.status', 0)
  307. // ->where('a.use_min_price', '<=', $_price)
  308. //// ->whereFindinSet('b.category_id', $value)
  309. //// ->where(function ($query)use($where){
  310. //// $query->whereOr($where);
  311. //// })
  312. // ->where(implode(' or ', $where))
  313. // ->where('b.type', 1)
  314. // ->field('a.*,b.type')
  315. // ->order('a.coupon_price', 'DESC')
  316. // ->select()
  317. // ->hidden(['status', 'is_fail'])
  318. // ->toArray();
  319. // $lst2 = array_merge($lst2, $coupon);
  320. // }
  321. // if (isset($lst1)) {
  322. // foreach ($lst1 as $value) {
  323. // if ($value) {
  324. // foreach ($value as $v) {
  325. // if ($v) {
  326. // $list[] = $v;
  327. // }
  328. // }
  329. // }
  330. // }
  331. // }
  332. // if (isset($lst2)) {
  333. // foreach ($lst2 as $value) {
  334. // if ($value) {
  335. // foreach ($value as $v) {
  336. // if ($v) {
  337. // $list[] = $v;
  338. // }
  339. // }
  340. // }
  341. // }
  342. // }
  343. // $lst3 = $model->alias('a')
  344. // ->join('store_coupon b', 'b.id=a.cid')
  345. // ->where('a.uid', $uid)
  346. // ->where('a.is_fail', 0)
  347. // ->where('a.status', 0)
  348. // ->where('a.use_min_price', '<=', $price)
  349. // ->where('b.type', 0)
  350. // ->field('a.*,b.type')
  351. // ->order('a.coupon_price', 'DESC')
  352. // ->select()
  353. // ->hidden(['status', 'is_fail'])
  354. // ->toArray();
  355. // $list = array_merge($list, $lst3);
  356. // $list = array_unique_fb($list);
  357. //
  358. // foreach ($list as &$item) {
  359. // $item['add_time'] = date('Y/m/d', $item['add_time']);
  360. // $item['end_time'] = date('Y/m/d', $item['end_time']);
  361. // $item['title'] = $item['coupon_title'];
  362. // }
  363. // return $list;
  364. // }
  365. public static function getUsableCouponList($uid, $cartGroup, $price, $mer_id = '')
  366. {
  367. $model = new self();
  368. $list = [];
  369. $catePrice = [];
  370. foreach ($cartGroup['valid'] as $value) {
  371. if (!empty($value['seckill_id']) || !empty($value['bargain_id']) || !empty($value['combination_id'] || !empty($value['assistance_id']))) continue;
  372. $value['cate_id'] = StoreProduct::where('id', $value['product_id'])->value('cate_id');
  373. if (!isset($catePrice[$value['cate_id']])) $catePrice[$value['cate_id']] = 0;
  374. $catePrice[$value['cate_id']] = bcadd(bcmul($value['truePrice'], $value['cart_num'], 2), $catePrice[$value['cate_id']], 2);
  375. }
  376. // var_dump($cartGroup['valid']);die;
  377. foreach ($cartGroup['valid'] as $value) {
  378. $lst1[] = $model->alias('a')
  379. ->where('a.mer_id', $mer_id)
  380. ->join('store_coupon b', 'b.id=a.cid')
  381. ->where('a.uid', $uid)
  382. ->where('a.is_fail', 0)
  383. ->where('a.status', 0)
  384. ->where('a.use_min_price', '<=', bcmul($value['truePrice'], $value['cart_num'], 2))
  385. ->whereFindinSet('b.product_id', $value['product_id'])
  386. ->where('b.type', 2)
  387. ->field('a.*,b.type')
  388. ->order('a.coupon_price', 'DESC')
  389. ->select()
  390. ->hidden(['status', 'is_fail'])
  391. ->toArray();
  392. }
  393. foreach ($catePrice as $cateIds => $_price) {
  394. $cateId = explode(',', $cateIds);
  395. foreach ($cateId as $value) {
  396. $temp[] = StoreCategory::where('id', $value)->value('pid');
  397. }
  398. $cateId = array_merge($cateId, $temp);
  399. $cateId = array_unique($cateId);
  400. foreach ($cateId as $value) {
  401. $lst2[] = $model->alias('a')
  402. ->where('a.mer_id', $mer_id)
  403. ->join('store_coupon b', 'b.id=a.cid')
  404. ->where('a.uid', $uid)
  405. ->where('a.is_fail', 0)
  406. ->where('a.status', 0)
  407. ->where('a.use_min_price', '<=', $_price)
  408. ->whereFindinSet('b.category_id', $value)
  409. ->where('b.type', 1)
  410. ->field('a.*,b.type')
  411. ->order('a.coupon_price', 'DESC')
  412. ->select()
  413. ->hidden(['status', 'is_fail'])
  414. ->toArray();
  415. }
  416. }
  417. if (isset($lst1)) {
  418. foreach ($lst1 as $value) {
  419. if ($value) {
  420. foreach ($value as $v) {
  421. if ($v) {
  422. $list[] = $v;
  423. }
  424. }
  425. }
  426. }
  427. }
  428. if (isset($lst2)) {
  429. foreach ($lst2 as $value) {
  430. if ($value) {
  431. foreach ($value as $v) {
  432. if ($v) {
  433. $list[] = $v;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. $bool = true;
  440. if(count($cartGroup['valid']) > 1){
  441. $redundant_price = 0;
  442. $bool = false;
  443. foreach ($cartGroup['valid'] as $value) {
  444. if(isset($value['productInfo']['is_ban']) && $value['productInfo']['is_ban'] == 1){
  445. $redundant_price += $value['truePrice'];
  446. }else{
  447. $bool = true;
  448. }
  449. }
  450. $price = bcsub($price, $redundant_price, 2);
  451. }else{
  452. if(isset($cartGroup['valid'][0]['productInfo']['is_ban']) && $cartGroup['valid'][0]['productInfo']['is_ban'] == 1){
  453. $bool = false;
  454. }
  455. }
  456. if($bool){
  457. $lst3 = $model->alias('a')
  458. ->where('a.mer_id', $mer_id)
  459. ->join('store_coupon b', 'b.id=a.cid')
  460. ->where('a.uid', $uid)
  461. ->where('a.is_fail', 0)
  462. ->where('a.status', 0)
  463. ->where('a.use_min_price', '<=', $price)
  464. ->where('b.type', 0)
  465. ->field('a.*,b.type')
  466. ->order('a.coupon_price', 'DESC')
  467. ->select()
  468. ->hidden(['status', 'is_fail'])
  469. ->toArray();
  470. $list = array_merge($list, $lst3);
  471. }
  472. $list = array_unique_fb($list);
  473. foreach ($list as &$item) {
  474. $item['add_time'] = date('Y/m/d', $item['add_time']);
  475. $item['end_time'] = date('Y/m/d', $item['end_time']);
  476. $item['title'] = $item['coupon_title'];
  477. }
  478. return $list;
  479. }
  480. /**
  481. * TODO 恢复优惠券
  482. * @param $id
  483. * @return StoreCouponUser|bool
  484. */
  485. public static function recoverCoupon($id)
  486. {
  487. $status = self::where('id', $id)->value('status');
  488. if ($status) return self::where('id', $id)->update(['status' => 0, 'use_time' => '']);
  489. else return true;
  490. }
  491. }