WxGoods.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class WxGoods extends Base
  5. {
  6. public function updates(){
  7. $data = request()->post();
  8. $data['org_id'] = cur_org_id();
  9. $result = validate('WxGoods')->check($data,[],'');
  10. if(true !== $result){
  11. $this->error = validate('WxGoods')->getError();
  12. return false;
  13. }
  14. if(!empty($data['imgs'])){
  15. $data['imgs']=implode(',',$data['imgs']);;
  16. }
  17. $cateInfo = Db::name('wx_goods_cate')
  18. ->where('id',$data['cate_id'])
  19. ->find();
  20. if($cateInfo['is_water']==1){
  21. if(empty($data['barrel_id'])){
  22. $this->error = '请选择空桶';
  23. return false;
  24. }
  25. }else{
  26. $data['barrel_id'] = 0;
  27. }
  28. $id = $data['id'];
  29. unset($data['id']);
  30. if($id > 0){
  31. $data['update_time'] = date('Y-m-d H:i:s');
  32. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  33. }else{
  34. $data['create_time'] = date('Y-m-d H:i:s');
  35. $ret = $this->allowField(true)->save($data);
  36. }
  37. if(!$ret){
  38. $this->error = '操作失败';
  39. return false;
  40. }
  41. return true;
  42. }
  43. public function cateGoodsList($userId,$orgId,$cateId,$title,$desc_name='sale',$desc='desc'){
  44. $map[] = ['org_id','=',$orgId];
  45. $map[] = ['enable','=',1];
  46. $map[] = ['del','=',0];
  47. $cateInfo = [];
  48. if($cateId > 0){
  49. $map[] = ['cate_id','=',$cateId];
  50. $cateInfo = Db::name('wx_goods_cate')
  51. ->field('id,title,is_water,path')
  52. ->where(['id'=>$cateId])->find();
  53. }
  54. if($title != ''){
  55. $map[] = ['title','like','%'.$title.'%'];
  56. }
  57. $ret=Db::name('wx_goods')
  58. ->field('id,title,price,img,price,label,dec')
  59. ->where($map)
  60. ->order([$desc_name=>$desc,'sort'=>'asc','id'=>'desc',])
  61. ->select();
  62. foreach ($ret as $k=>$v){
  63. $ret[$k]['label_name']=$v['label'] >0?Db::name('wx_goods_label')
  64. ->where('id',$v['label'])
  65. ->value('title'):'';
  66. $cart = Db::name('wx_goods_cart')
  67. ->where('user_id',$userId)
  68. ->where('goods_id',$v['id'])
  69. ->find();
  70. $ret[$k]['cart_num'] = $cart?$cart['nums']:0;
  71. $ret[$k]['cartId'] = $cart?$cart['id']:0;
  72. }
  73. $data =[
  74. 'list'=>$ret,
  75. 'cateInfo'=>$cateInfo,
  76. ];
  77. return $data;
  78. }
  79. public function details($id,$userId){
  80. $ret=Db::name('wx_goods')
  81. ->field('id,title,price,img,imgs,sale,content,label')
  82. ->where('id',$id)
  83. ->where('del',0)
  84. ->where('enable',1)
  85. ->find();
  86. if($ret){
  87. $ret['isFav'] = 0;
  88. $ret['imgs']=$ret['imgs']?explode(',',$ret['imgs']):[];
  89. $ret['label_name']=$ret['label'] >0?Db::name('wx_goods_label')
  90. ->where('id',$ret['label'])
  91. ->value('title'):'';
  92. $checkFav = Db::name('wx_goods_fav')
  93. ->where('user_id',$userId)
  94. ->where('goods_id',$ret['id'])
  95. ->find();
  96. if($checkFav){
  97. $ret['isFav'] = 1;
  98. }
  99. }
  100. return $ret;
  101. }
  102. public function getTjGoods($orgId){
  103. $list = Db::name('wx_goods')
  104. ->field('id,title,img,price,label')
  105. ->where('del',0)
  106. ->where('org_id',$orgId)
  107. ->where('enable',1)
  108. ->where('is_tj',1)
  109. ->order(['sort'=>'asc','id'=>'desc'])
  110. ->limit(3)
  111. ->select();
  112. foreach ($list as $k=>$v){
  113. $list[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  114. ->where('id',$v['label'])
  115. ->value('title'):"";
  116. }
  117. return $list?$list:[];
  118. }
  119. public function getTjGoodsList($orgId,$page,$size){
  120. $list = Db::name('wx_goods')
  121. ->field('id,title,img,price,label,sale')
  122. ->where('del',0)
  123. ->where('org_id',$orgId)
  124. ->where('enable',1)
  125. ->where('is_tj',1)
  126. ->order(['sort'=>'asc','id'=>'desc'])
  127. ->page($page,$size)
  128. ->select();
  129. foreach ($list as $k=>$v){
  130. $list[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  131. ->where('id',$v['label'])
  132. ->value('title'):"";
  133. }
  134. return $list?$list:[];
  135. }
  136. public function getIndexGoods($orgId){
  137. $list = Db::name('wx_goods')
  138. ->field('id,title,img,price,label,sale')
  139. ->where('del',0)
  140. ->where('org_id',$orgId)
  141. ->where('enable',1)
  142. // ->where('is_tj',0)
  143. ->order(['sort'=>'asc','id'=>'desc'])
  144. ->limit(10)
  145. ->select();
  146. foreach ($list as $k=>$v){
  147. $list[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  148. ->where('id',$v['label'])
  149. ->value('title'):"";
  150. }
  151. return $list?$list:[];
  152. }
  153. public function getIndexGoodsList($orgId,$page,$size,$type,$desc_name='sale',$desc='desc',$kw=''){
  154. $map = [];
  155. if($kw!=''){
  156. $map[] = ['title','like','%'.$kw.'%'];
  157. }
  158. if($type ==0){
  159. $order= [$desc_name=>$desc,'sort'=>'asc','id'=>'desc'];
  160. }else{
  161. $order= ['sort'=>'asc','id'=>'desc'];
  162. }
  163. if($type ==1){
  164. $map[] = ['label','=',1];
  165. }elseif ($type==2){
  166. $map[] = ['barrel_id','>',0];
  167. }elseif ($type==3){
  168. $map[] = ['is_tj','=',1];
  169. }
  170. $list = Db::name('wx_goods')
  171. ->field('id,title,img,price,label,sale,dec')
  172. ->where('del',0)
  173. ->where('org_id',$orgId)
  174. ->where('enable',1)
  175. ->where($map)
  176. ->order($order)
  177. ->page($page,$size)
  178. ->select();
  179. foreach ($list as $k=>$v){
  180. $list[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  181. ->where('id',$v['label'])
  182. ->value('title'):"";
  183. }
  184. return $list?$list:[];
  185. }
  186. public function favGoodsList($userId,$orgId,$page,$size){
  187. $ids = Db::name('wx_goods_fav')
  188. ->where('user_id',$userId)
  189. ->group('goods_id')
  190. ->column('goods_id');
  191. if(empty($ids)){
  192. $map[] = ['id','=',-1];
  193. }else{
  194. $map[] = ['id','in',$ids];
  195. }
  196. $list = Db::name('wx_goods')
  197. ->field('id,title,img,price,label,sale,dec')
  198. ->where('del',0)
  199. ->where('org_id',$orgId)
  200. ->where('enable',1)
  201. ->where($map)
  202. ->order(['sort'=>'asc','id'=>'desc'])
  203. ->page($page,$size)
  204. ->select();
  205. foreach ($list as $k=>$v){
  206. $list[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  207. ->where('id',$v['label'])
  208. ->value('title'):"";
  209. }
  210. return $list?$list:[];
  211. }
  212. public function barrelList($orgId,$page,$size){
  213. $map[] = ['org_id','=',$orgId];
  214. $map[] = ['del','=',0];
  215. $map[] = ['enable','=',1];
  216. $list = Db::name('wx_barrel')
  217. ->field('id,title,cash_price,path')
  218. ->where($map)
  219. ->order(['sort'=>'asc','id'=>'desc'])
  220. ->page($page,$size)
  221. ->select();
  222. return $list?$list:[];
  223. }
  224. public function myBarrelList($orgId,$userId,$page,$size){
  225. $map[] = ['a.org_id','=',$orgId];
  226. $map[] = ['a.del','=',0];
  227. $map[] = ['a.user_id','=',$userId];
  228. $list = Db::name('wx_cash')
  229. ->alias('a')
  230. ->join('wx_barrel b','a.barrel_id=b.id')
  231. ->field('a.id,b.title,b.cash_price,b.path,a.num,a.amount')
  232. ->where($map)
  233. ->order(['a.id'=>'desc'])
  234. ->page($page,$size)
  235. ->select();
  236. return $list?$list:[];
  237. }
  238. public function myRefundBarrelList($orgId,$userId,$page,$size){
  239. $map[] = ['a.org_id','=',$orgId];
  240. $map[] = ['a.del','=',0];
  241. $map[] = ['a.user_id','=',$userId];
  242. $list = Db::name('wx_cash_refund')
  243. ->alias('a')
  244. ->join('wx_barrel b','a.barrel_id=b.id')
  245. ->field('a.id,b.title,b.cash_price,b.path,a.num,a.amount,a.status,a.create_time,a.remark')
  246. ->where($map)
  247. ->order(['a.id'=>'desc'])
  248. ->page($page,$size)
  249. ->select();
  250. foreach ($list as $k=>$v){
  251. $list[$k]['status_name'] =isset(model('WxCash')->status[$v['status']])?model('WxCash')->status[$v['status']]:'';
  252. $list[$k]['remark'] = $v['remark']?$v['remark']:"";
  253. }
  254. return $list?$list:[];
  255. }
  256. }