JCouponController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, 0, false );
  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. $cursor = JCoupon::model()->findAll($criteria);
  47. $rows = CommonFn::getRowsFromCursor($cursor);
  48. $parsedRows = JCoupon::model()->parse($rows);
  49. $total = $cursor->count();
  50. echo CommonFn::composeDatagridData($parsedRows, $total);
  51. }
  52. public function actionEdit()
  53. {
  54. $id = Yii::app()->request->getParam('id','');
  55. $status = (int)Yii::app()->request->getParam('status',0);
  56. if (!CommonFn::isMongoId($id)) {
  57. CommonFn::requestAjax(false,'id错误');exit;
  58. }
  59. $coupon = JCoupon::get(new MongoId($id));
  60. $coupon->status = $status;
  61. $coupon->save();
  62. CommonFn::requestAjax(true,'保存成功');exit;
  63. }
  64. public function actionAdd()
  65. {
  66. $num = (int)Yii::app()->request->getParam('num','');
  67. $coding_num = (int)Yii::app()->request->getParam('coding_num','');
  68. $gift_id = Yii::app()->request->getParam('gift_id','');
  69. $channel = Yii::app()->request->getParam('bus_id','');
  70. $expire_time = Yii::app()->request->getParam('expire_time_str','');
  71. if (!$num ||!$channel || !$gift_id || !$expire_time) {
  72. CommonFn::requestAjax(false,'请填写完整信息');exit;
  73. }
  74. $gift = Gift::get(new MongoId($gift_id));
  75. if (empty($gift)) {
  76. CommonFn::requestAjax(false,'礼包不存在');exit;
  77. }
  78. $c = new EMongoCriteria();
  79. $c->coding_num('==',$coding_num);
  80. $j = JCoupon::model()->findAll($c);
  81. if (count($j) > 0) {
  82. CommonFn::requestAjax(false,'起始值重复');exit;
  83. }
  84. $total = 100000;
  85. for($i=0; $i<$num; $i++) {
  86. $coupon = new JCoupon();
  87. $coupon->gift_id = $gift_id;
  88. $coupon->coding_num = $coding_num;
  89. $flag = true;
  90. while($flag) {
  91. $pwd = $coupon->generateRandomString(8);
  92. $tmp = JCoupon::getByPwd($pwd);
  93. if (empty(count($tmp))){
  94. $flag = false;
  95. }
  96. }
  97. $total++;
  98. $coupon->pwd = $pwd;
  99. $coupon->coupon_coding = $gift->gift_num.substr(date('Y'),2).date('m').'0'.substr($total,1);
  100. $coupon->channel = $channel;
  101. $coupon->status = 0;
  102. $coupon->expire_time = $expire_time;
  103. $coupon->save();
  104. $coding_num++;
  105. }
  106. CommonFn::requestAjax(true,'生成成功');exit;
  107. }
  108. public function actionOutputExcel()
  109. {
  110. $start_num = (int)Yii::app()->request->getParam('start_num','');
  111. $total = (int)Yii::app()->request->getParam('total','');
  112. $criteria = new EMongoCriteria();
  113. $criteria->coding_num('>=',(int)$start_num);
  114. $criteria->coding_num('<',(int)($start_num+$total));
  115. $cursor = JCoupon::model()->findAll($criteria);
  116. $rows = CommonFn::getRowsFromCursor($cursor);
  117. $parsedRows = JCoupon::model()->parse($rows);
  118. $res=Service::factory('JCouponService')->push($parsedRows);
  119. }
  120. }