Card.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\models\system;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\services\UtilService;
  5. use crmeb\traits\ModelTrait;
  6. class Card extends BaseModel
  7. {
  8. use ModelTrait;
  9. public function getCreateTimeAttr($name)
  10. {
  11. return date("Y-m-d H:i:s",$name);
  12. }
  13. /**
  14. * 生成卡券
  15. * @param $data
  16. * @return Card|bool|\think\Model
  17. */
  18. public static function do_create($data)
  19. {
  20. if(!isset($data['title']) || $data['title'] == '') return self::setErrorInfo('批次名称不能为空!', true);
  21. if(!isset($data['totle_num']) || $data['totle_num'] == 0) return self::setErrorInfo('批次数量不能为0!', true);
  22. $data['create_time'] = time();
  23. $rs = self::create($data);
  24. $maxid = CardInfo::whereDay('create_time')->where('type',$data['type'])->order('id desc')->value('card_number');
  25. $left = sprintf("%s%s",date("ymd"),str_pad($data['type'],2,'0',STR_PAD_LEFT));
  26. if($maxid) {
  27. $maxid = bcadd(substr($maxid, strlen($left) - 1), 1);
  28. }
  29. else
  30. {
  31. $maxid = 1;
  32. }
  33. $data1['card_id'] = $rs['id'];
  34. $data1['type'] = $rs['type'];
  35. $all = [];
  36. for ($i=0;$i<$data['totle_num'];$i++)
  37. {
  38. $data1['card_number'] = $left.str_pad($maxid,7,'0',STR_PAD_LEFT);
  39. $data1['card_password'] = UtilService::random_code_type(8);
  40. $data1['create_time'] = time();
  41. $data1['amount'] = $data['amount'];
  42. $all[] = $data1;
  43. $maxid++;
  44. }
  45. CardInfo::limit(1000)->insertAll($all);
  46. return $rs;
  47. }
  48. /**
  49. * 获取列表
  50. * @param $where
  51. * @return array
  52. */
  53. public static function lst($where)
  54. {
  55. $model = new self;
  56. if(isset($where['amount']) && $where['amount']>0)
  57. {
  58. $model = $model->where('amount','=',$where['amount']);
  59. }
  60. if(isset($where['type']) && $where['type']>-1)
  61. {
  62. $model = $model->where('type','=',$where['type']);
  63. }
  64. if(isset($where['status']) && $where[
  65. 'status']>-1)
  66. {
  67. $model = $model->where('status','=',$where['status']);
  68. }
  69. if (isset($where['data']) && $where['data'] != '') {
  70. list($startTime, $endTime) = explode(' - ', $where['data']);
  71. $model = $model->where('create_time', '>', strtotime($startTime));
  72. $model = $model->where('create_time', '<', strtotime($endTime) + 24 * 3600);
  73. }
  74. if(isset($where['key']) && $where['key']!='')
  75. {
  76. $model = $model->whereLike('title','%'.$where['key'].'%');
  77. }
  78. $model = $model->order("id desc");
  79. $count = $model->value('count(id)')?:0;
  80. $data = $model->page($where['page'],$where['limit'])->select()->toarray();
  81. return compact('count','data');
  82. }
  83. }