optional_goods.php 11 KB

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