OrderController.php 38 KB

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