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