order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 bonus_amount()
  64. {
  65. return $this->info['order_info']['user_bonus'];
  66. }
  67. public function room_id()
  68. {
  69. return intval($this->info['order_info']['room_id']);
  70. }
  71. public function room_bonus()
  72. {
  73. return $this->info['order_info']['room_bonus'];
  74. }
  75. public function order_sn() {
  76. return $this->info['order_info']['order_sn'];
  77. }
  78. }
  79. class order extends IAsync
  80. {
  81. private $simple_order;
  82. private $user_info;
  83. private $paysn;
  84. public function __construct($paysn)
  85. {
  86. $this->paysn = $paysn;
  87. $order = $this->order(['pay_sn' => $paysn]);
  88. if(empty($order)) throw new Exception("错误的订单");
  89. $this->simple_order = new simple_order($order);
  90. $user = $this->simple_order->user();
  91. $this->user_info = new member_info($user);
  92. }
  93. public function run() {
  94. }
  95. //支付成功
  96. public function onPaied()
  97. {
  98. push_helper::user_order_paied($this->user_info->member_id(),$this->paysn);
  99. $inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $this->user_info->member_id()]);
  100. $this->notify_paided_goods($inviters);
  101. }
  102. private function notify_paided_goods($inviters)
  103. {
  104. if(empty($inviters)) return false;
  105. $goods = $this->simple_order->recommend_goods();
  106. $level = 0;
  107. $ex_invitee = $this->user_info->member_id();
  108. foreach ($inviters as $inviter)
  109. {
  110. $this->nofity_paied_inviter($inviter,$level,$goods);
  111. $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $inviter]);
  112. $invitees = $this->ex_user($invitees,$ex_invitee);
  113. $this->nofity_paied_invitees($inviter,$invitees,$level,$goods);
  114. $ex_invitee = $inviter;
  115. $level += 1;
  116. }
  117. return true;
  118. }
  119. private function nofity_paied_inviter($inviter,$level,$goods)
  120. {
  121. push_helper::inviter_order_paied($inviter,$this->user_info->member_id(),$level + 1,$goods);
  122. }
  123. private function nofity_paied_invitees($inviter,$invitees,$level,$goods)
  124. {
  125. $i = 0;
  126. foreach ($invitees as $invitee)
  127. {
  128. if($i < self::max_level_pushs) {
  129. push_helper::invitee_order_paied($invitee, $this->user_info->member_id(), $goods);
  130. }
  131. $i++;
  132. }
  133. }
  134. //发货
  135. public function onSended($logistics_no,$logistics_company)
  136. {
  137. $mobile = $this->user_info->mobile();
  138. sms_helper::send_nostore_message($mobile,sms_helper::order_sended,['company'=> $logistics_company,'logistics'=>$logistics_no,'time'=>'5']);
  139. }
  140. //取消订单
  141. public function onCancel($order_sn)
  142. {
  143. $order = $this->order(['order_sn' => $order_sn]);
  144. $simple = new simple_order($order);
  145. if(intval($simple->bonus_amount() * 100 + 0.5) > 0)
  146. {
  147. $params = bonus\parameters::order_cancel([defaultBonusRate() => $simple->bonus_amount()]);
  148. foreach ($params as $param)
  149. {
  150. $rate_money = $param['rate_money'];
  151. $ret = bonus_helper::make_bonus($param,$rate_money);
  152. $rate = $rate_money[0]['rate'];
  153. $amount = $rate_money[0]['amount'];
  154. $user = $this->user_info->member_id();
  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' => $order_sn])->update(['bonus_amount' => '','bonus_rate' => '']);
  164. }
  165. if($simple->room_id() > 0 && intval($simple->room_bonus() * 100 + 0.5) > 0)
  166. {
  167. //todo
  168. }
  169. }
  170. //确认收货
  171. public function onComplete()
  172. {
  173. $num = fcode\operator::unlock($this->paysn);
  174. if($num > 0) {
  175. push_helper::fcode_unlock($this->user_info->member_id(),$num);
  176. }
  177. sms_helper::send_nostore_message($this->user_info->mobile(),sms_helper::order_received,['time'=>'5']);
  178. $this->send_complete_bonus();
  179. $inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $this->user_info->member_id()]);
  180. $this->notify_complete_inviter($inviters);
  181. }
  182. private function notify_complete_inviter($inviters)
  183. {
  184. if(empty($inviters)) return false;
  185. $level = 0;
  186. $ex_invitee = $this->user_info->member_id();
  187. foreach ($inviters as $inviter)
  188. {
  189. $this->reward_inviter($inviter,$level);
  190. $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $inviter]);
  191. $invitees = $this->ex_user($invitees,$ex_invitee);
  192. $this->reward_invitees($inviter,$invitees,$level);
  193. $ex_invitee = $inviter;
  194. $level += 1;
  195. }
  196. return true;
  197. }
  198. private function reward_inviter($inviter, $level)
  199. {
  200. $cash = $this->simple_order->total_cash();
  201. $params = bonus\parameters::order_complete_inviter($cash,$this->user_info->nickname(),$level);
  202. foreach ($params as $param)
  203. {
  204. $type = bonus_helper::create_type_input($param);
  205. $money = $type->getTotal_amount();
  206. $rate = $type->bonus_rate();
  207. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  208. if($ret != false) {
  209. $type_sn = $ret['type_sn'];
  210. bonus_helper::send($type_sn,[$inviter]);
  211. push_helper::inviter_order_complete_bonus($inviter,$this->user_info->member_id(),$level + 1,$money,$rate,$type_sn);
  212. $this->add_reward($inviter,$money);
  213. } else {
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. private function reward_invitees($inviter, $invitees, $level)
  220. {
  221. if(empty($invitees)) return false;
  222. $cash = $this->simple_order->total_cash();
  223. if($cash < 50) return false;
  224. $num = count($invitees);
  225. $params = bonus\parameters::order_complete_invitee($this->user_info->nickname(),$level,$num);
  226. foreach ($params as $param)
  227. {
  228. $type = bonus_helper::create_type_input($param);
  229. $money = $type->getTotal_amount();
  230. $rate = $type->bonus_rate();
  231. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  232. if($ret != false)
  233. {
  234. $type_sn = $ret['type_sn'];
  235. bonus_helper::send($type_sn,$invitees);
  236. $i = 0;
  237. foreach ($invitees as $invitee)
  238. {
  239. $amount = $money / $num;
  240. $this->add_reward($invitee,$amount);
  241. if($i < self::max_level_pushs) {
  242. push_helper::invitee_order_complete_bonus($invitee,$this->user_info,$amount,$rate,$type_sn);
  243. }
  244. $i++;
  245. }
  246. }
  247. else {
  248. return false;
  249. }
  250. }
  251. return true;
  252. }
  253. private function send_complete_bonus()
  254. {
  255. $total_amount = $this->simple_order->total_cash();
  256. $fixed_params = bonus\parameters::order_complete_fixed($total_amount);
  257. foreach ($fixed_params as $param)
  258. {
  259. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  260. if($ret != false) {
  261. $type_sn = $ret['type_sn'];
  262. $bonus = bonus_helper::send($type_sn, [$this->user_info->member_id()]);
  263. $bonus_value = $bonus[0]['bonus_value'];
  264. $bonus_rate = $bonus[0]['bonus_rate'];
  265. push_helper::user_order_complete_fixed($this->user_info->member_id(),$bonus_value,$bonus_rate,$type_sn);
  266. }
  267. }
  268. $random_params = bonus\parameters::order_complete_random($total_amount);
  269. foreach ($random_params as $param)
  270. {
  271. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  272. if($ret != false) {
  273. $type_sn = $ret['type_sn'];
  274. bonus_helper::send($type_sn, [$this->user_info->member_id()]);
  275. push_helper::user_order_complete_random($this->user_info->member_id(),$param['rate_money']['num'],$type_sn);
  276. }
  277. }
  278. }
  279. //评论
  280. public function onEvaluate()
  281. {
  282. $this->send_evaluate_bonus();
  283. $inviters = search\relation_client::instance()->fetch_inviters(['user_id' => $this->user_info->member_id()]);
  284. $this->notify_inviter_evaluate($inviters);
  285. }
  286. private function send_evaluate_bonus()
  287. {
  288. $fixed_params = bonus\parameters::order_evaluate_fixed();
  289. foreach ($fixed_params as $param)
  290. {
  291. $ret = bonus_helper::make_bonus($param,$param['rate_money']);
  292. if($ret != false) {
  293. $type_sn = $ret['type_sn'];
  294. $bonus = bonus_helper::send($type_sn, [$this->user_info->member_id()]);
  295. $bonus_value = $bonus[0]['bonus_value'];
  296. $bonus_rate = $bonus[0]['bonus_rate'];
  297. push_helper::order_evaluate_bonus($this->user_info->member_id(),$bonus_value,$bonus_rate,$type_sn);
  298. sms_helper::send_nostore_message($this->user_info->mobile(),sms_helper::after_comment,['money' => $bonus_value]);
  299. }
  300. }
  301. }
  302. private function notify_inviter_evaluate($inviters)
  303. {
  304. if(empty($inviters)) return false;
  305. $goods = $this->simple_order->recommend_goods();
  306. $level = 0;
  307. $ex_invitee = $this->user_info->member_id();
  308. foreach ($inviters as $inviter)
  309. {
  310. $this->nofity_evaluate_inviter($inviter,$level,$goods);
  311. $invitees = search\relation_client::instance()->fetch_invitees(['user_id' => $inviter]);
  312. $invitees = $this->ex_user($invitees,$ex_invitee);
  313. $this->nofity_evaluate_invitees($inviter,$invitees,$level,$goods);
  314. $ex_invitee = $inviter;
  315. $level += 1;
  316. }
  317. return true;
  318. }
  319. private function nofity_evaluate_inviter($inviter,$level,$goods)
  320. {
  321. push_helper::inviter_order_evaluated($inviter,$this->user_info->member_id(),$level + 1,$goods);
  322. }
  323. private function nofity_evaluate_invitees($inviter,$invitees,$level,$goods)
  324. {
  325. $i = 0;
  326. foreach ($invitees as $invitee)
  327. {
  328. if($i < self::max_level_pushs) {
  329. push_helper::invitee_order_evaluated($invitee,$this->user_info->member_id(),$goods);
  330. }
  331. $i++;
  332. }
  333. }
  334. private function order($condition)
  335. {
  336. $model_order = Model('order');
  337. $order_list = $model_order->getNormalOrderList($condition,0, '*', 'order_id desc', '', ['order_common','order_address', 'order_goods']);
  338. $order_helper = new order_helper($order_list);
  339. $orders = $order_helper->format();
  340. if(!empty($orders)) {
  341. return $orders[0];
  342. } else {
  343. return false;
  344. }
  345. }
  346. }