OrderController.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <?php
  2. /**
  3. * OrderController o2o下单相关接口
  4. */
  5. class OrderController extends O2oBaseController{
  6. public function actionAdd(){
  7. $user_id = Yii::app()->getRequest()->getParam("user_id");
  8. $products = json_decode(Yii::app()->getRequest()->getParam("products","[]"),true);
  9. $memo = Yii::app()->getRequest()->getParam("memo","");
  10. $booking_time = Yii::app()->getRequest()->getParam("booking_time",'');
  11. $precedence = intval(Yii::app()->getRequest()->getParam("precedence",0));
  12. $coupons = json_decode(Yii::app()->getRequest()->getParam("coupons","[]"),true);
  13. $address_id = Yii::app()->getRequest()->getParam("address_id");
  14. $tech_id = Yii::app()->getRequest()->getParam("tech_id");
  15. $station = Yii::app()->getRequest()->getParam("station");
  16. $order_channel = Yii::app()->getRequest()->getParam("order_channel");
  17. $counts = Yii::app()->request->getParam('counts', 1);
  18. $extra = Yii::app()->request->getParam('extra',array());
  19. $extra = json_decode($extra);
  20. $balance = floatval(Yii::app()->getRequest()->getParam("balance",0));//余额支付的金额
  21. //CommonFn::requestAjax(false,'系统升级中,暂时不能下单');
  22. if(!$precedence){
  23. $booking_check = strtotime($booking_time);
  24. $verity_check = $booking_check - time();
  25. if($verity_check < 13800){
  26. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  27. }
  28. }
  29. if(!$user_id||!$products||!$address_id){
  30. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  31. }
  32. $user_obj = CommonFn::apigetObJ($user_id,'ZUser',CommonFn::getMessage('user','id_not_exist'),array(),201);
  33. if($balance){
  34. if($user_obj->balance < $balance){
  35. CommonFn::requestAjax(false,'余额不足哦');
  36. }
  37. }
  38. $address_list = $user_obj->shop_address?$user_obj->shop_address:array();
  39. $address = array();
  40. foreach ($address_list as $value) {
  41. if($address_id == $value['address_id']){
  42. $address = $value;
  43. }
  44. }
  45. if(!$address){
  46. file_put_contents('/data/erroraddressc.log',var_export($address,true),FILE_APPEND);
  47. CommonFn::requestAjax(false,CommonFn::getMessage('shop','address_false'));
  48. }else{
  49. if($station){
  50. $station = Station::get(new MongoId($station));
  51. if(!$station){
  52. file_put_contents('/data/erroraddressa.log',var_export($address,true),FILE_APPEND);
  53. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','address_cannot_service'));
  54. }
  55. $divide_station = $station->_id;
  56. }else{
  57. file_put_contents('/data/erroraddressb.log',var_export($address,true),FILE_APPEND);
  58. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','address_cannot_service'));
  59. }
  60. $o2o_address['name'] = $address['name'];
  61. $o2o_address['mobile'] = $address['mobile'];
  62. $o2o_address['province'] = $address['address']['province'];
  63. $o2o_address['city'] = $address['address']['city'];
  64. $o2o_address['area'] = $address['address']['area'];
  65. $o2o_address['detail'] = $address['address']['detail'];
  66. $o2o_address['position'] = isset($address['position'])?$address['position']:array(0,0);
  67. $o2o_address['poi'] = isset($address['address']['poi'])?$address['address']['poi']:array();
  68. }
  69. $product_list = array();
  70. $price = 0;
  71. $service_type = 0;
  72. foreach ($products as $product) {
  73. if(isset($product['product_id']) && $product['count'] >= 1){
  74. $product_temp = array();
  75. $product_obj = Product::get(new MongoId($product['product_id']));
  76. if($product_obj){
  77. $price += ($product_obj->price*$product['count']);
  78. $product_temp['product'] = $product_obj->_id;
  79. $product_temp['count'] = $product['count'];
  80. $product_temp['extra'] = $extra;
  81. $product_list[] = $product_temp;
  82. if($product_obj->is_extra == 0){
  83. $service_type = $product_obj->type;
  84. }
  85. }else{
  86. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_not_exist'));
  87. }
  88. }else{
  89. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  90. }
  91. }
  92. if($precedence){
  93. $price += 40;
  94. }
  95. $final_price = $price;
  96. $used_coupon = array();
  97. foreach ($coupons as $coupon){
  98. $user_coupon = UserCoupon::get(new MongoId($coupon));
  99. $current_time = time();
  100. if($user_coupon && (string)$user_coupon->user == $user_id && $user_coupon->start_time < $current_time && $user_coupon->end_time > $current_time && $user_coupon->status == 1){
  101. $coupon = Coupon::get($user_coupon->coupon);
  102. //判断优惠券可用时间
  103. $weekend_check = (date('w',strtotime($booking_time))==0 || date('w',strtotime($booking_time))==6)?1:0;
  104. $booking_hour = date('H',strtotime($booking_time));
  105. if($weekend_check){
  106. if($coupon->workday_limit==1){
  107. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  108. }
  109. }else{
  110. if($coupon->workday_limit==2){
  111. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  112. }
  113. }
  114. if($coupon->time_limit_start && $coupon->time_limit_end && ($coupon->time_limit_start > $booking_hour || $coupon->time_limit_end < $booking_hour)){
  115. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  116. }
  117. $new_user_coupons = Yii::app()->params['new_user_coupons'];
  118. if(($coupon->type == 0 || $coupon->type == $service_type ) && $coupon->status==1 && $coupon->min_price <= $final_price){
  119. $final_price = $final_price-$coupon->value>=0?$final_price-$coupon->value:0;
  120. $used_coupon[] = $user_coupon->_id;
  121. $user_coupon->status = -1;
  122. $user_coupon->use_time = time();
  123. $user_coupon->update(array('status','use_time'),true);
  124. }else{
  125. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  126. }
  127. }else{
  128. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','coupon_unuseable'));
  129. }
  130. }
  131. $order = new ROrder();
  132. $order->order_time = time();
  133. $order->booking_time = strtotime($booking_time);
  134. $order->products = $product_list;
  135. $order->precedence = $precedence;
  136. $order->price = $price;
  137. $order->channel = $order_channel;
  138. $order->counts = $counts;
  139. $order->final_price = ceil($final_price*100)/100-$balance;
  140. $order->address = $o2o_address;
  141. $order->memo = $memo;
  142. $order->station = $divide_station;
  143. $order->coupons = $used_coupon;
  144. if(isset($user_coupon->coupon)){
  145. $order->coupon_type = $user_coupon->coupon;
  146. }
  147. if($order->final_price == 0){
  148. $order->status = 1;
  149. }else{
  150. $order->status = 0;
  151. }
  152. $order->user = $user_obj->_id;
  153. $order->type = $service_type;
  154. $order->technician = intval($tech_id);
  155. if($order->save()){
  156. $data = ROrder::model()->parseRow($order);
  157. foreach ($products as $product) {
  158. if(isset($product['product_id']) && $product['count'] >= 1){
  159. $product_obj = Product::get(new MongoId($product['product_id']));
  160. if($product_obj){
  161. $product_obj->sale_count+=$product['count'];
  162. $product_obj->update(array('sale_count'),true);
  163. }
  164. }
  165. }
  166. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data);
  167. }
  168. }
  169. public function actionList(){
  170. $user_id = Yii::app()->getRequest()->getParam("user_id");
  171. $type = Yii::app()->getRequest()->getParam("type");
  172. $order_type = Yii::app()->getRequest()->getParam("order_type");
  173. if(!$order_type){
  174. $order_type = $type;
  175. }
  176. $user_obj = CommonFn::apigetObJ($user_id,"ZUser",CommonFn::getMessage('user','id_not_exist'),201);
  177. $page = intval(Yii::app()->getRequest()->getParam("page",1));
  178. $pagesize = Yii::app()->params['ROrderListPageSize'];
  179. if($order_type == 1 ){
  180. $conditions = array(
  181. 'user'=>array('==',$user_obj->_id),
  182. 'status'=>array('notin',array(-3,-2,-1,6,7))
  183. );
  184. }elseif ($order_type == 2) {
  185. $conditions = array(
  186. 'user'=>array('==',$user_obj->_id),
  187. 'status'=>array('in',array(-3,-2,-1,7))
  188. );
  189. }elseif($order_type == 3){
  190. $conditions = array(
  191. 'user'=>array('==',$user_obj->_id),
  192. 'status'=>array('==',6)
  193. );
  194. }else{
  195. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  196. }
  197. $order = array(
  198. 'booking_time'=>'desc'
  199. );
  200. $model = new ROrder();
  201. $pagedata = CommonFn::getPagedata($model,$page,$pagesize,$conditions,$order);
  202. $order_list = $pagedata['res'];
  203. foreach ($order_list as $key => $value) {
  204. $order_list[$key] = $model->output($value);
  205. }
  206. $data = array_values($order_list);
  207. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,200,array('sum_count' => $pagedata['sum_count'],'sum_page'=>$pagedata['sum_page'],'page_size'=>$pagedata['page_size'],'current_page'=>$pagedata['current_page']));
  208. }
  209. public function actionPay(){
  210. $user_id = Yii::app()->getRequest()->getParam("user_id");
  211. $pay_channel = Yii::app()->getRequest()->getParam("pay_channel");
  212. $order_id = Yii::app()->getRequest()->getParam("order_id");
  213. if(!$user_id || !$pay_channel || !$order_id || !CommonFn::isMongoId($order_id)){
  214. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  215. }
  216. $user_obj = RUser::get(new MongoId($user_id));
  217. $order = ROrder::get(new MongoId($order_id));
  218. if(!$order || !$user_obj){
  219. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  220. }
  221. if($order && (string)$order->user != $user_id){
  222. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  223. }
  224. if($order->charge_id){
  225. CommonFn::requestAjax(false,'此订单已支付过',array('have_pay'=>1));
  226. }
  227. foreach ($order->products as $value) {
  228. if(isset($value['product'])){
  229. $product = Product::get($value['product']);
  230. }
  231. if($product){
  232. break;
  233. }
  234. }
  235. if(isset($product)&&$product){
  236. $str = isset(Yii::app()->params['o2o_service'][$product->type]['name'])?Yii::app()->params['o2o_service'][$product->type]['name']:'';
  237. }
  238. $str = isset($str)&&$str?$str:'上门';
  239. $amount = ceil($order->final_price*1000)/10;
  240. $result = Service::factory('PayService')->Pay($pay_channel,$amount,(string)$order->_id,$str,$str,$user_obj->wx_pub_openid);
  241. if($result === false){
  242. CommonFn::requestAjax(false,'支付遇到点问题了,请稍候再试');
  243. }else{
  244. // 支付成功后用户有效订单数增加
  245. if (isset($user_obj->order_count)) {
  246. $user_obj->order_count += 1;
  247. } else {
  248. $user_obj->order_count = 1;
  249. }
  250. $user_obj->save();
  251. CommonFn::requestAjax(true,'success',json_decode($result),200,array('booking_time'=>$order->booking_time));
  252. }
  253. }
  254. public function actionDel(){
  255. $user_id = Yii::app()->getRequest()->getParam("user_id");
  256. $order_id = Yii::app()->getRequest()->getParam("order_id");
  257. if(!$user_id || !$order_id || !CommonFn::isMongoId($order_id)){
  258. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  259. }
  260. $user_obj = RUser::get(new MongoId($user_id));
  261. $order = ROrder::get(new MongoId($order_id));
  262. $status = -1;
  263. if(!$order || !$user_obj){
  264. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  265. }
  266. if($order && (string)$order->user != $user_id){
  267. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  268. }
  269. if($order->status != 0){
  270. CommonFn::requestAjax(false,'此订单暂不支持取消');
  271. }
  272. if( $order->status!=-1 && $order->status!=-2 ){
  273. foreach ($order->coupons as $user_coupon) {
  274. $user_coupon = UserCoupon::get($user_coupon);
  275. $user_coupon->status = 1;
  276. $user_coupon->update(array('status'),true);
  277. }
  278. }
  279. $order->status = $status;
  280. $arr_order = array('status');
  281. $success = $order->update($arr_order,true);
  282. if($success){
  283. $order_info = $order->parseRow($order);
  284. }
  285. CommonFn::requestAjax($success, '');
  286. }
  287. public function actionUsableCoupon(){
  288. $user_id = Yii::app()->getRequest()->getParam("user_id");
  289. $get_all = Yii::app()->getRequest()->getParam("get_all");
  290. $type = Yii::app()->getRequest()->getParam("type");
  291. $booking_time = Yii::app()->getRequest()->getParam("booking_time");
  292. if($booking_time){
  293. $booking_time = strtotime($booking_time);
  294. $weekend_check = (date('w',$booking_time)==0||date('w',$booking_time)==6)?1:0;
  295. $booking_hour = date('H',$booking_time);
  296. }
  297. $device_id = Yii::app()->getRequest()->getParam("device_id",'');
  298. $products = json_decode(Yii::app()->getRequest()->getParam("products",'[]'),true);
  299. if(!CommonFn::isMongoId($user_id)){
  300. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  301. }
  302. $UserCoupon = new UserCoupon();
  303. if($get_all){
  304. $criteria = new EMongoCriteria();
  305. $criteria->user('==',new MongoId($user_id));
  306. $current_time = time();
  307. $criteria->end_time('>=',$current_time);
  308. $criteria->status('==',1);
  309. $coupons = UserCoupon::model()->findAll($criteria);
  310. $coupon_list = array();
  311. $coupon_list['useable_coupons'] = array();
  312. foreach ($coupons as $value) {
  313. $coupon = Coupon::get($value->coupon);
  314. if($coupon->status!=1){
  315. continue;
  316. }
  317. $coupon_list['useable_coupons'][] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  318. }
  319. if(count($coupon_list['useable_coupons']) <= 15){
  320. $criteria = new EMongoCriteria();
  321. $criteria->user('==',new MongoId($user_id));
  322. $criteria->status('==',-1);
  323. $coupons = UserCoupon::model()->findAll($criteria);
  324. $coupon_list['used_coupons'] = array();
  325. foreach ($coupons as $value) {
  326. $coupon = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  327. $coupon['unuseable_reason'] = '已使用';
  328. $coupon_list['used_coupons'][] = $coupon;
  329. }
  330. $criteria = new EMongoCriteria();
  331. $criteria->user('==',new MongoId($user_id));
  332. $current_time = time();
  333. $criteria->end_time('<',$current_time);
  334. $coupons = UserCoupon::model()->findAll($criteria);
  335. $coupon_list['overtime_coupons'] = array();
  336. foreach ($coupons as $value) {
  337. $coupon = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  338. $coupon['unuseable_reason'] = '已过期';
  339. $coupon_list['overtime_coupons'][] = $coupon;
  340. }
  341. }else{
  342. $coupon_list['used_coupons'] = array();
  343. $coupon_list['overtime_coupons'] = array();
  344. }
  345. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$coupon_list);
  346. }
  347. $price = 0;
  348. if($products){
  349. foreach ($products as $product) {
  350. if(isset($product['product_id']) && $product['count'] >= 1){
  351. $product_temp = array();
  352. $product_obj = Product::get(new MongoId($product['product_id']));
  353. if($product_obj){
  354. $price += ($product_obj->price*$product['count']);
  355. }else{
  356. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_not_exist'));
  357. }
  358. }else{
  359. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  360. }
  361. }
  362. }
  363. $criteria = new EMongoCriteria();
  364. $criteria->user('==',new MongoId($user_id));
  365. $current_time = time();
  366. $criteria->start_time('<=',$current_time);
  367. $criteria->end_time('>=',$current_time);
  368. $criteria->status('==',1);
  369. $coupons = UserCoupon::model()->findAll($criteria);
  370. $coupon_list = array();
  371. $unuseable_coupons = array();
  372. foreach ($coupons as $value) {
  373. $coupon = Coupon::get($value->coupon);
  374. if($coupon->status!=1){
  375. continue;
  376. }
  377. if($coupon->min_price>$price || ($type && $coupon->type != $type && $coupon->type != 0)){
  378. continue;
  379. }
  380. if($booking_time){
  381. if($weekend_check){
  382. if($coupon->workday_limit==1){
  383. $unuseable_coupons[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  384. continue;
  385. }
  386. }else{
  387. if($coupon->workday_limit==2){
  388. $unuseable_coupons[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  389. continue;
  390. }
  391. }
  392. if($coupon->time_limit_start && $coupon->time_limit_end && ($coupon->time_limit_start > $booking_hour || $coupon->time_limit_end < $booking_hour)){
  393. $unuseable_coupons[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  394. continue;
  395. }
  396. }
  397. $coupon_list[] = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  398. }
  399. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$coupon_list,200,array('unuseable_coupons' => $unuseable_coupons));
  400. }
  401. public function actionCheckAddress(){
  402. $user_id = Yii::app()->getRequest()->getParam("user_id");
  403. $address_id = Yii::app()->getRequest()->getParam("address_id");
  404. $user_obj = CommonFn::apigetObJ($user_id,'ZUser',CommonFn::getMessage('user','id_not_exist'),array(),201);
  405. $address_list = $user_obj->shop_address?$user_obj->shop_address:array();
  406. $address = array();
  407. foreach ($address_list as $value) {
  408. if($address_id == $value['address_id']){
  409. $address = $value;
  410. }
  411. }
  412. if(!$address){
  413. file_put_contents('/data/erroraddress_user.log',var_export($user_id,true),FILE_APPEND);
  414. CommonFn::requestAjax(false,CommonFn::getMessage('shop','address_false'));
  415. }else{
  416. if(isset($address['address']['province']) && ($address['address']['province'] == '上海市' || $address['address']['province'] == '')){
  417. CommonFn::requestAjax(true,'success',array('station' => '5548b05e0eb9fbc5728b51ea'));
  418. }else{
  419. file_put_contents('/data/erroraddress.log',var_export($address,true),FILE_APPEND);
  420. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','address_cannot_service'));
  421. }
  422. }
  423. }
  424. public function actionAppend(){
  425. $user_id = Yii::app()->getRequest()->getParam("user_id");
  426. $order_id = Yii::app()->getRequest()->getParam("order_id");
  427. $products = json_decode(Yii::app()->getRequest()->getParam("products","[]"),true);
  428. if(!$user_id || !$products || !$order_id || !CommonFn::isMongoId($order_id) || !CommonFn::isMongoId($user_id)){
  429. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  430. }
  431. $user_obj = RUser::get(new MongoId($user_id));
  432. $order = ROrder::get(new MongoId($order_id));
  433. if(!$order || !$user_obj){
  434. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  435. }
  436. if($order && (string)$order->user != $user_id){
  437. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  438. }
  439. $product_list = array();
  440. $price = 0;
  441. foreach ($products as $product) {
  442. if(isset($product['product_id']) && $product['count'] >= 1){
  443. $product_temp = array();
  444. $product_obj = Product::get(new MongoId($product['product_id']));
  445. if($product_obj){
  446. $price += ($product_obj->price*$product['count']);
  447. $product_temp['product'] = $product_obj->_id;
  448. $product_temp['count'] = $product['count'];
  449. $product_list[] = $product_temp;
  450. if($product_obj->is_extra != 1){
  451. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  452. }
  453. }else{
  454. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_not_exist'));
  455. }
  456. }else{
  457. CommonFn::requestAjax(false,CommonFn::getMessage('o2o','product_illegal'));
  458. }
  459. }
  460. $append_product = new AppendOrder();
  461. $append_product->order = $order->_id;
  462. $append_product->products = $product_list;
  463. $append_product->price = $price;
  464. $append_product->append_time = time();
  465. $success = $append_product->save();
  466. if($success){
  467. $order->append_orders[] = $append_product->_id;
  468. $order->update(array('append_orders'),true);
  469. $list = new ARedisList('append_order_list');
  470. $list->push((string)$order->_id);
  471. $append_info = $append_product->parseRow($append_product);
  472. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$append_info);
  473. }
  474. CommonFn::requestAjax($success, '');
  475. }
  476. public function actionAppendPay(){
  477. $user_id = Yii::app()->getRequest()->getParam("user_id");
  478. $pay_channel = Yii::app()->getRequest()->getParam("pay_channel");
  479. $order_id = Yii::app()->getRequest()->getParam("order_id");
  480. $append_id = Yii::app()->getRequest()->getParam("append_id");
  481. if(!$user_id || !$pay_channel || !$order_id || !$append_id || !CommonFn::isMongoId($append_id)){
  482. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  483. }
  484. $user_obj = RUser::get(new MongoId($user_id));
  485. $order = ROrder::get(new MongoId($order_id));
  486. $append_product = AppendOrder::get(new MongoId($append_id));
  487. if(!$order || !$user_obj){
  488. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  489. }
  490. if($order && (string)$order->user != $user_id){
  491. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  492. }
  493. if($append_product->status == 1){
  494. CommonFn::requestAjax(false,'此订单已支付过',array('have_pay'=>1));
  495. }
  496. $str = '附加服务';
  497. $amount = ceil($append_product->price*1000)/10;
  498. $result = Service::factory('PayService')->Pay($pay_channel,$amount,(string)$append_product->_id,$str,$str,$user_obj->wx_pub_openid);
  499. if($result === false){
  500. CommonFn::requestAjax(false,'支付遇到点问题了,请稍候再试');
  501. }else{
  502. // 追加订单并付款后通知保洁师
  503. if (!empty($order->technician)) {
  504. $techObj = TechInfo::get($order->technician);
  505. if (!empty($techObj) && !empty($techObj->weixin_userid)) {
  506. $wechat = O2oApp::getWechatActive();
  507. $url_prefix = ENVIRONMENT == 'product' ? 'http:// api.yiguanjia.me' : 'http:// apitest.yiguanjia.me';
  508. $wechat_data = array(
  509. 'touser' => $techObj->weixin_userid,
  510. 'msgtype' => 'news',
  511. 'agentid' => '24',
  512. 'news' => array(
  513. 'articles' => array(
  514. array(
  515. 'title' => '壹管家提示-新的追加订单',
  516. 'description' => $techObj->name.'你好!预定时间在'.date('m月d日H:i', $order->booking_time).'的订单刚刚被用户追加了新的服务,请点击查看。',
  517. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.(string)$order->_id.'&user='.$order->technician,
  518. ),
  519. ),
  520. ),
  521. );
  522. $wechat->sendMessage($wechat_data);
  523. }
  524. }
  525. CommonFn::requestAjax(true,'success',json_decode($result));
  526. }
  527. }
  528. public function actionRetrieve(){
  529. $user_id = Yii::app()->getRequest()->getParam("user_id");
  530. $order_id = Yii::app()->getRequest()->getParam("order_id");
  531. if(!$user_id || !$order_id ){
  532. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  533. }
  534. $user_obj = RUser::get(new MongoId($user_id));
  535. $order = ROrder::get(new MongoId($order_id));
  536. if(!$order || !$user_obj){
  537. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  538. }
  539. if($order && (string)$order->user != $user_id){
  540. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  541. }
  542. if($order->status != 1){
  543. CommonFn::requestAjax(false,'此订单不可申请退款');
  544. }
  545. $order->status = -3;
  546. $order->apply_refund_time = time();
  547. if($order->update(array('status','apply_refund_time'),true)){
  548. // 发送通知给保洁师
  549. if (!empty($order->technician)) {
  550. $techObj = TechInfo::get($order->technician);
  551. if (!empty($techObj) && !empty($techObj->weixin_userid)) {
  552. $wechat = O2oApp::getWechatActive();
  553. $url_prefix = ENVIRONMENT == 'product' ? 'http:// api.yiguanjia.me' : 'http:// apitest.yiguanjia.me';
  554. $wechat_data = array(
  555. 'touser' => $techObj->weixin_userid,
  556. 'msgtype' => 'news',
  557. 'agentid' => '24',
  558. 'news' => array(
  559. 'articles' => array(
  560. array(
  561. 'title' => '壹管家提示-申请退款',
  562. 'description' => $techObj->name.'你好!预定时间在'.date('m月d日H:i', $order->booking_time).'的订单刚刚已被用户申请退款,请点击查看。',
  563. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.$order_id.'&user='.$order->technician,
  564. ),
  565. ),
  566. ),
  567. );
  568. $wechat->sendMessage($wechat_data);
  569. }
  570. }
  571. CommonFn::requestAjax(true,'申请退款成功,请等待确认');
  572. }else{
  573. CommonFn::requestAjax(false,'请稍后再试');
  574. }
  575. }
  576. public function actionConfirmComplete(){
  577. $user_id = Yii::app()->getRequest()->getParam("user_id");
  578. $order_id = Yii::app()->getRequest()->getParam("order_id");
  579. if(!$user_id || !$order_id ){
  580. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  581. }
  582. $user_obj = RUser::get(new MongoId($user_id));
  583. $order = ROrder::get(new MongoId($order_id));
  584. if(!$order || !$user_obj){
  585. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  586. }
  587. if($order && (string)$order->user != $user_id){
  588. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  589. }
  590. if(!in_array($order->status, array(1,2,3,4,5))){
  591. CommonFn::requestAjax(false,'此订单不支持修改状态');
  592. }
  593. $order->status = 6;
  594. $order->finish_time = time();
  595. $success = $order->update(array('finish_time','status'),true);
  596. if($success ){
  597. // 保洁师订单统计
  598. if ($order->technician) {
  599. $tech_obj = TechInfo::get($order->technician);
  600. if ($tech_obj) {
  601. $order_count = $tech_obj->order_count + 1;
  602. $tech_obj->order_count = $order_count;
  603. }
  604. }
  605. CommonFn::requestAjax(true,'订单已完成,期待您的下次预约');
  606. }else{
  607. CommonFn::requestAjax(false,'请稍后再试');
  608. }
  609. }
  610. public function actionDetail(){
  611. $user_id = Yii::app()->getRequest()->getParam("user_id");
  612. $order_id = Yii::app()->getRequest()->getParam("order_id");
  613. if(!$user_id || !$order_id || !CommonFn::isMongoId($order_id) || !CommonFn::isMongoId($user_id)){
  614. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  615. }
  616. $user_obj = RUser::get(new MongoId($user_id));
  617. $order = ROrder::get(new MongoId($order_id));
  618. if(!$order || !$user_obj){
  619. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  620. }
  621. if($order && (string)$order->user != $user_id){
  622. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  623. }
  624. $order_info = $order->parseRow($order);
  625. CommonFn::requestAjax(true,'',$order_info);
  626. }
  627. //选择保洁师
  628. public function actionSelectTech(){
  629. $booking_time = Yii::app()->getRequest()->getParam("booking_time");
  630. $service_type = json_decode(Yii::app()->getRequest()->getParam("service_type","[]"),true);
  631. $user_id = Yii::app()->getRequest()->getParam("user_id");
  632. $address_id = Yii::app()->getRequest()->getParam("address_id");
  633. $user_obj = CommonFn::apigetObJ($user_id,'ZUser',CommonFn::getMessage('user','id_not_exist'),array(),201);
  634. $address_list = $user_obj->shop_address?$user_obj->shop_address:array();
  635. $address = array();
  636. foreach ($address_list as $value) {
  637. if($address_id == $value['address_id']){
  638. $address = $value;
  639. }
  640. }
  641. if(strtotime($booking_time) < time()){
  642. CommonFn::requestAjax(false,'此时间段不可预约');
  643. }
  644. if(!$address){
  645. CommonFn::requestAjax(false,CommonFn::getMessage('shop','address_false'));
  646. }
  647. if(!isset($address['position'][0])||$address['position'][0]==0||is_int($address['position'][0])){
  648. $address_res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&address='.$address['address']['area'].$address['address']['detail'].'&city='.$address['address']['city'].'&output=json');
  649. $address_info = json_decode($address_res,true);
  650. $position = $address_info['result']['location'];
  651. $detail_res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&location='.$position['lat'].','.$position['lng'].'&output=json&pois=1');
  652. $detail_info = json_decode($detail_res,true);
  653. $business = explode(',',$detail_info['result']['business']);
  654. }else{
  655. $detail_res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&location='.$address['position'][1].','.$address['position'][0].'&output=json&pois=1');
  656. $detail_info = json_decode($detail_res,true);
  657. $business = explode(',',$detail_info['result']['business']);
  658. }
  659. $user_model = new TechInfo();
  660. $criteria = new EMongoCriteria();
  661. $criteria->service_type('all', $service_type);
  662. if( !in_array(9, $service_type) && !in_array(10, $service_type)){
  663. foreach ($business as $value) {
  664. $criteria->addCond('business','or',$value);
  665. }
  666. }
  667. $criteria->status('==', 1);
  668. $cursor = $user_model->findAll($criteria);
  669. //获取满足服务类型保洁师列表
  670. $match_service_tech_list = array();
  671. $tech_list = array();
  672. foreach ($cursor as $key => $value) {
  673. $match_service_tech_list[] = $value->_id;
  674. $tech_list[$value->_id] = $value;
  675. }
  676. $parse_time = strtotime(date('Y-m-d H:00',strtotime($booking_time)));
  677. $FreeTimeRecord = FreeTimeRecord::get($parse_time);
  678. // $last_parse_time = strtotime(date('Y-m-d H:00',strtotime($booking_time)))-3600;
  679. // $LastFreeTimeRecord = FreeTimeRecord::get($last_parse_time);
  680. // $next_parse_time = strtotime(date('Y-m-d H:00',strtotime($booking_time)))-3600;
  681. // $NextFreeTimeRecord = FreeTimeRecord::get($next_parse_time);
  682. //获取此时段空闲保洁师列表
  683. $free_tech_list = array();
  684. // $last_free_tech_list = array();
  685. // $next_free_tech_list = array();
  686. if($FreeTimeRecord){
  687. $free_tech_list = $FreeTimeRecord->free_technician;
  688. }
  689. // if($LastFreeTimeRecord){
  690. // $last_free_tech_list = $LastFreeTimeRecord->free_technician;
  691. // }
  692. // if($NextFreeTimeRecord){
  693. // $next_free_tech_list = $NextFreeTimeRecord->free_technician;
  694. // }
  695. //空闲且满足服务能力保洁师,推荐用户选择
  696. $recommend_tech = array_intersect($match_service_tech_list,$free_tech_list);
  697. $can_select_tech = array();
  698. foreach ($recommend_tech as $key => $value) {
  699. $tech = $tech_list[$value];
  700. unset($tech_list[$value]);
  701. $tmp['id'] = $tech->_id;
  702. $tmp['name'] = $tech->name;
  703. $tmp['desc'] = $tech->desc;
  704. $tmp['service_type'] = $tech->service_type;
  705. $tmp['avatar'] = $tech->avatar;
  706. $tmp['favourable_count'] = $tech->favourable_count;
  707. $can_select_tech[] = $tmp;
  708. }
  709. //满足服务能力,但此时段不空闲,展示给用户此保洁师下个空闲时段
  710. $service_match_tech = array();
  711. foreach ($tech_list as $key => $value) {
  712. $tmp['id'] = $value->_id;
  713. $tmp['name'] = $value->name;
  714. $tmp['desc'] = $value->desc;
  715. $tmp['service_type'] = $value->service_type;
  716. $tmp['avatar'] = $value->avatar;
  717. $tmp['favourable_count'] = $value->favourable_count;
  718. $criteria = new EMongoCriteria();
  719. $criteria->free_technician('==', $tmp['id']);
  720. $criteria->_id('>',time());
  721. $criteria->limit(1);
  722. $criteria->sort('_id', EMongoCriteria::SORT_ASC);
  723. if(FreeTimeRecord::model()->count($criteria)){
  724. $free_time = FreeTimeRecord::model()->findAll($criteria);
  725. foreach ($free_time as $next_free_time) {
  726. $tmp['next_free_time'] = FreeTimeRecord::parseFreeTime($next_free_time->_id);
  727. break;
  728. }
  729. }else{
  730. continue;
  731. }
  732. $service_match_tech[] = $tmp;
  733. }
  734. $data['can_select_tech'] = $can_select_tech;
  735. $data['service_match_tech'] = $service_match_tech;
  736. CommonFn::requestAjax(true,'',$data);
  737. }
  738. /**
  739. * 获取评价列表接口
  740. */
  741. public function actionTechComment() {
  742. $page = intval(Yii::app()->getRequest()->getParam("page",1));
  743. $tech_id = intval(Yii::app()->getRequest()->getParam("tech_id"));
  744. $pagesize = Yii::app()->params['O2oCommentListPageSize'];
  745. if(!$tech_id){
  746. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  747. }
  748. $conditions = array(
  749. 'technician'=>array('==',$tech_id),
  750. 'status'=>array('==',1),
  751. );
  752. $order = array(
  753. 'time' => 'desc'
  754. );
  755. $model = new Comment();
  756. $pagedata = CommonFn::getPagedata($model,$page,$pagesize,$conditions,$order);
  757. $data['comments'] = $pagedata['res'];
  758. CommonFn::requestAjax(true,CommonFn::getMessage('message','operation_success'),$data,200,array('sum_count' => $pagedata['sum_count'],'sum_page'=>$pagedata['sum_page'],'page_size'=>$pagedata['page_size'],'current_page'=>$pagedata['current_page']));
  759. }
  760. }