present_manager.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/5/9
  6. * Time: 下午10:35
  7. */
  8. namespace fcode;
  9. require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
  10. require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
  11. use StatesHelper;
  12. use Log;
  13. use special_manager;
  14. use commonid_helper;
  15. use algorithm;
  16. class present_manager
  17. {
  18. public static $stInstance;
  19. private $mFcodes;
  20. private $mCidBcodes;
  21. private function __construct()
  22. {
  23. }
  24. static public function instance()
  25. {
  26. if(self::$stInstance == null) {
  27. self::$stInstance = new present_manager();
  28. }
  29. if(StatesHelper::fetch_state('present_fcode')) {
  30. Log::record("present_manager reinit data.",Log::DEBUG);
  31. self::$stInstance->init();
  32. }
  33. return self::$stInstance;
  34. }
  35. private function init()
  36. {
  37. global $config;
  38. $special_id = $config['autosend_fcodes']['order_present'];
  39. if($special_id <= 0) return false;
  40. $blocks = special_manager::instance()->special($special_id,$goods_ids);
  41. if(empty($blocks)) return false;
  42. $fcodes = [];
  43. $cid_bcodes = [];
  44. foreach ($blocks as $block)
  45. {
  46. $item_type = $block['item_type'];
  47. if($item_type == 'home1')
  48. {
  49. $items = $block['items'];
  50. if(empty($items)) continue;
  51. $item = $items[0];
  52. $batch_code = $item['title'];
  53. if(empty($batch_code)) continue;
  54. $goods_id = intval($item['data']);
  55. $cid = commonid_helper::instance()->common_id($goods_id);
  56. if($cid > 0) {
  57. $fcodes[$cid] = $batch_code;
  58. $cid_bcodes[] = "{$cid}.{$batch_code}";
  59. }
  60. }
  61. }
  62. $this->mFcodes = $fcodes;
  63. sort($cid_bcodes);
  64. $this->mCidBcodes = $cid_bcodes;
  65. return true;
  66. }
  67. public function fetch($mobile,$session_id)
  68. {
  69. if(empty($mobile) || $this->can_send($mobile) == false)
  70. return false;
  71. foreach ($this->mFcodes as $commonid => $batch_code)
  72. {
  73. $oper = new operator($commonid,$batch_code,$mobile,$session_id);
  74. if($oper->grabed() == false)
  75. {
  76. $fcode = $oper->grab();
  77. if($fcode != false) {
  78. return ['fcode' => $fcode,'common_id' => $commonid,'batch_code' => $batch_code];
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84. private function can_send($mobile)
  85. {
  86. $mod_fcode = Model('goods_fcode');
  87. $codes = $mod_fcode->getFcodeList(['mobile' => $mobile]);
  88. $user_fcodes = [];
  89. foreach ($codes as $item)
  90. {
  91. $fcoder = new mfcode($item);
  92. $cid = $fcoder->commonid();
  93. $bcode = $fcoder->batch_code();
  94. $user_fcodes[] = "{$cid}.{$bcode}";
  95. }
  96. sort($user_fcodes);
  97. $out = algorithm::set_intersection($user_fcodes,$this->mCidBcodes);
  98. return empty($out) ? true : false;
  99. }
  100. }