FenxiaoModel.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 分销模型模型
  6. * @author fish
  7. *
  8. */
  9. class FenxiaoModel {
  10. public $table = 'pin';
  11. public function getSiteUrl()
  12. {
  13. $config_info = M('config')->where( array('name' => 'SITE_URL') )->find();
  14. $url = $config_info['value'];
  15. return $url;
  16. }
  17. /**
  18. 只有拼团成功或者单独购买已经发货的 , 订单退款取消佣金
  19. **/
  20. public function back_order_commiss_money($order_id)
  21. {
  22. $member_commiss_order_list = M('member_commiss_order')->where( array('order_id' =>$order_id,'state' => 1 ) )->select();
  23. if(!empty($member_commiss_order_list))
  24. {
  25. foreach($member_commiss_order_list as $member_commiss_order)
  26. {
  27. //分佣订单
  28. M('member_commiss_order')->where( array('id' =>$member_commiss_order['id'] ) )->save( array('state' => 2) );
  29. M('member_commiss')->where( array('member_id' => $member_commiss_order['member_id']) )->setDec('money',$member_commiss_order['money']);
  30. }
  31. }
  32. }
  33. public function send_order_commiss_money($order_id)
  34. {
  35. $member_commiss_order_list = M('member_commiss_order')->where( array('order_id' =>$order_id,'state' => 0 ) )->select();
  36. if(!empty($member_commiss_order_list))
  37. {
  38. foreach($member_commiss_order_list as $member_commiss_order)
  39. {
  40. //分佣订单
  41. M('member_commiss_order')->where( array('id' =>$member_commiss_order['id'] ) )->save( array('state' => 1) );
  42. M('member_commiss')->where( array('member_id' => $member_commiss_order['member_id']) )->setInc('money',$member_commiss_order['money']);
  43. }
  44. }
  45. }
  46. /**
  47. 给上级会员分佣
  48. **/
  49. public function ins_member_commiss_order($member_id,$order_id,$store_id,$order_goods_id)
  50. {
  51. $member_info = M('member')->where( array('member_id' => $member_id) )->find();
  52. //一级
  53. if(intval($member_info['share_id']) > 0)
  54. {
  55. $order_goods = M('order_goods')->where( array('order_goods_id' => $order_goods_id) )->find();
  56. if(!empty($order_goods))
  57. {
  58. $commiss_one_money = $order_goods['commiss_one_money'];
  59. if($commiss_one_money > 0)
  60. {
  61. $data = array();
  62. $data['member_id'] = $member_info['share_id'];
  63. $data['child_member_id'] = $member_id;
  64. $data['order_id'] = $order_id;
  65. $data['order_goods_id'] = $order_goods_id;
  66. $data['store_id'] = $store_id;
  67. $data['state'] = 0;
  68. $data['level'] = 1;
  69. $data['money'] = $commiss_one_money;
  70. $data['addtime'] = time();
  71. M('member_commiss_order')->add($data);
  72. $share_member = M('member')->field('we_openid,openid')->where( array('member_id' => $member_info['share_id']) )->find();
  73. $member_formid_info = M('member_formid')->where( array('member_id' => $member_info['share_id'],'formid' => array('neq',''), 'state' => 0) )->order('id desc')->find();
  74. //更新
  75. /**
  76. {{first.DATA}}
  77. 商品名称:{{keyword1.DATA}}
  78. 商品佣金:{{keyword2.DATA}}
  79. 订单状态:{{keyword3.DATA}}
  80. {{remark.DATA}}
  81. 点击了解更多佣金详情
  82. **/
  83. $wx_template_data = array();
  84. $wx_template_data['first'] = array('value' => '1级会员:'.$member_info['uname'].'购买', 'color' => '#030303');
  85. $wx_template_data['keyword1'] = array('value' => $order_goods['name'], 'color' => '#030303');
  86. $wx_template_data['keyword2'] = array('value' => round($commiss_one_money,2).'元', 'color' => '#030303');
  87. $wx_template_data['keyword3'] = array('value' => '支付成功', 'color' => '#030303');
  88. $wx_template_data['remark'] = array('value' => '点击了解更多佣金详情', 'color' => '#030303');
  89. if(!empty($share_member['openid']))
  90. {
  91. $url = C('SITE_URL')."index.php?s=/tuanbonus/groupleaderindex.html";
  92. send_template_msg($wx_template_data,$url,$share_member['openid'],C('weixin_neworder_commiss'));
  93. }
  94. if(!empty($member_formid_info))
  95. {
  96. \Think\Log::record('测试日志信息,ininin');
  97. $template_data['keyword1'] = array('value' => 'FX'.$order_id, 'color' => '#030303');
  98. $template_data['keyword2'] = array('value' => $order_goods['name'], 'color' => '#030303');
  99. $template_data['keyword3'] = array('value' => round($order_goods['total'],2).'元', 'color' => '#030303');
  100. $template_data['keyword4'] = array('value' => '1级会员购买,佣金'.$commiss_one_money.'元', 'color' => '#030303');
  101. $pay_order_msg_info = M('config')->where( array('name' => 'weprog_neworder_commiss') )->find();
  102. $template_id = $pay_order_msg_info['value'];
  103. $url =C('SITE_URL');
  104. $pagepath = 'pages/dan/me';
  105. send_wxtemplate_msg($template_data,$url,$pagepath,$share_member['we_openid'],$template_id,$member_formid_info['formid']);
  106. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  107. }
  108. }
  109. }
  110. //二级
  111. $member_info = M('member')->where( array('member_id' => $member_info['share_id']) )->find();
  112. if(intval($member_info['share_id']) > 0)
  113. {
  114. $commiss_two_money = $order_goods['commiss_two_money'];
  115. if($commiss_two_money > 0)
  116. {
  117. $data = array();
  118. $data['member_id'] = $member_info['share_id'];
  119. $data['child_member_id'] = $member_id;
  120. $data['order_id'] = $order_id;
  121. $data['order_goods_id'] = $order_goods_id;
  122. $data['store_id'] = $store_id;
  123. $data['state'] = 0;
  124. $data['level'] = 2;
  125. $data['money'] = $commiss_two_money;
  126. $data['addtime'] = time();
  127. M('member_commiss_order')->add($data);
  128. //TODO 发送模板消息2级下级购买,佣金多少
  129. $share_member = M('member')->field('we_openid,openid')->where( array('member_id' => $member_info['share_id']) )->find();
  130. $wx_template_data = array();
  131. $wx_template_data['first'] = array('value' => '2级会员购买', 'color' => '#030303');
  132. $wx_template_data['keyword1'] = array('value' => $order_goods['name'], 'color' => '#030303');
  133. $wx_template_data['keyword2'] = array('value' => round($commiss_two_money,2).'元', 'color' => '#030303');
  134. $wx_template_data['keyword3'] = array('value' => '支付成功', 'color' => '#030303');
  135. $wx_template_data['remark'] = array('value' => '点击了解更多佣金详情', 'color' => '#030303');
  136. if(!empty($share_member['openid']))
  137. {
  138. $url = C('SITE_URL')."index.php?s=/tuanbonus/groupleaderindex.html";
  139. send_template_msg($wx_template_data,$url,$share_member['openid'],C('weixin_neworder_commiss'));
  140. }
  141. $member_formid_info = M('member_formid')->where( array('member_id' => $member_info['share_id'],'formid' => array('neq',''), 'state' => 0) )->order('id desc')->find();
  142. //更新
  143. if(!empty($member_formid_info))
  144. {
  145. $template_data['keyword1'] = array('value' => 'FX'.$order_id, 'color' => '#030303');
  146. $template_data['keyword2'] = array('value' => $order_goods['name'], 'color' => '#030303');
  147. $template_data['keyword3'] = array('value' => round($order_goods['total'],2).'元', 'color' => '#030303');
  148. $template_data['keyword4'] = array('value' => '2级会员购买,佣金'.$commiss_two_money.'元', 'color' => '#030303');
  149. $pay_order_msg_info = M('config')->where( array('name' => 'weprog_neworder_commiss') )->find();
  150. $template_id = $pay_order_msg_info['value'];
  151. $url =C('SITE_URL');
  152. $pagepath = 'pages/dan/me';
  153. send_wxtemplate_msg($template_data,$url,$pagepath,$share_member['we_openid'],$template_id,$member_formid_info['formid']);
  154. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  155. }
  156. }
  157. //三级
  158. $member_info = M('member')->where( array('member_id' => $member_info['share_id']) )->find();
  159. if(intval($member_info['share_id']) > 0)
  160. {
  161. $commiss_three_money = $order_goods['commiss_three_money'];
  162. if($commiss_three_money > 0)
  163. {
  164. $data = array();
  165. $data['member_id'] = $member_info['share_id'];
  166. $data['child_member_id'] = $member_id;
  167. $data['order_id'] = $order_id;
  168. $data['order_goods_id'] = $order_goods_id;
  169. $data['store_id'] = $store_id;
  170. $data['state'] = 0;
  171. $data['level'] = 3;
  172. $data['money'] = $commiss_three_money;
  173. $data['addtime'] = time();
  174. M('member_commiss_order')->add($data);
  175. //TODO 发送模板消息3级下级购买,佣金多少
  176. $share_member = M('member')->field('we_openid,openid')->where( array('member_id' => $member_info['share_id']) )->find();
  177. $wx_template_data = array();
  178. $wx_template_data['first'] = array('value' => '3级会员购买', 'color' => '#030303');
  179. $wx_template_data['keyword1'] = array('value' => $order_goods['name'], 'color' => '#030303');
  180. $wx_template_data['keyword2'] = array('value' => round($commiss_three_money,2).'元', 'color' => '#030303');
  181. $wx_template_data['keyword3'] = array('value' => '支付成功', 'color' => '#030303');
  182. $wx_template_data['remark'] = array('value' => '点击了解更多佣金详情', 'color' => '#030303');
  183. if(!empty($share_member['openid']))
  184. {
  185. $url = C('SITE_URL')."index.php?s=/tuanbonus/groupleaderindex.html";
  186. send_template_msg($wx_template_data,$url,$share_member['openid'],C('weixin_neworder_commiss'));
  187. }
  188. $member_formid_info = M('member_formid')->where( array('member_id' => $member_info['share_id'],'formid' => array('neq',''), 'state' => 0) )->order('id desc')->find();
  189. //更新
  190. if(!empty($member_formid_info))
  191. {
  192. $template_data['keyword1'] = array('value' => 'FX'.$order_id, 'color' => '#030303');
  193. $template_data['keyword2'] = array('value' => $order_goods['name'], 'color' => '#030303');
  194. $template_data['keyword3'] = array('value' => round($order_goods['total'],2).'元', 'color' => '#030303');
  195. $template_data['keyword4'] = array('value' => '3级会员购买,佣金'.$commiss_three_money.'元', 'color' => '#030303');
  196. $pay_order_msg_info = M('config')->where( array('name' => 'weprog_neworder_commiss') )->find();
  197. $template_id = $pay_order_msg_info['value'];
  198. $url =C('SITE_URL');
  199. $pagepath = 'pages/dan/me';
  200. send_wxtemplate_msg($template_data,$url,$pagepath,$share_member['we_openid'],$template_id,$member_formid_info['formid']);
  201. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. 给上级会员分佣
  210. **/
  211. public function ins_member_commiss_order2($member_id,$order_id,$store_id)
  212. {
  213. $member_info = M('member')->where( array('member_id' => $member_id) )->find();
  214. //share_id
  215. if(intval($member_info['share_id']) > 0)
  216. {
  217. $order_goods = M('order_goods')->where( array('order_id' => $order_id) )->find();
  218. if(!empty($order_goods))
  219. {
  220. $commiss_one_money = $order_goods['commiss_one_money'];
  221. if($commiss_one_money > 0)
  222. {
  223. $data = array();
  224. $data['member_id'] = $member_info['share_id'];
  225. $data['child_member_id'] = $member_id;
  226. $data['order_id'] = $order_id;
  227. $data['store_id'] = $store_id;
  228. $data['state'] = 0;
  229. $data['money'] = $commiss_one_money;
  230. $data['addtime'] = time();
  231. M('member_commiss_order')->add($data);
  232. }
  233. }
  234. }
  235. }
  236. /**
  237. 更新普通会员为分销商
  238. **/
  239. public function updateCommissUser($member_id = 0)
  240. {
  241. $notify_model = D('Home/Weixinnotify');
  242. $member_info = M('member')->where( array('member_id' => $member_id) )->find();
  243. if($member_info['comsiss_flag'] != 1)
  244. {
  245. M('member')->where( array('member_id' => $member_id) )->save( array('comsiss_flag' => 1) );
  246. //TODO SEND MSG
  247. $notify_model->send_super_tuanz_msg($member_info['openid']);
  248. }
  249. }
  250. /**
  251. 互为上下级关系
  252. **/
  253. public function relation_fenxiao($parent_member_id,$child_member_id)
  254. {
  255. $notify_model = D('Home/Weixinnotify');
  256. //自己不互为上下线
  257. if($parent_member_id != $child_member_id)
  258. {
  259. $parent_info = M('member')->where( array('member_id' => $parent_member_id) )->find();
  260. $child_info = M('member')->where( array('member_id' => $child_member_id) )->find();
  261. if($parent_info['comsiss_flag'] == 1 && $child_info['share_id'] == 0 && $parent_info['share_id'] != $child_member_id)
  262. {
  263. //上级必须是分销商,下级必须无上级
  264. //开始更新分销商
  265. M('member')->where( array('member_id' => $child_member_id) )->save( array('share_id' => $parent_member_id) );
  266. //TODO 去发送消息给分销商
  267. //send_fenxiao_invitemember($openid,$username) openid
  268. $notify_model->send_fenxiao_invitemember($parent_info['openid'],$child_info['name']);
  269. }
  270. }
  271. }
  272. }