Flea.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Flea extends BaseModel
  12. {
  13. public $page_info;
  14. /**
  15. * 商品保存
  16. * @access public
  17. * @author csdeshang
  18. * @param array $data 商品资料
  19. * @return boolean
  20. */
  21. public function addFlea($data)
  22. {
  23. if (empty($data)) {
  24. return false;
  25. }
  26. $goods_array = array();
  27. $goods_array['goods_name'] = $data['goods_name'];
  28. $goods_array['fleaclass_id'] = $data['fleaclass_id'];
  29. $goods_array['fleaclass_name'] = $data['fleaclass_name'];
  30. $goods_array['member_id'] = $data['member_id'];
  31. $goods_array['member_name'] = $data['member_name'];
  32. //$goods_array['goods_image'] = $data['goods_image'];
  33. $goods_array['flea_quality'] = $data['flea_quality'];
  34. $goods_array['fleaarea_id'] = $data['fleaarea_id'];
  35. $goods_array['fleaarea_name'] = $data['fleaarea_name'];
  36. $goods_array['flea_pname'] = $data['flea_pname'];
  37. $goods_array['flea_pphone'] = $data['flea_pphone'];
  38. $goods_array['goods_tag'] = $data['goods_tag'];
  39. $goods_array['goods_price'] = $data['goods_price'];
  40. $goods_array['goods_store_price'] = $data['goods_store_price'];
  41. $goods_array['goods_show'] = $data['goods_show'];
  42. //$goods_array['goods_commend'] = $data['goods_commend'];
  43. $goods_array['goods_addtime'] = TIMESTAMP;
  44. $goods_array['goods_body'] = $data['goods_body'];
  45. $goods_array['goods_keywords'] = $data['goods_keywords'];
  46. $goods_array['goods_description'] = $data['goods_description'];
  47. $result = Db::name('flea')->insertGetId($goods_array);
  48. return $result;
  49. }
  50. /**
  51. * 获取单个闲置
  52. * @access public
  53. * @author csdeshang
  54. * @param type $condition 查询条件
  55. * @return type
  56. */
  57. public function getOneFlea($condition)
  58. {
  59. return Db::name('flea')->where($condition)->find();
  60. }
  61. /**
  62. * 商品列表
  63. * @access public
  64. * @author csdeshang
  65. * @param type $special_condition 查询条件
  66. * @param type $pagesize 分页信息
  67. * @param type $field 字段
  68. * @return type
  69. */
  70. public function getFleaList($special_condition, $pagesize = '', $field = '*', $order = 'goods_id desc', $limit = 0)
  71. {
  72. $where = $this->getCondition($special_condition);
  73. if ($pagesize) {
  74. $res = Db::name('flea')->alias('flea')->where($where)->field($field)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  75. $this->page_info = $res;
  76. $list_goods = $res->items();
  77. } else {
  78. $list_goods = Db::name('flea')->alias('flea')->where($where)->field($field)->order($order)->limit($limit)->select()->toArray();
  79. }
  80. return $list_goods;
  81. }
  82. /**
  83. * 他们正在卖的
  84. * @access public
  85. * @author csdeshang
  86. * @param type $condition 条件
  87. * @param type $limit 分页信息
  88. * @param type $field 字段
  89. * @return type
  90. */
  91. public function getSaleFleaList($condition, $limit = '10', $field = 'member.member_id,member.member_name,flea.*')
  92. {
  93. $order = 'goods_id desc';
  94. $list_goods = Db::name('flea')->alias('flea')->join('member member', 'flea.member_id=member.member_id', 'LEFT')->field($field)->order($order)->where($condition)->limit($limit)->select()->toArray();
  95. return $list_goods;
  96. }
  97. /**
  98. * 他们正在统计当前卖家正在出售闲置个数卖的
  99. * @access public
  100. * @author csdeshang
  101. * @param type $member_id 会员id
  102. * @return type
  103. */
  104. public function getFleaStatistic($member_id)
  105. {
  106. $field = 'member.member_avatar,member.member_qq,member.member_id,member.member_name,count(*) as num';
  107. $group = 'member.member_id';
  108. $goods_array = Db::name('flea')->alias('flea')->join('member member', 'flea.member_id=member.member_id', 'LEFT')->field($field)->where('member.member_id', $member_id)->group($group)->select()->toArray();
  109. return $goods_array['0'];
  110. }
  111. /**
  112. * 闲置物品多图
  113. * @access public
  114. * @author csdeshang
  115. * @param array $condition 列表条件
  116. * @param array $pagesize 分页页数
  117. * @return type
  118. */
  119. public function getFleauploadList($condition, $pagesize = '')
  120. {
  121. if ($pagesize) {
  122. $member_list = Db::name('fleaupload')->where($condition)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  123. $this->page_info = $member_list;
  124. $result = $member_list->items();
  125. } else {
  126. $result = Db::name('fleaupload')->where($condition)->select()->toArray();
  127. }
  128. return $result;
  129. }
  130. /**
  131. * 得到商品所有缩略图,带商品路径
  132. * @access public
  133. * @author csdeshang
  134. * @param type $goods 商品列表
  135. * @param type $path 商品路径
  136. * @return type
  137. */
  138. public function getThumb(&$goods, $path)
  139. {
  140. if (is_array($goods)) {
  141. foreach ($goods as $k => $v) {
  142. $goods[$k]['thumb_small'] = ds_get_pic($path, $v['fleafile_name']);
  143. $goods[$k]['thumb_big'] = ds_get_pic($path, str_replace('_small', '_big', $v['fleafile_name']));
  144. }
  145. }
  146. }
  147. /**
  148. * 商品信息更新
  149. * @access public
  150. * @author csdeshang
  151. * @param type $data 数据
  152. * @param type $goods_id 商品id
  153. * @return boolean
  154. */
  155. public function editFlea($data, $condition)
  156. {
  157. if (empty($data)) {
  158. return false;
  159. }
  160. $update = Db::name('flea')->where($condition)->update($data);
  161. return $update;
  162. }
  163. /**
  164. * 闲置物品数量
  165. * @access public
  166. * @author csdeshang
  167. * @param type $condition 条件
  168. * @return int
  169. */
  170. public function getFleaCount($condition)
  171. {
  172. if (empty($condition)) {
  173. return false;
  174. }
  175. $count = Db::name('flea')->where($condition)->count();
  176. return $count;
  177. }
  178. /**
  179. * 闲置物品删除
  180. * @access public
  181. * @author csdeshang
  182. * @param type $goods_id 商品id
  183. * @return boolean
  184. */
  185. public function delFlea($goods_id)
  186. {
  187. if (empty($goods_id)) {
  188. return false;
  189. }
  190. if (is_array($goods_id)) {
  191. $del_state = Db::name('flea')->where('goods_id', 'in', $goods_id)->delete();
  192. } else {
  193. $del_state = Db::name('flea')->where('goods_id', $goods_id)->delete();
  194. }
  195. if ($del_state) {
  196. $image_more = Db::name('fleaupload')->field('fleafile_name')->whereIn('item_id', $goods_id)->whereIn('fleaupload_type', '12,13')->select()->toArray();
  197. if (is_array($image_more) && !empty($image_more)) {
  198. foreach ($image_more as $v) {
  199. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_MFLEA . DIRECTORY_SEPARATOR . $v['store_id'] . DIRECTORY_SEPARATOR . $v['fleafile_name']);
  200. }
  201. }
  202. Db::name('fleaupload')->whereIn('item_id', $goods_id)->whereIn('fleaupload_type', '12,13')->select()->toArray();
  203. }
  204. return true;
  205. }
  206. /**
  207. * 按所属分类查找闲置物品
  208. * @access public
  209. * @author csdeshang
  210. * @param type $condition 条件
  211. * @return type
  212. */
  213. public function getFleaByClass($condition, $field = '*', $order = '', $limit = 10)
  214. {
  215. $goods_array = Db::name('flea')->alias('flea')->join('fleaclass fleaclass', 'flea.fleaclass_id=fleaclass.fleaclass_id')->where($condition)->field($field)->order($order)->limit($limit)->select()->toArray();
  216. return $goods_array;
  217. }
  218. /**
  219. * 将条件数组组合为SQL语句的条件部分
  220. * @access public
  221. * @author csdeshang
  222. * @param type $condition_array 条件数组
  223. * @return string
  224. */
  225. private function getCondition($condition_array)
  226. {
  227. $condition_sql = array();
  228. if (isset($condition_array['member_id']) && $condition_array['member_id'] != '') {
  229. $condition_sql[] = array('member_id', '=', $condition_array['member_id']);
  230. }
  231. if (isset($condition_array['image_store_id']) && $condition_array['image_store_id'] != '') {
  232. $condition_sql[] = array('store_id', '=', $condition_array['image_store_id']);
  233. $condition_sql[] = array('item_id', '=', $condition_array['item_id']);
  234. $condition_sql[] = array('fleaupload_type', '=', $condition_array['image_type']);
  235. }
  236. //添加不等于某商品的条件
  237. if (isset($condition_array['goods_id_diff']) && $condition_array['goods_id_diff'] != 0) {
  238. $condition_sql[] = array('goods_id', '=', $condition_array['goods_id_diff']);
  239. }
  240. if (isset($condition_array['fleaclass_id_list']) && $condition_array['fleaclass_id_list'] != '') {
  241. $condition_sql[] = array('flea.fleaclass_id', 'in', ltrim($condition_array['fleaclass_id_list'], ','));
  242. }
  243. if (isset($condition_array['goods_id']) && $condition_array['goods_id'] != 0) {
  244. $condition_sql[] = array('goods_id', '=', $condition_array['goods_id']);
  245. }
  246. if (isset($condition_array['keyword']) && $condition_array['keyword'] != '') {
  247. $condition_sql[] = array('goods_name', 'LIKE', "%" . $condition_array['keyword'] . "%");
  248. }
  249. if (isset($condition_array['upload_id']) && $condition_array['upload_id'] != '') {
  250. $condition_sql[] = array('fleaupload_id', '=', $condition_array['upload_id']);
  251. }
  252. if (isset($condition_array['goods_id_in'])) {
  253. if ($condition_array['goods_id_in'] == '') {
  254. $condition_sql[] = array('flea.goods_id', 'in', '');
  255. } else {
  256. $condition_sql[] = array('flea.goods_id', 'in', $condition_array['goods_id_in']);
  257. }
  258. }
  259. if (isset($condition_array['fleaclass_id']) && $condition_array['fleaclass_id'] != '') {
  260. $condition_sql[] = array('fleaclass_id', 'in', $this->_getRecursiveClass(array($condition_array['fleaclass_id'])));
  261. }
  262. if (isset($condition_array['fleaclass_id_in'])) {
  263. if ($condition_array['fleaclass_id_in'] == '') {
  264. $condition_sql[] = array('flea.fleaclass_id', 'in', '');
  265. } else {
  266. $condition_sql[] = array('flea.fleaclass_id', 'in', $condition_array['fleaclass_id_in']);
  267. }
  268. }
  269. if (isset($condition_array['key_input']) && $condition_array['key_input'] != '') {
  270. $condition_sql[] = array('goods_name|goods_tag', 'LIKE', "%" . $condition_array['key_input'] . "%");
  271. }
  272. if (isset($condition_array['like_member_name']) && $condition_array['like_member_name'] != '') {
  273. $condition_sql[] = array('member_name', 'LIKE', "%" . $condition_array['like_member_name'] . "%");
  274. }
  275. /* 检索 */
  276. if (isset($condition_array['pic_input']) && $condition_array['pic_input'] == 2) {
  277. $condition_sql[] = array('goods_image', '<>', '');
  278. }
  279. if (isset($condition_array['body_input']) && $condition_array['body_input'] == 2) {
  280. $condition_sql[] = array('goods_body', '<>', '');
  281. }
  282. if (isset($condition_array['seller_input']) && $condition_array['seller_input'] != '') {
  283. $condition_sql[] = array('member_id', '=', $condition_array['seller_input']);
  284. }
  285. if (isset($condition_array['quality_input']) && $condition_array['quality_input'] != '') {
  286. if ($condition_array['quality_input'] == 7) {
  287. $condition_sql[] = array('flea_quality', '<=', 7);
  288. } else {
  289. $condition_sql[] = array('flea_quality', '>=', $condition_array['quality_input']);
  290. }
  291. }
  292. if (isset($condition_array['start_input']) && $condition_array['start_input'] != '') {
  293. $condition_sql[] = array('goods_store_price', '>=', $condition_array['start_input']);
  294. }
  295. if (isset($condition_array['end_input']) && $condition_array['end_input'] != '') {
  296. $condition_sql[] = array('goods_store_price', '<=', $condition_array['end_input']);
  297. }
  298. if (isset($condition_array['areaid']) && $condition_array['areaid'] != '') {
  299. $condition_sql[] = array('fleaarea_id', 'in', $condition_array['areaid']);
  300. }
  301. return $condition_sql;
  302. }
  303. /**
  304. * 递归得到商品分类的ID
  305. * @access public
  306. * @author csdeshang
  307. * @staticvar string $class_list
  308. * @param type $class_id 分类ID
  309. * @return type
  310. */
  311. private function _getRecursiveClass($class_id)
  312. {
  313. static $class_list = '';
  314. $id = implode(',', $class_id);
  315. $class_list .= ',' . $id;
  316. $temp_list = Db::name('fleaclass')->where('fleaclass_parent_id', 'in', $id)->field('fleaclass_id')->select()->toArray();
  317. if (!empty($temp_list)) {
  318. $_tmp = array(); //取得ID组成的一维数组
  319. foreach ($temp_list as $key => $val) {
  320. $_tmp[] = $val['fleaclass_id'];
  321. }
  322. unset($temp_list);
  323. $temp_list = $_tmp;
  324. $id = $this->_getRecursiveClass($temp_list);
  325. }
  326. return trim($class_list, ',');
  327. }
  328. }