Coupon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\common\model\CouponAreaModel;
  4. use app\common\model\CouponPlanLogModel;
  5. use app\common\model\CouponPlanModel;
  6. use think\cache\driver\Redis;
  7. use think\db\Where;
  8. use think\facade\Db;
  9. use think\Request;
  10. use think\Response;
  11. class coupon
  12. {
  13. // 大区列表
  14. public function getCouponAreaList(Request $request)
  15. {
  16. $apikey = 'getCouponAreaList';
  17. // $redis = new Redis();
  18. // if ($redis->has($apikey)){
  19. // $list = $redis->get($apikey);
  20. // return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  21. // }
  22. $modelarea = new CouponAreaModel();
  23. $list = $modelarea->where('status', '1')->field("id, name, buy_time,hidden_endtime")->order('is_today desc')->select()->toArray();
  24. $curtime = time();
  25. foreach ($list as $key=>&$value){
  26. if (in_array($value['id'], [17, 18])){
  27. $value['is_fl'] = 1;
  28. }else{
  29. $value['is_fl'] = 2;
  30. }
  31. if ($value['buy_time']){
  32. // 当前这个大区最大的一个id的计划的创建时间是否在增长中
  33. $max_id = Db::name('coupon_plan')->where('area_id', $value['id'])->max('id');
  34. $max_info = Db::name('coupon_plan')->where('id', $max_id)->find();
  35. $cur_begin_new = false;
  36. if (sr_getcurtime(strtotime($max_info['create_time']), 'Y-m-d') == sr_getcurtime($curtime, 'Y-m-d')){
  37. // 如果最大一个创建的是今天的话
  38. // 就找到倒数第二个maxid
  39. $max_second_id = Db::name('coupon_plan')->where('area_id', $value['id'])->where('id', '<', $max_id)->max('id');
  40. $max_info = Db::name('coupon_plan')->where('id', $max_second_id)->find();
  41. $cur_begin_new = true;
  42. }
  43. $is_begin = false;
  44. if (strtotime($max_info['create_time']) + env('common.PLAN_EMIT_TIME') < time()){
  45. $is_begin = true;
  46. }
  47. // if ($value['id'] == 14){
  48. // return $is_begin?11:22;
  49. // }
  50. $curdata = json_decode($value['buy_time']);
  51. $day = sr_getcurtime($curdata->day, 'Y-m-d');
  52. $today = sr_getcurtime(time(), 'Y-m-d');
  53. if ($is_begin){
  54. if ($day == $today){
  55. if ($cur_begin_new){
  56. $list[$key]['is_buyying'] = 0;
  57. }else{
  58. $list[$key]['is_buyying'] = 1;
  59. }
  60. }else{
  61. $list[$key]['is_buyying'] = 0;
  62. }
  63. }else{
  64. $list[$key]['is_buyying'] = 0;
  65. }
  66. $arr_time = $curdata->time;
  67. $buy_times = [];
  68. $hidden_endtime = 1;
  69. for ($i = 0; $i < count($arr_time); $i++){
  70. $val = $curdata->time[$i];
  71. // $start_time = strtotime($day.' '.sr_getcurtime($val->start_time, 'H:i:s'));
  72. $start_time = $val->start_time;
  73. // $end_time = strtotime($day.' '.sr_getcurtime($val->end_time, 'H:i:s'));
  74. $end_time = $val->end_time;
  75. if ($curtime < $start_time){
  76. if ($is_begin){
  77. $state = 1;
  78. if ($cur_begin_new){
  79. $state = 3;
  80. }
  81. $buy_times[] = ['start_time' => $start_time, 'state' => $state, 'end_time' => $end_time, 'time' => $start_time - $curtime, 'hidden_endtime' => $hidden_endtime];
  82. }else{
  83. $buy_times[] = ['start_time' => $start_time, 'state' => 4, 'end_time' => $end_time, 'time' => $start_time - $curtime, 'hidden_endtime' => $hidden_endtime];
  84. }
  85. }
  86. if ($curtime > $start_time && $curtime < $end_time){
  87. if ($is_begin){
  88. $buy_times[] = ['start_time' => $start_time, 'state' => 2, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime];
  89. }else{
  90. $buy_times[] = ['start_time' => $start_time, 'state' => 4, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime];
  91. }
  92. }
  93. if ($curtime > $end_time){
  94. if ($is_begin){
  95. $buy_times[] = ['start_time' => $start_time, 'state' => 3, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime];
  96. }else{
  97. $buy_times[] = ['start_time' => $start_time, 'state' => 4, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime];
  98. }
  99. }
  100. }
  101. $list[$key]['buy_time'] = $buy_times;
  102. }else{
  103. $list[$key]['buy_time'] = [];
  104. $list[$key]['is_buyying'] = 0;
  105. }
  106. }
  107. // $redis->set($apikey, $list);
  108. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  109. }
  110. // 计划列表
  111. public function getCouponPlanList(Request $request)
  112. {
  113. $params = $request->param();
  114. $modelplan = new CouponPlanModel();
  115. $where = [];
  116. if (intval($params['state']) != 0){
  117. $where[] = ['p.state', '=', $params['state']];
  118. }
  119. $where[] = ['p.area_id', '=', $params['area_id']];
  120. if (in_array($params['area_id'], [13, 14])){
  121. $where[] = ['p.lun_num', 'not in', [1]];
  122. }
  123. $where[] =
  124. $list = $modelplan
  125. ->alias('p')
  126. ->where($where)
  127. ->leftJoin('coupon_area a', 'a.id = p.area_id')
  128. ->field("p.*,a.coupon_pic,a.buy_time")
  129. ->withAttr('state', function ($val, $data){
  130. if ($val == 0){
  131. // 今天这一期是否还在增长中
  132. $is_begin = false;
  133. if (strtotime($data['create_time']) + env('common.PLAN_EMIT_TIME') < time()){
  134. $is_begin = true;
  135. }
  136. if (!$is_begin){
  137. return -1;
  138. }
  139. // 如果在这一期就判定这个增长时间是否在三个数组内
  140. $curdata = json_decode($data['buy_time']);
  141. $arr_time = $curdata->time;
  142. $valday = $curdata->day;
  143. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  144. $endToday=mktime(24,0,0,date('m'),date('d'),date('Y'));
  145. $is_today = false;
  146. if ($beginToday<=$valday && $endToday>$valday){
  147. $is_today = true;
  148. }
  149. if (!$is_today){
  150. return -1;
  151. }
  152. return $val;
  153. // $can_buy = false;
  154. // $cur = time();
  155. // for ($i = 0; $i < count($arr_time); $i++) {
  156. // $value = $curdata->time[$i];
  157. // $start_time = $value->start_time;
  158. // $end_time = $value->end_time;
  159. // if ($cur>=$start_time && $cur<$end_time){
  160. // $can_buy = true;
  161. // }
  162. // }
  163. // if ($can_buy){
  164. // return $val;
  165. // }
  166. // return -1;
  167. }
  168. return $val;
  169. })
  170. ->withAttr('target_num', function ($val, $data){
  171. return 1;
  172. })
  173. ->withAttr('cur_num', function ($val, $data){
  174. return 1;
  175. })
  176. ->order("p.id desc")
  177. ->select()
  178. ->toArray();
  179. foreach ($list as $key=>&$val){
  180. unset($list[$key]['buy_time']);
  181. if ($val['state'] == -1){
  182. $list[$key]['cur_num'] = 0;
  183. }
  184. if (in_array($val['area_id'], [17, 18])){
  185. $val['is_fl'] = 1;
  186. }else{
  187. $val['is_fl'] = 2;
  188. }
  189. }
  190. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  191. }
  192. // 参加计划
  193. public function addCouponPlan(Request $request)
  194. {
  195. $params = $request->param();
  196. $model = new CouponPlanModel();
  197. $get_time = time();
  198. $is_coin = $request->param('is_coin', 1);
  199. Db::startTrans();
  200. try {
  201. getActionBefore(1);
  202. $model->addPlan($request->uid, $params['money'], $params['plan_id'], ($is_coin==2?true:false), 0, $get_time);
  203. Db::commit();
  204. }catch (\Exception $e){
  205. Db::rollback();
  206. if ($e->getCode() == 33333){
  207. // 超出购买了金额
  208. $coupon = Db::name('coupon_plan')->where('id', $params['plan_id'])->find();
  209. Db::name('coupon_plan')->where('id', $params['plan_id'])->save(['target_num'=>$coupon['cur_num']+10]);
  210. }
  211. if ($e->getCode() == 478){
  212. // sr_log('购买消费券接口478:'.$request->uid.'-'.$request->user['mobile'].'--'.sr_getcurtime($get_time).'---'.json_encode($request->param()));
  213. return api_succ_return(['msg'=>$e->getMessage(), 'code'=>$e->getCode()]);
  214. }else{
  215. if (strpos($e->getMessage(), 'SQLSTATE[22003]') !== false){
  216. return api_error_return('失败,请稍后在试');
  217. }else{
  218. return api_error_return($e->getMessage());
  219. }
  220. }
  221. }
  222. return api_succ_return('购买成功');
  223. }
  224. // 认购记录
  225. public function couponPlanLog(Request $request){
  226. $model = new CouponPlanLogModel();
  227. $post = $request->post();
  228. $where = [];
  229. $where[] = ['l.uid', '=', $request->uid];
  230. if (!empty($post['state'])){
  231. $where[] = ['l.state', '=', $post['state']];
  232. }
  233. if (!empty($post['area_id'])){
  234. $where[] = ['l.area_id', '=', $post['area_id']];
  235. }
  236. // return $where;
  237. // $list = $modelplan
  238. // ->alias('p')
  239. // ->where($where)
  240. // ->leftJoin('coupon_area a', 'a.id = p.area_id')
  241. // ->field("p.*,a.coupon_pic")
  242. // ->select()
  243. // ->toArray();
  244. $data = $model
  245. ->alias('l')
  246. ->field('l.*,p.plan_name,a.coupon_pic,a.short_name,p.lun_num,p.qi_num')
  247. ->where($where)
  248. ->leftJoin('coupon_plan p', 'p.id = l.plan_id')
  249. ->leftJoin('coupon_area a', 'a.id = l.area_id')
  250. ->paginate($request->data['limit'])->toArray();
  251. return api_succ_return(['msg'=>'成功', 'data'=>$data['data']]);
  252. }
  253. // 福利消费抢购购买记录
  254. public function getflCouponLog(Request $request){
  255. $plan_id = $request->post('plan_id');
  256. $plan_info = Db::name('coupon_plan')->where('id', $plan_id)->find();
  257. if (!$plan_info){
  258. return api_error_return('参数错误');
  259. }
  260. if (!in_array($plan_info['area_id'], [17, 18])){
  261. return api_error_return('错误');
  262. }
  263. // 如果是 最大的一个福利区,并且小于当前抢购的时间返回空
  264. $time = time();
  265. if ($time - strtotime(sr_getcurtime($time, 'Y-m-d')) < 72000){
  266. $max_id = Db::name('coupon_plan')->where('area_id', $plan_info['area_id'])->max('id');
  267. if ($max_id == $plan_id){
  268. return api_succ_return(['msg'=>'成功', 'data'=>[]]);
  269. }
  270. }
  271. $where = [];
  272. // $where[] = ['l.uid', '=', $request->uid];
  273. $where[] = ['l.plan_id', '=', $plan_id];
  274. // if (!empty($post['state'])){
  275. // $where[] = ['l.state', '=', $post['state']];
  276. // }
  277. // if (!empty($post['area_id'])){
  278. // $where[] = ['l.area_id', '=', $post['area_id']];
  279. // }
  280. $model = new CouponPlanLogModel();
  281. $list = $model
  282. ->alias('l')
  283. // ->field('l.*,u.nickname,a.short_name,p.lun_num,p.qi_num')
  284. ->field('l.buy_num,l.create_time,l.id,u.nickname,p.plan_name,a.coupon_pic,a.short_name,p.lun_num,p.qi_num')
  285. ->where($where)
  286. ->leftJoin('user u', 'u.id = l.uid')
  287. ->leftJoin('coupon_plan p', 'p.id = l.plan_id')
  288. ->leftJoin('coupon_area a', 'a.id = l.area_id')
  289. ->page($request->post('page'), $request->post('limit'))
  290. ->select()
  291. ->toArray();
  292. foreach ($list as $key=>&$val){
  293. $val['create_time'] = sr_getcurtime(strtotime($val['create_time']), 'Y-m-d');
  294. }
  295. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  296. }
  297. }