Info.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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)->find($id);
  212. if ($info) {
  213. $info['sku_id'] = 0;
  214. if ($sku > 0) {
  215. $where['info_id'] = $info['id'];
  216. $where['id'] = $sku;
  217. $sku = Dever::db('goods/info_sku')->find($where);
  218. if ($sku) {
  219. $info['sku_id'] = $sku['id'];
  220. $info['price'] = $sku['price'];
  221. $info['num'] = $sku['num'];
  222. }
  223. }
  224. /*
  225. $info['freight_id'] = 0;
  226. $info['freight_price'] = 0;
  227. $freight = $this->freight($info, $info['price'], $user, $num);
  228. if ($freight) {
  229. $info['freight_id'] = $freight['id'];
  230. $info['freight_price'] = $freight['price'];
  231. }
  232. */
  233. # 佣金计算
  234. //$info['reward'] = $this->reward($info, $info['price']*$num);
  235. }
  236. return $info;
  237. }
  238. # 获取自提信息
  239. public function storeInfo($goods_id, $area_id = '')
  240. {
  241. $info = Dever::db($this->table_info)->find($goods_id);
  242. if (!$info) {
  243. return array();
  244. }
  245. $array = explode(',', $info['category']);
  246. $cate = $store = array();
  247. $total = count($array);
  248. $total -= 1;
  249. # 用户所在地理位置
  250. if ($area_id) {
  251. $where['area'] = $area_id;
  252. }
  253. foreach ($array as $k => $v) {
  254. $cate[$k] = $v;
  255. if ($k < $total) {
  256. $cate[] = '-1';
  257. }
  258. $where['category'] = $cate;
  259. $data = Dever::db('goods/store')->state($where);
  260. if ($data) {
  261. $store += $data;
  262. }
  263. }
  264. return $store;
  265. }
  266. # 获取运费信息
  267. public function getFreight($id, $goods_price, $user, $num)
  268. {
  269. $info = Dever::db($this->table_info)->find($id);
  270. return $this->freight($info, $goods_price, $user, $num);
  271. }
  272. # 获取基本信息
  273. public function getInfo($id, $attr = true, $order = 'view_reorder', $user = array(), $reward = false)
  274. {
  275. $info = Dever::db($this->table_info)->find($id);
  276. if ($info && $attr) {
  277. $info = $this->info($info, $order, false, $user, $reward);
  278. }
  279. if ($info['pic']) {
  280. $info['pic'] = explode(',', $info['pic']);
  281. }
  282. return $info;
  283. }
  284. # 获取基本信息
  285. public function info($info, $key = 'list_reorder', $is_sell = false, $user = array(), $reward = false)
  286. {
  287. if ($info['price_type'] == 2) {
  288. $attr = $this->setAttr($info['category'], $key);
  289. $attr_data = $attr->one($info['id']);
  290. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  291. $info['attr'] = $info['sell_attr'] = array();
  292. if ($attr->config['attr']) {
  293. foreach ($attr->config['attr'] as $k => $v) {
  294. if (isset($v[$key]) && $v[$key] > 0) {
  295. if (isset($attr_data['attr_' . $v['id']])) {
  296. $v['value'] = $attr_data['attr_' . $v['id']];
  297. } else {
  298. continue;
  299. $v['value'] = '';
  300. }
  301. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  302. if ($v['ename'] == 'price') {
  303. $info['price'] = $v['value_string'];
  304. } else {
  305. $v['bg'] = 'background-image: url('.$v['icon'].')';
  306. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  307. if ($is_sell > 0) {
  308. if ($is_sell == $v['is_sell']) {
  309. $info['attr'][] = $v;
  310. }
  311. } else {
  312. $info['attr'][] = $v;
  313. # 设置sku
  314. $this->sku($v, $info);
  315. }
  316. }
  317. }
  318. }
  319. }
  320. } else {
  321. $price = $info['price'];
  322. $s_price = $info['s_price'];
  323. $info['price'] = array();
  324. $info['price']['list'] = array();
  325. $info['price']['min'] = array
  326. (
  327. 'price' => $price,
  328. 's_price' => $s_price
  329. );
  330. $info['price']['value'] = $price;
  331. $info['price']['max'] = array();
  332. }
  333. //$info['price']['value'] = $info['price']['min']['price'];
  334. if (isset($info['cdate']) && $info['cdate']) {
  335. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  336. }
  337. $info = $this->getInfoLink($info);
  338. //$info['freight'] = 0;
  339. unset($info['f_price']);
  340. unset($info['cdate']);
  341. unset($info['udate']);
  342. unset($info['category']);
  343. unset($info['top_category_id']);
  344. unset($info['second_category_id']);
  345. unset($info['category_id']);
  346. unset($info['area']);
  347. unset($info['mode']);
  348. unset($info['top']);
  349. unset($info['youhui']);
  350. unset($info['status']);
  351. unset($info['reorder']);
  352. unset($info['state']);
  353. return $info;
  354. }
  355. # 佣金计算
  356. private function reward($info, $price)
  357. {
  358. if ($info['reward_value'] && $info['reward_value'] > 0) {
  359. if ($info['reward_type'] == 2) {
  360. $info['reward_value'] = round(($info['reward_value']/100) * $price, 2);
  361. }
  362. return $info['reward_value'];
  363. }
  364. $array = explode(',', $info['category']);
  365. $cate = $reward = array();
  366. $total = count($array);
  367. $total -= 1;
  368. foreach ($array as $k => $v) {
  369. $cate[$k] = $v;
  370. if ($k < $total) {
  371. $cate[] = '-1';
  372. }
  373. $where['category'] = $cate;
  374. $data = Dever::db('goods/reward')->one($where);
  375. if ($data) {
  376. $reward = $data;
  377. }
  378. }
  379. if ($reward) {
  380. if ($reward['value'] && $reward['value'] > 0) {
  381. if ($reward['type'] == 2) {
  382. $reward['value'] = round(($reward['value']/100) * $price, 2);
  383. }
  384. return $reward['value'];
  385. }
  386. }
  387. return 0;
  388. }
  389. # 运费计算,暂时用省份代表地区
  390. private function freight($info, $goods_price, $user = array(), $num = 1)
  391. {
  392. $array = explode(',', $info['category']);
  393. $cate = $freight = array();
  394. $total = count($array);
  395. $total -= 1;
  396. if ($info && $info['goods_area']) {
  397. $where['goods_area'] = $info['goods_area'];
  398. }
  399. # 用户所在地理位置
  400. if ($user && isset($user['area_id']) && $user['area_id']) {
  401. $area = explode(',', $user['area_id']);
  402. $where['area'] = $area[0];
  403. }
  404. foreach ($array as $k => $v) {
  405. $cate[$k] = $v;
  406. if ($k < $total) {
  407. $cate[] = '-1';
  408. }
  409. $where['category'] = $cate;
  410. $data = Dever::db('goods/freight')->one($where);
  411. if ($data) {
  412. $freight = $data;
  413. }
  414. }
  415. if ($freight) {
  416. $price = $freight['first_price'];
  417. if ($num > $freight['first_num']) {
  418. $num = $num - $freight['first_num'];
  419. $price = $freight['first_price'] + ($freight['next_price']*$num);
  420. }
  421. if ($freight['type'] == 2) {
  422. $freight['price'] = round($goods_price*$num*($price/100), 2);
  423. } else {
  424. $freight['price'] = $price;
  425. }
  426. }
  427. return $freight;
  428. }
  429. private function sku($data, &$info)
  430. {
  431. # 获取销售属性
  432. if ($data['is_sell'] == 2) {
  433. if ($data['option']) {
  434. $value = explode(',', $data['value']);
  435. foreach($data['option'] as $k => $v) {
  436. if (in_array($v['id'], $value)) {
  437. $data['option_sku'][] = array
  438. (
  439. 'id' => $v['id'],
  440. 'info_id' => $v['info_id'],
  441. 'name' => $v['name'],
  442. 'icon' => $v['icon'],
  443. 'value' => $v['info_id'] . '-' . $v['id'],
  444. );
  445. }
  446. }
  447. unset($data['option']);
  448. }
  449. $info['sell_attr'][] = $data;
  450. # 获取最便宜的价格
  451. $where['info_id'] = $info['id'];
  452. $info['price'] = array();
  453. $info['price']['list'] = Dever::db('goods/info_sku')->getData($where);
  454. $info['price']['min'] = Dever::db('goods/info_sku')->getMinOne($where);
  455. $info['price']['max'] = Dever::db('goods/info_sku')->getMaxOne($where);
  456. }
  457. }
  458. public function getInfoLink($info)
  459. {
  460. if (isset($this->view_path)) {
  461. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  462. $info['view_link'] = Dever::url($info['view_path']);
  463. }
  464. return $info;
  465. }
  466. public function setViewPath($path)
  467. {
  468. $this->view_path = $path;
  469. }
  470. }