order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/11/4
  6. * Time: 下午10:17
  7. */
  8. namespace async;
  9. use push_helper;
  10. use order_helper;
  11. use member_info;
  12. use Exception;
  13. use search;
  14. use bonus;
  15. use bonus_helper;
  16. use sms_helper;
  17. use Sms;
  18. use fcode;
  19. use Log;
  20. class simple_order
  21. {
  22. private $info;
  23. private $rgoods;
  24. public function __construct($order_info) {
  25. $this->info = $order_info;
  26. $this->rgoods = $this->find_recommend();
  27. }
  28. private function find_recommend()
  29. {
  30. $result = null;
  31. foreach ($this->info['order_goods'] as $good)
  32. {
  33. if(empty($result)) {
  34. $result = $good;
  35. }
  36. else
  37. {
  38. if($result['goods_price'] < $good['goods_price']) {
  39. $result = $good;
  40. }
  41. }
  42. }
  43. return $result;
  44. }
  45. public function user() {
  46. return $this->info['order_info']['buyer_id'];
  47. }
  48. public function discount() {
  49. return $this->info['order_info']['pd_amount'];
  50. }
  51. public function recommend_goods() {
  52. return $this->rgoods;
  53. }
  54. public function total_cash() {
  55. return $this->info['order_info']['pay_cash'];
  56. }
  57. public function bonus_rates() {
  58. return $this->info['order_info']['bonus_rates'];
  59. }
  60. public function pd_amount() {
  61. return $this->info['order_info']['pd_amount'];
  62. }
  63. public function order_sn() {
  64. return $this->info['order_info']['order_sn'];
  65. }
  66. }
  67. class order extends IAsync
  68. {
  69. private $simple_order;
  70. private $user_info;
  71. private $paysn;
  72. public function __construct($paysn)
  73. {
  74. $this->paysn = $paysn;
  75. $order = $this->order(['pay_sn' => $paysn]);
  76. if(empty($order)) throw new Exception("错误的订单");
  77. $this->simple_order = new simple_order($order);
  78. $user = $this->simple_order->user();
  79. $this->user_info = new member_info($user);
  80. }
  81. public function run() {
  82. }
  83. //支付成功
  84. public function onPaied()
  85. {
  86. push_helper::user_order_paied($this->user_info->member_id(),$this->paysn);
  87. $inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $this->user_info->member_id()]);
  88. $this->notify_paided_goods($inviters);
  89. }
  90. private function notify_paided_goods($inviters)
  91. {
  92. if(empty($inviters)) return false;
  93. $goods = $this->simple_order->recommend_goods();
  94. $level = 0;
  95. $ex_invitee = $this->user_info->member_id();
  96. foreach ($inviters as $inviter)
  97. {
  98. $this->nofity_paied_inviter($inviter,$level,$goods);
  99. $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $inviter]);
  100. $invitees = $this->ex_user($invitees,$ex_invitee);
  101. $this->nofity_paied_invitees($inviter,$invitees,$level,$goods);
  102. $ex_invitee = $inviter;
  103. $level += 1;
  104. }
  105. return true;
  106. }
  107. private function nofity_paied_inviter($inviter,$level,$goods)
  108. {
  109. push_helper::inviter_order_paied($inviter,$this->user_info->member_id(),$level + 1,$goods);
  110. }
  111. private function nofity_paied_invitees($inviter,$invitees,$level,$goods)
  112. {
  113. $i = 0;
  114. foreach ($invitees as $invitee)
  115. {
  116. if($i < self::max_level_pushs) {
  117. push_helper::invitee_order_paied($invitee, $this->user_info->member_id(), $goods);
  118. }
  119. $i++;
  120. }
  121. }
  122. //发货
  123. public function onSended($logistics_no,$logistics_company)
  124. {
  125. $mobile = $this->user_info->mobile();
  126. sms_helper::send_nostore_message($mobile,Sms::order_sended,['company'=> $logistics_company,'logistics'=>$logistics_no,'time'=>'5']);
  127. }
  128. //取消订单
  129. public function onCancel()
  130. {
  131. $pd_amount = $this->simple_order->pd_amount();
  132. $bonus_rates = $this->simple_order->bonus_rates();
  133. if(empty($bonus_rates)) {
  134. $bonus_rates = [30 => $pd_amount];
  135. }
  136. else
  137. {
  138. $tmp = [];
  139. foreach ($bonus_rates as $val) {
  140. $rate = $val['rate'];
  141. $amount = $val['total'];
  142. $tmp[$rate] = $amount;
  143. }
  144. $bonus_rates = $tmp;
  145. }
  146. $params = bonus\parameters::order_cancel($bonus_rates);
  147. foreach ($params as $param)
  148. {
  149. $rate_money = $param['rate_money'];
  150. $ret = bonus_helper::make_bonus($param,$rate_money);
  151. $rate = $rate_money[0]['rate'];
  152. $amount = $rate_money[0]['amount'];
  153. $user = $this->user_info->member_id();
  154. $order_sn = $this->simple_order->order_sn();
  155. if($ret != false) {
  156. $type_sn = $ret['type_sn'];
  157. bonus_helper::send($ret['type_sn'],[$user]);
  158. push_helper::order_cancel_bonus($user,$amount,$rate,$order_sn,$type_sn);
  159. } else {
  160. Log::record("给用户{$user}:order_sn={$order_sn},退款rate={$rate} amount={$amount}失败.");
  161. }
  162. }
  163. Model()->table('order')->where(['order_sn' => $this->simple_order->order_sn()])->update(['pd_amount' => 0,'bonus_rate' => '']);
  164. }
  165. //确认收货
  166. public function onComplete()
  167. {
  168. $num = fcode\operator::unlock($this->paysn);
  169. if($num > 0) {
  170. push_helper::fcode_unlock($this->user_info->member_id(),$num);
  171. }
  172. sms_helper::send_nostore_message($this->user_info->mobile(),Sms::order_received,['time'=>'5']);
  173. $this->send_complete_bonus();
  174. $inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $this->user_info->member_id()]);
  175. $this->notify_complete_inviter($inviters);
  176. }
  177. private function notify_complete_inviter($inviters)
  178. {
  179. if(empty($inviters)) return false;
  180. $level = 0;
  181. $ex_invitee = $this->user_info->member_id();
  182. foreach ($inviters as $inviter)
  183. {
  184. $this->reward_inviter($inviter,$level);
  185. $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $inviter]);
  186. $invitees = $this->ex_user($invitees,$ex_invitee);
  187. $this->reward_invitees($inviter,$invitees,$level);
  188. $ex_invitee = $inviter;
  189. $level += 1;
  190. }
  191. return true;
  192. }
  193. private function reward_inviter($inviter, $level)
  194. {
  195. $cash = $this->simple_order->total_cash();
  196. $params = bonus\parameters::order_complete_inviter($cash,$this->user_info->nickname(),$level);
  197. foreach ($params as $param)
  198. {
  199. $type = bonus_helper::create_type_input($param);
  200. $money = $type->getTotal_amount();
  201. $rate = $type->bonus_rate();
  202. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  203. if($ret != false) {
  204. $type_sn = $ret['type_sn'];
  205. bonus_helper::send($type_sn,[$inviter]);
  206. push_helper::inviter_order_complete_bonus($inviter,$this->user_info->member_id(),$level + 1,$money,$rate,$type_sn);
  207. $this->add_reward($inviter,$money);
  208. } else {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }
  214. private function reward_invitees($inviter, $invitees, $level)
  215. {
  216. if(empty($invitees)) return false;
  217. $cash = $this->simple_order->total_cash();
  218. if($cash < 50) return false;
  219. $num = count($invitees);
  220. $params = bonus\parameters::order_complete_invitee($this->user_info->nickname(),$level,$num);
  221. foreach ($params as $param)
  222. {
  223. $type = bonus_helper::create_type_input($param);
  224. $money = $type->getTotal_amount();
  225. $rate = $type->bonus_rate();
  226. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  227. if($ret != false)
  228. {
  229. $type_sn = $ret['type_sn'];
  230. bonus_helper::send($type_sn,$invitees);
  231. $i = 0;
  232. foreach ($invitees as $invitee)
  233. {
  234. $amount = $money / $num;
  235. $this->add_reward($invitee,$amount);
  236. if($i < self::max_level_pushs) {
  237. push_helper::invitee_order_complete_bonus($invitee,$this->user_info,$amount,$rate,$type_sn);
  238. }
  239. $i++;
  240. }
  241. }
  242. else {
  243. return false;
  244. }
  245. }
  246. return true;
  247. }
  248. private function send_complete_bonus()
  249. {
  250. $total_amount = $this->simple_order->total_cash();
  251. $fixed_params = bonus\parameters::order_complete_fixed($total_amount);
  252. foreach ($fixed_params as $param)
  253. {
  254. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  255. if($ret != false) {
  256. $type_sn = $ret['type_sn'];
  257. $bonus = bonus_helper::send($type_sn, [$this->user_info->member_id()]);
  258. $bonus_value = $bonus[0]['bonus_value'];
  259. $bonus_rate = $bonus[0]['bonus_rate'];
  260. push_helper::user_order_complete_fixed($this->user_info->member_id(),$bonus_value,$bonus_rate,$type_sn);
  261. }
  262. }
  263. $random_params = bonus\parameters::order_complete_random($total_amount);
  264. foreach ($random_params as $param)
  265. {
  266. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  267. if($ret != false) {
  268. $type_sn = $ret['type_sn'];
  269. bonus_helper::send($type_sn, [$this->user_info->member_id()]);
  270. push_helper::user_order_complete_random($this->user_info->member_id(),$param['rate_money']['num'],$type_sn);
  271. }
  272. }
  273. }
  274. //评论
  275. public function onEvaluate()
  276. {
  277. $this->send_evaluate_bonus();
  278. $inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $this->user_info->member_id()]);
  279. $this->notify_inviter_evaluate($inviters);
  280. }
  281. private function send_evaluate_bonus()
  282. {
  283. $fixed_params = bonus\parameters::order_evaluate_fixed();
  284. foreach ($fixed_params as $param)
  285. {
  286. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  287. if($ret != false) {
  288. $type_sn = $ret['type_sn'];
  289. $bonus = bonus_helper::send($type_sn, [$this->user_info->member_id()]);
  290. $bonus_value = $bonus[0]['bonus_value'];
  291. $bonus_rate = $bonus[0]['bonus_rate'];
  292. push_helper::order_evaluate_bonus($this->user_info->member_id(),$bonus_value,$bonus_rate,$type_sn);
  293. }
  294. }
  295. }
  296. private function notify_inviter_evaluate($inviters)
  297. {
  298. if(empty($inviters)) return false;
  299. $goods = $this->simple_order->recommend_goods();
  300. $level = 0;
  301. $ex_invitee = $this->user_info->member_id();
  302. foreach ($inviters as $inviter)
  303. {
  304. $this->nofity_evaluate_inviter($inviter,$level,$goods);
  305. $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $inviter]);
  306. $invitees = $this->ex_user($invitees,$ex_invitee);
  307. $this->nofity_evaluate_invitees($inviter,$invitees,$level,$goods);
  308. $ex_invitee = $inviter;
  309. $level += 1;
  310. }
  311. return true;
  312. }
  313. private function nofity_evaluate_inviter($inviter,$level,$goods)
  314. {
  315. push_helper::inviter_order_evaluated($inviter,$this->user_info->member_id(),$level + 1,$goods);
  316. }
  317. private function nofity_evaluate_invitees($inviter,$invitees,$level,$goods)
  318. {
  319. $i = 0;
  320. foreach ($invitees as $invitee)
  321. {
  322. if($i < self::max_level_pushs) {
  323. push_helper::invitee_order_evaluated($invitee,$this->user_info->member_id(),$goods);
  324. }
  325. $i++;
  326. }
  327. }
  328. private function order($condition)
  329. {
  330. $model_order = Model('order');
  331. $order_list = $model_order->getNormalOrderList($condition,0, '*', 'order_id desc', '', ['order_common','order_address', 'order_goods']);
  332. $order_helper = new order_helper($order_list);
  333. $orders = $order_helper->format();
  334. if(!empty($orders)) {
  335. return $orders[0];
  336. } else {
  337. return false;
  338. }
  339. }
  340. }