CardInfo.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\models\system;
  3. use app\models\user\User;
  4. use app\models\user\UserBill;
  5. use app\models\user\UserLevel;
  6. use crmeb\basic\BaseModel;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\services\{PHPExcelService};
  9. class CardInfo extends BaseModel
  10. {
  11. use ModelTrait;
  12. public function getCreateTimeAttr($name)
  13. {
  14. return date("Y-m-d H:i:s",$name);
  15. }
  16. public function getUseTimeAttr($name)
  17. {
  18. return date("Y-m-d H:i:s",$name);
  19. }
  20. /**
  21. * 激活卡片
  22. * @param $card_number
  23. * @param $card_password
  24. * @param int $uid
  25. * @param int $type
  26. * @return bool
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public static function verification($card_number,$card_password,$uid=0,$type=0,$store_id=0,$is_confirm=0)
  32. {
  33. $info = self::where('card_number',$card_number)->find();
  34. if(!$info) return self::setErrorInfo('卡券信息有误!',true);
  35. if($info['card_password']!=$card_password) return self::setErrorInfo('卡券密码错误!');
  36. if($is_confirm==0)
  37. {
  38. return true;
  39. }
  40. if($type == 0 && $store_id==0) return self::setErrorInfo('门店不能为空!',true);
  41. $data['use_uid'] = $uid;
  42. $data['use_time'] = time();
  43. $data['status'] = 1;
  44. self::where('card_number',$card_number)->update($data);
  45. switch (intval($info['type']))
  46. {
  47. case 0:
  48. SystemStoreMember::setstorecardno($uid,$store_id);
  49. $user = User::where('uid',$uid)->find();
  50. User::where('uid',$uid)->inc('consumer',$info['amount'])->update(['reg_store_id'=>$store_id]);
  51. UserLevel::setUserLevel($uid,1);
  52. UserBill::income('开通会员赠送消费券',$uid,'consumer','system_add_consumer',$info['amount'],$info['id'],bcadd($user['consumer'],$info['amount'],2),"开通会员赠送消费券".$info['amount']);
  53. if($user['spread_uid']>0)
  54. {
  55. $sp_user = User::where('uid',$user['spread_uid'])->find();
  56. $spread_consumer = sys_config('spread_consumer');
  57. User::where('uid',$user['spread_uid'])->inc('consumer',$spread_consumer)->update();
  58. UserBill::income('推荐会员赠送消费券',$user['spread_uid'],'consumer','spread_add_consumer',$spread_consumer,$info['id'],bcadd($sp_user['consumer'],$spread_consumer,2),"推荐会员赠送消费券".$spread_consumer);
  59. }
  60. break;
  61. case 1:
  62. $user = User::where('uid',$uid)->find();
  63. User::where('uid',$uid)->inc('now_money',$info['amount'])->update();
  64. UserBill::income('充值卡充值',$uid,'now_money','system_add',$info['amount'],$info['id'],bcadd($user['consumer'],$info['amount'],2),"充值卡充值".$info['amount']);
  65. break;
  66. }
  67. return true;
  68. }
  69. public static function lst($where)
  70. {
  71. $model = new self;
  72. if(isset($where['amount']) && $where['amount']>0)
  73. {
  74. $model = $model->where('amount','=',$where['amount']);
  75. }
  76. if(isset($where['type']) && $where['type']>-1)
  77. {
  78. $model = $model->where('type','=',$where['type']);
  79. }
  80. if(isset($where['status']) && $where['status']>-1)
  81. {
  82. $model = $model->where('status','=',$where['status']);
  83. }
  84. if (isset($where['card_id']) && $where['card_id'] >0) {
  85. $model = $model->where('card_id','=',$where['card_id']);
  86. }
  87. if (isset($where['data']) && $where['data'] != '') {
  88. list($startTime, $endTime) = explode(' - ', $where['data']);
  89. $model = $model->where('create_time', '>', strtotime($startTime));
  90. $model = $model->where('create_time', '<', strtotime($endTime) + 24 * 3600);
  91. }
  92. if(isset($where['key']) && $where['key']!='')
  93. {
  94. $model = $model->whereLike('card_number','%'.$where['key'].'%');
  95. }
  96. $model = $model->order("id desc");
  97. if(isset($where['excel']) && $where['excel']==1)
  98. {
  99. $export = [];
  100. foreach ($model->select() as $index => $item) {
  101. $export[] = [
  102. $item['card_number']."\t",
  103. $item['card_password'],
  104. $item['amount']
  105. ];
  106. }
  107. PHPExcelService::setExcelHeader(['卡号', '密码', '面值'])
  108. ->setExcelTile('卡券导出', '卡券导出' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  109. ->setExcelContent($export)
  110. ->ExcelSave();
  111. }
  112. $count = $model->value('count(id)')?:0;
  113. $data = $model->page($where['page'],$where['limit'])->select()->toarray();
  114. foreach ($data as &$v)
  115. {
  116. $v['title'] = Card::where('id',$v['card_id'])->value('title');
  117. $v['nickname'] = User::where('uid',$v['use_uid'])->value('nickname');
  118. }
  119. return compact('count','data');
  120. }
  121. }