Flea.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class Flea extends BaseModel
  16. {
  17. public $page_info;
  18. /**
  19. * 商品保存
  20. * @access public
  21. * @author csdeshang
  22. * @param array $data 商品资料
  23. * @return boolean
  24. */
  25. public function addFlea($data)
  26. {
  27. if (empty($data)) {
  28. return false;
  29. }
  30. $goods_array = array();
  31. $goods_array['goods_name'] = $data['goods_name'];
  32. $goods_array['fleaclass_id'] = $data['fleaclass_id'];
  33. $goods_array['fleaclass_name'] = $data['fleaclass_name'];
  34. $goods_array['member_id'] = $data['member_id'];
  35. $goods_array['member_name'] = $data['member_name'];
  36. //$goods_array['goods_image'] = $data['goods_image'];
  37. $goods_array['flea_quality'] = $data['flea_quality'];
  38. $goods_array['fleaarea_id'] = $data['fleaarea_id'];
  39. $goods_array['fleaarea_name'] = $data['fleaarea_name'];
  40. $goods_array['flea_pname'] = $data['flea_pname'];
  41. $goods_array['flea_pphone'] = $data['flea_pphone'];
  42. $goods_array['goods_tag'] = $data['goods_tag'];
  43. $goods_array['goods_price'] = $data['goods_price'];
  44. $goods_array['goods_store_price'] = $data['goods_store_price'];
  45. $goods_array['goods_show'] = $data['goods_show'];
  46. //$goods_array['goods_commend'] = $data['goods_commend'];
  47. $goods_array['goods_addtime'] = TIMESTAMP;
  48. $goods_array['goods_body'] = $data['goods_body'];
  49. $goods_array['goods_keywords'] = $data['goods_keywords'];
  50. $goods_array['goods_description'] = $data['goods_description'];
  51. $result = Db::name('flea')->insertGetId($goods_array);
  52. return $result;
  53. }
  54. /**
  55. * 获取单个闲置
  56. * @access public
  57. * @author csdeshang
  58. * @param type $condition 查询条件
  59. * @return type
  60. */
  61. public function getOneFlea($condition){
  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. if (empty($data)) {
  160. return false;
  161. }
  162. $update = Db::name('flea')->where($condition)->update($data);
  163. return $update;
  164. }
  165. /**
  166. * 闲置物品数量
  167. * @access public
  168. * @author csdeshang
  169. * @param type $condition 条件
  170. * @return int
  171. */
  172. public function getFleaCount($condition)
  173. {
  174. if (empty($condition)) {
  175. return false;
  176. }
  177. $count = Db::name('flea')->where($condition)->count();
  178. return $count;
  179. }
  180. /**
  181. * 闲置物品删除
  182. * @access public
  183. * @author csdeshang
  184. * @param type $goods_id 商品id
  185. * @return boolean
  186. */
  187. public function delFlea($goods_id)
  188. {
  189. if (empty($goods_id)) {
  190. return false;
  191. }
  192. if(is_array($goods_id)){
  193. $del_state = Db::name('flea')->where('goods_id','in', $goods_id)->delete();
  194. }else{
  195. $del_state = Db::name('flea')->where('goods_id', $goods_id)->delete();
  196. }
  197. if ($del_state) {
  198. $image_more = Db::name('fleaupload')->field('fleafile_name')->whereIn('item_id', $goods_id)->whereIn('fleaupload_type', '12,13')->select()->toArray();
  199. if (is_array($image_more) && !empty($image_more)) {
  200. foreach ($image_more as $v) {
  201. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_MFLEA . DIRECTORY_SEPARATOR . $v['store_id'] . DIRECTORY_SEPARATOR . $v['fleafile_name']);
  202. }
  203. }
  204. Db::name('fleaupload')->whereIn('item_id', $goods_id)->whereIn('fleaupload_type', '12,13')->select()->toArray();
  205. }
  206. return true;
  207. }
  208. /**
  209. * 按所属分类查找闲置物品
  210. * @access public
  211. * @author csdeshang
  212. * @param type $condition 条件
  213. * @return type
  214. */
  215. public function getFleaByClass($condition,$field='*',$order='',$limit=10)
  216. {
  217. $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();
  218. return $goods_array;
  219. }
  220. /**
  221. * 将条件数组组合为SQL语句的条件部分
  222. * @access public
  223. * @author csdeshang
  224. * @param type $condition_array 条件数组
  225. * @return string
  226. */
  227. private function getCondition($condition_array)
  228. {
  229. $condition_sql = array();
  230. if (isset($condition_array['member_id']) && $condition_array['member_id'] != '') {
  231. $condition_sql[]=array('member_id','=',$condition_array['member_id']);
  232. }
  233. if (isset($condition_array['image_store_id']) && $condition_array['image_store_id'] != '') {
  234. $condition_sql[]=array('store_id','=',$condition_array['image_store_id']);
  235. $condition_sql[]=array('item_id','=',$condition_array['item_id']);
  236. $condition_sql[]=array('fleaupload_type','=',$condition_array['image_type']);
  237. }
  238. //添加不等于某商品的条件
  239. if (isset($condition_array['goods_id_diff']) && $condition_array['goods_id_diff'] != 0) {
  240. $condition_sql[]=array('goods_id','=',$condition_array['goods_id_diff']);
  241. }
  242. if (isset($condition_array['fleaclass_id_list']) && $condition_array['fleaclass_id_list'] != '') {
  243. $condition_sql[]=array('flea.fleaclass_id','in',ltrim($condition_array['fleaclass_id_list'], ','));
  244. }
  245. if (isset($condition_array['goods_id']) && $condition_array['goods_id'] != 0) {
  246. $condition_sql[]=array('goods_id','=',$condition_array['goods_id']);
  247. }
  248. if (isset($condition_array['keyword']) && $condition_array['keyword'] != '') {
  249. $condition_sql[]=array('goods_name','LIKE',"%".$condition_array['keyword']."%");
  250. }
  251. if (isset($condition_array['upload_id']) && $condition_array['upload_id'] != '') {
  252. $condition_sql[]=array('fleaupload_id','=',$condition_array['upload_id']);
  253. }
  254. if (isset($condition_array['goods_id_in'])) {
  255. if ($condition_array['goods_id_in'] == '') {
  256. $condition_sql[]=array('flea.goods_id','in','');
  257. }
  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. }
  269. else {
  270. $condition_sql[]=array('flea.fleaclass_id','in',$condition_array['fleaclass_id_in']);
  271. }
  272. }
  273. if (isset($condition_array['key_input']) && $condition_array['key_input'] != '') {
  274. $condition_sql[]=array('goods_name|goods_tag','LIKE',"%".$condition_array['key_input']."%");
  275. }
  276. if (isset($condition_array['like_member_name']) && $condition_array['like_member_name'] != '') {
  277. $condition_sql[]=array('member_name','LIKE',"%".$condition_array['like_member_name']."%");
  278. }
  279. /* 检索 */
  280. if (isset($condition_array['pic_input']) && $condition_array['pic_input'] == 2) {
  281. $condition_sql[]=array('goods_image','<>','');
  282. }
  283. if (isset($condition_array['body_input']) && $condition_array['body_input'] == 2) {
  284. $condition_sql[]=array('goods_body','<>','');
  285. }
  286. if (isset($condition_array['seller_input']) && $condition_array['seller_input'] != '') {
  287. $condition_sql[]=array('member_id','=',$condition_array['seller_input']);
  288. }
  289. if (isset($condition_array['quality_input']) && $condition_array['quality_input'] != '') {
  290. if ($condition_array['quality_input'] == 7) {
  291. $condition_sql[]=array('flea_quality','<=',7);
  292. }
  293. else {
  294. $condition_sql[]=array('flea_quality','>=',$condition_array['quality_input']);
  295. }
  296. }
  297. if (isset($condition_array['start_input']) && $condition_array['start_input'] != '') {
  298. $condition_sql[]=array('goods_store_price','>=',$condition_array['start_input']);
  299. }
  300. if (isset($condition_array['end_input']) && $condition_array['end_input'] != '') {
  301. $condition_sql[]=array('goods_store_price','<=',$condition_array['end_input']);
  302. }
  303. if (isset($condition_array['areaid']) && $condition_array['areaid'] != '') {
  304. $condition_sql[]=array('fleaarea_id','in',$condition_array['areaid']);
  305. }
  306. return $condition_sql;
  307. }
  308. /**
  309. * 递归得到商品分类的ID
  310. * @access public
  311. * @author csdeshang
  312. * @staticvar string $class_list
  313. * @param type $class_id 分类ID
  314. * @return type
  315. */
  316. private function _getRecursiveClass($class_id)
  317. {
  318. static $class_list = '';
  319. $id = implode(',', $class_id);
  320. $class_list .= ',' . $id;
  321. $temp_list = Db::name('fleaclass')->where('fleaclass_parent_id','in',$id)->field('fleaclass_id')->select()->toArray();
  322. if (!empty($temp_list)) {
  323. $_tmp = array(); //取得ID组成的一维数组
  324. foreach ($temp_list as $key => $val) {
  325. $_tmp[] = $val['fleaclass_id'];
  326. }
  327. unset($temp_list);
  328. $temp_list = $_tmp;
  329. $id = $this->_getRecursiveClass($temp_list);
  330. }
  331. return trim($class_list, ',');
  332. }
  333. }