Info.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 `info_goods_'.$attr->config['name'].'` as t_1 Left Join `info_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. public function getPayState($uid, $id, $category)
  134. {
  135. $pay_where['category'] = $category;
  136. $pay = Dever::db('goods/pay')->one($pay_where);
  137. $pay_state = 0;
  138. if ($pay && $pay['num'] > 0) {
  139. $pay_state = $pay['num'];
  140. } elseif (strstr($category, ',')) {
  141. $category = explode(',', $category);
  142. $category = array_pop($category);
  143. $pay_state = $this->getPayState($uid, $id, $category);
  144. }
  145. //$update['where_id'] = $id;
  146. //$update['poster_pay_type'] = $pay_state;
  147. //Dever::db($this->table_info)->update($update);
  148. return $pay_state;
  149. }
  150. # 更新需求
  151. public function update($top, $cate, $data, $id, $uid)
  152. {
  153. if ($cate) {
  154. $category = $top . ',' . $cate;
  155. } else {
  156. $category = $top;
  157. }
  158. $attr = $this->setAttr($category);
  159. $attr_update = Dever::preInput('attr_');
  160. if ($attr_update) {
  161. foreach ($attr_update as $k => $v) {
  162. if (strstr($k, '_s')) {
  163. $k = str_replace('_s', '', $k);
  164. $attr_update[$k] = $v;
  165. }
  166. if (strstr($k, '_e')) {
  167. $k = str_replace('_e', '', $k);
  168. $attr_update[$k] .= ',' . $v;
  169. }
  170. }
  171. }
  172. $update = array();
  173. $update['name'] = $data['name'];
  174. $update['category'] = $category;
  175. $update['pic'] = $data['pic'];
  176. //$update['pic_cover'] = $data['pic'];
  177. $update['poster_uid'] = $uid;
  178. if (isset($data['view_price']) && $data['view_price']) {
  179. $update['view_price'] = $data['view_price'];
  180. }
  181. if ($uid && $id) {
  182. # 此处要判断是用户自己的
  183. $info = Dever::db($this->table_info)->one($id);
  184. /*
  185. if ($uid == $info['poster_uid']) {
  186. Dever::alert('错误的用户参数');
  187. }
  188. */
  189. $update['where_id'] = $attr_update['where_id'] = $id;
  190. Dever::db($this->table_info)->update($update);
  191. Dever::db($this->table_attr)->update($attr_update);
  192. $pay = 0;
  193. } else {
  194. $id = Dever::db($this->table_info)->insert($update);
  195. $attr_update['category'] = $update['category'];
  196. $attr_update['info_id'] = $id;
  197. $attr_update['id'] = $id;
  198. Dever::db($this->table_attr)->insert($attr_update);
  199. # 验证是否需要支付,根据category判断
  200. $pay = false;
  201. //$pay = $this->getPayState($uid, $id, $category);
  202. $info = Dever::db($this->table_info)->one($id);
  203. $info = $this->getInfoLink($info);
  204. }
  205. return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
  206. }
  207. # 获取需求基本信息
  208. public function getInfo($id, $attr = true, $order = 'view_reorder')
  209. {
  210. $info = Dever::db($this->table_info)->one($id);
  211. if ($info && $attr) {
  212. $info = $this->info($info, $order);
  213. }
  214. if ($info['pic']) {
  215. $info['pic'] = explode(',', $info['pic']);
  216. }
  217. return $info;
  218. }
  219. # 获取基本信息
  220. public function info($info, $key = 'list_reorder', $is_sell = false)
  221. {
  222. # 销量
  223. $info['sell_num'] = $info['sell_num'] + $info['sell_add_num'];
  224. # 获取运费
  225. $info['freight'] = $this->freight($info);
  226. $attr = $this->setAttr($info['category'], $key);
  227. $attr_data = $attr->one($info['id']);
  228. $info['category_array'] = Dever::load('category/api')->string($info['category']);
  229. $info['attr'] = $info['sell_attr'] = array();
  230. if ($attr->config['attr']) {
  231. foreach ($attr->config['attr'] as $k => $v) {
  232. if (isset($v[$key]) && $v[$key] > 0) {
  233. if (isset($attr_data['attr_' . $v['id']])) {
  234. $v['value'] = $attr_data['attr_' . $v['id']];
  235. } else {
  236. continue;
  237. $v['value'] = '';
  238. }
  239. $v['value_string'] = Dever::load('attr/api')->getValue($v);
  240. if ($v['ename'] == 'price') {
  241. $info['price'] = $v['value_string'];
  242. } else {
  243. $v['bg'] = 'background-image: url('.$v['icon'].')';
  244. $v['pic'] = '<img src="'.$v['icon'].'" width="18" />';
  245. if ($is_sell > 0) {
  246. if ($is_sell == $v['is_sell']) {
  247. $info['attr'][] = $v;
  248. }
  249. } else {
  250. $info['attr'][] = $v;
  251. # 设置sku
  252. $this->sku($v, $info);
  253. }
  254. }
  255. }
  256. }
  257. }
  258. if (isset($info['cdate']) && $info['cdate']) {
  259. $info['cdate_string'] = Dever::mdate($info['cdate'], 2);
  260. }
  261. $info = $this->getInfoLink($info);
  262. return $info;
  263. }
  264. private function freight($info)
  265. {
  266. # 之后要加入当前用户的地理位置
  267. $array = explode(',', $info['category']);
  268. $cate = $freight = array();
  269. $total = count($array);
  270. $total -= 1;
  271. foreach ($array as $k => $v) {
  272. $cate[$k] = $v;
  273. if ($k < $total) {
  274. $cate[] = '-1';
  275. }
  276. $where['category'] = $cate;
  277. $data = Dever::db('goods/freight')->one($where);
  278. if ($data) {
  279. $freight = $data;
  280. }
  281. }
  282. return $freight;
  283. }
  284. private function sku($data, &$info)
  285. {
  286. # 获取销售属性
  287. if ($data['is_sell'] == 2) {
  288. if ($data['option']) {
  289. $value = explode(',', $data['value']);
  290. foreach($data['option'] as $k => $v) {
  291. if (in_array($v['id'], $value)) {
  292. $data['option_sku'][] = array
  293. (
  294. 'id' => $v['id'],
  295. 'info_id' => $v['info_id'],
  296. 'name' => $v['name'],
  297. 'icon' => $v['icon'],
  298. );
  299. }
  300. }
  301. }
  302. $info['sell_attr'][] = $data;
  303. # 获取最便宜的价格
  304. $where['info_id'] = $info['id'];
  305. $info['price']['min'] = Dever::db('goods/info_sku')->getMinOne($where);
  306. $info['price']['max'] = Dever::db('goods/info_sku')->getMaxOne($where);
  307. }
  308. }
  309. public function getInfoLink($info)
  310. {
  311. if (isset($this->view_path)) {
  312. $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id'];
  313. $info['view_link'] = Dever::url($info['view_path']);
  314. }
  315. return $info;
  316. }
  317. public function setViewPath($path)
  318. {
  319. $this->view_path = $path;
  320. }
  321. }