CouponMember.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\common\model;
  3. class CouponMember extends \think\Model
  4. {
  5. protected $pk = 'id';
  6. protected $table = 'cy_coupon_member';
  7. protected $dateFormat = false; //时间戳格式不自动转换
  8. protected $resultSetType = 'collection';
  9. public function getUseTimeAttr($value)
  10. {
  11. if ($value > 0) {
  12. return date('Y-m-d H:i:s', $value);
  13. } else {
  14. return '';
  15. }
  16. }
  17. public function getUpdateTimeAttr($value)
  18. {
  19. if ($value > 0) {
  20. return date('Y-m-d H:i:s', $value);
  21. } else {
  22. return '';
  23. }
  24. }
  25. public function getStartTimeAttr($value)
  26. {
  27. if ($value > 0) {
  28. return date('Y-m-d H:i:s', $value);
  29. } else {
  30. return '';
  31. }
  32. }
  33. public function getEndTimeAttr($value)
  34. {
  35. if ($value > 0) {
  36. return date('Y-m-d H:i:s', $value);
  37. } else {
  38. return '';
  39. }
  40. }
  41. public function getPayCoupon($userid, $gameid, $money, $coupon_id = 0)
  42. {
  43. $where = [];
  44. if ($coupon_id > 0) {
  45. $where['a.id'] = $coupon_id;
  46. }
  47. //查询参数
  48. $where['a.member_id'] = $userid;
  49. $where['a.examine'] = 2;
  50. $where['a.is_use'] = 1;
  51. $time = strtotime(date("Y-m-d"));
  52. $couponMemberModel = model('CouponMember');
  53. $list = $couponMemberModel
  54. ->alias('a')
  55. ->join('cy_coupon c', 'a.coupon_id=c.id')
  56. ->where($where)
  57. ->where(function ($query) use ($gameid, $time) {
  58. $query->where([
  59. 'c.state' => 1,
  60. 'a.state' => 1,
  61. ])->whereRaw(sprintf('(c.type_id =1 and c.end_time>=%d) or (c.type_id=2 and a.end_time>%d)', $time, $time))
  62. ->whereRaw(sprintf('(c.game_id="" or FIND_IN_SET(%s,c.game_id))', $gameid));
  63. })
  64. ->field('a.*,c.name,c.money,c.min_money,c.start_time,c.end_time,a.start_time as coupon_start_time,a.end_time as coupon_end_time,c.type_id,c.game_id,c.state as coupon_state')
  65. ->order('a.id desc')
  66. ->select()->toArray();
  67. if ($list) {
  68. $tmpGameList = model('Common/Game')->getAllByCondition('id,name,nickname');
  69. $gameList = array();
  70. foreach ($tmpGameList as $game) {
  71. $gameList[$game['id']] = $game;
  72. }
  73. $trueArr = $falseArr = [];
  74. foreach ($list as $k => $v) {
  75. if ($v['type_id'] == 2) {
  76. $v['start_time'] = date('Y-m-d H:i:s',$v['coupon_start_time']);
  77. // $v['end_time'] = date('Y-m-d H:i:s',$v['coupon_end_time']+86399);
  78. $v['end_time'] = date('Y-m-d H:i:s',$v['coupon_end_time']);
  79. }else{
  80. // $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])+86399);
  81. $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time']));
  82. }
  83. if ($v['game_id']) {
  84. $game_ids = explode(',', $v['game_id']);
  85. $tmp = [];
  86. foreach ($game_ids as $kk => $vv) {
  87. $tmp[] = $gameList[$vv]['nickname'];
  88. }
  89. $v['game_name'] = implode(' ', array_unique($tmp));
  90. } else {
  91. $v['game_name'] = '全部游戏';
  92. }
  93. //金额判断是否可用
  94. if ($v['min_money'] <= $money) {
  95. $v['is_pay'] = 1;
  96. $trueArr[] = $v;
  97. } else {
  98. $v['is_pay'] = 0;
  99. $falseArr[] = $v;
  100. }
  101. }
  102. if ($trueArr) {
  103. $sort = array_column($trueArr, 'money');
  104. array_multisort($sort, SORT_DESC, $trueArr);
  105. }
  106. if ($falseArr) {
  107. $sort = array_column($falseArr, 'money');
  108. array_multisort($sort, SORT_DESC, $falseArr);
  109. }
  110. $list = array_merge($trueArr, $falseArr);
  111. }
  112. return $list;
  113. }
  114. public function updateCouponIsUseById($id,$is_use){
  115. //3已使用
  116. if($is_use == 3){
  117. if($this->where('id',$id)->where('is_use',2)->update(['is_use'=>3,'update_time'=>time()])){
  118. return true;
  119. }else{
  120. return false;
  121. }
  122. }else{
  123. if($this->where('id',$id)->where('is_use',2)->update(['is_use'=>1,'update_time'=>time(),'use_time'=>0])){
  124. return true;
  125. }else{
  126. return false;
  127. }
  128. }
  129. }
  130. }