CouponCode.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. class CouponCode extends MongoAr
  3. {
  4. public $_id; //object id
  5. public $code; //兑换码 string
  6. public $channel; //使用渠道 唯一批次标记
  7. public $desc; //批次说明
  8. public $coupons = array();
  9. public $stop_time;//过期时间
  10. public $use_time;//使用时间
  11. public $user;//使用者
  12. public $user_device_id;//领用用户的device_id or unionid
  13. public $alway = 0; //是否可以重复使用
  14. public $status = 0; //状态 0为未使用,1为已使用
  15. public function __construct($scenario='insert'){
  16. $this->setMongoDBComponent(Yii::app()->getComponent('mongodb_o2o'));
  17. parent::__construct($scenario);
  18. $time = time();
  19. $this->code = substr($time,2);
  20. }
  21. public static $status_option = array(
  22. 0 => array('name' => '未使用'),
  23. 1 => array('name' => '已使用'),
  24. );
  25. public static function model($className=__CLASS__)
  26. {
  27. return parent::model($className);
  28. }
  29. public function getCollectionName()
  30. {
  31. return 'coupon_code';
  32. }
  33. public function parseRow($row,$output=array()){
  34. $newRow = array();
  35. $newRow['id'] = (string)$row['_id'];
  36. $newRow['code'] = CommonFn::get_val_if_isset($row,'code','');
  37. $newRow['channel'] = CommonFn::get_val_if_isset($row,'channel','');
  38. $newRow['desc'] = CommonFn::get_val_if_isset($row,'desc','');
  39. $newRow['user_device_id'] = CommonFn::get_val_if_isset($row,'user_device_id','');
  40. $user = array();
  41. if(isset($row['user'])){
  42. $_user = RUser::get($row['user']);
  43. if($_user){
  44. $user = $_user->parseRow($_user->attributes,array('user_name','id','avatar'));
  45. }
  46. }
  47. $newRow['user'] = $user;
  48. $newRow['stop_time'] = CommonFn::get_val_if_isset($row,'stop_time');
  49. if($newRow['stop_time']){
  50. $newRow['stop_time_str'] = CommonFn::bgmdate("Y年n月d日", $newRow['stop_time'],1);
  51. }
  52. $newRow['use_time'] = CommonFn::get_val_if_isset($row,'use_time');
  53. if($newRow['use_time']) {
  54. $newRow['use_time_str'] = CommonFn::bgmdate("Y年n月d日", $newRow['use_time'], 1);
  55. }
  56. $newRow['status'] = CommonFn::get_val_if_isset($row,'status',0);
  57. $newRow['always'] = CommonFn::get_val_if_isset($row,'always',0);
  58. $coupons = array();
  59. if(isset($row['coupons']) && !empty($row['coupons'])){
  60. foreach($row['coupons'] as $coupon_id){
  61. $_coupon = Coupon::get($coupon_id);
  62. if($_coupon){
  63. $coupons[] = Coupon::model()->parse($_coupon->attributes,false,array('id','name'));
  64. }else{
  65. continue;
  66. }
  67. }
  68. }
  69. $newRow['coupons'] = $coupons;
  70. $newRow['action_user'] = CommonFn::get_val_if_isset($row,'action_user',"");
  71. $newRow['action_time'] = CommonFn::get_val_if_isset($row,'action_time',"");
  72. $newRow['action_log'] = CommonFn::get_val_if_isset($row,'action_log',"");
  73. if(APPLICATION=='api'){
  74. unset($newRow['action_user']);
  75. unset($newRow['action_time']);
  76. unset($newRow['action_log']);
  77. }
  78. return $this->output($newRow,$output);
  79. }
  80. public static function get($_id) {
  81. if(CommonFn::isMongoId($_id)){
  82. $criteria = new EMongoCriteria();
  83. $criteria->_id('==', $_id);
  84. $model = self::model()->find($criteria);
  85. return $model;
  86. }else{
  87. return false;
  88. }
  89. }
  90. }