Info.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. # 核心类,数据获取类
  3. namespace Goods\Lib;
  4. use Dever;
  5. class Info
  6. {
  7. private $table_info = 'goods/info';
  8. private $table_attr = 'goods/info_attr';
  9. private $table_sku = 'goods/info_sku';
  10. /**
  11. * instance
  12. *
  13. * @var string
  14. */
  15. protected static $instance;
  16. /**
  17. * init
  18. *
  19. * @return mixed
  20. */
  21. public static function init($top_category = false)
  22. {
  23. if (empty(self::$instance[$top_category])) {
  24. self::$instance[$top_category] = new self($top_category);
  25. }
  26. return self::$instance[$top_category];
  27. }
  28. public function __construct($top_category = false)
  29. {
  30. $this->top_category = $top_category;
  31. # 必有有分类才能进入到需求中
  32. if (!$this->top_category) {
  33. $this->top_category = -1;
  34. //Dever::alert('错误的分类信息');
  35. }
  36. }
  37. private function setAttr($cate, $order = 'list_reorder')
  38. {
  39. if ($order != 'list_reorder') {
  40. Dever::config('base')->attrOrder = $order;
  41. }
  42. Dever::setInput('update_category', $cate);
  43. $attr = Dever::db($this->table_attr, array(), $cate);
  44. return $attr;
  45. }
  46. public function getAttr($cate)
  47. {
  48. return Dever::load('attr/api')->getAttrByCate($cate, 'list');
  49. }
  50. # 根据分类获取需求列表数据
  51. public function getData($cate = false, $limit = '0,10', $page = false, $attr = true)
  52. {
  53. if (!$limit) {
  54. $limit = '0,10';
  55. }
  56. $where = $data = array();
  57. if (!$cate) {
  58. if ($this->top_category > 0) {
  59. $where['top_category_id'] = $this->top_category;
  60. }
  61. } elseif (strstr($cate, ',')) {
  62. $where['category'] = $this->top_category . ',' . $cate;
  63. } else {
  64. $where['second_category_id'] = $cate;
  65. }
  66. if ($page) {
  67. $method = 'getPageAll';
  68. } else {
  69. $where['limit'] = $limit;
  70. $method = 'getAll';
  71. }
  72. $name = Dever::input('name');
  73. if ($name) {
  74. $where['name'] = $name;
  75. }
  76. $data = Dever::db($this->table_info)->$method($where);
  77. if ($data && $attr) {
  78. foreach ($data as $k => $v) {
  79. $data[$k] = $this->info($v, 'list_reorder');
  80. }
  81. }
  82. return $data;
  83. }
  84. # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化
  85. public function getDataByAttr($cate = false, $where = array())
  86. {
  87. if (!$cate) {
  88. Dever::alert('错误的分类信息');
  89. }
  90. $where_sql = array();
  91. $name = Dever::input('name');
  92. if ($name) {
  93. //$where['name'] = $name;
  94. $where_sql[] = ' t_2.name = "'.$name.'"';
  95. }
  96. $attr = $this->setAttr($this->top_category . ',' . $cate);
  97. //print_r($attr->config['attr']);
  98. $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql);
  99. if (!$where_sql) {
  100. return array();
  101. }
  102. # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了
  103. //$data = $attr->getPageAll($where);
  104. $page['template'] = 'list';
  105. $page['num'] = 10;
  106. $where_sql = implode(' and ', $where_sql);
  107. $sql = 'SELECT t_2.* FROM `{project}_goods_'.$attr->config['name'].'` as t_1 Left Join `{project}_goods_info` AS t_2 ON t_1.`info_id`=t_2.`id` WHERE '.$where_sql.' order by t_2.`reorder` desc,t_2.`id` desc';
  108. $data = $attr->fetchAll($sql, array(), $page);
  109. if ($data) {
  110. foreach ($data as $k => $v) {
  111. $data[$k] = $this->info($v, 'list_reorder');
  112. }
  113. }
  114. return $data;
  115. }
  116. # 获取列表页的搜索条件
  117. public function getSearch($cate = false)
  118. {
  119. # 获取搜索条件
  120. if (!$cate) {
  121. $search_cate = $cate = $this->top_category;
  122. } else {
  123. $search_cate = $cate;
  124. }
  125. # 1是当前用户所在的城市,暂时写死
  126. # 获取搜索条件
  127. $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2);
  128. # 下级分类
  129. $search['cate'] = Dever::load('category/api')->getList($cate);
  130. # 上级分类
  131. $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category);
  132. return $search;
  133. }
  134. # 获取支付信息
  135. public function getPayState($uid, $id, $category)
  136. {
  137. $pay_where['category'] = $category;
  138. $pay = Dever::db('goods/pay')->one($pay_where);
  139. $pay_state = 0;
  140. if ($pay && $pay['num'] > 0) {
  141. $pay_state = $pay['num'];
  142. } elseif (strstr($category, ',')) {
  143. $category = explode(',', $category);
  144. $category = array_pop($category);
  145. $pay_state = $this->getPayState($uid, $id, $category);
  146. }
  147. //$update['where_id'] = $id;
  148. //$update['poster_pay_type'] = $pay_state;
  149. //Dever::db($this->table_info)->update($update);
  150. return $pay_state;
  151. }
  152. # 更新需求
  153. public function update($top, $cate, $data, $id, $uid)
  154. {
  155. if ($cate) {
  156. $category = $top . ',' . $cate;
  157. } else {
  158. $category = $top;
  159. }
  160. $attr = $this->setAttr($category);
  161. $attr_update = Dever::preInput('attr_');
  162. if ($attr_update) {
  163. foreach ($attr_update as $k => $v) {
  164. if (strstr($k, '_s')) {
  165. $k = str_replace('_s', '', $k);
  166. $attr_update[$k] = $v;
  167. }
  168. if (strstr($k, '_e')) {
  169. $k = str_replace('_e', '', $k);
  170. $attr_update[$k] .= ',' . $v;
  171. }
  172. }
  173. }
  174. $update = array();
  175. $update['name'] = $data['name'];
  176. $update['category'] = $category;
  177. $update['pic'] = $data['pic'];
  178. //$update['pic_cover'] = $data['pic'];
  179. $update['poster_uid'] = $uid;
  180. if (isset($data['view_price']) && $data['view_price']) {
  181. $update['view_price'] = $data['view_price'];
  182. }
  183. if ($uid && $id) {
  184. # 此处要判断是用户自己的
  185. $info = Dever::db($this->table_info)->one($id);
  186. /*
  187. if ($uid == $info['poster_uid']) {
  188. Dever::alert('错误的用户参数');
  189. }
  190. */
  191. $update['where_id'] = $attr_update['where_id'] = $id;
  192. Dever::db($this->table_info)->update($update);
  193. Dever::db($this->table_attr)->update($attr_update);
  194. $pay = 0;
  195. } else {
  196. $id = Dever::db($this->table_info)->insert($update);
  197. $attr_update['category'] = $update['category'];
  198. $attr_update['info_id'] = $id;
  199. $attr_update['id'] = $id;
  200. Dever::db($this->table_attr)->insert($attr_update);
  201. # 验证是否需要支付,根据category判断
  202. $pay = false;
  203. //$pay = $this->getPayState($uid, $id, $category);
  204. $info = Dever::db($this->table_info)->one($id);
  205. $info = $this->getInfoLink($info);
  206. }
  207. return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
  208. }
  209. # 获取支付所需要的信息
  210. public function getPayInfo($id, $sku = 0, $type = 1, $value = '')
  211. {
  212. $info = is_array($id) ? $id : Dever::db($this->table_info)->find($id);
  213. if ($info) {
  214. if ($type == 2) {
  215. $info['price'] = $info['f_price'];
  216. unset($info['s_price']);
  217. } else {
  218. unset($info['f_price']);
  219. }
  220. $sku = $sku > 0 ? $sku : $info['sku_id'];
  221. if ($sku > 0) {
  222. $where['info_id'] = $info['id'];
  223. $where['id'] = $sku;
  224. $sku = Dever::db('goods/info_sku')->find($where);
  225. if ($sku) {
  226. if ($sku['type'] == 3) {
  227. $price = $sku['price'];
  228. $s_price = $sku['s_price'];
  229. $f_price = $sku['f_price'];
  230. $sku['price'] = 0;
  231. $sku['s_price'] = 0;
  232. $sku['f_price'] = 0;
  233. # 计算价格
  234. $key = explode('_', $sku['key']);
  235. $attr = Dever::db('attr/info')->find($key[0]);
  236. if ($attr) {
  237. if ($value >= $attr['sell_value']) {
  238. $sku['price'] = $price;
  239. $sku['s_price'] = $s_price;
  240. $sku['f_price'] = $f_price;
  241. }
  242. }
  243. }
  244. $info['sku_id'] = $sku['id'];
  245. if ($type == 2) {
  246. $info['price'] = $sku['f_price'];
  247. } else {
  248. $info['price'] = $sku['price'];
  249. $info['s_price'] = $sku['s_price'];
  250. }
  251. $info['min'] = $sku['min'];
  252. //$info['attr_json'] = $sku['attr'];
  253. $info['attr'] = Dever::json_decode($sku['attr']);
  254. if ($info['attr']) {
  255. $info['attr'] = Dever::load('attr/api')->getInfoByJson($info['attr']);
  256. $info['sku_name'] = $info['attr']['string'];
  257. }
  258. }
  259. }
  260. /*
  261. $info['freight_id'] = 0;
  262. $info['freight_price'] = 0;
  263. $freight = $this->freight($info, $info['price'], $user, $num);
  264. if ($freight) {
  265. $info['freight_id'] = $freight['id'];
  266. $info['freight_price'] = $freight['price'];
  267. }
  268. */
  269. # 佣金计算
  270. //$info['reward'] = $this->reward($info, $info['price']*$num);
  271. }
  272. return $info;
  273. }
  274. # 获取自提信息
  275. public function storeInfo($goods_id, $area_id = '')
  276. {
  277. $info = Dever::db($this->table_info)->find($goods_id);
  278. if (!$info) {
  279. return array();
  280. }
  281. $array = explode(',', $info['category']);
  282. $cate = $store = array();
  283. $total = count($array);
  284. $total -= 1;
  285. # 用户所在地理位置
  286. if ($area_id) {
  287. $where['area'] = $area_id;
  288. }
  289. foreach ($array as $k => $v) {
  290. $cate[$k] = $v;
  291. if ($k < $total) {
  292. $cate[] = '-1';
  293. }
  294. $where['category'] = $cate;
  295. $data = Dever::db('goods/store')->state($where);
  296. if ($data) {
  297. $store += $data;
  298. }
  299. }
  300. return $store;
  301. }
  302. # 获取运费信息
  303. public function getFreight($id, $goods_price, $user, $num)
  304. {
  305. $info = Dever::db($this->table_info)->find($id);
  306. return $this->freight($info, $goods_price, $user, $num);
  307. }
  308. # 根据已经选择的sku获取商品信息
  309. public function getInfoBySku($id, $sku_id)
  310. {
  311. $info = Dever::db($this->table_info)->find($id);
  312. if ($info && $sku_id > 0) {
  313. $sku = Dever::db($this->table_sku)->find($sku_id);
  314. if ($sku && $sku['attr']) {
  315. $sku['attr'] = Dever::json_decode($sku['attr']);
  316. $info['sku'] = Dever::load('attr/api')->getInfoByJson($sku['attr']);
  317. }
  318. }
  319. return $info;
  320. }
  321. # 获取基本信息
  322. public function getInfo($info, $attr = true, $other = false, $order = 'view_reorder', $user = array(), $reward = false)
  323. {
  324. if (!is_array($info)) {
  325. $info = Dever::db($this->table_info)->find($info);
  326. }
  327. if ($info && $attr) {
  328. $info = $this->info($info, $order, $other, $user, $reward);
  329. } else {
  330. $this->getGroup($info);
  331. }
  332. if (!isset($info['goods_id'])) {
  333. $info['goods_id'] = $info['id'];
  334. }
  335. if ($info['pic']) {
  336. $info['pic'] = explode(',', $info['pic']);
  337. }
  338. if ($info['tag_id']) {
  339. $info['tag'] = Dever::db('goods/tag')->getAllByIds($info['tag_id']);
  340. }
  341. return $info;
  342. }
  343. # 获取组合商品
  344. public function getGroup(&$info)
  345. {
  346. if ($info && $info['price_type'] > 2 && $info['goods'] && is_string($info['goods'])) {
  347. # 获取套餐或者组合的商品
  348. $goods = Dever::array_decode($info['goods']);
  349. $goods_info = array();
  350. foreach ($goods as $k => $v) {
  351. $ginfo = Dever::db('goods/info')->one($v['goods_id']);
  352. if ($ginfo) {
  353. $goods_info[] = array
  354. (
  355. 'id' => $ginfo['id'],
  356. 'name' => $ginfo['name'],
  357. 'price' => $ginfo['price'],
  358. 'cover' => $ginfo['cover'],
  359. 'num' => $v['num'],
  360. );
  361. }
  362. }
  363. $info['goods'] = $goods_info;
  364. }
  365. }
  366. # 获取基本信息
  367. public function info($info, $key = 'view_reorder', $other = false, $user = array(), $reward = false)
  368. {
  369. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  370. if ($info['price_type'] == 2) {
  371. $attr = $this->setAttr($info['category'], $key);
  372. $attr_data = $attr->one($info['id']);
  373. $info['attr'] = $info['group_attr'] = $info['single_attr'] = $info['input_attr'] = array();
  374. if ($attr->config['attr']) {
  375. $i = 0;
  376. foreach ($attr->config['attr'] as $k => $v) {
  377. if (isset($v[$key]) && $v[$key] > 0 && $v['state'] == 1) {
  378. if (isset($attr_data['attr_' . $v['id']])) {
  379. $v['value'] = $attr_data['attr_' . $v['id']];
  380. } else {
  381. continue;
  382. $v['value'] = '';
  383. }
  384. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  385. if ($v['ename'] == 'price') {
  386. $info['price'] = $v['value_string'];
  387. } else {
  388. if ($v['icon']) {
  389. $v['bg'] = 'background-image: url('.$v['icon'].')';
  390. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  391. }
  392. $info['attr'][$i] = $this->attr($i, $v, $info);
  393. $i++;
  394. }
  395. }
  396. }
  397. # 获取sku的价格
  398. $this->getPrice($info, $other);
  399. }
  400. if (!$info['attr']) {
  401. $info['price_type'] = 1;
  402. }
  403. }
  404. if (isset($info['cdate']) && $info['cdate']) {
  405. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  406. }
  407. $info = $this->getInfoLink($info);
  408. $this->getGroup($info);
  409. //$info['freight'] = 0;
  410. //unset($info['goods']);
  411. unset($info['f_price']);
  412. unset($info['cdate']);
  413. unset($info['udate']);
  414. unset($info['category']);
  415. unset($info['top_category_id']);
  416. unset($info['second_category_id']);
  417. unset($info['category_id']);
  418. unset($info['area']);
  419. unset($info['mode']);
  420. unset($info['top']);
  421. unset($info['youhui']);
  422. unset($info['status']);
  423. unset($info['reorder']);
  424. unset($info['state']);
  425. return $info;
  426. }
  427. # 佣金计算
  428. private function reward($info, $price)
  429. {
  430. if ($info['reward_value'] && $info['reward_value'] > 0) {
  431. if ($info['reward_type'] == 2) {
  432. $info['reward_value'] = round(($info['reward_value']/100) * $price, 2);
  433. }
  434. return $info['reward_value'];
  435. }
  436. $array = explode(',', $info['category']);
  437. $cate = $reward = array();
  438. $total = count($array);
  439. $total -= 1;
  440. foreach ($array as $k => $v) {
  441. $cate[$k] = $v;
  442. if ($k < $total) {
  443. $cate[] = '-1';
  444. }
  445. $where['category'] = $cate;
  446. $data = Dever::db('goods/reward')->one($where);
  447. if ($data) {
  448. $reward = $data;
  449. }
  450. }
  451. if ($reward) {
  452. if ($reward['value'] && $reward['value'] > 0) {
  453. if ($reward['type'] == 2) {
  454. $reward['value'] = round(($reward['value']/100) * $price, 2);
  455. }
  456. return $reward['value'];
  457. }
  458. }
  459. return 0;
  460. }
  461. # 运费计算,暂时用省份代表地区
  462. private function freight($info, $goods_price, $user = array(), $num = 1)
  463. {
  464. $array = explode(',', $info['category']);
  465. $cate = $freight = array();
  466. $total = count($array);
  467. $total -= 1;
  468. if ($info && $info['goods_area']) {
  469. $where['goods_area'] = $info['goods_area'];
  470. }
  471. # 用户所在地理位置
  472. if ($user && isset($user['area_id']) && $user['area_id']) {
  473. $area = explode(',', $user['area_id']);
  474. $where['area'] = $area[0];
  475. }
  476. foreach ($array as $k => $v) {
  477. $cate[$k] = $v;
  478. if ($k < $total) {
  479. $cate[] = '-1';
  480. }
  481. $where['category'] = $cate;
  482. $data = Dever::db('goods/freight')->one($where);
  483. if ($data) {
  484. $freight = $data;
  485. }
  486. }
  487. if ($freight) {
  488. $price = $freight['first_price'];
  489. if ($num > $freight['first_num']) {
  490. $num = $num - $freight['first_num'];
  491. $price = $freight['first_price'] + ($freight['next_price']*$num);
  492. }
  493. if ($freight['type'] == 2) {
  494. $freight['price'] = round($goods_price*$num*($price/100), 2);
  495. } else {
  496. $freight['price'] = $price;
  497. }
  498. }
  499. return $freight;
  500. }
  501. private function attr($key, $data, &$info)
  502. {
  503. # 获取销售属性
  504. if ($data['option']) {
  505. $value = explode(',', $data['value']);
  506. $option = $data['option'];
  507. $data['option'] = array();
  508. foreach ($value as $k => $v) {
  509. if (isset($option[$v])) {
  510. $option[$v]['parent_name'] = $data['name'];
  511. $data['option'][] = $info['option'][$option[$v]['price_key']] = $option[$v];
  512. }
  513. }
  514. } else {
  515. unset($data['option']);
  516. }
  517. if ($data['is_sell'] == 2) {
  518. if ($data['sell_type'] == 1) {
  519. $info['group_attr'][] = $key;
  520. } elseif ($data['sell_type'] == 2) {
  521. $info['single_attr'][] = $key;
  522. } elseif ($data['sell_type'] == 3) {
  523. $info['input_attr'][] = $key;
  524. }
  525. }
  526. return $data;
  527. }
  528. private function getPrice(&$info, $other = false)
  529. {
  530. # 获取价格
  531. $where['info_id'] = $info['id'];
  532. $info['price_array'] = array();
  533. $list = Dever::db('goods/info_sku')->getData($where);
  534. $min = $max = array();
  535. if ($list) {
  536. foreach ($list as $k => $v) {
  537. if ($v['key']) {
  538. $v['name'] = array();
  539. $temp = explode('_', $v['key']);
  540. foreach ($temp as $k1 => $v1) {
  541. if (isset($info['option'][$v1])) {
  542. $v['name'][] = $info['option'][$v1]['name'];
  543. }
  544. }
  545. $v['name'] = implode(',', $v['name']);
  546. if ($other) {
  547. foreach ($other[1] as $k1 => $v1) {
  548. $v[$v1] = isset($other[0][$v['id']][$v1]) ? $other[0][$v['id']][$v1] : 0;
  549. }
  550. }
  551. if ($v['type'] == 1) {
  552. if (!$min) {
  553. $min = $v;
  554. }
  555. if (!$max) {
  556. $max = $v;
  557. }
  558. if ($min['price'] > $v['price']) {
  559. $min = $v;
  560. }
  561. if ($max['price'] < $v['price']) {
  562. $max = $v;
  563. }
  564. }
  565. $info['price_array'][$v['key']] = $v;
  566. }
  567. }
  568. }
  569. $info['price_min'] = $min;
  570. $info['price_max'] = $max;
  571. if ($min) {
  572. $info['price'] = $min['price'];
  573. $info['s_price'] = $min['s_price'];
  574. $info['sku_id'] = $min['id'];
  575. }
  576. unset($info['option']);
  577. }
  578. public function getInfoLink($info)
  579. {
  580. if (isset($this->view_path)) {
  581. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  582. $info['view_link'] = Dever::url($info['view_path']);
  583. }
  584. return $info;
  585. }
  586. public function setViewPath($path)
  587. {
  588. $this->view_path = $path;
  589. }
  590. # 根据分类获取商品
  591. public function getGoods_api()
  592. {
  593. $where['category_id'] = Dever::input('cate_id');
  594. $factory_id = Dever::input('id');
  595. $data = Dever::db('goods/info_category')->getData($where);
  596. return $data;
  597. }
  598. }