InvitegiftModel.class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 邀新有礼模块
  6. * @author cy 2021-03-11
  7. *
  8. */
  9. class InvitegiftModel{
  10. public function getInvitegiftInfo($member_id){
  11. $need_data = [];
  12. //邀新有礼配置信息
  13. $config_data = $this->getInvitegiftConfig();
  14. if(isset($config_data['is_invite_open_status']) && $config_data['is_invite_open_status'] == 1){
  15. //邀新有礼活动顶部背景图
  16. $need_data['invite_activity_topback_img'] = $config_data['invite_activity_topback_img'];
  17. //分享标题
  18. $need_data['invite_share_title'] = $config_data['invite_share_title'];
  19. //分享图片
  20. $need_data['invite_share_img'] = $config_data['invite_share_img'];
  21. //邀请下单分享标题
  22. $need_data['invite_order_share_title'] = $config_data['invite_order_share_title'];
  23. //邀请下单分享图片
  24. $need_data['invite_order_share_img'] = $config_data['invite_order_share_img'];
  25. //活动规则
  26. $need_data['invite_activity_rules'] = $config_data['invite_activity_rules'];
  27. //邀新有礼海报配置
  28. //背景 0、纯色背景,1、背景图片
  29. $need_data['invite_poster_back_type'] = $config_data['invite_poster_back_type'];
  30. //背景颜色
  31. $need_data['invite_poster_back_color'] = $config_data['invite_poster_back_color'];
  32. //背景图片
  33. $need_data['invite_poster_back_img'] = $config_data['invite_poster_back_img'];
  34. //二维码图片
  35. $need_data['invite_poster_qrcode_img'] = $this->getInvitegiftWxQrcode($member_id, $config_data);
  36. //二维码背景色
  37. $need_data['invite_poster_qrcode_backcolor'] = $config_data['invite_poster_qrcode_backcolor'];
  38. //二维码线条色
  39. $need_data['invite_poster_qrcode_linecolor'] = $config_data['invite_poster_qrcode_linecolor'];
  40. //二维码边角类型:0、圆角,1、直角
  41. $need_data['invite_poster_qrcode_corner_type'] = $config_data['invite_poster_qrcode_corner_type'];
  42. //二维码大小
  43. $need_data['invite_poster_qrcode_size'] = $config_data['invite_poster_qrcode_size'];
  44. //二维码边框状态:1、开启,0、关闭
  45. $need_data['invite_poster_qrcode_border_status'] = $config_data['invite_poster_qrcode_border_status'];
  46. //二维码边框颜色
  47. $need_data['invite_poster_qrcode_bordercolor'] = $config_data['invite_poster_qrcode_bordercolor'];
  48. //二维码距画布顶部位置
  49. $need_data['invite_poster_qrcode_top'] = $config_data['invite_poster_qrcode_top'];
  50. //二维码距画布左侧位置
  51. $need_data['invite_poster_qrcode_left'] = $config_data['invite_poster_qrcode_left'];
  52. }
  53. //我的活动收益
  54. $invitegift_data = $this->getMyInvitegiftIncome($member_id);
  55. $need_data['income'] = $invitegift_data;
  56. $need_data['suid'] = $member_id;
  57. return $need_data;
  58. }
  59. /**
  60. * @author cy 2021-03-18
  61. * @desc 获取会员的邀请有礼小程序码
  62. * @param $member_id
  63. * @param $config_data
  64. */
  65. public function getInvitegiftWxQrcode($member_id, $config_data){
  66. $invite_poster_update_time = $config_data['invite_poster_update_time'];
  67. $member_invitegift = M('lionfish_comshop_member_invitegift')->where(array('member_id'=>$member_id))->find();
  68. $load_new = false;
  69. if( empty($member_invitegift) )
  70. {
  71. $load_new = true;
  72. }else {
  73. if( $member_invitegift['invite_addtime'] < $invite_poster_update_time )
  74. {
  75. $load_new = true;
  76. }
  77. }
  78. if( $load_new )
  79. {
  80. $qrcode_image = D('Home/Pingoods')->_get_invite_wxqrcode("lionfish_comshop/moduleB/invite/share", $member_id ,$config_data['invite_poster_qrcode_backcolor'],$config_data['invite_poster_qrcode_linecolor']);
  81. $data = array();
  82. $data['image_path'] = $qrcode_image;
  83. $ed_time = time() + 5;
  84. $js_arr = array('member_id' => $member_id,'invite_addtime' => $ed_time,'invite_image_path' => $data['image_path'] );
  85. M('lionfish_comshop_member_invitegift')->add($js_arr);
  86. }else{
  87. $data = array();
  88. $data['image_path'] = $member_invitegift['invite_image_path'];
  89. }
  90. return tomedia($data['image_path']);
  91. }
  92. /**
  93. * @author cy 2021-03-11
  94. * @desc 我的活动收益
  95. * @param int $user_id
  96. * @return array
  97. */
  98. public function getMyInvitegiftIncome($user_id){
  99. $income_data = [];
  100. //邀请成功人数(已领取的被邀请人数)
  101. $invitee_succ_count = $this->getInviteeSuccCout($user_id);
  102. //累计邀请人数(已邀请的被邀请人数)
  103. $invitee_count = $this->getInviteeCout($user_id);
  104. //累计奖励(邀请者已领取的奖励次数)
  105. $inviter_total_count = $this->getInviterTotalCout($user_id);
  106. $income_data['invitee_succ_count'] = $invitee_succ_count;
  107. $income_data['invitee_count'] = $invitee_count;
  108. $income_data['inviter_total_count'] = $inviter_total_count;
  109. return $income_data;
  110. }
  111. /**
  112. * @author cy 2021-02-18
  113. * @desc 邀请成功人数(已领取的被邀请人数)
  114. * @param unknown $user_id
  115. * @param unknown $city_id
  116. * @return number
  117. */
  118. public function getInviteeSuccCout($user_id){
  119. $condition['user_id'] = $user_id;
  120. $condition['invitee_status'] = 1;
  121. $count = $this->getInvitegiftRecordCount($condition);
  122. return $count;
  123. }
  124. /**
  125. * @author cy 2021-02-18
  126. * @desc 累计邀请人数(已邀请的被邀请人数)
  127. * @param unknown $user_id
  128. * @param unknown $city_id
  129. * @return number
  130. */
  131. public function getInviteeCout($user_id){
  132. $condition['user_id'] = $user_id;
  133. $count = $this->getInvitegiftRecordCount($condition);
  134. return $count;
  135. }
  136. /**
  137. * @author cy 2021-02-18
  138. * @desc 累计奖励(邀请者已领取的奖励次数)
  139. * @param unknown $user_id
  140. * @param unknown $city_id
  141. * @return number
  142. */
  143. public function getInviterTotalCout($user_id){
  144. $condition['user_id'] = $user_id;
  145. $condition['status'] = 1;
  146. $count = $this->getInvitegiftRecordCount($condition);
  147. return $count;
  148. }
  149. /**
  150. * @author cy 2021-03-11
  151. * @desc 根据条件获取邀新有礼记录数
  152. * @param unknown $condition
  153. * @return number
  154. */
  155. public function getInvitegiftRecordCount($condition){
  156. $count = M('lionfish_comshop_invitegift_record')->where($condition)->count();
  157. return !empty($count) ? $count : 0;
  158. }
  159. /**
  160. * @author cy 2021-03-11
  161. * @desc 邀新有礼配置信息
  162. * @return array
  163. */
  164. public function getInvitegiftConfig(){
  165. $invitegift_config = array();
  166. $city_config = D('Seller/Config')->get_all_config();
  167. if(!empty($city_config['is_invite_open_status'])){
  168. //邀新有礼开启状态
  169. $invitegift_config['is_invite_open_status'] = $city_config['is_invite_open_status'];
  170. //邀请者奖励类型
  171. $invitegift_config['inviter_reward_type'] = $city_config['inviter_reward_type'];
  172. //邀请者奖励优惠券id
  173. $invitegift_config['inviter_reward_couponid'] = $city_config['inviter_reward_couponid'];
  174. //邀请者奖励积分数
  175. $invitegift_config['inviter_reward_point'] = $city_config['inviter_reward_point'];
  176. //被邀请者奖励类型
  177. $invitegift_config['invitees_reward_type'] = $city_config['invitees_reward_type'];
  178. //被邀请者新人注册(授权登录)奖励类型
  179. $invitegift_config['invitees_register_reward_type'] = $city_config['invitees_register_reward_type'];
  180. //被邀请者新人注册(授权登录)奖励优惠券id
  181. $invitegift_config['invitees_register_reward_couponid'] = $city_config['invitees_register_reward_couponid'];
  182. //被邀请者新人注册(授权登录)奖励积分数
  183. $invitegift_config['invitees_register_reward_point'] = $city_config['invitees_register_reward_point'];
  184. //被邀请者下单完成奖励类型
  185. $invitegift_config['invitees_order_reward_type'] = $city_config['invitees_order_reward_type'];
  186. //被邀请者下单完成奖励优惠券id
  187. $invitegift_config['invitees_order_reward_couponid'] = $city_config['invitees_order_reward_couponid'];
  188. //被邀请者下单完成奖励积分数
  189. $invitegift_config['invitees_order_reward_point'] = $city_config['invitees_order_reward_point'];
  190. //被邀请者下单完成活动奖励时间:0、下单支付完成获得奖励,1、订单售后期结束获得奖励
  191. $invitegift_config['invite_activity_reward_type'] = $city_config['invite_activity_reward_type'];
  192. //是否开启邀请人数限制:1、开启,0、关闭
  193. $invitegift_config['is_open_invitation_limit'] = $city_config['is_open_invitation_limit'];
  194. //邀请人数限制人数
  195. $invitegift_config['invitation_limit_person'] = $city_config['invitation_limit_person'];
  196. //邀新有礼活动顶部背景图
  197. if(!empty($city_config['invite_activity_topback_img'])){
  198. $invitegift_config['invite_activity_topback_img'] = tomedia($city_config['invite_activity_topback_img']);
  199. }else{
  200. $invitegift_config['invite_activity_topback_img'] = "";
  201. }
  202. //活动打开页面顶部背景图
  203. if(!empty($city_config['invite_activity_open_topback_img'])){
  204. $invitegift_config['invite_activity_open_topback_img'] = tomedia($city_config['invite_activity_open_topback_img']);
  205. }else{
  206. $invitegift_config['invite_activity_open_topback_img'] = "";
  207. }
  208. //活动积分/优惠卷使用规则
  209. $invitegift_config['invite_activity_use_rules'] = htmlspecialchars_decode($city_config['invite_activity_use_rules']);
  210. //分享标题
  211. $invitegift_config['invite_share_title'] = $city_config['invite_share_title'];
  212. //分享图片
  213. if(!empty($city_config['invite_share_img'])){
  214. $invitegift_config['invite_share_img'] = tomedia($city_config['invite_share_img']);
  215. }else{
  216. $invitegift_config['invite_share_img'] = "";
  217. }
  218. //邀请下单分享标题
  219. $invitegift_config['invite_order_share_title'] = $city_config['invite_order_share_title'];
  220. //邀请下单分享图片
  221. if(!empty($city_config['invite_order_share_img'])){
  222. $invitegift_config['invite_order_share_img'] = tomedia($city_config['invite_order_share_img']);
  223. }else{
  224. $invitegift_config['invite_order_share_img'] = "";
  225. }
  226. //活动规则
  227. $invitegift_config['invite_activity_rules'] = htmlspecialchars_decode($city_config['invite_activity_rules']);
  228. //邀新有礼海报配置
  229. //背景 0、纯色背景,1、背景图片
  230. $invitegift_config['invite_poster_back_type'] = $city_config['invite_poster_back_type'];
  231. //背景颜色
  232. $invitegift_config['invite_poster_back_color'] = $city_config['invite_poster_back_color'];
  233. //背景图片
  234. if(!empty($city_config['invite_poster_back_img'])){
  235. $invitegift_config['invite_poster_back_img'] = tomedia($city_config['invite_poster_back_img']);
  236. }else{
  237. $invitegift_config['invite_poster_back_img'] = "";
  238. }
  239. //二维码
  240. if(!empty($city_config['invite_poster_qrcode_img'])){
  241. $invitegift_config['invite_poster_qrcode_img'] = tomedia($city_config['invite_poster_qrcode_img']);
  242. }else{
  243. $invitegift_config['invite_poster_qrcode_img'] = "";
  244. }
  245. //二维码背景颜色
  246. $invitegift_config['invite_poster_qrcode_backcolor'] = $city_config['invite_poster_qrcode_backcolor'];
  247. //二维码线条颜色
  248. $invitegift_config['invite_poster_qrcode_linecolor'] = $city_config['invite_poster_qrcode_linecolor'];
  249. //二维码边角类型:0、圆角,1、直角
  250. $invitegift_config['invite_poster_qrcode_corner_type'] = $city_config['invite_poster_qrcode_corner_type'];
  251. //二维码大小
  252. $invitegift_config['invite_poster_qrcode_size'] = $city_config['invite_poster_qrcode_size'];
  253. //二维码边框状态:1、开启,0、关闭
  254. $invitegift_config['invite_poster_qrcode_border_status'] = $city_config['invite_poster_qrcode_border_status'];
  255. //二维码边框颜色
  256. $invitegift_config['invite_poster_qrcode_bordercolor'] = $city_config['invite_poster_qrcode_bordercolor'];
  257. //二维码距画布顶部
  258. $invitegift_config['invite_poster_qrcode_top'] = $city_config['invite_poster_qrcode_top'];
  259. //二维码距画布左侧
  260. $invitegift_config['invite_poster_qrcode_left'] = $city_config['invite_poster_qrcode_left'];
  261. //二维码更新时间
  262. $invitegift_config['invite_poster_update_time'] = $city_config['invite_poster_update_time'];
  263. }
  264. return $invitegift_config;
  265. }
  266. /**
  267. * @author cy 2021-03-11
  268. * @desc 邀请好友列表
  269. * @param $member_id
  270. * @param string $type
  271. * @param int $page
  272. * @param int $perpage
  273. * @return array
  274. */
  275. public function getInvitegiftRecordList($member_id , $type = '', $page = 1, $perpage = 10){
  276. $condition = " a.user_id = {$member_id} ";
  277. if(!empty($type)){
  278. if($type == 'success'){
  279. $condition .= " and a.is_old = 0 and invitee_status = 1 ";
  280. }
  281. }
  282. $offset = ($page - 1) * $perpage;
  283. $sql = "select a.* from ".C('DB_PREFIX')."lionfish_comshop_invitegift_record as a where $condition order by a.addtime desc limit {$offset},{$perpage} ";
  284. $list = M()->query( $sql );
  285. $need_data = [];
  286. if( !empty($list) )
  287. {
  288. foreach( $list as &$v )
  289. {
  290. $tmp_arr = $this->getInvitegiftRecordDetail($v);
  291. $need_data[] = $tmp_arr;
  292. }
  293. }
  294. return $need_data;
  295. }
  296. /**
  297. * @author cy 2021-02-18
  298. * @desc 获取单条邀新有礼记录信息
  299. * @param array $collect
  300. */
  301. public function getInvitegiftRecordDetail($invitegift_record){
  302. $tmp_arr = [];
  303. $member_info = M('lionfish_comshop_member')->where(array('member_id'=>$invitegift_record['invitee_userid']))->field('avatar,username')->find();
  304. $tmp_arr['invitee_userid'] = $invitegift_record['invitee_userid'];
  305. $tmp_arr['avatar'] = isset($member_info['avatar']) ? $member_info['avatar'] : '';
  306. $tmp_arr['nickname'] = isset($member_info['username']) ? $member_info['username'] : '';
  307. if($invitegift_record['is_old'] == 1){
  308. $tmp_arr['user_type'] = 'old';
  309. $tmp_arr['invite_type'] = 'old';
  310. $tmp_arr['reward_content'] = '无奖励';
  311. $tmp_arr['receive_content'] = '暂不参与';
  312. }else{
  313. $tmp_arr['user_type'] = 'new';
  314. if($invitegift_record['invitee_status'] == 1){//已领取
  315. $tmp_arr['reward_content'] = '奖励已到账';
  316. $tmp_arr['receive_content'] = '邀请成功';
  317. $tmp_arr['invite_type'] = 'success';
  318. }else{//未领取
  319. $tmp_arr['invite_type'] = 'wait';
  320. if($invitegift_record['invitees_reward_type'] == 1){//新人注册(授权登录)即奖励
  321. $tmp_arr['reward_content'] = '奖励待到账';
  322. $tmp_arr['receive_content'] = '注册成功登录后即奖励';
  323. $tmp_arr['invite_order_btn'] = 0;
  324. }else{
  325. if($invitegift_record['invitees_reward_type'] == 2){//新人注册(授权登录)并且下单奖励
  326. $tmp_arr['reward_content'] = '邀请下单';
  327. $tmp_arr['receive_content'] = '注册成功,受邀人下单后即为邀请成功';
  328. $tmp_arr['invite_order_btn'] = 1;
  329. }else if($invitegift_record['invitees_reward_type'] == 0){//新人注册(授权登录)和下单分别奖励
  330. $tmp_arr['reward_content'] = '邀请下单';
  331. $tmp_arr['receive_content'] = '注册成功,下单后继续奖励';
  332. $tmp_arr['invite_order_btn'] = 1;
  333. }
  334. if($invitegift_record['invite_activity_reward_type'] == 1){//订单售后期结束获得奖励
  335. //会员已付款订单数
  336. $success_where = " and member_id = ".$invitegift_record['invitee_userid']." and order_status_id in (1,4,6,7,11,14) and type <> 'integral' ";
  337. $order_count = D('Seller/Order')->get_order_count($success_where);
  338. //会员退款订单数
  339. $refund_cancel_where = " and member_id = ".$invitegift_record['invitee_userid']." and order_status_id in (7) and type <> 'integral' ";
  340. $pay_refund_count = D('Seller/Order')->get_order_count($refund_cancel_where);
  341. if($order_count > 0){
  342. if($order_count > $pay_refund_count){
  343. $tmp_arr['invite_order_btn'] = 0;
  344. $tmp_arr['reward_content'] = '奖励待到账';
  345. $tmp_arr['reward_tip'] = '被邀请人的订单在售后期内,需要等待售后期结束才能获得活动奖励';
  346. }else{
  347. $tmp_arr['invite_type'] = 'invalid';
  348. $tmp_arr['reward_content'] = '奖励失效';
  349. $tmp_arr['invite_order_btn'] = 1;
  350. $tmp_arr['reward_tip'] = '被邀请人的订单在售后期内发生退款,需要被邀请人重新下单才可获得奖励';
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. $tmp_arr['addtime'] = date('Y-m-d H:i',$invitegift_record['addtime']);
  358. return $tmp_arr;
  359. }
  360. /**
  361. * @author cy 2021-03-12
  362. * @desc 推荐有礼页面
  363. */
  364. public function getInvitegiftIndex($member_id){
  365. $need_data = [];
  366. $config_data = $this->getInvitegiftConfig();
  367. if(isset($config_data['is_invite_open_status']) && $config_data['is_invite_open_status'] == 1){
  368. //活动打开页面顶部背景图
  369. $need_data['invite_activity_open_topback_img'] = $config_data['invite_activity_open_topback_img'];
  370. //活动规则
  371. $need_data['invite_activity_rules'] = $config_data['invite_activity_rules'];
  372. //活动积分/优惠卷使用规则
  373. $need_data['invite_activity_use_rules'] = $config_data['invite_activity_use_rules'];
  374. }
  375. return $need_data;
  376. }
  377. /**
  378. * @author cy 2021-03-12
  379. * @desc 保存邀请记录并返回是否可以领取礼包
  380. * @param $share_id 邀请人
  381. * @param $member_id 被邀请人
  382. * @param $is_old 是否老用户:1、是,0、不是
  383. * @return int 1、可以领取,0、不能领取,2、老用户不能领取
  384. */
  385. public function insertInvitegiftRecord($share_id,$member_id, $is_old){
  386. $is_can_collect_gift = 0;
  387. $city_config = $this->getInvitegiftConfig();
  388. if(isset($city_config['is_invite_open_status']) && $city_config['is_invite_open_status'] == 1){
  389. $insert_data = [];
  390. $insert_data['user_id'] = $share_id;
  391. $insert_data['invitee_userid'] = $member_id;
  392. $insert_data['is_old'] = $is_old;
  393. $insert_data['inviter_reward_type'] = $city_config['inviter_reward_type'];
  394. $insert_data['inviter_reward_couponid'] = $city_config['inviter_reward_couponid'];
  395. $insert_data['inviter_reward_point'] = $city_config['inviter_reward_point'];
  396. $insert_data['invitees_reward_type'] = $city_config['invitees_reward_type'];
  397. $insert_data['invitees_register_reward_type'] = $city_config['invitees_register_reward_type'];
  398. $insert_data['invitees_register_reward_couponid'] = $city_config['invitees_register_reward_couponid'];
  399. $insert_data['invitees_register_reward_point'] = $city_config['invitees_register_reward_point'];
  400. $insert_data['invitees_order_reward_type'] = $city_config['invitees_order_reward_type'];
  401. $insert_data['invitees_order_reward_couponid'] = $city_config['invitees_order_reward_couponid'];
  402. $insert_data['invitees_order_reward_point'] = $city_config['invitees_order_reward_point'];
  403. $insert_data['invite_activity_reward_type'] = $city_config['invite_activity_reward_type'];
  404. $insert_data['addtime'] = time();
  405. if($is_old == 1){//老用户默认已领取
  406. $insert_data['invitee_status'] = 1;
  407. $insert_data['invitee_register_status'] = 1;
  408. $insert_data['status'] = 1;
  409. }
  410. $this->addInvitegiftRecord($insert_data);
  411. if($is_old == 1){
  412. $is_can_collect_gift = 2;
  413. }else{
  414. //1、新人注册(授权登录)即奖励 0、新人注册(授权登录)和下单分别奖励
  415. if($city_config['invitees_reward_type'] == 1 || $city_config['invitees_reward_type'] == 0){
  416. $is_can_collect_gift = 1;
  417. }else{
  418. $is_can_collect_gift = 0;
  419. }
  420. }
  421. }
  422. return $is_can_collect_gift;
  423. }
  424. /**
  425. * @author cy 2021-03-12
  426. * @desc 添加邀新有礼记录
  427. * @param $data
  428. * @return int
  429. */
  430. public function addInvitegiftRecord( $data )
  431. {
  432. $id = M('lionfish_comshop_invitegift_record')->add( $data );
  433. return $id;
  434. }
  435. /************************************领取邀新礼包 begin*********************************************/
  436. /**
  437. * @author cy 2021-03-12
  438. * @desc 新用户领取礼包
  439. * @param $user_id
  440. * @return array
  441. */
  442. public function collectInvitegift( $user_id ){
  443. $need_data = [];
  444. M()->startTrans();
  445. $invitegift_record = $this->getInvitegiftRecordCollect($user_id);
  446. if(!empty($invitegift_record)){
  447. //被邀请者未领取礼包
  448. if($invitegift_record['invitee_status'] == 0){
  449. if($invitegift_record['invitees_reward_type'] == 0 && $invitegift_record['invitee_register_status'] == 1){
  450. // 事务回滚
  451. M()->rollback();
  452. return ['code'=>4 ,'message'=>'已领取新人礼包,下单后继续领取'];
  453. //新人注册(授权登录)和下单分别奖励
  454. }else if($invitegift_record['invitees_reward_type'] == 0 && $invitegift_record['invitee_register_status'] == 0){
  455. //新人注册(授权登录)奖励
  456. $gift_data = $this->receiveRegisterInvitegift($invitegift_record);
  457. //更新被邀请者已领取
  458. $this->updateInvitegiftRecord($invitegift_record['id'], array('invitee_register_status'=>1));
  459. $need_data['coupon_info'] = $gift_data['coupon_info'];
  460. $need_data['point'] = $gift_data['point'];
  461. //新人注册(授权登录)即奖励
  462. }else if($invitegift_record['invitees_reward_type'] == 1){
  463. //新人注册(授权登录)奖励
  464. $gift_data = $this->receiveRegisterInvitegift($invitegift_record);
  465. //更新被邀请者已领取
  466. $this->updateInvitegiftRecord($invitegift_record['id'], array('invitee_status'=>1,'invitee_register_status'=>1));
  467. //邀请者领取礼包
  468. $this->inviterReceiveInvitegift($invitegift_record);
  469. $need_data['coupon_info'] = $gift_data['coupon_info'];
  470. $need_data['point'] = $gift_data['point'];
  471. //新人注册(授权登录)并且下单奖励 暂不领取
  472. }else if($invitegift_record['invitees_reward_type'] == 2){
  473. // 事务回滚
  474. M()->rollback();
  475. return ['code'=>3,'message'=>'下单后才能领取礼包'];
  476. }
  477. }else{
  478. // 事务回滚
  479. M()->rollback();
  480. return ['code'=>2,'message'=>'礼包已领取过'];
  481. }
  482. }else{
  483. // 事务回滚
  484. M()->rollback();
  485. return ['code'=>1,'message'=>'没有未领取礼包'];
  486. }
  487. M()->commit();
  488. $need_data['code'] = 0;
  489. return $need_data;
  490. }
  491. /**
  492. * @author cy 2021-03-12
  493. * @desc 新人注册即奖励
  494. * @param $invitegift_record
  495. */
  496. public function receiveRegisterInvitegift($invitegift_record){
  497. $need_data = [];
  498. //被邀请者新人注册(授权登录)奖励类型
  499. //赠送优惠券id
  500. $couponid = $invitegift_record['invitees_register_reward_couponid'];
  501. //赠送积分数
  502. $point = $invitegift_record['invitees_register_reward_point'];
  503. //0 送优惠券和积分
  504. if($invitegift_record['invitees_register_reward_type'] == 0){
  505. //新人送优惠券
  506. $need_data['coupon_info'] = $this->sendInvitegiftCoupon($couponid, $invitegift_record['invitee_userid'], $invitegift_record['id'], 'invitegift_new');
  507. //新人送积分
  508. $this->sendInvitegiftPoint($point, $invitegift_record['invitee_userid'], $invitegift_record['id'], 'invitegift_new');
  509. $need_data['point'] = $point;
  510. //1 送优惠券
  511. }else if($invitegift_record['invitees_register_reward_type'] == 1){
  512. //新人送优惠券
  513. $need_data['coupon_info'] = $this->sendInvitegiftCoupon($couponid, $invitegift_record['invitee_userid'], $invitegift_record['id'],'invitegift_new');
  514. $need_data['point'] = 0;
  515. //2 送积分
  516. }else if($invitegift_record['invitees_register_reward_type'] == 2){
  517. //新人送积分
  518. $this->sendInvitegiftPoint($point, $invitegift_record['invitee_userid'], $invitegift_record['id'], 'invitegift_new');
  519. $need_data['point'] = $point;
  520. $need_data['coupon_info'] = [];
  521. }
  522. return $need_data;
  523. }
  524. /**
  525. * @author cy 2021-02-20
  526. * @desc 邀请者领取礼包
  527. * @param $invitegift_record
  528. */
  529. public function inviterReceiveInvitegift($invitegift_record){
  530. //优惠券
  531. $couponid = $invitegift_record['inviter_reward_couponid'];
  532. //积分
  533. $point = $invitegift_record['inviter_reward_point'];
  534. //送优惠券和积分
  535. if($invitegift_record['inviter_reward_type'] == 0){
  536. $this->sendInvitegiftCoupon($couponid, $invitegift_record['user_id'], $invitegift_record['id'], 'invitegift');
  537. $this->sendInvitegiftPoint($point, $invitegift_record['user_id'], $invitegift_record['id'], 'invitegift');
  538. //送优惠券
  539. }else if($invitegift_record['inviter_reward_type'] == 1){
  540. $this->sendInvitegiftCoupon($couponid, $invitegift_record['user_id'], $invitegift_record['id'], 'invitegift');
  541. //送积分
  542. }else if($invitegift_record['inviter_reward_type'] == 2){
  543. $this->sendInvitegiftPoint($point, $invitegift_record['user_id'], $invitegift_record['id'], 'invitegift');
  544. }
  545. //更新邀请者已领取
  546. $this->updateInvitegiftRecord($invitegift_record['id'],array('status'=>1));
  547. }
  548. /**
  549. * @author cy 2021-03-12
  550. * @desc 邀新有礼送优惠券
  551. * @param $couponid
  552. * @param $user_id
  553. * @param $receive_type invitegift_new 被邀请者,invitegift 邀请者
  554. */
  555. public function sendInvitegiftCoupon($couponid, $user_id , $invite_id, $receive_type){
  556. //发放优惠券
  557. $result = D('Home/Voucher')->send_invitegift_coupon_byId($couponid ,$user_id, $invite_id ,$receive_type);
  558. if($result['code'] == 0){
  559. $coupons = M('lionfish_comshop_coupon_list')->where( array('id' => $result['id']) )->find();
  560. $coupons['begin_time'] = date('Y-m-d H:i:s',$coupons['begin_time']);
  561. $coupons['end_time'] = date('Y-m-d H:i:s',$coupons['end_time']);
  562. return $coupons;
  563. }else{
  564. return array();
  565. }
  566. }
  567. /**
  568. * @author cy 2021-02-20
  569. * @desc 邀新有礼送积分
  570. * @param $point
  571. * @param $user_id
  572. * @param $receive_type invitegift_new 被邀请者,invitegift 邀请者
  573. */
  574. public function sendInvitegiftPoint($point, $user_id , $invite_id,$receive_type){
  575. $order_model = D('Seller/Order');
  576. $order_model->charge_member_score( $user_id , $point,'in', $receive_type, $invite_id);
  577. }
  578. /**
  579. * @author cy 2021-03-12
  580. * @desc 更新邀新有礼记录表
  581. * @param $id
  582. * @param $data
  583. * @return mixed
  584. */
  585. public function updateInvitegiftRecord($id,$data){
  586. return M('lionfish_comshop_invitegift_record')->where(array('id'=>$id))->save($data);
  587. }
  588. /**
  589. * @author cy 2021-03-12
  590. * @desc 通过被邀请人获取邀新有礼记录
  591. * @param $user_id
  592. * @return mixed
  593. */
  594. public function getInvitegiftRecordCollect($user_id){
  595. $collect = M('lionfish_comshop_invitegift_record')->where(array('invitee_userid'=>$user_id))->find();
  596. return $collect;
  597. }
  598. /************************************领取邀新礼包 end*********************************************/
  599. /**
  600. * @author cy 2021-03-12
  601. * @desc 立即邀请判断
  602. * @param $user_id
  603. * @return array
  604. */
  605. public function checkInvitegift($member_id){
  606. $result = [];
  607. $result['code'] = 0;
  608. $member_info = M('lionfish_comshop_member')->where(array('member_id'=>$member_id))->field('share_status')->find();
  609. if($member_info['share_status'] == 0){
  610. $result['code'] = 1;
  611. $result['message'] = "邀请人数超过活动限制";
  612. }else{
  613. $config_data = $this->getInvitegiftConfig();
  614. //是否开启邀请人数限制:1、开启,0、关闭
  615. $is_open_invitation_limit = $config_data['is_open_invitation_limit'];
  616. //邀请人数限制人数
  617. $invitation_limit_person = $config_data['invitation_limit_person'];
  618. if($is_open_invitation_limit == 1){
  619. $count = $this->getInviteeCout($member_id);
  620. if($count >= $invitation_limit_person){
  621. $result['code'] = 1;
  622. $result['message'] = "邀请人数超过活动限制";
  623. }
  624. }
  625. }
  626. return $result;
  627. }
  628. /**
  629. * @author cy 2021-03-12
  630. * @desc 邀请优惠券列表
  631. * @param $member_id
  632. * @param int $page
  633. * @param int $perpage
  634. * @return array
  635. */
  636. public function getInvitegiftCouponList( $member_id , $page = 1, $perpage = 10 ){
  637. $offset = ($page - 1)*$perpage;
  638. $condition = " user_id=".$member_id." and receive_type in ('invitegift','invitegift_new') ";
  639. //未使用
  640. $condition .= " and consume= 'N' and end_time> ".time();
  641. $coupon_list = M('lionfish_comshop_coupon_list')->where($condition)->order('add_time desc')->limit($offset,$perpage)->select();
  642. foreach($coupon_list as $k=>&$v){
  643. $v['begin_time'] = date('Y-m-d H:i:s', $v['begin_time']);
  644. $v['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
  645. }
  646. return $coupon_list;
  647. }
  648. /**
  649. * @author cy 2021-03-12
  650. * @desc 邀请积分明细列表
  651. * @param $user_id
  652. * @param int $page
  653. * @param int $perpage
  654. * @return array
  655. */
  656. public function getInvitegiftPointsList( $member_id , $page = 1, $perpage = 10 ){
  657. $offset = ($page - 1)*$perpage;
  658. $condition = " member_id=".$member_id." and type in ('invitegift_new','invitegift')";
  659. $point_list = M('lionfish_comshop_member_integral_flow')->where($condition)->order('addtime desc')->limit($offset,$perpage)->select();
  660. if( !empty($point_list) )
  661. {
  662. foreach($point_list as &$collect )
  663. {
  664. $collect['addtime'] = date('Y-m-d H:i:s', $collect['addtime']);
  665. }
  666. }
  667. return $point_list;
  668. }
  669. /**
  670. * @author cy 2021-03-12
  671. * @desc 邀请总积分
  672. * @param $member_id
  673. * @return int
  674. */
  675. public function getInvitegiftPointsTotal($member_id){
  676. $condition = " member_id=".$member_id." and type in ('invitegift_new','invitegift')";
  677. $total_point = M('lionfish_comshop_member_integral_flow')->where($condition)->sum('score');
  678. return empty($total_point) ? 0 : $total_point;
  679. }
  680. /**
  681. * @author cy 2021-03-18
  682. * @desc 新用户下单后领取礼包
  683. * @param $order_collect
  684. * @param $order_type
  685. * @return array
  686. */
  687. public function collectInvitegiftAfterOrder( $order_info, $order_type ){
  688. $need_data = [];
  689. $user_id = $order_info['member_id'];
  690. $invitegift_record = $this->getInvitegiftRecordCollect($user_id);
  691. if(!empty($invitegift_record)){
  692. //被邀请者未领取礼包
  693. if($invitegift_record['invitee_status'] == 0){
  694. //活动奖励:0、下单支付完成获得奖励,1、订单售后期结束获得奖励
  695. $invite_activity_reward_type = $invitegift_record['invite_activity_reward_type'];
  696. //0、新人注册(授权登录)和下单分别奖励 或者 2、新人注册(授权登录)并且下单奖励
  697. if($invitegift_record['invitees_reward_type'] == 0 || $invitegift_record['invitees_reward_type'] == 2){
  698. //0、下单支付完成获得奖励,1、订单售后期结束获得奖励
  699. if(($invite_activity_reward_type == 0 && $order_type == 'orderpay') || ($invite_activity_reward_type == 1 && $order_type == 'aftersaletime')){
  700. //下单完成奖励
  701. $gift_data = $this->receiveOrderInvitegift($invitegift_record);
  702. //更新被邀请者已领取
  703. $this->updateInvitegiftRecord($invitegift_record['id'],['invitee_status'=>1]);
  704. //邀请者领取礼包
  705. $this->inviterReceiveInvitegift($invitegift_record);
  706. $need_data['coupon_info'] = $gift_data['coupon_info'];
  707. $need_data['point'] = $gift_data['point'];
  708. }else{
  709. return ['code'=>3,'message'=>'礼包未达领取条件'];
  710. }
  711. }else{
  712. return ['code'=>3,'message'=>'礼包未达领取条件'];
  713. }
  714. }else{
  715. return ['code'=>2,'message'=>'礼包已领取过'];
  716. }
  717. }else{
  718. return ['code'=>1,'message'=>'没有未领取礼包'];
  719. }
  720. $need_data['code'] = 0;
  721. return $need_data;
  722. }
  723. /**
  724. * @author cy 2021-02-22
  725. * @desc 下单完成即奖励
  726. * @param $invitegift_record
  727. */
  728. public function receiveOrderInvitegift($invitegift_record){
  729. $need_data = [];
  730. //被邀请者下单完成奖励类型
  731. //赠送优惠券id
  732. $couponid = $invitegift_record['invitees_order_reward_couponid'];
  733. //赠送积分数
  734. $point = $invitegift_record['invitees_order_reward_point'];
  735. //0 送优惠券和积分
  736. if($invitegift_record['invitees_order_reward_type'] == 0){
  737. //新人送优惠券
  738. $need_data['coupon_info'] = $this->sendInvitegiftCoupon($couponid, $invitegift_record['invitee_userid'], $invitegift_record['id'], 'invitegift_new');
  739. //新人送积分
  740. $this->sendInvitegiftPoint($point, $invitegift_record['invitee_userid'], $invitegift_record['id'],'invitegift_new');
  741. $need_data['point'] = $point;
  742. //1 送优惠券
  743. }else if($invitegift_record['invitees_order_reward_type'] == 1){
  744. //新人送优惠券
  745. $need_data['coupon_info'] = $this->sendInvitegiftCoupon($couponid, $invitegift_record['invitee_userid'], $invitegift_record['id'], 'invitegift_new');
  746. $need_data['point'] = 0;
  747. //2 送积分
  748. }else if($invitegift_record['invitees_order_reward_type'] == 2){
  749. //新人送积分
  750. $this->sendInvitegiftPoint($point, $invitegift_record['invitee_userid'], $invitegift_record['id'],'invitegift_new');
  751. $need_data['point'] = $point;
  752. $need_data['coupon_info'] = [];
  753. }
  754. return $need_data;
  755. }
  756. }