Product.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\common\library\Common;
  5. use think\Db;
  6. /**
  7. * 商品信息model
  8. */
  9. class Product Extends Model
  10. {
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'TIMESTAMP';
  13. // 定义时间戳字段名
  14. protected $createTime = 'create_at';
  15. protected $updateTime = 'update_at';
  16. // 追加属性
  17. protected $append = [
  18. ];
  19. /**
  20. * 添加、编辑商品信息(普通商品)
  21. * @param $data
  22. * @return array
  23. */
  24. public static function add_edit($data){
  25. $user = app()->session->get('us');
  26. $userinfo = User::where('id',$user['id'])->find();
  27. if ($userinfo['type'] != 2)
  28. return Common::return_error('非商家,无法添加!');
  29. $merchInfo = UserMerchInfo::where('user_id',$user['id'])->where('audit',1)->where('status',1)->find();
  30. if (!$merchInfo)
  31. return Common::return_error('商家审核未通过或被禁用,无法添加!');
  32. $cate_id = $merchInfo['category_one_id'];
  33. $twocate = Category::get($cate_id);
  34. if ($twocate['type'] != 1){
  35. return Common::return_error('商家类别错误,无法添加!');
  36. }
  37. $data['label'] = implode(',',$data['label']);
  38. $data['imgs'] = implode(',',$data['imgs']);
  39. Db::startTrans();
  40. try {
  41. if ($data['id']){
  42. $data['update_at'] = date('Y-m-d H:i:s');
  43. self::where('id',$data['id'])->update($data);
  44. Db::commit();
  45. self::price_log($data['id'],$data['price'],$data['first_price']);
  46. return Common::return_success('编辑成功');
  47. }else{
  48. $data['user_id'] = $userinfo->id;
  49. $data['merch_id'] = $merchInfo->id;
  50. $product = self::create($data);
  51. Db::commit();
  52. self::price_log($product->id,$data['price'],$data['first_price']);
  53. return Common::return_success('添加成功');
  54. }
  55. } catch (Exception $e) {
  56. Db::rollback();
  57. return Common::return_error('失败');
  58. }
  59. }
  60. /**
  61. * 商品价格变动记录
  62. */
  63. public static function price_log($product_id,$o_price,$first_price){
  64. $data['product_id'] = $product_id;
  65. $data['o_price'] = $o_price;
  66. $data['first_price'] = $first_price;
  67. ProductPriceLog::create($data);
  68. }
  69. /**
  70. * 添加、编辑商品信息(加油站商品)
  71. * @param $data
  72. * @return array
  73. */
  74. public static function add_edit_station($data){
  75. $user = app()->session->get('us');
  76. $userinfo = User::where('id',$user['id'])->find();
  77. if ($userinfo['type'] != 2)
  78. return Common::return_error('非商家,无法添加!');
  79. $merchInfo = UserMerchInfo::where('user_id',$user['id'])->where('audit',1)->where('status',1)->find();
  80. if (!$merchInfo)
  81. return Common::return_error('商家审核未通过或被禁用,无法添加!');
  82. $cate_id = $merchInfo['category_one_id'];
  83. $twocate = Category::get($cate_id);
  84. if ($twocate['type'] != 2){
  85. return Common::return_error('商家类别错误,无法添加!');
  86. }
  87. $data['specifications'] = json_encode($data['specifications'],true);
  88. $data['imgs'] = implode(',',$data['imgs']);
  89. Db::startTrans();
  90. try {
  91. if ($data['id']){
  92. $data['update_at'] = date('Y-m-d H:i:s');
  93. self::where('id',$data['id'])->update($data);
  94. Db::commit();
  95. return Common::return_success('编辑成功');
  96. }else{
  97. $data['user_id'] = $userinfo->id;
  98. $data['merch_id'] = $merchInfo->id;
  99. self::create($data);
  100. Db::commit();
  101. return Common::return_success('添加成功');
  102. }
  103. } catch (Exception $e) {
  104. Db::rollback();
  105. return Common::return_error('失败');
  106. }
  107. }
  108. /**
  109. * 获取商品信息列表
  110. */
  111. public static function product_list($Nowpage,$limits){
  112. $user = app()->session->get('us');
  113. $count = self::where('is_del',1)->where('user_id',$user['id'])->count();
  114. $data['count'] = $count;
  115. $list = self::where('is_del',1)->where('user_id',$user['id'])->page($Nowpage, $limits)->order('id desc')->select();
  116. if ($list){
  117. $list = $list->toArray();
  118. foreach ($list as $k=>$v){
  119. $list[$k]['label'] = explode(',',$v['label']);
  120. $list[$k]['image'] = explode(',',$v['imgs'])[0];
  121. $list[$k]['imgs'] = explode(',',$v['imgs']);
  122. $list[$k]['specifications'] = json_decode($v['specifications'],true);
  123. }
  124. } else{
  125. $list = [];
  126. }
  127. $data['list'] = $list;
  128. return Common::return_success('成功',$data);
  129. }
  130. /**
  131. * 删除商品信息
  132. */
  133. public static function del_product($id){
  134. $user = app()->session->get('us');
  135. $job = self::get($id);
  136. if (!$job)
  137. return Common::return_error('信息不存在');
  138. if ($job->user_id!=$user['id'])
  139. return Common::return_error('无法删除');
  140. $job->is_del = 0;
  141. if ($job->save())
  142. return Common::return_success('删除成功');
  143. else
  144. return Common::return_error('删除失败');
  145. }
  146. /**
  147. * 商品列表
  148. */
  149. public static function productList($merch_id,$Nowpage,$limits){
  150. $user = app()->session->get('us');
  151. $merch = UserMerchInfo::where('id',$merch_id)->where('audit',1)->where('status',1)->find();
  152. if (!$merch)
  153. return Common::return_error('商家不存在');
  154. //商品
  155. $product['count'] = self::where('merch_id',$merch_id)->where('is_del',1)->order('id desc')->count();
  156. $product_list = self::where('merch_id',$merch_id)->where('is_del',1)
  157. ->order('id desc')->field('id,merch_id,user_id,product_name,price,product_info,imgs,specifications')
  158. ->page($Nowpage, $limits)
  159. ->select();
  160. if ($product_list){
  161. $product_list = $product_list->toArray();
  162. foreach ($product_list as $k=>$v){
  163. if ($v['price']==0){
  164. $product_list[$k]['price'] = json_decode($v['specifications'],true)[0]['price'];
  165. $product[$k]['is_first_buy'] = false;
  166. }else{
  167. if ($v['first_price']>0){
  168. $order = Order::where('user_id',$user['id'])->where('product_id',$v['id'])->whereNotIn('status','0,4')->count();
  169. if ($order){
  170. $product[$k]['is_first_buy'] = false;
  171. }else{
  172. $product[$k]['is_first_buy'] = true;
  173. $product[$k]['price'] = $v['first_price'];
  174. }
  175. }else{
  176. $product[$k]['is_first_buy'] = false;
  177. }
  178. }
  179. $product_list[$k]['image'] = explode(',',$v['imgs'])[0];
  180. $product_list[$k]['is_collect'] = self::checkUserCollect($v['id']) ? 'true' : 'false';
  181. unset($product_list[$k]['imgs']);
  182. }
  183. $product['list'] = $product_list;
  184. } else{
  185. $product['list'] = [];
  186. }
  187. return Common::return_success('成功',$product);
  188. }
  189. /**
  190. * 商品详情页
  191. */
  192. public static function productDetail($product_id,$Nowpage,$limits){
  193. $user = app()->session->get('us');
  194. $detail = self::where('id',$product_id)->where('is_del',1)->find();
  195. if (!$detail)
  196. return Common::return_error('商品不存在');
  197. $merch = UserMerchInfo::where('id',$detail['merch_id'])->where('audit',1)->where('status',1)->field('id,user_id,name,category_one_id,category_two_id,log,lat,address,phone,capita,imgs,lat,log')->find()->toArray();
  198. $merch['imgs'] = explode(',',$merch['imgs']);
  199. $merch['evaluation'] = UserMerchInfo::merchEvaluation($merch['id']); //评价几颗星
  200. $merch['distance'] = Common::getDistance($merch['lat'],$merch['log'])."km"; //距离
  201. $detail = $detail->toArray();
  202. if ($detail['price']==0){
  203. $detail['is_first_buy'] = false;
  204. }else{
  205. if ($detail['first_price']>0){
  206. $order = Order::where('user_id',$user['id'])->where('product_id',$product_id)->whereNotIn('status','0,4')->count();
  207. if ($order){
  208. $detail['is_first_buy'] = false;
  209. }else{
  210. $detail['is_first_buy'] = true;
  211. $detail['price'] = $detail['first_price'];
  212. }
  213. }else{
  214. $detail['is_first_buy'] = false;
  215. }
  216. }
  217. $d[0]['id'] = $detail['id'];
  218. $d[0]['product_name'] = $detail['product_name'];
  219. $productlist = self::where('merch_id',$detail['merch_id'])->where('id','<>',$product_id)->where('is_del',1)->field('id,product_name')->select();
  220. if ($productlist){
  221. $product_list = array_merge($d,$productlist->toArray());
  222. }else{
  223. $product_list = $d;
  224. }
  225. self::browseAdd($product_id); //浏览数
  226. //获取商家的类别
  227. $type = UserMerchInfo::where('a.id',$detail['merch_id'])->alias('a')->join('Category b','a.category_one_id=b.id')->value('b.type');
  228. $detail['imgs'] = $detail['imgs'] ? explode(',',$detail['imgs']) : [];
  229. $detail['type'] = $type;
  230. $detail['label'] = explode(',',$detail['label']);
  231. $detail['specifications'] = json_decode($detail['specifications'],true);
  232. $dates = date('Y-m');
  233. $timebegin = strtotime($dates) ; //开始时间戳
  234. $day = date('t',$timebegin);
  235. $timeend = $timebegin + 86400 * $day - 1; //结束时间戳
  236. $start = date('Y-m-d H:i:s',$timebegin);
  237. $end = date('Y-m-d H:i:s',$timeend);
  238. //商品月销量
  239. $detail['sales'] = Order::where('product_id',$product_id)->where('status','in','2,3')->count();
  240. //$detail['sales'] = Order::where('product_id',$product_id)->whereBetweenTime('create_at',$start,$end)->where('status','in','2,3')->count();
  241. //评论
  242. $evaluation['count'] = ProductEvaluation::where('product_id',$product_id)->where('is_del',1)->count();
  243. $evaluation_list = ProductEvaluation::where('a.product_id',$product_id)
  244. ->alias('a')
  245. ->where('a.is_del',1)
  246. ->join('User b','a.user_id=b.id')
  247. ->field('a.*,b.nickname,b.headimg')
  248. ->order('a.is_optimization desc,a.id desc')
  249. ->page($Nowpage, $limits)
  250. ->select();
  251. if ($evaluation_list){
  252. $evaluation_list = $evaluation_list->toArray();
  253. foreach ($evaluation_list as $k=>$v){
  254. $evaluation_list[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
  255. $evaluation_list[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
  256. $evaluation_list[$k]['is_zan'] = ProductEvaluation::checkUserEvaluationZan($v['id']) ? 'true' : 'false';
  257. }
  258. $evaluation['list'] = $evaluation_list;
  259. }else {
  260. $evaluation['list'] = [];
  261. }
  262. return Common::return_success('成功',compact('product_list','detail','evaluation','merch'));
  263. }
  264. /**
  265. * 判断用户是否收藏商品
  266. */
  267. public static function checkUserCollect($product_id){
  268. $user = app()->session->get('us');
  269. if ($user){
  270. $collect = ProductCollect::where('user_id',$user['id'])->where('product_id',$product_id)->count();
  271. if ($collect)
  272. return true;
  273. }
  274. return false;
  275. }
  276. /**
  277. * 商品浏览量增加
  278. */
  279. public static function browseAdd($product_id){
  280. self::where('id',$product_id)->setInc('browse');
  281. }
  282. }