Info.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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, $num = 1, $user = array())
  211. {
  212. $info = Dever::db($this->table_info)->find($id);
  213. if ($info) {
  214. unset($info['f_price']);
  215. $sku = $sku > 0 ? $sku : $info['sku_id'];
  216. if ($sku > 0) {
  217. $where['info_id'] = $info['id'];
  218. $where['id'] = $sku;
  219. $sku = Dever::db('goods/info_sku')->find($where);
  220. if ($sku) {
  221. $info['sku_id'] = $sku['id'];
  222. $info['price'] = $sku['price'];
  223. $info['s_price'] = $sku['s_price'];
  224. //$info['num'] = $sku['num'];
  225. $info['attr'] = Dever::json_decode($sku['attr']);
  226. if ($info['attr']) {
  227. $info['attr'] = Dever::load('attr/api')->getInfoByJson($info['attr']);
  228. }
  229. }
  230. }
  231. /*
  232. $info['freight_id'] = 0;
  233. $info['freight_price'] = 0;
  234. $freight = $this->freight($info, $info['price'], $user, $num);
  235. if ($freight) {
  236. $info['freight_id'] = $freight['id'];
  237. $info['freight_price'] = $freight['price'];
  238. }
  239. */
  240. # 佣金计算
  241. //$info['reward'] = $this->reward($info, $info['price']*$num);
  242. }
  243. return $info;
  244. }
  245. # 获取自提信息
  246. public function storeInfo($goods_id, $area_id = '')
  247. {
  248. $info = Dever::db($this->table_info)->find($goods_id);
  249. if (!$info) {
  250. return array();
  251. }
  252. $array = explode(',', $info['category']);
  253. $cate = $store = array();
  254. $total = count($array);
  255. $total -= 1;
  256. # 用户所在地理位置
  257. if ($area_id) {
  258. $where['area'] = $area_id;
  259. }
  260. foreach ($array as $k => $v) {
  261. $cate[$k] = $v;
  262. if ($k < $total) {
  263. $cate[] = '-1';
  264. }
  265. $where['category'] = $cate;
  266. $data = Dever::db('goods/store')->state($where);
  267. if ($data) {
  268. $store += $data;
  269. }
  270. }
  271. return $store;
  272. }
  273. # 获取运费信息
  274. public function getFreight($id, $goods_price, $user, $num)
  275. {
  276. $info = Dever::db($this->table_info)->find($id);
  277. return $this->freight($info, $goods_price, $user, $num);
  278. }
  279. # 根据已经选择的sku获取商品信息
  280. public function getInfoBySku($id, $sku_id)
  281. {
  282. $info = Dever::db($this->table_info)->find($id);
  283. if ($info && $sku_id > 0) {
  284. $sku = Dever::db($this->table_sku)->find($sku_id);
  285. if ($sku && $sku['attr']) {
  286. $sku['attr'] = Dever::json_decode($sku['attr']);
  287. $info['sku'] = Dever::load('attr/api')->getInfoByJson($sku['attr']);
  288. }
  289. }
  290. return $info;
  291. }
  292. # 获取基本信息
  293. public function getInfo($info, $attr = true, $other = false, $order = 'view_reorder', $user = array(), $reward = false)
  294. {
  295. if (!is_array($info)) {
  296. $info = Dever::db($this->table_info)->find($info);
  297. }
  298. if ($info && $attr) {
  299. $info = $this->info($info, $order, $other, false, $user, $reward);
  300. } else {
  301. $this->getGroup($info);
  302. }
  303. if (!isset($info['goods_id'])) {
  304. $info['goods_id'] = $info['id'];
  305. }
  306. if ($info['pic']) {
  307. $info['pic'] = explode(',', $info['pic']);
  308. }
  309. if ($info['tag_id']) {
  310. $info['tag'] = Dever::db('goods/tag')->getAllByIds($info['tag_id']);
  311. }
  312. return $info;
  313. }
  314. # 获取组合商品
  315. public function getGroup(&$info)
  316. {
  317. if ($info && $info['price_type'] > 2 && $info['goods'] && is_string($info['goods'])) {
  318. # 获取套餐或者组合的商品
  319. $goods = Dever::array_decode($info['goods']);
  320. $goods_info = array();
  321. foreach ($goods as $k => $v) {
  322. $ginfo = Dever::db('goods/info')->one($v['goods_id']);
  323. if ($ginfo) {
  324. $goods_info[] = array
  325. (
  326. 'id' => $ginfo['id'],
  327. 'name' => $ginfo['name'],
  328. 'price' => $ginfo['price'],
  329. 'cover' => $ginfo['cover'],
  330. 'num' => $v['num'],
  331. );
  332. }
  333. }
  334. $info['goods'] = $goods_info;
  335. }
  336. }
  337. # 获取基本信息
  338. public function info($info, $key = 'view_reorder', $other = false, $is_sell = false, $user = array(), $reward = false)
  339. {
  340. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  341. if ($info['price_type'] == 2) {
  342. $attr = $this->setAttr($info['category'], $key);
  343. $attr_data = $attr->one($info['id']);
  344. $info['attr'] = $info['sell_attr'] = $info['option_attr'] = array();
  345. if ($attr->config['attr']) {
  346. foreach ($attr->config['attr'] as $k => $v) {
  347. if (isset($v[$key]) && $v[$key] > 0) {
  348. if (isset($attr_data['attr_' . $v['id']])) {
  349. $v['value'] = $attr_data['attr_' . $v['id']];
  350. } else {
  351. continue;
  352. $v['value'] = '';
  353. }
  354. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  355. if ($v['ename'] == 'price') {
  356. $info['price_array'] = $v['value_string'];
  357. } else {
  358. $v['bg'] = 'background-image: url('.$v['icon'].')';
  359. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  360. if ($is_sell > 0) {
  361. if ($is_sell == $v['is_sell']) {
  362. $info['attr'][] = $v;
  363. }
  364. } else {
  365. $info['attr'][] = $v;
  366. # 设置sku
  367. $this->sku($v, $info);
  368. }
  369. }
  370. }
  371. }
  372. # 获取sku的价格
  373. $this->getPrice($info, $other);
  374. }
  375. }
  376. if (isset($info['cdate']) && $info['cdate']) {
  377. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  378. }
  379. $info = $this->getInfoLink($info);
  380. $this->getGroup($info);
  381. //$info['freight'] = 0;
  382. unset($info['f_price']);
  383. unset($info['cdate']);
  384. unset($info['udate']);
  385. unset($info['category']);
  386. unset($info['top_category_id']);
  387. unset($info['second_category_id']);
  388. unset($info['category_id']);
  389. unset($info['area']);
  390. unset($info['mode']);
  391. unset($info['top']);
  392. unset($info['youhui']);
  393. unset($info['status']);
  394. unset($info['reorder']);
  395. unset($info['state']);
  396. return $info;
  397. }
  398. # 佣金计算
  399. private function reward($info, $price)
  400. {
  401. if ($info['reward_value'] && $info['reward_value'] > 0) {
  402. if ($info['reward_type'] == 2) {
  403. $info['reward_value'] = round(($info['reward_value']/100) * $price, 2);
  404. }
  405. return $info['reward_value'];
  406. }
  407. $array = explode(',', $info['category']);
  408. $cate = $reward = array();
  409. $total = count($array);
  410. $total -= 1;
  411. foreach ($array as $k => $v) {
  412. $cate[$k] = $v;
  413. if ($k < $total) {
  414. $cate[] = '-1';
  415. }
  416. $where['category'] = $cate;
  417. $data = Dever::db('goods/reward')->one($where);
  418. if ($data) {
  419. $reward = $data;
  420. }
  421. }
  422. if ($reward) {
  423. if ($reward['value'] && $reward['value'] > 0) {
  424. if ($reward['type'] == 2) {
  425. $reward['value'] = round(($reward['value']/100) * $price, 2);
  426. }
  427. return $reward['value'];
  428. }
  429. }
  430. return 0;
  431. }
  432. # 运费计算,暂时用省份代表地区
  433. private function freight($info, $goods_price, $user = array(), $num = 1)
  434. {
  435. $array = explode(',', $info['category']);
  436. $cate = $freight = array();
  437. $total = count($array);
  438. $total -= 1;
  439. if ($info && $info['goods_area']) {
  440. $where['goods_area'] = $info['goods_area'];
  441. }
  442. # 用户所在地理位置
  443. if ($user && isset($user['area_id']) && $user['area_id']) {
  444. $area = explode(',', $user['area_id']);
  445. $where['area'] = $area[0];
  446. }
  447. foreach ($array as $k => $v) {
  448. $cate[$k] = $v;
  449. if ($k < $total) {
  450. $cate[] = '-1';
  451. }
  452. $where['category'] = $cate;
  453. $data = Dever::db('goods/freight')->one($where);
  454. if ($data) {
  455. $freight = $data;
  456. }
  457. }
  458. if ($freight) {
  459. $price = $freight['first_price'];
  460. if ($num > $freight['first_num']) {
  461. $num = $num - $freight['first_num'];
  462. $price = $freight['first_price'] + ($freight['next_price']*$num);
  463. }
  464. if ($freight['type'] == 2) {
  465. $freight['price'] = round($goods_price*$num*($price/100), 2);
  466. } else {
  467. $freight['price'] = $price;
  468. }
  469. }
  470. return $freight;
  471. }
  472. private function sku($data, &$info)
  473. {
  474. # 获取销售属性
  475. if ($data['is_sell'] == 2) {
  476. if ($data['option']) {
  477. $value = explode(',', $data['value']);
  478. foreach($data['option'] as $k => $v) {
  479. if (in_array($v['id'], $value)) {
  480. $key = $v['info_id'] . '-' . $v['id'];
  481. $info['option_sku'][$key] = $data['option_sku'][] = array
  482. (
  483. 'id' => $v['id'],
  484. 'info_id' => $v['info_id'],
  485. 'name' => $v['name'],
  486. 'parent_name' => $data['name'],
  487. 'icon' => $v['icon'],
  488. 'value' => $key,
  489. );
  490. }
  491. }
  492. unset($data['option']);
  493. }
  494. $info['sell_attr'][] = $data;
  495. } elseif ($data['is_sell'] == 3) {
  496. print_r($data);die;
  497. $info['option_attr'][] = $data;
  498. }
  499. }
  500. private function getPrice(&$info, $other = false)
  501. {
  502. # 获取价格
  503. $where['info_id'] = $info['id'];
  504. $info['price_array'] = array();
  505. $list = Dever::db('goods/info_sku')->getData($where);
  506. $info['price_array']['list'] = array();
  507. $min = $max = array();
  508. if ($list) {
  509. foreach ($list as $k => $v) {
  510. $v['name'] = array();
  511. $temp = explode('_', $v['key']);
  512. foreach ($temp as $k1 => $v1) {
  513. if (isset($info['option_sku'][$v1])) {
  514. $v['name'][] = $info['option_sku'][$v1]['name'];
  515. }
  516. }
  517. $v['name'] = implode(';', $v['name']);
  518. if ($other) {
  519. foreach ($other[1] as $k1 => $v1) {
  520. $v[$v1] = isset($other[0][$v['id']][$v1]) ? $other[0][$v['id']][$v1] : 0;
  521. }
  522. }
  523. $info['price_array']['list'][$v['key']] = $v;
  524. if (!$min) {
  525. $min = $v;
  526. }
  527. if (!$max) {
  528. $max = $v;
  529. }
  530. if ($min['price'] > $v['price']) {
  531. $min = $v;
  532. }
  533. if ($max['price'] < $v['price']) {
  534. $max = $v;
  535. }
  536. }
  537. }
  538. $info['price_array']['min'] = $min;
  539. $info['price_array']['max'] = $max;
  540. if ($min) {
  541. $info['price'] = $min['price'];
  542. $info['s_price'] = $min['s_price'];
  543. $info['sku_id'] = $min['id'];
  544. }
  545. unset($info['option_sku']);
  546. }
  547. public function getInfoLink($info)
  548. {
  549. if (isset($this->view_path)) {
  550. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  551. $info['view_link'] = Dever::url($info['view_path']);
  552. }
  553. return $info;
  554. }
  555. public function setViewPath($path)
  556. {
  557. $this->view_path = $path;
  558. }
  559. # 根据分类获取商品
  560. public function getGoods_api()
  561. {
  562. $where['category_id'] = Dever::input('cate_id');
  563. $factory_id = Dever::input('id');
  564. $data = Dever::db('goods/info_category')->getData($where);
  565. return $data;
  566. }
  567. }