generator.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/4/6
  6. * Time: 下午1:51
  7. */
  8. namespace fcode;
  9. use Log;
  10. class generator
  11. {
  12. private $mCommonId;
  13. private $mNumber;
  14. private $mBatchCode;
  15. private $mUserKey;
  16. private $mUsableDays;
  17. public function __construct($commonid, $num, $batch_code, $usable_days)
  18. {
  19. $this->mCommonId = $commonid;
  20. $this->mNumber = intval($num);
  21. if(empty($batch_code)) {
  22. $this->mBatchCode = 'XMMZ';
  23. } else {
  24. $this->mBatchCode = $batch_code;
  25. }
  26. $this->mUserKey = mt_rand(1000, 9999);
  27. if(intval($usable_days) <= 0) {
  28. $usable_days = 60;
  29. }
  30. $this->mUsableDays = $usable_days;
  31. }
  32. public function make()
  33. {
  34. $mod_fcode = Model('goods_fcode');
  35. for ($i = 0; $i < $this->mNumber;)
  36. {
  37. $fc_code = sprintf("%06s%04d%06d",$this->mBatchCode,$this->mCommonId,mt_rand(100000, 999999));
  38. $data = [];
  39. $data['fc_state'] = 0;
  40. $data['user_key'] = $this->mUserKey;
  41. $data['goods_commonid'] = $this->mCommonId;
  42. $data['fc_code'] = $fc_code;
  43. $data['usable_days'] = $this->mUsableDays;
  44. $data['batch_code'] = $this->mBatchCode;
  45. $ret = $mod_fcode->insert($data);
  46. $affect_rows = $mod_fcode->affected_rows();
  47. Log::record("fcode make i={$i} ret={$ret} rows={$affect_rows}",Log::DEBUG);
  48. if($ret != false && $affect_rows > 0) {
  49. ++$i;
  50. }
  51. }
  52. $url = BASE_SITE_URL . "/mobile/index.php?act=fcode&op=index&common_id={$this->mCommonId}&batch_code={$this->mBatchCode}";
  53. return $url;
  54. }
  55. }