RUserController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. class RUserController extends AdminController{
  3. public function actionIndex()
  4. {
  5. // 订单类型
  6. $type_data = Yii::app()->params['o2o_service'];
  7. $type = CommonFn::getComboboxData($type_data, 100, true, 100);
  8. $this->render('index', array(
  9. 'type' => $type
  10. ));
  11. }
  12. public function actionList(){
  13. $search = Yii::app()->request->getParam('search', '');
  14. $id = Yii::app()->request->getParam('id', '');
  15. $params = CommonFn::getPageParams();
  16. if(isset($params['sort']) && isset($params['sort']['register_time'])){
  17. $params['sort'] = array('_id' => $params['sort']['register_time']);
  18. }
  19. $criteria = new EMongoCriteria($params);
  20. if ($id != ''){
  21. $user_id = new MongoId($id);
  22. $criteria->_id('==', $user_id);
  23. }
  24. if ($search != '' && !CommonFn::isMongoId($search) && !CommonFn::isDeviceId($search)){
  25. $criteria->user_name('or', new MongoRegex('/' . $search . '/'));
  26. if (CommonFn::isMongoId($search)){
  27. $criteria->_id('or', new MongoId($search));
  28. }
  29. }
  30. if (CommonFn::isMongoId($search)) {
  31. $criteria = new EMongoCriteria();
  32. $criteria->_id('==', new MongoId($search));
  33. }
  34. $cursor = RUser::model()->findAll($criteria);
  35. $total = $cursor->count();
  36. $rows = CommonFn::getRowsFromCursor($cursor);
  37. $parsedRows = RUser::model()->parse($rows);
  38. echo CommonFn::composeDatagridData($parsedRows, $total);
  39. }
  40. public function actionUpdate(){
  41. $id = Yii::app()->request->getParam('id', '');
  42. if(!$id){
  43. CommonFn::requestAjax(false, "缺少必须参数");
  44. }
  45. $criteria = new EMongoCriteria();
  46. $criteria->_id = new MongoId($id);
  47. $user = RUser::model()->find($criteria);
  48. $keys = array('status','certify_status','is_fake_user','city_info');
  49. $success = $user->save(true, $keys, true);
  50. CommonFn::requestAjax($success, $message, array());
  51. }
  52. public function actionGetCoupons(){
  53. $user_id = Yii::app()->getRequest()->getParam("user_id");
  54. $UserCoupon = new UserCoupon();
  55. $criteria = new EMongoCriteria();
  56. $criteria->user('==',new MongoId($user_id));
  57. $current_time = time();
  58. $criteria->end_time('>=',$current_time);
  59. $criteria->status('==',1);
  60. $coupons = UserCoupon::model()->findAll($criteria);
  61. $coupon_list = array();
  62. foreach ($coupons as $value) {
  63. $coupon = Coupon::get($value->coupon);
  64. if($coupon->status!=1){
  65. continue;
  66. }
  67. $coupon = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  68. $coupon['unuseable_reason'] = '可使用';
  69. $coupon_list[] = $coupon;
  70. }
  71. $criteria = new EMongoCriteria();
  72. $criteria->user('==',new MongoId($user_id));
  73. $criteria->status('==',-1);
  74. $coupons = UserCoupon::model()->findAll($criteria);
  75. foreach ($coupons as $value) {
  76. $coupon = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  77. $coupon['unuseable_reason'] = '已使用';
  78. $coupon_list[] = $coupon;
  79. }
  80. $criteria = new EMongoCriteria();
  81. $criteria->user('==',new MongoId($user_id));
  82. $current_time = time();
  83. $criteria->end_time('<',$current_time);
  84. $coupons = UserCoupon::model()->findAll($criteria);
  85. foreach ($coupons as $value) {
  86. $coupon = $UserCoupon->parseRow($value,array('id','start_time','end_time','start_time_str','end_time_str','coupon'));
  87. $coupon['unuseable_reason'] = '已过期';
  88. $coupon_list[] = $coupon;
  89. }
  90. foreach ($coupon_list as $key => $value) {
  91. $coupon_list[$key]['name'] = $value['coupon']['name'];
  92. $coupon_list[$key]['value'] = $value['coupon']['value'];
  93. $coupon_list[$key]['min_price'] = $value['coupon']['min_price'];
  94. $coupon_list[$key]['type_str'] = $value['coupon']['type_str'];
  95. }
  96. $total = count($coupon_list);
  97. $data = $coupon_list;
  98. echo CommonFn::composeDatagridData($data, $total);
  99. }
  100. public function actionChangeBalance(){
  101. $id = Yii::app()->request->getParam('id');
  102. $amount = intval(Yii::app()->request->getParam('amount'));
  103. $memo = Yii::app()->request->getParam('memo');
  104. $type = Yii::app()->request->getParam('type');
  105. if(!$id||!$amount||!$memo||!$type){
  106. CommonFn::requestAjax(false, "缺少必须参数");
  107. }
  108. $user = RUser::get(new MongoId($id));
  109. if(!$user){
  110. CommonFn::requestAjax(false, "用户不存在");
  111. }
  112. $user->balance = $user->balance+$amount;
  113. if($user->balance<0){
  114. CommonFn::requestAjax(false, "用户余额不能小于0");
  115. }
  116. $user->save();
  117. $balance_log = new BalanceLog();
  118. $balance_log->time = time();
  119. $balance_log->user = $user->_id;
  120. $balance_log->memo = $memo;
  121. $balance_log->type = $type;
  122. $balance_log->amout = $amount;
  123. $balance_log->save(true);
  124. CommonFn::requestAjax(true, '修改成功', array());
  125. }
  126. public function actionBalanceLog(){
  127. $id = Yii::app()->request->getParam('$id', '');
  128. $params = CommonFn::getPageParams();
  129. $criteria = new EMongoCriteria($params);
  130. if ($id != ''){
  131. $id = new MongoId($id);
  132. $criteria->user('==', $id);
  133. }else{
  134. CommonFn::requestAjax(false, "缺少必须参数");
  135. }
  136. $cursor = BalanceLog::model()->findAll($criteria);
  137. $total = $cursor->count();
  138. $rows = CommonFn::getRowsFromCursor($cursor);
  139. $parsedRows = BalanceLog::model()->parse($rows);
  140. echo CommonFn::composeDatagridData($parsedRows, $total);
  141. }
  142. public function actionSendCoupon() {
  143. $user_id = Yii::app()->request->getParam('user_id', '');
  144. $coupon_id = Yii::app()->request->getParam('coupon_id', '');
  145. $start_time = Yii::app()->request->getParam('start_time', '');
  146. $end_time = Yii::app()->request->getParam('end_time', '');
  147. $need_sms = intval(Yii::app()->request->getParam('need_sms', '0'));
  148. $copy = Yii::app()->request->getParam('copy', '');
  149. $mongo = new MongoClient(DB_CONNETC);
  150. $db = $mongo->fuwu;
  151. $coll = 'admin_send_coupon_log';
  152. $collection = $db->selectCollection($coll);
  153. $admin = Yii::app()->user;
  154. $admin_id = $admin->id;
  155. $user_id = new MongoId($user_id);
  156. $coupon_id = new MongoId($coupon_id);
  157. $time = time();
  158. $user = RUser::get($user_id);
  159. //开始与结束时间处理
  160. $start_time = empty($start_time) ? $time : strtotime($start_time);
  161. $end_time = empty($end_time) ? strtotime('+30 day', $start_time) : strtotime($end_time);
  162. //插入user_coupons表
  163. $flag_user_coupon = Service::factory('CouponService')->giveCoupon($user_id, $coupon_id, $start_time, $end_time);
  164. if (!$flag_user_coupon) {
  165. CommonFn::requestAjax($flag_user_coupon, '发放优惠券失败', array());
  166. }
  167. //插入admin_send_coupon_log表
  168. $data = array(
  169. 'admin_id' => $admin_id,
  170. 'user_id' => $user_id,
  171. 'coupon_id' => $coupon_id,
  172. 'time' => $time
  173. );
  174. $flag_send_log = $collection->insert($data);
  175. if ($flag_send_log['err'] != null) {
  176. CommonFn::requestAjax($flag_user_coupon, '优惠券已发放,日志记录失败', array());
  177. }
  178. //发送短信
  179. if ($need_sms) {
  180. if ($user->mobile != '') {
  181. Service::factory('SendSMSService')->send_sms($copy, $user->mobile);
  182. } else if (!empty($user->shop_address) && isset($user->shop_address[0]['mobile'])) {
  183. $mobile = $user->shop_address[0]['mobile'];
  184. Service::factory('SendSMSService')->send_sms($copy, $mobile);
  185. }
  186. }
  187. CommonFn::requestAjax(true, '优惠券已成功发放');
  188. }
  189. }