JCouponController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, 100, true, 100 );
  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. $gift_num = intval(Yii::app()->request->getParam('gift', 100));
  40. $criteria = new EMongoCriteria($pageParams);
  41. if ($status != 100 ) {
  42. $criteria->status('==',$status);
  43. }
  44. if ($gift_num != 100 ) {
  45. $gifts = Gift::model()->findAll();
  46. $tmp =[];
  47. foreach ($gifts as $gift) {
  48. $tmp[] = array('name' => $gift->title,'id' => (string)$gift->_id);
  49. }
  50. $criteria->gift_id('==',$tmp[$gift_num]['id']);
  51. }
  52. if ($search) {
  53. $criteria->addCond('coupon_coding','or',new MongoRegex('/' . $search . '/'));
  54. }
  55. $criteria->sort('coupon_coding',EMongoCriteria::SORT_ASC);
  56. $cursor = JCoupon::model()->findAll($criteria);
  57. $rows = CommonFn::getRowsFromCursor($cursor);
  58. $parsedRows = JCoupon::model()->parse($rows);
  59. $total = $cursor->count();
  60. echo CommonFn::composeDatagridData($parsedRows, $total);
  61. }
  62. public function actionEdit()
  63. {
  64. $id = Yii::app()->request->getParam('id','');
  65. $status = (int)Yii::app()->request->getParam('status',0);
  66. if (!CommonFn::isMongoId($id)) {
  67. CommonFn::requestAjax(false,'id错误');exit;
  68. }
  69. $coupon = JCoupon::get(new MongoId($id));
  70. $coupon->status = $status;
  71. if (empty($coupon->add_time)) {
  72. $coupon->add_time = time();
  73. }
  74. $coupon->save();
  75. CommonFn::requestAjax(true,'保存成功');exit;
  76. }
  77. public function actionAdd()
  78. {
  79. $num = (int)Yii::app()->request->getParam('num','');
  80. // $coding_num = (int)Yii::app()->request->getParam('coding_num','');
  81. $gift_id = Yii::app()->request->getParam('gift_id','');
  82. $gift = Yii::app()->request->getParam('gift','');
  83. $channel = Yii::app()->request->getParam('bus_id','');
  84. $bus = (int)Yii::app()->request->getParam('bus','');
  85. $expire_time = Yii::app()->request->getParam('expire_time_str','');
  86. if ($bus == 100 || !$num ||!$channel || !$gift_id || !$expire_time) {
  87. CommonFn::requestAjax(false,'请填写完整信息');exit;
  88. }
  89. if ($gift == 100) {
  90. CommonFn::requestAjax(false,'请选择礼包');exit;
  91. }
  92. $gift = Gift::get(new MongoId($gift_id));
  93. if (empty($gift)) {
  94. CommonFn::requestAjax(false,'礼包不存在');exit;
  95. }
  96. // if ($coding_num > 100000) {
  97. // CommonFn::requestAjax(false,'起始值不能超过100000');exit;
  98. // }
  99. /*$c = new EMongoCriteria();
  100. $c->coding_num('==',$coding_num);
  101. $j = JCoupon::model()->findAll($c);
  102. if (count($j) > 0) {
  103. CommonFn::requestAjax(false,'起始值重复');exit;
  104. }*/
  105. $total = 10000;
  106. for($i=0; $i<$num; $i++) {
  107. $coupon = new JCoupon();
  108. $coupon->gift_id = $gift_id;
  109. // $tmp1 = $coding_num + 100000;
  110. // $coupon->coding_num = date('d').substr($tmp1,1);
  111. $flag = true;
  112. while($flag) {
  113. $pwd = $coupon->generateRandomString(8);
  114. $tmp = JCoupon::getByPwd($pwd);
  115. if (empty(count($tmp))){
  116. $flag = false;
  117. }
  118. }
  119. $total++;
  120. $coupon->pwd = $pwd;
  121. $coupon->coupon_coding = $gift->gift_num.substr(date('Y'),2).date('md').substr($total,1);
  122. $coupon->channel = $channel;
  123. $coupon->status = 0;
  124. $coupon->expire_time = $expire_time;
  125. $coupon->created_at = time();
  126. // if (substr($coupon->coding_num,5) == substr($coupon->coupon_coding,-3)) {
  127. // CommonFn::requestAjax(false,'起始值重复');exit;
  128. // }
  129. $coupon->add_time = time();
  130. $coupon->save();
  131. // $coding_num++;
  132. }
  133. CommonFn::requestAjax(true,'生成成功');exit;
  134. }
  135. public function actionOutputExcel()
  136. {
  137. $start_num = Yii::app()->request->getParam('start_num','');
  138. // $total = (int)Yii::app()->request->getParam('total','');
  139. $criteria = new EMongoCriteria();
  140. $criteria->addCond('coupon_coding','or',new MongoRegex('/' . $start_num . '/'));
  141. // $num = (int)'1'.substr($start_num,2,6);
  142. // for ($i=0;$i<$total;$i++) {
  143. // $criteria = new EMongoCriteria();
  144. // $day = substr($start_num,0,2);
  145. //
  146. // $tmp = $day.substr($num,1,6);
  147. // $criteria->coding_num('==',$tmp);
  148. // $coupon = JCoupon::model()->find($criteria);
  149. // if (!empty($coupon)) {
  150. // $data[] = [
  151. // 'coupon_coding' => $coupon->coupon_coding,
  152. // 'coding_num' => $coupon->coding_num,
  153. // 'pwd_str' => $coupon->pwd,
  154. // ];
  155. // }
  156. // $num++;
  157. //
  158. // }
  159. $cursor = JCoupon::model()->findAll($criteria);
  160. $rows = CommonFn::getRowsFromCursor($cursor);
  161. $parsedRows = JCoupon::model()->parse($rows);
  162. $res=Service::factory('JCouponService')->push($parsedRows);
  163. }
  164. public function actionOutputExcel1()
  165. {
  166. $start_num = Yii::app()->request->getParam('start_num','');
  167. $total = Yii::app()->request->getParam('num','');
  168. $id = Yii::app()->request->getParam('id','');
  169. $criteria = new EMongoCriteria();
  170. $num = substr($start_num,0,strlen($start_num) - 4);
  171. for ($i=0;$i<$total;$i++) {
  172. $criteria = new EMongoCriteria();
  173. $day = intval("1".substr($start_num,-4,4)) +$i;
  174. $tmp = $num.substr($day,1,strlen($day) - 1);
  175. $criteria->coupon_coding('==',$tmp);
  176. $coupon = JCoupon::model()->find($criteria);
  177. if (!empty($coupon)) {
  178. $coupon->channel = $id;
  179. $coupon->save();
  180. }
  181. }
  182. CommonFn::requestAjax(true,'修改成功');exit;
  183. }
  184. }