Flea.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 数据层模型
  15. */
  16. class Flea extends BaseModel
  17. {
  18. public $page_info;
  19. /**
  20. * 商品保存
  21. * @access public
  22. * @author csdeshang
  23. * @param array $data 商品资料
  24. * @return boolean
  25. */
  26. public function addFlea($data)
  27. {
  28. if (empty($data)) {
  29. return false;
  30. }
  31. $goods_array = array();
  32. $goods_array['goods_name'] = $data['goods_name'];
  33. $goods_array['fleaclass_id'] = $data['fleaclass_id'];
  34. $goods_array['fleaclass_name'] = $data['fleaclass_name'];
  35. $goods_array['member_id'] = $data['member_id'];
  36. $goods_array['member_name'] = $data['member_name'];
  37. //$goods_array['goods_image'] = $data['goods_image'];
  38. $goods_array['flea_quality'] = $data['flea_quality'];
  39. $goods_array['fleaarea_id'] = $data['fleaarea_id'];
  40. $goods_array['fleaarea_name'] = $data['fleaarea_name'];
  41. $goods_array['flea_pname'] = $data['flea_pname'];
  42. $goods_array['flea_pphone'] = $data['flea_pphone'];
  43. $goods_array['goods_tag'] = $data['goods_tag'];
  44. $goods_array['goods_price'] = $data['goods_price'];
  45. $goods_array['goods_store_price'] = $data['goods_store_price'];
  46. $goods_array['goods_show'] = $data['goods_show'];
  47. //$goods_array['goods_commend'] = $data['goods_commend'];
  48. $goods_array['goods_addtime'] = TIMESTAMP;
  49. $goods_array['goods_body'] = $data['goods_body'];
  50. $goods_array['goods_keywords'] = $data['goods_keywords'];
  51. $goods_array['goods_description'] = $data['goods_description'];
  52. $result = Db::name('flea')->insertGetId($goods_array);
  53. return $result;
  54. }
  55. /**
  56. * 获取单个闲置
  57. * @access public
  58. * @author csdeshang
  59. * @param type $condition 查询条件
  60. * @return type
  61. */
  62. public function getOneFlea($condition){
  63. return Db::name('flea')->where($condition)->find();
  64. }
  65. /**
  66. * 商品列表
  67. * @access public
  68. * @author csdeshang
  69. * @param type $special_condition 查询条件
  70. * @param type $pagesize 分页信息
  71. * @param type $field 字段
  72. * @return type
  73. */
  74. public function getFleaList($special_condition, $pagesize = '', $field = '*',$order = 'goods_id desc',$limit=0)
  75. {
  76. $where = $this->getCondition($special_condition);
  77. if ($pagesize) {
  78. $res = Db::name('flea')->alias('flea')->where($where)->field($field)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  79. $this->page_info = $res;
  80. $list_goods = $res->items();
  81. }else{
  82. $list_goods = Db::name('flea')->alias('flea')->where($where)->field($field)->order($order)->limit($limit)->select()->toArray();
  83. }
  84. return $list_goods;
  85. }
  86. /**
  87. * 他们正在卖的
  88. * @access public
  89. * @author csdeshang
  90. * @param type $condition 条件
  91. * @param type $limit 分页信息
  92. * @param type $field 字段
  93. * @return type
  94. */
  95. public function getSaleFleaList($condition, $limit = '10', $field = 'member.member_id,member.member_name,flea.*')
  96. {
  97. $order = 'goods_id desc';
  98. $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();
  99. return $list_goods;
  100. }
  101. /**
  102. * 他们正在统计当前卖家正在出售闲置个数卖的
  103. * @access public
  104. * @author csdeshang
  105. * @param type $member_id 会员id
  106. * @return type
  107. */
  108. public function getFleaStatistic($member_id)
  109. {
  110. $field = 'member.member_avatar,member.member_qq,member.member_id,member.member_name,count(*) as num';
  111. $group = 'member.member_id';
  112. $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();
  113. return $goods_array['0'];
  114. }
  115. /**
  116. * 闲置物品多图
  117. * @access public
  118. * @author csdeshang
  119. * @param array $condition 列表条件
  120. * @param array $pagesize 分页页数
  121. * @return type
  122. */
  123. public function getFleauploadList($condition,$pagesize='')
  124. {
  125. if($pagesize){
  126. $member_list = Db::name('fleaupload')->where($condition)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  127. $this->page_info = $member_list;
  128. $result= $member_list->items();
  129. }else{
  130. $result = Db::name('fleaupload')->where($condition)->select()->toArray();
  131. }
  132. return $result;
  133. }
  134. /**
  135. * 得到商品所有缩略图,带商品路径
  136. * @access public
  137. * @author csdeshang
  138. * @param type $goods 商品列表
  139. * @param type $path 商品路径
  140. * @return type
  141. */
  142. public function getThumb(&$goods, $path)
  143. {
  144. if (is_array($goods)) {
  145. foreach ($goods as $k => $v) {
  146. $goods[$k]['thumb_small'] = ds_get_pic($path , $v['fleafile_name']);
  147. $goods[$k]['thumb_big'] = ds_get_pic($path , str_replace('_small', '_big', $v['fleafile_name']));
  148. }
  149. }
  150. }
  151. /**
  152. * 商品信息更新
  153. * @access public
  154. * @author csdeshang
  155. * @param type $data 数据
  156. * @param type $goods_id 商品id
  157. * @return boolean
  158. */
  159. public function editFlea($data, $condition) {
  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. }
  259. else {
  260. $condition_sql[]=array('flea.goods_id','in',$condition_array['goods_id_in']);
  261. }
  262. }
  263. if (isset($condition_array['fleaclass_id']) && $condition_array['fleaclass_id'] != '') {
  264. $condition_sql[]=array('fleaclass_id','in',$this->_getRecursiveClass(array($condition_array['fleaclass_id'])));
  265. }
  266. if (isset($condition_array['fleaclass_id_in'])) {
  267. if ($condition_array['fleaclass_id_in'] == '') {
  268. $condition_sql[]=array('flea.fleaclass_id','in','');
  269. }
  270. else {
  271. $condition_sql[]=array('flea.fleaclass_id','in',$condition_array['fleaclass_id_in']);
  272. }
  273. }
  274. if (isset($condition_array['key_input']) && $condition_array['key_input'] != '') {
  275. $condition_sql[]=array('goods_name|goods_tag','LIKE',"%".$condition_array['key_input']."%");
  276. }
  277. if (isset($condition_array['like_member_name']) && $condition_array['like_member_name'] != '') {
  278. $condition_sql[]=array('member_name','LIKE',"%".$condition_array['like_member_name']."%");
  279. }
  280. /* 检索 */
  281. if (isset($condition_array['pic_input']) && $condition_array['pic_input'] == 2) {
  282. $condition_sql[]=array('goods_image','<>','');
  283. }
  284. if (isset($condition_array['body_input']) && $condition_array['body_input'] == 2) {
  285. $condition_sql[]=array('goods_body','<>','');
  286. }
  287. if (isset($condition_array['seller_input']) && $condition_array['seller_input'] != '') {
  288. $condition_sql[]=array('member_id','=',$condition_array['seller_input']);
  289. }
  290. if (isset($condition_array['quality_input']) && $condition_array['quality_input'] != '') {
  291. if ($condition_array['quality_input'] == 7) {
  292. $condition_sql[]=array('flea_quality','<=',7);
  293. }
  294. else {
  295. $condition_sql[]=array('flea_quality','>=',$condition_array['quality_input']);
  296. }
  297. }
  298. if (isset($condition_array['start_input']) && $condition_array['start_input'] != '') {
  299. $condition_sql[]=array('goods_store_price','>=',$condition_array['start_input']);
  300. }
  301. if (isset($condition_array['end_input']) && $condition_array['end_input'] != '') {
  302. $condition_sql[]=array('goods_store_price','<=',$condition_array['end_input']);
  303. }
  304. if (isset($condition_array['areaid']) && $condition_array['areaid'] != '') {
  305. $condition_sql[]=array('fleaarea_id','in',$condition_array['areaid']);
  306. }
  307. return $condition_sql;
  308. }
  309. /**
  310. * 递归得到商品分类的ID
  311. * @access public
  312. * @author csdeshang
  313. * @staticvar string $class_list
  314. * @param type $class_id 分类ID
  315. * @return type
  316. */
  317. private function _getRecursiveClass($class_id)
  318. {
  319. static $class_list = '';
  320. $id = implode(',', $class_id);
  321. $class_list .= ',' . $id;
  322. $temp_list = Db::name('fleaclass')->where('fleaclass_parent_id','in',$id)->field('fleaclass_id')->select()->toArray();
  323. if (!empty($temp_list)) {
  324. $_tmp = array(); //取得ID组成的一维数组
  325. foreach ($temp_list as $key => $val) {
  326. $_tmp[] = $val['fleaclass_id'];
  327. }
  328. unset($temp_list);
  329. $temp_list = $_tmp;
  330. $id = $this->_getRecursiveClass($temp_list);
  331. }
  332. return trim($class_list, ',');
  333. }
  334. }