CouponreceiveController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\index\controller;
  3. use think\exception\ValidateException;
  4. use app\model\CouponReceive;
  5. use app\model\Coupon;
  6. use app\model\Goods;
  7. use app\model\Category;
  8. class CouponreceiveController extends Base
  9. {
  10. public function submitorder()
  11. {
  12. $price = input('get.price', '', 'serach_in');
  13. $goods_id = input('get.goodsId', '', 'serach_in');
  14. $where['weid'] = weid();
  15. $where['uid'] = UID();
  16. $query = CouponReceive::where($where);
  17. $query->where(['is_use' => 0]);
  18. $query->where('end_time', '>=', time());
  19. if (!empty($price)) {
  20. $query->where('min_price', '<=', $price);
  21. }
  22. $query->where(function ($q) use ($goods_id) {
  23. $Goods = Goods::find($goods_id);
  24. if (!empty($Goods)) {
  25. $Goods = $Goods->toArray();
  26. $parentIds = Category::getParentIdsstr($Goods['cat_id']);
  27. $q->where('use_goods', 0)->whereOr('cat_ids', 'in', $parentIds)->whereOr('goods_ids', $goods_id);
  28. } else {
  29. $q->where('use_goods', 999);
  30. }
  31. });
  32. $data = $query->select()->toArray();
  33. //$sql = $query->getLastsql();
  34. foreach ($data as &$cvo) {
  35. /*
  36. if ($cvo['expire_type'] == 10) {
  37. $cvo['end_time'] = strtotime("+" . $cvo['expire_day'] . " day", $cvo['create_time']);
  38. CouponReceive::where('id', $cvo['id'])->update(['end_time'=>$cvo['end_time']]);
  39. }*/
  40. if($cvo['use_goods']==1){
  41. $cvo['cat_ids_name'] = Category::getTitle($cvo['cat_ids']);
  42. }
  43. if($cvo['use_goods']==2){
  44. $cvo['goods_ids_name'] = Goods::getGoodsName($cvo['goods_ids']);
  45. }
  46. $cvo['reduce_price'] = number_format($cvo['reduce_price'], 0);
  47. $cvo['min_price'] = number_format($cvo['min_price'], 0);
  48. $cvo['start_time'] = time_ymd($cvo['start_time']);
  49. $cvo['end_time'] = time_ymd($cvo['end_time']);
  50. }
  51. return $this->json(['data' => $data, 'sql' => $sql]);
  52. }
  53. public function mylist()
  54. {
  55. $ptype = input('get.ptype', '', 'serach_in');
  56. $price = input('get.price', '', 'serach_in');
  57. $where['weid'] = weid();
  58. $where['uid'] = UID();
  59. $query = CouponReceive::where($where);
  60. if (!empty($price)) {
  61. $query->where('min_price', '<=', $price);
  62. }
  63. if (empty($ptype)) {
  64. $query->where(['is_use' => 0]);
  65. } elseif ($ptype == 1) {
  66. $query->where(['is_use' => 1]);
  67. } elseif ($ptype == 2) {
  68. $query->where('end_time', '<', time());
  69. }
  70. $data = $query->select()->toArray();
  71. foreach ($data as &$cvo) {
  72. if($cvo['use_goods']==1){
  73. $cvo['cat_ids_name'] = Category::getTitle($cvo['cat_ids']);
  74. }
  75. if($cvo['use_goods']==2){
  76. $cvo['goods_ids_name'] = Goods::getGoodsName($cvo['goods_ids']);
  77. }
  78. $cvo['reduce_price'] = number_format($cvo['reduce_price'], 0);
  79. $cvo['min_price'] = number_format($cvo['min_price'], 0);
  80. $cvo['start_time'] = time_ymd($cvo['start_time']);
  81. $cvo['end_time'] = time_ymd($cvo['end_time']);
  82. }
  83. return $this->json(['data' => $data]);
  84. }
  85. //返回可领取的优惠券
  86. public function couponlist()
  87. {
  88. global $_W;
  89. $errno = 0;
  90. $message = '返回消息';
  91. $uid = UID();
  92. $CouponReceive = CouponReceive::where(['uid' => $uid])->select()->toArray();
  93. $coupon_ids = array();
  94. for ($i = 0; $i < count($CouponReceive); $i++) {
  95. array_push($coupon_ids, $CouponReceive[$i]['coupon_id']);
  96. }
  97. $data = Coupon::where(['weid' => weid(), 'ptype' => 1])->select()->toArray();
  98. foreach ($data as &$cvo) {
  99. $cvo['reduce_price'] = number_format($cvo['reduce_price'], 0);
  100. $cvo['min_price'] = number_format($cvo['min_price'], 0);
  101. $cvo['start_time'] = time_ymd($cvo['start_time']);
  102. $cvo['end_time'] = time_ymd($cvo['end_time']);
  103. if (in_array($cvo['id'], $coupon_ids)) {
  104. $cvo['fetch'] = 1;
  105. }
  106. if($cvo['use_goods']==1){
  107. $cvo['cat_ids_name'] = Category::getTitle($cvo['cat_ids']);
  108. }
  109. if($cvo['use_goods']==2){
  110. $cvo['goods_ids_name'] = Goods::getGoodsName($cvo['goods_ids']);
  111. }
  112. }
  113. return $this->json(['data' => $data]);
  114. }
  115. public function add()
  116. {
  117. $id = input('post.coupon_id', '', 'serach_in');
  118. $Coupondata = Coupon::where(['weid' => weid(), 'id' => $id])->find();
  119. if (!empty($Coupondata)) {
  120. $Coupondata = $Coupondata->toArray();
  121. if ($Coupondata['total_num'] == -1 || $Coupondata['total_num'] > $Coupondata['receive_num']) {
  122. unset($Coupondata['id']);
  123. $Coupondata['uid'] = UID();
  124. $Coupondata['coupon_id'] = $id;
  125. $r = CouponReceive::create($Coupondata);
  126. if ($r) {
  127. $message = "领取成功";
  128. $receive_num = $Coupondata['receive_num'] + 1;
  129. Coupon::update(['id' => $id, 'receive_num' => $receive_num]);
  130. } else {
  131. $message = "领取失败";
  132. }
  133. } else {
  134. $message = "领取失败,你来晚了";
  135. }
  136. }
  137. return $this->json(['message' => $message, 'data' => $data]);
  138. }
  139. //领取优惠券
  140. public function fetch()
  141. {
  142. $errno = 0;
  143. $message = '返回消息';
  144. $uid = UID();
  145. $id = input('post.id', '', 'intval');
  146. $Coupondata = Coupon::where(['weid' => weid(), 'id' => $id])->find();
  147. $havecoupon = CouponReceive::where(['coupon_id' => $id, 'uid' => $uid])->find();
  148. if (empty($havecoupon)) {
  149. if (!empty($Coupondata)) {
  150. $Coupondata = $Coupondata->toArray();
  151. //领取优惠券前,判断发放数量
  152. if ($Coupondata['total_num'] == -1 || $Coupondata['total_num'] > $Coupondata['receive_num']) {
  153. unset($Coupondata['id']);
  154. $Coupondata['uid'] = UID() ? UID() : 0;
  155. $Coupondata['coupon_id'] = $id;
  156. if ($Coupondata['expire_type'] == 10) {
  157. $Coupondata['end_time'] = strtotime("+" . $Coupondata['expire_day'] . " day");
  158. }
  159. unset($Coupondata['create_time']);
  160. unset($Coupondata['update_time']);
  161. $r = CouponReceive::create($Coupondata);
  162. if ($r) {
  163. $message = "领取成功";
  164. //领取成功后更新优惠券领取数量
  165. $receive_num = $Coupondata['receive_num'] + 1;
  166. Coupon::update(['id' => $id, 'receive_num' => $receive_num]);
  167. } else {
  168. $message = "领取失败";
  169. }
  170. } else {
  171. $errno = '2001';
  172. $message = '领取失败,你来晚了';
  173. }
  174. }
  175. } else {
  176. $errno = '2003';
  177. $message = '已经领取过了!';
  178. }
  179. return $this->json(['message' => $message, 'errno' => $errno, 'data' => $data]);
  180. }
  181. }