OrderController.php 38 KB

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