CouponPlanModel.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace app\common\model;
  3. use alipay\request\AlipayUserInfoAuthRequest;
  4. use app\api\services\CouponServices;
  5. use app\common\model\TimeModel;
  6. use jobs\CouponPlanJob;
  7. use think\db\builder\Oracle;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\Model;
  11. use utils\Queue;
  12. class CouponPlanModel extends Model
  13. {
  14. protected $name = "coupon_plan";
  15. public function area ()
  16. {
  17. return $this->belongsTo('app\common\model\CouponAreaModel', 'area_id', 'id');
  18. }
  19. // 添加计划 uid 用户uid fail_id 记录id get_time 请求进来时间cuo
  20. public function addPlan($uid, $money, $plan_id, $is_coin = false, $fail_id, $get_time)
  21. {
  22. // sr_testDb($uid, 99);
  23. if (!$money || !$plan_id)
  24. throw new Exception('购买失败,参数错误');
  25. if ($money % 10 > 0)
  26. {
  27. throw new Exception('购买失败,金额必须是10的整数倍');
  28. }
  29. $plan = self::where('id', $plan_id)->find();
  30. // 判断用户积分
  31. $m_uer = new UserModel();
  32. $userinfo = $m_uer->where('id', $uid)->field('score, is_auth,id,pid,level,path,medal,score_away,income_type')->find();
  33. // 老马的下级不能转赠
  34. if (strpos($userinfo['path'], '7493430') !== false){
  35. sr_throw('失败,请联系上面团队长');
  36. }
  37. if ($uid == 7493430){
  38. sr_throw('失败,请联系上面团队长');
  39. }
  40. $is_fl = false;
  41. if (in_array($plan['area_id'], [17,18])){
  42. // 判断锁定积分
  43. $is_fl = true;
  44. if ($userinfo['income_type'] != 3){
  45. sr_throw('暂无权限购买');
  46. }
  47. }
  48. if ($is_fl && $is_coin){
  49. sr_throw('福利区不能使用优先勋章');
  50. }
  51. if ($userinfo['is_auth'] != 1){
  52. throw new Exception('还未实名,参与失败');
  53. }
  54. if ($is_fl){
  55. if ($userinfo['score_away'] < $money){
  56. throw new Exception('购买失败,用户锁定积分不足');
  57. }
  58. }else{
  59. if ($userinfo['score'] < $money){
  60. throw new Exception('购买失败,用户积分不足');
  61. }
  62. }
  63. if (!$plan){
  64. throw new Exception('购买失败,计划不存在');
  65. }
  66. if ($plan && $plan['state'] != 0){
  67. throw new Exception('已被抢完啦~~~');
  68. }
  69. // 判断计划是否满足增长时间
  70. if ($get_time - strtotime($plan['create_time']) < env('COMMON.PLAN_EMIT_TIME')){
  71. // throw new Exception('购买失败,请在'.sr_getcurtime(strtotime($plan['create_time'])+env('COMMON.PLAN_EMIT_TIME')).'在购买');
  72. throw new Exception('暂未开放');
  73. }
  74. if ($money<$plan['buy_least']){
  75. throw new Exception('购买失败,购买数量小于最小购买数');
  76. }
  77. if (!in_array($uid, [7493267,7493266])){
  78. if ($money>$plan['buy_most']){
  79. throw new Exception('购买失败,购买数量大于最大购买数');
  80. }
  81. }
  82. $modelarea = new CouponAreaModel();
  83. $area = $modelarea->where('id', $plan['area_id'])->find();
  84. if (empty($area['buy_time'])){
  85. throw new Exception('购买失败,购买时间暂未开放');
  86. }
  87. if ($area['can_buy'] != 1){
  88. throw new Exception('购买失败,大区暂未开放,请联系客服');
  89. }
  90. $hasintime = false;
  91. $curdata = json_decode($area['buy_time']);
  92. $day = sr_getcurtime($curdata->day, 'Y-m-d');
  93. $arr_time = $curdata->time;
  94. $curtime = $get_time;
  95. for ($i = 0; $i < count($arr_time); $i++){
  96. $val1 = $curdata->time[$i];
  97. $start_time = strtotime($day.' '.sr_getcurtime($val1->start_time, 'H:i:s'));
  98. $end_time = strtotime($day.' '.sr_getcurtime($val1->end_time, 'H:i:s'));
  99. if ($curtime > $start_time && $curtime < $end_time){
  100. $hasintime = true;
  101. break;
  102. }
  103. }
  104. if (!$hasintime){
  105. if ($is_coin == true){
  106. // if ($userinfo['medal'] < 1){
  107. // throw new Exception('优先勋章不足,购买失败');
  108. // }
  109. $count = Db::name('medal_log')->whereDay('create_at', 'today')->where('uid', $uid)->where('type', 2)->count();
  110. if ($count >4){
  111. if (!in_array($uid, [7493267,7493266])){
  112. throw new Exception('每天使用优先勋章 最多五次');
  113. }
  114. }
  115. }else{
  116. if (in_array($plan['area_id'], [17,18])){
  117. sr_throw('还未到时间,暂未开放');
  118. }else{
  119. throw new Exception('购买失败,购买时间暂未开放', 478);
  120. }
  121. }
  122. }
  123. // if (!$is_coin){
  124. // if (Db::name('coupon_plan_log')->where('plan_id', $plan_id)->where('coin_type', 2)->where('uid', $uid)->count('id') > 4){
  125. // throw new Exception('每人限购五次');
  126. // }
  127. // }
  128. if ($plan['cur_num'] + $money > $plan['target_num']){
  129. $maxBuy = $plan['target_num']-$plan['cur_num'];
  130. if ($maxBuy <= 0){
  131. throw new Exception('购买失败,请稍后再试', 33333);
  132. }else{
  133. throw new Exception('购买失败,'.'最多可购买数量'. strval($maxBuy));
  134. }
  135. }
  136. if ($plan['less_num'] <=0 && $plan['target_num'] > $plan['cur_num']){
  137. Db::name('coupon_plan')->where('id', $plan_id)->inc('less_num', $plan['target_num'] - $plan['cur_num'])->update();
  138. }
  139. $userinfo = $m_uer->where('id', $uid)->field('score, is_auth,id,pid,level,path,medal,score_away,income_type')->find();
  140. if ($is_fl){
  141. if ($userinfo['score_away'] < $money){
  142. throw new Exception('购买失败,用户锁定积分不足');
  143. }
  144. }else{
  145. if ($userinfo['score'] < $money){
  146. throw new Exception('购买失败,用户积分不足');
  147. }
  148. }
  149. if ($is_coin){
  150. // 判断是否是最大额度的一半
  151. // if ($money > ($plan['buy_most']/2)){
  152. // sr_throw('优先勋章最多只能购买'.$plan['buy_most']/2);
  153. // }
  154. $total_cur_sum = $plan['target_num']-$plan['less_num'];
  155. $ed_yiban = $plan['target_num']/2;
  156. if (!in_array($uid, [7493267,7493266])){
  157. if ($total_cur_sum >= $ed_yiban){
  158. sr_throw('优先勋章额度已抢满');
  159. }
  160. if ($total_cur_sum + $money > $ed_yiban){
  161. sr_throw('优先勋章最多只能抢'.($ed_yiban-$total_cur_sum));
  162. }
  163. }
  164. //
  165. // 消耗勋章 并购买
  166. if ($userinfo['medal'] < 1){
  167. throw new Exception('优先勋章不足,购买失败');
  168. }
  169. edit_user_medal(2, $uid, 1);
  170. }
  171. if ($is_fl){
  172. edit_user_scoreaway(2, $uid, $money, $plan_id);
  173. }else{
  174. edit_user_score(1, $uid, $money, $plan_id);
  175. }
  176. // 赠送抢购值
  177. edit_user_couponnum(1, $uid, intval($money/10));
  178. self::where('id', $plan_id)->inc('cur_num', intval($money))->update();
  179. self::where('id', $plan_id)->dec('less_num', intval($money))->update();
  180. $modelplanlog = new CouponPlanLogModel();
  181. $modelplanlog->insert([
  182. 'uid'=>$uid,
  183. 'plan_id'=>$plan_id,
  184. 'buy_num'=>$money,
  185. 'area_id'=>$plan['area_id'],
  186. 'create_time'=>sr_getcurtime($get_time),
  187. 'coin_type'=>$is_coin?1:2
  188. ]);
  189. // 给用户添加业绩
  190. incUserPerformance($uid, $money);
  191. if ($plan['cur_num'] + $money >= $plan['target_num']){
  192. self::where('id', $plan_id)->save(['state' => 1]);
  193. Queue::instance()->log('执行成功新增计划')->do("couponAddNewPlan")->job(CouponPlanJob::class)->push([$plan['area_id'], $plan['id'], 1]);
  194. }
  195. }
  196. public function baocang($id){
  197. $row = self::find($id);
  198. if (!$row){throw new Exception('数据错误');}
  199. if ($row['state'] != 0){
  200. throw new Exception('状态错误');
  201. }
  202. $row->save(['state'=>3]);
  203. Queue::instance()->log('执行成功baocang')->do("couponAddNewPlan")->job(CouponPlanJob::class)->push([$row['area_id'], $id, 2]);
  204. }
  205. public function fafang($id){
  206. $row = self::find($id);
  207. if (!$row){throw new Exception('数据错误');}
  208. if ($row['state'] != 1){
  209. throw new Exception('状态错误');
  210. }
  211. $maxPlan = self::where('area_id', $row['area_id'])->where('lun_num', $row['lun_num'])->order('qi_num desc')->select()->toArray();
  212. if (count($maxPlan) > 0){
  213. if ($maxPlan[0]['qi_num'] - $row['qi_num'] < 3){
  214. throw new Exception('发放失败,认购中才第'.$maxPlan[0]['qi_num'].'期');
  215. }
  216. }
  217. // 改变计划状态
  218. // $row->save(['state'=>2]);
  219. Queue::instance()->log('执行成功fafang')->do("couponPlanFafang")->job(CouponPlanJob::class)->push([$id]);
  220. // $modellog = new CouponPlanLogModel();
  221. // $list = $modellog->where('plan_id', $id)->where('state', 1)->select()->toArray();
  222. // $scale = env('COUPON.FAFANG_SCALE', '0');
  223. // foreach ($list as $key=>$val){
  224. //// edit_user_score(, number_format());
  225. // $user_de = number_format($val['buy_num']*(1+$scale/100), 0, '.', '');
  226. // edit_user_score(2, $val['uid'], $user_de);
  227. // // 修改日志状态
  228. // $modellog->where('id', $val['id'])->save(['state'=>1]);
  229. // }
  230. }
  231. }