JCouponController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Created by north.Deng's MAC
  4. * User: north.Deng
  5. * Date: 2018/2/28
  6. * Time: 下午12:30
  7. * description :
  8. */
  9. class JCouponController extends AdminController
  10. {
  11. public function actionIndex()
  12. {
  13. $status = CommonFn::getComboboxData(JCoupon::$status_options, 100, true, 100);
  14. $gifts = Gift::model()->findAll();
  15. $tmp =[];
  16. foreach ($gifts as $gift) {
  17. $tmp[] = array('name' => $gift->title,'id' => (string)$gift->_id);
  18. }
  19. $tmp = CommonFn::getComboboxData($tmp, 0, false );
  20. $c = new EMongoCriteria();
  21. $c->type('==',1);
  22. $bs = Business::model()->findAll($c);
  23. $tmp1 =[];
  24. foreach ($bs as $gift) {
  25. $tmp1[] = array('name' => $gift->source,'id' => (string)$gift->_id);
  26. }
  27. $tmp1 = CommonFn::getComboboxData($tmp1, 100, true, 100);
  28. $this->render('index',array(
  29. 'status' => $status,
  30. 'gifts' => $tmp,
  31. 'bus' => $tmp1
  32. ));
  33. }
  34. public function actionList()
  35. {
  36. $pageParams = CommonFn::getPageParams();
  37. $search = Yii::app()->request->getParam('search', '');
  38. $status = intval(Yii::app()->request->getParam('status', 100));
  39. $criteria = new EMongoCriteria($pageParams);
  40. if ($status != 100 ) {
  41. $criteria->status('==',$status);
  42. }
  43. if ($search) {
  44. $criteria->addCond('title','or',new MongoRegex('/' . $search . '/'));
  45. }
  46. $criteria->sort('created_at',EMongoCriteria::SORT_DESC);
  47. $cursor = JCoupon::model()->findAll($criteria);
  48. $rows = CommonFn::getRowsFromCursor($cursor);
  49. $parsedRows = JCoupon::model()->parse($rows);
  50. $total = $cursor->count();
  51. echo CommonFn::composeDatagridData($parsedRows, $total);
  52. }
  53. public function actionEdit()
  54. {
  55. $id = Yii::app()->request->getParam('id','');
  56. $status = (int)Yii::app()->request->getParam('status',0);
  57. if (!CommonFn::isMongoId($id)) {
  58. CommonFn::requestAjax(false,'id错误');exit;
  59. }
  60. $coupon = JCoupon::get(new MongoId($id));
  61. $coupon->status = $status;
  62. $coupon->save();
  63. CommonFn::requestAjax(true,'保存成功');exit;
  64. }
  65. public function actionAdd()
  66. {
  67. $num = (int)Yii::app()->request->getParam('num','');
  68. $coding_num = (int)Yii::app()->request->getParam('coding_num','');
  69. $gift_id = Yii::app()->request->getParam('gift_id','');
  70. $channel = Yii::app()->request->getParam('bus_id','');
  71. $bus = (int)Yii::app()->request->getParam('bus','');
  72. $expire_time = Yii::app()->request->getParam('expire_time_str','');
  73. if ($bus == 100 || !$num ||!$channel || !$gift_id || !$expire_time) {
  74. CommonFn::requestAjax(false,'请填写完整信息');exit;
  75. }
  76. $gift = Gift::get(new MongoId($gift_id));
  77. if (empty($gift)) {
  78. CommonFn::requestAjax(false,'礼包不存在');exit;
  79. }
  80. if ($coding_num > 100000) {
  81. CommonFn::requestAjax(false,'起始值重复');exit;
  82. }
  83. /*$c = new EMongoCriteria();
  84. $c->coding_num('==',$coding_num);
  85. $j = JCoupon::model()->findAll($c);
  86. if (count($j) > 0) {
  87. CommonFn::requestAjax(false,'起始值重复');exit;
  88. }*/
  89. $total = 10000;
  90. for($i=0; $i<$num; $i++) {
  91. $coupon = new JCoupon();
  92. $coupon->gift_id = $gift_id;
  93. $tmp1 = $coding_num + 100000;
  94. $coupon->coding_num = date('d').substr($tmp1,1);
  95. $flag = true;
  96. while($flag) {
  97. $pwd = $coupon->generateRandomString(8);
  98. $tmp = JCoupon::getByPwd($pwd);
  99. if (empty(count($tmp))){
  100. $flag = false;
  101. }
  102. }
  103. $total++;
  104. $coupon->pwd = $pwd;
  105. $coupon->coupon_coding = $gift->gift_num.substr(date('Y'),2).date('md').substr($total,1);
  106. $coupon->channel = $channel;
  107. $coupon->status = 0;
  108. $coupon->expire_time = $expire_time;
  109. $coupon->created_at = time();
  110. if (substr($coupon->coding_num,5) == substr($coupon->coupon_coding,-3)) {
  111. CommonFn::requestAjax(false,'起始值重复');exit;
  112. }
  113. $coupon->save();
  114. $coding_num++;
  115. }
  116. CommonFn::requestAjax(true,'生成成功');exit;
  117. }
  118. public function actionOutputExcel()
  119. {
  120. $start_num = Yii::app()->request->getParam('start_num','');
  121. // $total = (int)Yii::app()->request->getParam('total','');
  122. $criteria = new EMongoCriteria();
  123. $criteria->addCond('coupon_coding','or',new MongoRegex('/' . $start_num . '/'));
  124. // $num = (int)'1'.substr($start_num,2,6);
  125. // for ($i=0;$i<$total;$i++) {
  126. // $criteria = new EMongoCriteria();
  127. // $day = substr($start_num,0,2);
  128. //
  129. // $tmp = $day.substr($num,1,6);
  130. // $criteria->coding_num('==',$tmp);
  131. // $coupon = JCoupon::model()->find($criteria);
  132. // if (!empty($coupon)) {
  133. // $data[] = [
  134. // 'coupon_coding' => $coupon->coupon_coding,
  135. // 'coding_num' => $coupon->coding_num,
  136. // 'pwd_str' => $coupon->pwd,
  137. // ];
  138. // }
  139. // $num++;
  140. //
  141. // }
  142. $cursor = JCoupon::model()->findAll($criteria);
  143. $rows = CommonFn::getRowsFromCursor($cursor);
  144. $parsedRows = JCoupon::model()->parse($rows);
  145. $res=Service::factory('JCouponService')->push($parsedRows);
  146. }
  147. public function actionOutputExcel1()
  148. {
  149. $start_num = Yii::app()->request->getParam('start_num','');
  150. $total = Yii::app()->request->getParam('num','');
  151. $id = Yii::app()->request->getParam('id','');
  152. $criteria = new EMongoCriteria();
  153. $num = substr($start_num,0,strlen($start_num) - 4);
  154. for ($i=0;$i<$total;$i++) {
  155. $criteria = new EMongoCriteria();
  156. $day = intval("1".substr($start_num,-4,4)) +$i;
  157. $tmp = $num.substr($day,1,strlen($day) - 1);
  158. $criteria->coupon_coding('==',$tmp);
  159. $coupon = JCoupon::model()->find($criteria);
  160. if (!empty($coupon)) {
  161. $coupon->channel = $id;
  162. $coupon->save();
  163. }
  164. }
  165. CommonFn::requestAjax(true,'修改成功');exit;
  166. }
  167. }