Info.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. /**
  10. * instance
  11. *
  12. * @var string
  13. */
  14. protected static $instance;
  15. /**
  16. * init
  17. *
  18. * @return mixed
  19. */
  20. public static function init($top_category = false)
  21. {
  22. if (empty(self::$instance[$top_category])) {
  23. self::$instance[$top_category] = new self($top_category);
  24. }
  25. return self::$instance[$top_category];
  26. }
  27. public function __construct($top_category = false)
  28. {
  29. $this->top_category = $top_category;
  30. # 必有有分类才能进入到需求中
  31. if (!$this->top_category) {
  32. $this->top_category = -1;
  33. //Dever::alert('错误的分类信息');
  34. }
  35. }
  36. private function setAttr($cate, $order = 'list_reorder')
  37. {
  38. if ($order != 'list_reorder') {
  39. Dever::config('base')->attrOrder = $order;
  40. }
  41. Dever::setInput('search_option_category', $cate);
  42. $attr = Dever::db($this->table_attr, array(), $cate);
  43. return $attr;
  44. }
  45. public function getAttr($cate)
  46. {
  47. return Dever::load('attr/api')->getAttrByCate($cate, 'list');
  48. }
  49. # 根据分类获取需求列表数据
  50. public function getData($cate = false, $limit = '0,10', $page = false, $attr = true)
  51. {
  52. if (!$limit) {
  53. $limit = '0,10';
  54. }
  55. $where = $data = array();
  56. if (!$cate) {
  57. if ($this->top_category > 0) {
  58. $where['top_category_id'] = $this->top_category;
  59. }
  60. } elseif (strstr($cate, ',')) {
  61. $where['category'] = $this->top_category . ',' . $cate;
  62. } else {
  63. $where['second_category_id'] = $cate;
  64. }
  65. if ($page) {
  66. $method = 'getPageAll';
  67. } else {
  68. $where['limit'] = $limit;
  69. $method = 'getAll';
  70. }
  71. $name = Dever::input('name');
  72. if ($name) {
  73. $where['name'] = $name;
  74. }
  75. $data = Dever::db($this->table_info)->$method($where);
  76. if ($data && $attr) {
  77. foreach ($data as $k => $v) {
  78. $data[$k] = $this->info($v, 'list_reorder');
  79. }
  80. }
  81. return $data;
  82. }
  83. # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化
  84. public function getDataByAttr($cate = false, $where = array())
  85. {
  86. if (!$cate) {
  87. Dever::alert('错误的分类信息');
  88. }
  89. $where_sql = array();
  90. $name = Dever::input('name');
  91. if ($name) {
  92. //$where['name'] = $name;
  93. $where_sql[] = ' t_2.name = "'.$name.'"';
  94. }
  95. $attr = $this->setAttr($this->top_category . ',' . $cate);
  96. //print_r($attr->config['attr']);
  97. $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql);
  98. if (!$where_sql) {
  99. return array();
  100. }
  101. # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了
  102. //$data = $attr->getPageAll($where);
  103. $page['template'] = 'list';
  104. $page['num'] = 10;
  105. $where_sql = implode(' and ', $where_sql);
  106. $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';
  107. $data = $attr->fetchAll($sql, array(), $page);
  108. if ($data) {
  109. foreach ($data as $k => $v) {
  110. $data[$k] = $this->info($v, 'list_reorder');
  111. }
  112. }
  113. return $data;
  114. }
  115. # 获取列表页的搜索条件
  116. public function getSearch($cate = false)
  117. {
  118. # 获取搜索条件
  119. if (!$cate) {
  120. $search_cate = $cate = $this->top_category;
  121. } else {
  122. $search_cate = $cate;
  123. }
  124. # 1是当前用户所在的城市,暂时写死
  125. # 获取搜索条件
  126. $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2);
  127. # 下级分类
  128. $search['cate'] = Dever::load('category/api')->getList($cate);
  129. # 上级分类
  130. $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category);
  131. return $search;
  132. }
  133. # 获取支付信息
  134. public function getPayState($uid, $id, $category)
  135. {
  136. $pay_where['category'] = $category;
  137. $pay = Dever::db('goods/pay')->one($pay_where);
  138. $pay_state = 0;
  139. if ($pay && $pay['num'] > 0) {
  140. $pay_state = $pay['num'];
  141. } elseif (strstr($category, ',')) {
  142. $category = explode(',', $category);
  143. $category = array_pop($category);
  144. $pay_state = $this->getPayState($uid, $id, $category);
  145. }
  146. //$update['where_id'] = $id;
  147. //$update['poster_pay_type'] = $pay_state;
  148. //Dever::db($this->table_info)->update($update);
  149. return $pay_state;
  150. }
  151. # 更新需求
  152. public function update($top, $cate, $data, $id, $uid)
  153. {
  154. if ($cate) {
  155. $category = $top . ',' . $cate;
  156. } else {
  157. $category = $top;
  158. }
  159. $attr = $this->setAttr($category);
  160. $attr_update = Dever::preInput('attr_');
  161. if ($attr_update) {
  162. foreach ($attr_update as $k => $v) {
  163. if (strstr($k, '_s')) {
  164. $k = str_replace('_s', '', $k);
  165. $attr_update[$k] = $v;
  166. }
  167. if (strstr($k, '_e')) {
  168. $k = str_replace('_e', '', $k);
  169. $attr_update[$k] .= ',' . $v;
  170. }
  171. }
  172. }
  173. $update = array();
  174. $update['name'] = $data['name'];
  175. $update['category'] = $category;
  176. $update['pic'] = $data['pic'];
  177. //$update['pic_cover'] = $data['pic'];
  178. $update['poster_uid'] = $uid;
  179. if (isset($data['view_price']) && $data['view_price']) {
  180. $update['view_price'] = $data['view_price'];
  181. }
  182. if ($uid && $id) {
  183. # 此处要判断是用户自己的
  184. $info = Dever::db($this->table_info)->one($id);
  185. /*
  186. if ($uid == $info['poster_uid']) {
  187. Dever::alert('错误的用户参数');
  188. }
  189. */
  190. $update['where_id'] = $attr_update['where_id'] = $id;
  191. Dever::db($this->table_info)->update($update);
  192. Dever::db($this->table_attr)->update($attr_update);
  193. $pay = 0;
  194. } else {
  195. $id = Dever::db($this->table_info)->insert($update);
  196. $attr_update['category'] = $update['category'];
  197. $attr_update['info_id'] = $id;
  198. $attr_update['id'] = $id;
  199. Dever::db($this->table_attr)->insert($attr_update);
  200. # 验证是否需要支付,根据category判断
  201. $pay = false;
  202. //$pay = $this->getPayState($uid, $id, $category);
  203. $info = Dever::db($this->table_info)->one($id);
  204. $info = $this->getInfoLink($info);
  205. }
  206. return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
  207. }
  208. # 获取支付所需要的信息
  209. public function getPayInfo($id, $sku, $num = 1, $user = array())
  210. {
  211. $info = Dever::db($this->table_info)->getOne($id);
  212. if ($info) {
  213. $info['freight_id'] = 0;
  214. $info['freight_price'] = 0;
  215. if ($sku > 0) {
  216. $where['info_id'] = $info['id'];
  217. $where['id'] = $sku;
  218. $sku = Dever::db('goods/info_sku')->getOne($where);
  219. if ($sku) {
  220. $info['price'] = $sku['price'];
  221. $info['num'] = $sku['num'];
  222. }
  223. }
  224. $freight = $this->freight($info, $info['price'], $user, $num);
  225. if ($freight) {
  226. $info['freight_id'] = $freight['id'];
  227. $info['freight_price'] = $freight['price'];
  228. }
  229. # 佣金计算
  230. $info['reward'] = $this->reward($info, $info['price']*$num);
  231. }
  232. return $info;
  233. }
  234. # 获取自提信息
  235. public function storeInfo($goods_id, $area_id = '')
  236. {
  237. $info = Dever::db($this->table_info)->getOne($goods_id);
  238. if (!$info) {
  239. return array();
  240. }
  241. $array = explode(',', $info['category']);
  242. $cate = $store = array();
  243. $total = count($array);
  244. $total -= 1;
  245. # 用户所在地理位置
  246. if ($area_id) {
  247. $where['area'] = $area_id;
  248. }
  249. foreach ($array as $k => $v) {
  250. $cate[$k] = $v;
  251. if ($k < $total) {
  252. $cate[] = '-1';
  253. }
  254. $where['category'] = $cate;
  255. $data = Dever::db('goods/store')->state($where);
  256. if ($data) {
  257. $store += $data;
  258. }
  259. }
  260. return $store;
  261. }
  262. # 获取运费信息
  263. public function getFreight($id, $goods_price, $user, $num)
  264. {
  265. $info = Dever::db($this->table_info)->getOne($id);
  266. return $this->freight($info, $goods_price, $user, $num);
  267. }
  268. # 获取基本信息
  269. public function getInfo($id, $attr = true, $order = 'view_reorder', $user = array(), $reward = false)
  270. {
  271. $info = Dever::db($this->table_info)->getOne($id);
  272. if ($info && $attr) {
  273. $info = $this->info($info, $order, false, $user, $reward);
  274. }
  275. if ($info['pic']) {
  276. $info['pic'] = explode(',', $info['pic']);
  277. }
  278. return $info;
  279. }
  280. # 获取基本信息
  281. public function info($info, $key = 'list_reorder', $is_sell = false, $user = array(), $reward = false)
  282. {
  283. if ($info['price_type'] == 2) {
  284. $attr = $this->setAttr($info['category'], $key);
  285. $attr_data = $attr->one($info['id']);
  286. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  287. $info['attr'] = $info['sell_attr'] = array();
  288. if ($attr->config['attr']) {
  289. foreach ($attr->config['attr'] as $k => $v) {
  290. if (isset($v[$key]) && $v[$key] > 0) {
  291. if (isset($attr_data['attr_' . $v['id']])) {
  292. $v['value'] = $attr_data['attr_' . $v['id']];
  293. } else {
  294. continue;
  295. $v['value'] = '';
  296. }
  297. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  298. if ($v['ename'] == 'price') {
  299. $info['price'] = $v['value_string'];
  300. } else {
  301. $v['bg'] = 'background-image: url('.$v['icon'].')';
  302. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  303. if ($is_sell > 0) {
  304. if ($is_sell == $v['is_sell']) {
  305. $info['attr'][] = $v;
  306. }
  307. } else {
  308. $info['attr'][] = $v;
  309. # 设置sku
  310. $this->sku($v, $info);
  311. }
  312. }
  313. }
  314. }
  315. }
  316. } else {
  317. $price = $info['price'];
  318. $s_price = $info['s_price'];
  319. $info['price'] = array();
  320. $info['price']['list'] = array();
  321. $info['price']['min'] = array
  322. (
  323. 'price' => $price,
  324. 's_price' => $s_price
  325. );
  326. $info['price']['max'] = array();
  327. }
  328. $info['platform_name'] = Dever::db('goods/info')->config['config_platform'][$info['platform']];
  329. if (isset($info['cdate']) && $info['cdate']) {
  330. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  331. }
  332. $info = $this->getInfoLink($info);
  333. $info['freight'] = 0;
  334. if ($info['platform'] == 1 && $user) {
  335. # 获取运费
  336. $info['freight'] = $this->freight($info, $info['price']['min']['price'], $user);
  337. }
  338. # 佣金计算
  339. if ($reward) {
  340. $info['reward'] = $this->reward($info, $info['price']['min']['price']);
  341. }
  342. return $info;
  343. }
  344. # 佣金计算
  345. private function reward($info, $price)
  346. {
  347. if ($info['reward_value'] && $info['reward_value'] > 0) {
  348. if ($info['reward_type'] == 2) {
  349. $info['reward_value'] = round(($info['reward_value']/100) * $price, 2);
  350. }
  351. return $info['reward_value'];
  352. }
  353. $array = explode(',', $info['category']);
  354. $cate = $reward = array();
  355. $total = count($array);
  356. $total -= 1;
  357. foreach ($array as $k => $v) {
  358. $cate[$k] = $v;
  359. if ($k < $total) {
  360. $cate[] = '-1';
  361. }
  362. $where['category'] = $cate;
  363. $data = Dever::db('goods/reward')->one($where);
  364. if ($data) {
  365. $reward = $data;
  366. }
  367. }
  368. if ($reward) {
  369. if ($reward['value'] && $reward['value'] > 0) {
  370. if ($reward['type'] == 2) {
  371. $reward['value'] = round(($reward['value']/100) * $price, 2);
  372. }
  373. return $reward['value'];
  374. }
  375. }
  376. return 0;
  377. }
  378. # 运费计算,暂时用省份代表地区
  379. private function freight($info, $goods_price, $user = array(), $num = 1)
  380. {
  381. $array = explode(',', $info['category']);
  382. $cate = $freight = array();
  383. $total = count($array);
  384. $total -= 1;
  385. if ($info && $info['goods_area']) {
  386. $where['goods_area'] = $info['goods_area'];
  387. }
  388. # 用户所在地理位置
  389. if ($user && isset($user['area_id']) && $user['area_id']) {
  390. $area = explode(',', $user['area_id']);
  391. $where['area'] = $area[0];
  392. }
  393. foreach ($array as $k => $v) {
  394. $cate[$k] = $v;
  395. if ($k < $total) {
  396. $cate[] = '-1';
  397. }
  398. $where['category'] = $cate;
  399. $data = Dever::db('goods/freight')->one($where);
  400. if ($data) {
  401. $freight = $data;
  402. }
  403. }
  404. if ($freight) {
  405. $price = $freight['first_price'];
  406. if ($num > $freight['first_num']) {
  407. $num = $num - $freight['first_num'];
  408. $price = $freight['first_price'] + ($freight['next_price']*$num);
  409. }
  410. if ($freight['type'] == 2) {
  411. $freight['price'] = round($goods_price*$num*($price/100), 2);
  412. } else {
  413. $freight['price'] = $price;
  414. }
  415. }
  416. return $freight;
  417. }
  418. private function sku($data, &$info)
  419. {
  420. # 获取销售属性
  421. if ($data['is_sell'] == 2) {
  422. if ($data['option']) {
  423. $value = explode(',', $data['value']);
  424. foreach($data['option'] as $k => $v) {
  425. if (in_array($v['id'], $value)) {
  426. $data['option_sku'][] = array
  427. (
  428. 'id' => $v['id'],
  429. 'info_id' => $v['info_id'],
  430. 'name' => $v['name'],
  431. 'icon' => $v['icon'],
  432. 'value' => $v['info_id'] . '-' . $v['id'],
  433. );
  434. }
  435. }
  436. unset($data['option']);
  437. }
  438. $info['sell_attr'][] = $data;
  439. # 获取最便宜的价格
  440. $where['info_id'] = $info['id'];
  441. $info['price'] = array();
  442. $info['price']['list'] = Dever::db('goods/info_sku')->getData($where);
  443. $info['price']['min'] = Dever::db('goods/info_sku')->getMinOne($where);
  444. $info['price']['max'] = Dever::db('goods/info_sku')->getMaxOne($where);
  445. }
  446. }
  447. public function getInfoLink($info)
  448. {
  449. if (isset($this->view_path)) {
  450. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  451. $info['view_link'] = Dever::url($info['view_path']);
  452. }
  453. return $info;
  454. }
  455. public function setViewPath($path)
  456. {
  457. $this->view_path = $path;
  458. }
  459. }