CouponController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. class CouponController extends AdminController{
  3. public function actionIndex()
  4. {
  5. $status_option = Coupon::$status_option;
  6. $status = CommonFn::getComboboxData($status_option, 100, true, 100);
  7. $type = CommonFn::getComboboxData(Yii::app()->params['o2o_service'], 0, true, 0);
  8. $workday_limit_option = Coupon::$workday_limit_option;
  9. $workday_limit = CommonFn::getComboboxData($workday_limit_option, 0, false);
  10. $this->render('index', array(
  11. 'status' => $status,
  12. 'type'=>$type,
  13. 'workday_limit' => $workday_limit
  14. ));
  15. }
  16. public function actionList(){
  17. $search = Yii::app()->request->getParam('search', '');
  18. $filter_status = intval(Yii::app()->request->getParam('status', 100));
  19. $id = Yii::app()->request->getParam('id', '');
  20. $params = CommonFn::getPageParams();
  21. $criteria = new EMongoCriteria($params);
  22. if ($id != ''){
  23. $coupon_id = new MongoId($id);
  24. $criteria->_id('==', $coupon_id);
  25. }
  26. if ($filter_status != 100){
  27. $criteria->status('==', $filter_status);
  28. }
  29. // 模糊搜索处理
  30. // 2015-11-19
  31. if ($search != '') {
  32. if (strlen($search) == 24) {
  33. $criteria->_id('or', new MongoId($search));
  34. }
  35. $criteria->name('or', new MongoRegex('/'.$search.'/'));
  36. }
  37. $criteria->sort('time', EMongoCriteria::SORT_DESC);
  38. $cursor = Coupon::model()->findAll($criteria);
  39. $total = $cursor->count();
  40. $rows = CommonFn::getRowsFromCursor($cursor);
  41. $parsedRows = Coupon::model()->parse($rows);
  42. echo CommonFn::composeDatagridData($parsedRows, $total);
  43. }
  44. public function actionEdit(){
  45. $id = Yii::app()->request->getParam('id', '');
  46. $status = intval(Yii::app()->request->getParam('status', 1));
  47. $name = Yii::app()->request->getParam('name', '');
  48. $alias_name = Yii::app()->request->getParam('alias_name', '');
  49. $memo = Yii::app()->request->getParam('memo', '');
  50. $type = Yii::app()->request->getParam('type', 0);
  51. $value = intval(Yii::app()->request->getParam('value', 0));
  52. $min_price = intval(Yii::app()->request->getParam('min_price', 0));
  53. if($status == 100 ){
  54. CommonFn::requestAjax(false, '必须指定状态和服务类型!');
  55. }
  56. if($name == ''||$alias_name == '' || !$value ||!$min_price ){
  57. CommonFn::requestAjax(false, '必填内容为空');
  58. }
  59. $status=$status>1?1:$status;
  60. if(!$id){
  61. $coupon = new Coupon();
  62. $coupon->time = time();
  63. }else{
  64. $criteria = new EMongoCriteria();
  65. $criteria->_id = new MongoId($id);
  66. $coupon = Coupon::model()->find($criteria);
  67. if (empty($coupon)){
  68. CommonFn::requestAjax(false, '代金券 不存在');
  69. }
  70. }
  71. $arr_coupon = array('status','name','value','min_price','type','memo','alias_name', 'time');
  72. $workday_limit = intval(Yii::app()->request->getParam('workday_limit', 0));
  73. $coupon->workday_limit = $workday_limit;
  74. array_push($arr_coupon, 'workday_limit');
  75. $time_limit_start = intval(Yii::app()->request->getParam('time_limit_start', ''));
  76. $time_limit_end = intval(Yii::app()->request->getParam('time_limit_end', ''));
  77. if (!empty($time_limit_start) && !empty($time_limit_end)) {
  78. $coupon->time_limit_start = intval($time_limit_start);
  79. $coupon->time_limit_end = intval($time_limit_end);
  80. array_push($arr_coupon, 'time_limit_start', 'time_limit_end');
  81. }
  82. $coupon->status = $status;
  83. $coupon->alias_name = $alias_name;
  84. $coupon->name = $name;
  85. $coupon->value = $value;
  86. $coupon->min_price = $min_price;
  87. $coupon->type = $type;
  88. $coupon->memo = $memo;
  89. $success = $coupon->save(true,$arr_coupon);
  90. CommonFn::requestAjax($success, '', array());
  91. }
  92. //生成兑换码
  93. public function actionCouponFactory(){
  94. $this->renderPartial('couponFactory');
  95. $count = Yii::app()->request->getParam('count',0) ;
  96. $coupons = Yii::app()->request->getParam('coupons','');
  97. $coupons_filter = str_replace(" ", "", $coupons);
  98. $coupons_filter_1 = str_replace(",", ",", $coupons_filter);
  99. // var_dump($coupons_filter);die();
  100. if(empty($coupons)){
  101. echo "请输入优惠码ID!";exit();
  102. }
  103. $couponsid = explode(',', $coupons_filter_1);
  104. if(count($couponsid)>5||count($couponsid)<1){
  105. echo "注意:0<优惠码数量<6!";
  106. exit;
  107. }
  108. if($count>99||$count<1){
  109. echo "注意:0<生成兑换码数量<100!";
  110. exit;
  111. }
  112. // echo strlen('55667f6c0eb9fb14518b6e0a');die;
  113. $date = time();
  114. $i = 0;
  115. $coupon_confirm = array();
  116. foreach ($couponsid as $key=>$value) {
  117. if(strlen($value)!=24){
  118. echo "请输入正确的优惠码ID!";exit;
  119. }
  120. $coupon_objId = new MongoId($value);
  121. $criteria = new EMongoCriteria();
  122. $criteria->_id = $coupon_objId;
  123. $criteria->status = 1;
  124. $coupon = Coupon::model()->find($criteria);
  125. // var_dump($coupon);die();
  126. if (empty($coupon)){
  127. echo '该代金券已暂停使用或已删除!';
  128. exit;
  129. }
  130. $coupon_confirm[] = $coupon_objId;
  131. }
  132. for (;$i<$count;$i++) {
  133. $model = new CouponCode();
  134. $model->coupons = $coupon_confirm;
  135. $model->code = $model->code + $i*1101;
  136. $model->stop_time = time() + 86400*30;
  137. $model->save();
  138. file_put_contents(APP_PATH.'/download/'.$date.'_coupon_code.txt',$model->code."\r\n",FILE_APPEND);
  139. }
  140. echo '兑换码生成成功,共'.$count.'张-----------------';
  141. echo "查看地址:".APP_PATH."/download/".$date.'_coupon_code.txt';
  142. }
  143. // 选择优惠券
  144. public function actionSelectCoupon() {
  145. $search = Yii::app()->request->getParam('coupon', '');
  146. $criteria = new EMongoCriteria();
  147. if (strlen($search) == 24) {
  148. $criteria->_id('or', new MongoId($search));
  149. }
  150. $criteria->name('or', new MongoRegex('/'.$search.'/'));
  151. $cursor = Coupon::model()->findAll($criteria);
  152. $data = array();
  153. foreach ($cursor as $key => $row) {
  154. $description = 'ID:'.(string)$row->_id;
  155. $description .= "<br>".$row->memo;
  156. $data[] = array(
  157. 'id' => $key,
  158. 'data' => $row->name,
  159. 'description' => $description,
  160. 'cid' => (string)$row->_id
  161. );
  162. }
  163. echo json_encode($data);
  164. }
  165. // 获取优惠券信息
  166. public function actionGetCouponInfo() {
  167. $id = Yii::app()->request->getParam('coupon_id', '');
  168. $success = 0;
  169. $message = '未查询到优惠券';
  170. $content = array();
  171. if (!empty($id) && strlen($id) == 24) {
  172. $_id = new MongoId($id);
  173. $coupon = Coupon::get($_id);
  174. if ($coupon != false) {
  175. if ($coupon->status == 1) {
  176. $success = 1;
  177. $message = 'success';
  178. $content = array(
  179. '_id' => (string)$coupon->_id,
  180. 'value' => $coupon->value,
  181. 'name' => $coupon->name
  182. );
  183. } else {
  184. $message = '优惠券不可用';
  185. }
  186. }
  187. }
  188. $data = array(
  189. 'success' => $success,
  190. 'message' => $message,
  191. 'content' => $content,
  192. );
  193. echo json_encode($data);
  194. }
  195. public function actionCouponCodeStates(){
  196. $exchange_code = intval(Yii::app()->getRequest()->getParam("exchange_code",0));
  197. $criteria = new EMongoCriteria();
  198. $criteria->code('==',$exchange_code);
  199. $exchange_code = CouponCode::model()->find($criteria);
  200. if($exchange_code){
  201. if($exchange_code && $exchange_code->status == 0 && $exchange_code->stop_time > time()){
  202. $data = array(
  203. 'success' => true,
  204. 'message' => '此兑换码可用',
  205. );
  206. }else{
  207. $data = array(
  208. 'success' => false,
  209. 'message' => '此兑换码不可用',
  210. );
  211. }
  212. }else{
  213. $data = array(
  214. 'success' => false,
  215. 'message' => '此兑换码不存在',
  216. );
  217. }
  218. echo json_encode($data);
  219. }
  220. }