JCouponController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $gift_id = Yii::app()->request->getParam('gift_id','');
  68. $channel = Yii::app()->request->getParam('bus_id','');
  69. $expire_time = Yii::app()->request->getParam('expire_time_str','');
  70. if (!$num ||!$channel || !$gift_id || !$expire_time) {
  71. CommonFn::requestAjax(false,'请填写完整信息');exit;
  72. }
  73. $gift = Gift::get(new MongoId($gift_id));
  74. if (empty($gift)) {
  75. CommonFn::requestAjax(false,'礼包不存在');exit;
  76. }
  77. for($i=0; $i<$num; $i++) {
  78. $coupon = new JCoupon();
  79. $coupon->gift_id = $gift_id;
  80. $flag = true;
  81. while($flag) {
  82. $pwd = $coupon->generateRandomString(8);
  83. $tmp = JCoupon::getByPwd($pwd);
  84. if (empty(count($tmp))){
  85. $flag = false;
  86. }
  87. }
  88. $coupon->pwd = $pwd;
  89. $coupon->channel = $channel;
  90. $coupon->status = 0;
  91. $coupon->expire_time = $expire_time;
  92. $coupon->save();
  93. }
  94. CommonFn::requestAjax(true,'生成成功');exit;
  95. }
  96. }