optional_goods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. declare(strict_types=0);
  3. /**
  4. * Created by PhpStorm.
  5. * User: stanley-king
  6. * Date: 2017/8/30
  7. * Time: 下午9:14
  8. */
  9. namespace activity;
  10. use algorithm;
  11. use StatesHelper;
  12. use Log;
  13. use commonid_helper;
  14. //M元买N件商品活动
  15. class opgroup
  16. {
  17. private $mPrice;
  18. private $mOptions;
  19. private $mStartTm;
  20. private $mEndTm;
  21. private $mGoods;
  22. private $mRepeat;
  23. private $mSpecialID;
  24. public function __construct($price,$options,$start,$end,$repeat,$special_id)
  25. {
  26. $this->mPrice = $price;
  27. $this->mOptions = $options;
  28. $this->mStartTm = $start;
  29. $this->mEndTm = $end;
  30. $this->mRepeat = $repeat;
  31. $this->mSpecialID = $special_id;
  32. $this->mGoods = [];
  33. }
  34. public function match($goods_nums)
  35. {
  36. if($this->acting() == false) return false;
  37. $gids = array_keys($goods_nums);
  38. sort($gids);
  39. $match_gids = algorithm::set_intersection($this->mGoods,$gids);
  40. $matched = false;
  41. if($this->mRepeat == false)
  42. {
  43. if(count($match_gids) >= $this->mOptions) {
  44. $matched = true;
  45. }
  46. }
  47. else
  48. {
  49. $goods_num = $this->goods_num($match_gids, $goods_nums);
  50. if ($goods_num >= $this->mOptions) {
  51. $matched = true;
  52. }
  53. }
  54. if($matched)
  55. {
  56. $matched_goods = [];
  57. foreach ($match_gids as $gid) {
  58. $matched_goods[$gid] = $goods_nums[$gid];
  59. }
  60. return ['goods_nums' => $matched_goods,'price' => $this->mPrice,'options' => $this->mOptions,'repeat' => $this->mRepeat];
  61. } else {
  62. return false;
  63. }
  64. }
  65. public function info($gid)
  66. {
  67. if($this->acting() == false) return false;
  68. if(algorithm::binary_search($this->mGoods,$gid))
  69. {
  70. $result['special_id'] = $this->mSpecialID;
  71. $price = $this->mPrice / 100;
  72. $result['desc'] = "{$price}元任选{$this->mOptions}件";
  73. $result['short_desc'] = "{$price}选{$this->mOptions}";
  74. return $result;
  75. } else {
  76. return false;
  77. }
  78. }
  79. private function goods_num($gids,$gid_nums)
  80. {
  81. $num = 0;
  82. foreach ($gids as $gid)
  83. {
  84. $num += $gid_nums[$gid];
  85. }
  86. return $num;
  87. }
  88. public function add_goods($gid)
  89. {
  90. $gid = intval($gid);
  91. if($gid <= 0) return false;
  92. if(algorithm::binary_search($this->mGoods,$gid)) {
  93. return false;
  94. } else {
  95. $pos = algorithm::lower_bonud($this->mGoods,$gid);
  96. algorithm::array_insert($this->mGoods,$pos,$gid);
  97. return true;
  98. }
  99. }
  100. public function acting() {
  101. $cur_tm = time();
  102. return ($cur_tm >= $this->mStartTm && $cur_tm < $this->mEndTm);
  103. }
  104. public function equal($price,$options) {
  105. return ($this->mPrice == $price && $this->mOptions == $options);
  106. }
  107. }
  108. class optional_goods
  109. {
  110. private $mAllGoods;
  111. private $mOpgroups;
  112. private $mSpecials;
  113. private $mErrGoods;
  114. static private $stInstance = null;
  115. private function __construct()
  116. {
  117. $this->mOpgroups = [];
  118. $this->mAllGoods = [];
  119. $this->mSpecials = [];
  120. $this->mErrGoods = [];
  121. }
  122. static public function instance()
  123. {
  124. if (self::$stInstance == null) {
  125. self::$stInstance = new optional_goods();
  126. }
  127. if (StatesHelper::fetch_state('optional_goods')) {
  128. Log::record("optional_goods reinit data.", Log::DEBUG);
  129. self::$stInstance->init();
  130. }
  131. return self::$stInstance;
  132. }
  133. public function layout_error()
  134. {
  135. if (empty($this->mErrGoods)) {
  136. return false;
  137. } else {
  138. return $this->mErrGoods;
  139. }
  140. }
  141. public function all_goods()
  142. {
  143. return $this->mAllGoods;
  144. }
  145. public function exist_goods($gid)
  146. {
  147. $gid = intval($gid);
  148. if ($gid <= 0) return false;
  149. if (algorithm::binary_search($this->mAllGoods, $gid)) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155. public function info($goods_id)
  156. {
  157. $gid = intval($goods_id);
  158. if ($gid <= 0) return false;
  159. if (algorithm::binary_search($this->mAllGoods, $gid))
  160. {
  161. foreach ($this->mOpgroups as $group)
  162. {
  163. $info = $group->info($gid);
  164. if($info == false) continue;
  165. if($group->acting() == false) {
  166. return false;
  167. } else {
  168. return $info;
  169. }
  170. }
  171. return false;
  172. } else {
  173. return false;
  174. }
  175. }
  176. public function match($gidnums)
  177. {
  178. $input_goods = $this->input_goods($gidnums);
  179. $match_goods = algorithm::set_intersection($this->mAllGoods, $input_goods);
  180. if (empty($match_goods)) return false;
  181. $match_goods = $this->sel_goods($match_goods, $gidnums);
  182. $result = [];
  183. foreach ($this->mOpgroups as $opgroup)
  184. {
  185. if (empty($match_goods)) break;
  186. $part = $opgroup->match($match_goods);
  187. if ($part != false) {
  188. $result[] = $part;
  189. $goods_nums = $part['goods_nums'];
  190. foreach ($goods_nums as $gid => $num) {
  191. unset($match_goods[$gid]);
  192. }
  193. }
  194. }
  195. return $result;
  196. }
  197. private function sel_goods($gids, $gidnums)
  198. {
  199. $result = [];
  200. foreach ($gids as $gid) {
  201. $result[$gid] = $gidnums[$gid];
  202. }
  203. return $result;
  204. }
  205. private function input_goods($gidnums)
  206. {
  207. $result = [];
  208. foreach ($gidnums as $gid => $num) {
  209. if ($num > 0) {
  210. $result[] = $gid;
  211. }
  212. }
  213. sort($result);
  214. return $result;
  215. }
  216. private function init()
  217. {
  218. global $config;
  219. $opids = $config['optional_goods'];
  220. if (empty($opids)) return true;
  221. $this->mOpgroups = [];
  222. $this->mAllGoods = [];
  223. $this->mSpecials = [];
  224. $this->mErrGoods = [];
  225. $this->mSpecials = array_unique($opids);
  226. foreach ($this->mSpecials as $special_id) {
  227. $this->add_special($special_id);
  228. }
  229. sort($this->mErrGoods);
  230. }
  231. private function add_special($special_id)
  232. {
  233. $mod_special = Model('mb_special');
  234. $ret = $mod_special->getMbSpecialItemUsableListByID($special_id);
  235. $blocks = $ret['blocks'];
  236. $cur_group = null;
  237. foreach ($blocks as $block)
  238. {
  239. $type = $block['item_type'];
  240. if ($type == 'home1') {
  241. $tmp_group = $this->add_group($block, $special_id);
  242. if ($tmp_group != null) {
  243. $cur_group = $tmp_group;
  244. $this->mOpgroups[] = $cur_group;
  245. }
  246. } else {
  247. $this->add_goods($cur_group, $block);
  248. }
  249. }
  250. }
  251. private function add_goods($group, $block)
  252. {
  253. $items = $block['items'];
  254. if (empty($items)) return;
  255. foreach ($items as $item)
  256. {
  257. if (array_key_exists('type', $item) && $item['type'] == 'goods')
  258. {
  259. $goods_id = intval($item['data']);
  260. $spugids = $this->spu_goods($goods_id);
  261. if($spugids == false) continue;
  262. foreach ($spugids as $goods_id)
  263. {
  264. if ($group != null && $goods_id > 0 && $this->exist_goods($goods_id) == false) {
  265. if ($group->add_goods($goods_id)) {
  266. $this->addgoods($goods_id);
  267. }
  268. } else {
  269. $this->mErrGoods[] = $goods_id;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. private function spu_goods($gid)
  276. {
  277. $cid = commonid_helper::instance()->common_id($gid);
  278. if($cid != false)
  279. {
  280. $gids = commonid_helper::instance()->goods_ids($cid);
  281. if($gids == false) return false;
  282. if(count($gids) == 1) {
  283. return $gids;
  284. }
  285. else
  286. {
  287. $goods_map = [];
  288. $mod_goods = Model('goods');
  289. $goodses = $mod_goods->getGoodsOnlineList(array('goods_id' => array('in', $gids)));
  290. foreach ($goodses as $goods) {
  291. $gid = intval($goods['goods_id']);
  292. $goods_map[$gid] = $goods['goods_price'];
  293. }
  294. $goods_price = $goods_map[$gid];
  295. $result = [];
  296. foreach ($goods_map as $goods_id => $price)
  297. {
  298. if($price == $goods_price) {
  299. $result[] = $goods_id;
  300. }
  301. }
  302. return empty($result) ? false : $result;
  303. }
  304. }
  305. else {
  306. return false;
  307. }
  308. }
  309. private function add_group($block,$special_id)
  310. {
  311. $items = $block['items'];
  312. if(empty($items)) return null;
  313. $reserved = $items['reserved'];
  314. $params = preg_split("/#/",$reserved);
  315. if(count($params) == 5)
  316. {
  317. $price = intval($params[0] * 100 + 0.5);
  318. $options = intval($params[1]);
  319. $repeat = intval($params[2]) == 1 ? true : false;
  320. $startm = strtotime($params[3]);
  321. $hours = intval($params[4]);
  322. if($price > 0 && $options > 1)
  323. {
  324. $group = $this->find($price,$options);
  325. if($group == null)
  326. {
  327. if($startm > 0 && $hours > 0 && $startm + 3600 * $hours > time()) {
  328. $group = new opgroup($price,$options,$startm,$startm + 3600 * $hours,$repeat,$special_id);
  329. }
  330. }
  331. return $group;
  332. }
  333. else {
  334. return null;
  335. }
  336. }
  337. }
  338. private function find($price,$options)
  339. {
  340. foreach ($this->mOpgroups as $item)
  341. {
  342. if($item->equal($price,$options)) {
  343. return $item;
  344. }
  345. }
  346. return null;
  347. }
  348. private function addgoods($gid)
  349. {
  350. $gid = intval($gid);
  351. if($gid <= 0) return false;
  352. if(algorithm::binary_search($this->mAllGoods,$gid)) {
  353. return false;
  354. } else {
  355. $pos = algorithm::lower_bonud($this->mAllGoods,$gid);
  356. algorithm::array_insert($this->mAllGoods,$pos,$gid);
  357. return true;
  358. }
  359. }
  360. }