GoodsService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AgentModel;
  13. use App\Models\GoodsModel;
  14. use App\Models\MemberModel;
  15. use App\Models\MerchantModel;
  16. use App\Models\OrderModel;
  17. use App\Models\ShopModel;
  18. use App\Models\TradeModel;
  19. use App\Services\BaseService;
  20. use App\Services\ConfigService;
  21. use App\Services\RedisService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 商品管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * Class GoodsService
  28. * @package App\Services\Api
  29. */
  30. class GoodsService extends BaseService
  31. {
  32. // 静态对象
  33. protected static $instance = null;
  34. /**
  35. * 构造函数
  36. * @author laravel开发员
  37. * @since 2020/11/11
  38. * GoodsService constructor.
  39. */
  40. public function __construct()
  41. {
  42. $this->model = new GoodsModel();
  43. }
  44. /**
  45. * 静态入口
  46. * @return static|null
  47. */
  48. public static function make()
  49. {
  50. if (!self::$instance) {
  51. self::$instance = (new static());
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * 列表数据
  57. * @param $params
  58. * @param int $pageSize
  59. * @return array
  60. */
  61. public function getDataList($params, $pageSize = 15, $field = '', $userId=0)
  62. {
  63. $where = ['a.mark' => 1,'c.status'=>2,'c.mark'=>1,'d.mark'=>1];
  64. $field = $field? $field : 'lev_a.id,lev_a.user_id,lev_a.merch_id,lev_a.goods_name,lev_a.attr,lev_a.sales,lev_a.service_type,lev_a.intro_effect,lev_a.thumb,lev_a.intro_albums,lev_a.intro_process_albums,lev_a.intro_other_albums,lev_a.buy_notice,lev_a.cate_id,lev_a.type,lev_a.service_fee,lev_a.price,lev_a.discount,lev_a.unit,lev_a.sku,lev_a.sales,lev_a.intro_effect,lev_a.status,lev_b.name as cate_name,lev_c.lat,lev_c.lng,lev_c.trade_status,lev_c.name as merch_name';
  65. $lat = isset($params['lat']) ? $params['lat'] : 0.00;
  66. $lng = isset($params['lng']) ? $params['lng'] : 0.00;
  67. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
  68. $order = 'id desc';
  69. if($sortType == 1){
  70. $order = $lng&&$lat? 'lev_c.trade_status asc,distance asc, lev_a.id desc':'lev_c.trade_status asc,lev_a.id desc';
  71. }else if($sortType == 2){
  72. $order = 'lev_a.sales desc, lev_a.id desc';
  73. }
  74. $distanceField = '';
  75. if ($lat && $lng) {
  76. $distanceField = "ROUND(
  77. 6378.138 * 2 * ASIN(
  78. SQRT(
  79. POW(
  80. SIN(
  81. (
  82. {$lat} * PI() / 180 - `lev_c`.`lat` * PI() / 180
  83. ) / 2
  84. ),
  85. 2
  86. ) + COS({$lat} * PI() / 180) * COS(`lev_c`.`lat` * PI() / 180) * POW(
  87. SIN(
  88. (
  89. {$lng} * PI() / 180 - `lev_c`.`lng` * PI() / 180
  90. ) / 2
  91. ),
  92. 2
  93. )
  94. )
  95. ) * 1000
  96. )";
  97. $field .= ", {$distanceField} AS distance";
  98. }
  99. $distanceLimit = ConfigService::make()->getConfigByCode('distance_limit');
  100. $distanceLimit = $distanceLimit? $distanceLimit : 0;
  101. $model = $this->model->from('goods as a')
  102. ->leftJoin('goods_category as b', 'a.cate_id', '=', 'b.id')
  103. ->leftJoin('merchant as c', 'a.merch_id', '=', 'c.id')
  104. ->leftJoin('member as d', 'd.id', '=', 'c.user_id')
  105. ->where($where)
  106. ->where(function ($query) use ($params,$distanceField,$distanceLimit) {
  107. $type = isset($params['type']) ? $params['type'] : 0;
  108. if ($type > 0) {
  109. $query->where('a.type', $type);
  110. }
  111. // 商户
  112. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  113. if ($merchId > 0) {
  114. $query->where('a.merch_id', $merchId);
  115. }
  116. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  117. if ($userId > 0) {
  118. $query->where('a.user_id', $userId);
  119. }
  120. // 推荐
  121. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  122. if ($isRecommend > 0) {
  123. $query->where('a.is_recommend', $isRecommend);
  124. }
  125. // 首页展示
  126. $isHome = isset($params['is_home']) ? $params['is_home'] : 0;
  127. if ($isHome > 0) {
  128. $query->where('a.is_home', $isHome);
  129. }
  130. // 分类
  131. $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0;
  132. if ($cateId > 0) {
  133. $query->where('a.cate_id', $cateId);
  134. }
  135. $lat = isset($params['lat']) ? $params['lat'] : 0.00;
  136. $lng = isset($params['lng']) ? $params['lng'] : 0.00;
  137. if($lat>0 && $lng>0 && $distanceLimit>0 && $distanceField) {
  138. $query->whereRaw("{$distanceField} <= {$distanceLimit}");
  139. }
  140. $agentId = isset($params['agent_id'])? intval($params['agent_id']) : 0;
  141. if($agentId>0){
  142. $agentCity = AgentModel::where(['id'=>$agentId,'status'=>2,'mark'=>1])->value('city');
  143. $agentCity = $agentCity? $agentCity : '不存在';
  144. $query->where('c.city', $agentCity);
  145. }
  146. })
  147. ->where(function ($query) use ($params) {
  148. $keyword = isset($params['kw']) ? $params['kw'] : '';
  149. if ($keyword) {
  150. $query->where('a.goods_name', 'like', "%{$keyword}%")->orWhere('b.name', 'like', "%{$keyword}%")->orWhere('c.name', 'like', "%{$keyword}%");
  151. }
  152. });
  153. $countModel = clone $model;
  154. $countModel1 = clone $model;
  155. $countModel2 = clone $model;
  156. $counts = [
  157. 'status1'=> $countModel->where(['a.status'=>2])->count('a.id'),
  158. 'status2'=> $countModel1->where(['a.status'=>1])->count('a.id'),
  159. 'status3'=> $countModel2->whereIn('a.status',[0,3])->count('a.id'),
  160. ];
  161. $list = $model->where(function ($query) use ($params) {
  162. $status = isset($params['status']) ? $params['status'] : 0;
  163. $tabType = isset($params['tab_type']) ? $params['tab_type'] : 0;
  164. $status = $status > 0 ? $status : 2;
  165. if ($status > 0 && $tabType<=0) {
  166. $query->where('a.status', $status);
  167. }
  168. if ($tabType == 1) {
  169. $query->where('a.status', 2);
  170. }else if($tabType == 2){
  171. $query->where('a.status', 1);
  172. }else if($tabType == 3){
  173. $query->whereIn('a.status', [0,3]);
  174. }
  175. })->selectRaw($field)
  176. ->orderByRaw($order)
  177. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  178. $list = $list ? $list->toArray() : [];
  179. if ($list && $list['data']) {
  180. foreach ($list['data'] as &$item) {
  181. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  182. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  183. $item['sales'] = isset($item['sales'])? ($item['sales']>1000? '1000+': $item['sales']):0;
  184. if(isset($item['distance'])){
  185. $item['distance_text'] = $item['lat']>0? formatDistance($item['distance']) : '';
  186. }
  187. if(isset($item['albums'])){
  188. $albums = $item['albums']? json_decode($item['albums'], true):[];
  189. $item['albums'] = $albums? get_images_preview($albums) : [];
  190. }
  191. if(isset($item['video'])){
  192. $item['video'] = $item['video']? get_image_url($item['video']) : '';
  193. }
  194. if(isset($item['intro_albums'])){
  195. $albums = $item['intro_albums']? json_decode($item['intro_albums'], true):[];
  196. $item['intro_albums'] = $albums? get_images_preview($albums) : [];
  197. }
  198. if(isset($item['intro_process_albums'])){
  199. $albums = $item['intro_process_albums']? json_decode($item['intro_process_albums'], true):[];
  200. $item['intro_process_albums'] = $albums? get_images_preview($albums) : [];
  201. }
  202. if(isset($item['intro_other_albums'])){
  203. $albums = $item['intro_other_albums']? json_decode($item['intro_other_albums'], true):[];
  204. $item['intro_other_albums'] = $albums? get_images_preview($albums) : [];
  205. }
  206. if(isset($item['service_type']) && $item['service_type']){
  207. $item['service_type'] = explode(',', $item['service_type']);
  208. }
  209. $item['service_type'] = $item['service_type']? $item['service_type'] : [2,3];
  210. $price = isset($item['price'])? moneyFormat($item['price'],2) : 0;
  211. $discount = isset($item['discount'])? floatval($item['discount']) : 0;
  212. $item['price'] = $discount>0 && $discount<10? moneyFormat($price * $discount/10, 2) : $price;
  213. $item['original_price'] = $price;
  214. $showType = isset($params['show_type'])? $params['show_type'] : 0;
  215. if($showType){
  216. $item['num'] = CartService::make()->getGoodsCartNum($userId, $item['id']);
  217. }
  218. }
  219. }
  220. return [
  221. 'pageSize' => $pageSize,
  222. 'counts' => $counts,
  223. 'total' => isset($list['total']) ? $list['total'] : 0,
  224. 'list' => isset($list['data']) ? $list['data'] : []
  225. ];
  226. }
  227. /**
  228. * 列表数据
  229. * @param $params
  230. * @param int $pageSize
  231. * @return array
  232. */
  233. public function getIndexList($params, $pageSize = 6)
  234. {
  235. $where = ['a.is_home'=>1,'a.status'=>2];
  236. $field = 'lev_a.id,lev_a.user_id,lev_a.merch_id,lev_a.goods_name,lev_a.thumb,lev_a.type,lev_a.sales,lev_a.price,lev_a.unit,lev_a.sku,lev_c.lat,lev_c.lng,lev_c.trade_status,lev_c.name as merch_name';
  237. $lat = isset($params['lat']) ? $params['lat'] : 0.00;
  238. $lng = isset($params['lng']) ? $params['lng'] : 0.00;
  239. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 0;
  240. $order = 'lev_a.id desc,lev_a.create_time desc';
  241. if($sortType == 1){
  242. $order = $lng&&$lat? 'lev_c.trade_status asc,distance asc, lev_a.id desc':'lev_c.trade_status asc,lev_a.id desc';
  243. }else if($sortType == 2){
  244. $order = 'lev_c.trade_status asc,lev_a.sales desc, lev_a.id desc';
  245. }
  246. $distanceField = '';
  247. if ($lat && $lng) {
  248. $distanceField = "ROUND(
  249. 6378.138 * 2 * ASIN(
  250. SQRT(
  251. POW(
  252. SIN(
  253. (
  254. {$lat} * PI() / 180 - `lev_c`.`lat` * PI() / 180
  255. ) / 2
  256. ),
  257. 2
  258. ) + COS({$lat} * PI() / 180) * COS(`lev_c`.`lat` * PI() / 180) * POW(
  259. SIN(
  260. (
  261. {$lng} * PI() / 180 - `lev_c`.`lng` * PI() / 180
  262. ) / 2
  263. ),
  264. 2
  265. )
  266. )
  267. ) * 1000
  268. )";
  269. $field .= ", {$distanceField} AS distance";
  270. }
  271. $type = isset($params['type']) ? $params['type'] : 0;
  272. if($type){
  273. $where['a.type'] = $type;
  274. }
  275. $where['a.mark'] = 1;
  276. $where['c.status'] = 2;
  277. $where['c.mark'] = 1;
  278. $where['b.mark'] = 1;
  279. $distanceLimit = ConfigService::make()->getConfigByCode('distance_limit');
  280. $distanceLimit = $distanceLimit? $distanceLimit : 0;
  281. $list = $this->model->from('goods as a')
  282. ->leftJoin('merchant as c', 'a.merch_id', '=', 'c.id')
  283. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  284. ->where($where)
  285. ->where(function ($query) use ($params,$distanceField,$distanceLimit) {
  286. // 商户
  287. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  288. if ($merchId > 0) {
  289. $query->where('a.merch_id', $merchId);
  290. }
  291. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  292. if ($userId > 0) {
  293. $query->where('a.user_id', $userId);
  294. }
  295. // 推荐
  296. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  297. if ($isRecommend > 0) {
  298. $query->where('a.is_recommend', $isRecommend);
  299. }
  300. // 分类
  301. $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0;
  302. if ($cateId > 0) {
  303. $query->where('a.cate_id', $cateId);
  304. }
  305. $lat = isset($params['lat']) ? $params['lat'] : 0.00;
  306. $lng = isset($params['lng']) ? $params['lng'] : 0.00;
  307. if($lat>0 && $lng>0 && $distanceLimit>0 && $distanceField) {
  308. $query->whereRaw("{$distanceField} <= {$distanceLimit}");
  309. }
  310. })
  311. ->selectRaw($field)
  312. ->orderByRaw($order)
  313. ->limit($pageSize)
  314. ->get();
  315. $list = $list ? $list->toArray() : [];
  316. if ($list) {
  317. foreach ($list as &$item) {
  318. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  319. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  320. $item['sales'] = isset($item['sales'])? ($item['sales']>1000? '1000+': $item['sales']):0;
  321. if(isset($item['distance'])){
  322. $item['distance_text'] = $item['lat']>0? formatDistance($item['distance']) : '';
  323. }
  324. $price = isset($item['price'])? moneyFormat($item['price'],2) : 0;
  325. $discount = isset($item['discount'])? floatval($item['discount']) : 0;
  326. $item['price'] = $discount>0 && $discount<10? moneyFormat($price * $discount/10, 2) : $price;
  327. $item['original_price'] = $price;
  328. }
  329. }
  330. return $list;
  331. }
  332. /**
  333. * 详情
  334. * @param $id
  335. * @return array
  336. */
  337. public function getInfo($id, $userId=0)
  338. {
  339. $field = ['a.id','a.goods_name','a.user_id','a.merch_id','a.type','a.service_type','a.thumb','a.cate_id','a.views','a.intro_albums','a.intro_process_albums','a.intro_other_albums','a.intro_other','a.buy_notice','a.unit','a.sku','a.price','a.attr','a.detail_img','a.service_fee','a.albums','a.sales','a.discount','a.status','a.stock','a.intro_effect','a.intro_people','a.video','a.intro_unsuitable','a.intro_service','a.intro_process','a.content','b.name as merch_name','b.type as merch_type','b.logo','b.category as merch_cate','b.service_time','b.trade_status','c.type as merch_cate_type'];
  340. $info = $this->model->from('goods as a')->with(['category'])
  341. ->leftJoin('merchant as b','b.id','=','a.merch_id')
  342. ->leftJoin('merchant_category as c','c.id','=','b.category')
  343. ->where(['a.id'=> $id,'a.mark'=>1,'b.mark'=>1])
  344. ->select($field)
  345. ->first();
  346. $info = $info? $info->toArray() : [];
  347. if($info){
  348. if(isset($info['logo'])){
  349. $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
  350. }
  351. if(isset($info['thumb'])){
  352. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  353. }
  354. if(isset($info['detail_img'])){
  355. $info['detail_img'] = $info['detail_img']? get_image_url($info['detail_img']) : '';
  356. }
  357. if(isset($info['video'])){
  358. $info['video'] = $info['video']? get_image_url($info['video']) : '';
  359. }
  360. if(isset($info['intro_albums'])){
  361. $info['intro_albums'] = $info['intro_albums']? json_decode($info['intro_albums'], true) : [];
  362. $info['intro_albums'] = $info['intro_albums']? get_images_preview($info['intro_albums']) : [];
  363. }
  364. if(isset($info['intro_process_albums'])){
  365. $info['intro_process_albums'] = $info['intro_process_albums']? json_decode($info['intro_process_albums'], true) : [];
  366. $info['intro_process_albums'] = $info['intro_process_albums']? get_images_preview($info['intro_process_albums']) : [];
  367. }
  368. if(isset($info['intro_other_albums'])){
  369. $info['intro_other_albums'] = $info['intro_other_albums']? json_decode($info['intro_other_albums'], true) : [];
  370. $info['intro_other_albums'] = $info['intro_other_albums']? get_images_preview($info['intro_other_albums']) : [];
  371. }
  372. if(isset($info['albums'])){
  373. $info['albums'] = $info['albums']? json_decode($info['albums'], true) : [];
  374. $info['albums'] = $info['albums']? get_images_preview($info['albums']) : [];
  375. }
  376. $price = isset($info['price'])? moneyFormat($info['price'],2) : 0;
  377. $discount = isset($info['discount'])? floatval($info['discount']) : 0;
  378. $info['price'] = $discount>0 && $discount<10? moneyFormat($price * $discount/10, 2) : $price;
  379. $info['original_price'] = $price;
  380. if(isset($info['service_type']) && $info['service_type']){
  381. $info['service_type'] = explode(',', $info['service_type']);
  382. }
  383. $info['service_type'] = $info['service_type']? $info['service_type'] : [2,3];
  384. $info['coupon_num'] = CouponService::make()->getCountByMerch($info['merch_id']);
  385. $info['service_status'] = 2;
  386. $info['service_time'] = isset($info['service_time']) && $info['service_time']? $info['service_time']:'08点~22点';
  387. $serviceTime = $info['service_time']? str_replace('~','~',$info['service_time']):'';
  388. $times = $serviceTime? explode('~',$serviceTime) : [];
  389. $times[0] = isset($times[0]) && !empty($times[0])? $times[0] : '08点';
  390. $times[1] = isset($times[1]) && $times[1]? $times[1] : '22点';
  391. if($times && date('H点')>= $times[0] && date('H点')<=$times[1]){
  392. $info['service_status'] = 1;
  393. }
  394. if($info['trade_status'] != 1){
  395. $info['service_status'] = 2;
  396. }
  397. $time1 = isset($times[0]) && !empty($times[0])? str_replace('点','',$times[0]) : '08';
  398. $time2 = isset($times[1]) && $times[1]? intval(str_replace('点','',$times[1])) : 22;
  399. $info['minDate'] = strtotime(date("Y-m-d"))*1000;
  400. $info['maxDate'] = (strtotime(date("Y-m-d")) + 30*86400)*1000;
  401. $info['minHour'] = intval($time1);
  402. $info['maxHour'] = intval($time2>1?$time2-1:$time2);
  403. $info['maxHour'] = $info['maxHour']<= $info['minHour']? 23: $info['maxHour'];
  404. $this->updateView($userId, $id);
  405. }
  406. return $info;
  407. }
  408. /**
  409. * 更新浏览量
  410. * @param $userId
  411. * @param $dynamicId
  412. * @return array|mixed
  413. */
  414. public function updateView($userId, $id)
  415. {
  416. $cacheKey = "caches:articles:views:u{$userId}_d{$id}";
  417. $data = RedisService::get($cacheKey);
  418. if($data){
  419. return false;
  420. }
  421. $data = $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  422. RedisService::set($cacheKey, $id, rand(1,3)*7200);
  423. return $data;
  424. }
  425. /**
  426. * \获取技师服务列表
  427. * @param $ids
  428. * @return mixed
  429. */
  430. public function getListByIds($ids)
  431. {
  432. $field = 'lev_a.id,lev_a.user_id,lev_a.merch_id,lev_a.goods_name,lev_a.attr,lev_a.sales,lev_a.intro_effect,lev_a.thumb,lev_a.cate_id,lev_a.type,lev_a.service_fee,lev_a.price,lev_a.discount,lev_a.unit,lev_a.sku,lev_a.sales,lev_a.intro_effect,lev_a.status,lev_b.name as cate_name,lev_c.lat,lev_c.lng,lev_c.name as merch_name,lev_c.type as merch_type';
  433. $datas = $this->model->from('goods as a')
  434. ->leftJoin('goods_category as b', 'a.cate_id', '=', 'b.id')
  435. ->leftJoin('merchant as c', 'a.merch_id', '=', 'c.id')
  436. ->whereIn('a.id', $ids)
  437. ->where(['a.status'=>2,'a.type'=>2,'a.mark'=>1,'c.status'=>2,'c.mark'=>1])
  438. ->selectRaw($field)
  439. ->get();
  440. if($datas){
  441. foreach ($datas as &$item) {
  442. $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  443. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  444. $item['sales'] = isset($item['sales'])? ($item['sales']>1000? '1000+': $item['sales']):0;
  445. if(isset($item['distance'])){
  446. $item['distance_text'] = $item['lat']>0? formatDistance($item['distance']) : '';
  447. }
  448. $price = isset($item['price'])? moneyFormat($item['price'],2) : 0;
  449. $discount = isset($item['discount'])? floatval($item['discount']) : 0;
  450. $item['price'] = $discount>0 && $discount<10? moneyFormat($price * $discount/10, 2) : $price;
  451. $item['original_price'] = $price;
  452. }
  453. }
  454. return $datas;
  455. }
  456. /**
  457. * 发布/编辑商品信息
  458. * @param $goodsId
  459. * @return bool
  460. */
  461. public function submit($userId, $params)
  462. {
  463. $id = isset($params['id'])? intval($params['id']) : 0;
  464. $merchId = isset($params['merch_id'])? intval($params['merch_id']) : 0;
  465. $type = isset($params['type'])? intval($params['type']) : 2;
  466. $checkType = isset($params['check_type'])? intval($params['check_type']) : 1;
  467. $status = isset($params['status'])? intval($params['status']) : 1;
  468. $merchInfo = MerchantModel::where(['id'=> $merchId,'mark'=>1])
  469. ->select(['id','name','status'])
  470. ->first();
  471. $status = isset($merchInfo['status'])? $merchInfo['status'] : 0;
  472. if(empty($merchInfo) || $status != 2){
  473. $this->error = 2982;
  474. return false;
  475. }
  476. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  477. $this->error = 2981;
  478. return false;
  479. }
  480. $thumb = isset($params['thumb'][0])? get_image_path($params['thumb'][0]['url']) : '';
  481. $video = isset($params['video'][0])? get_image_path($params['video'][0]['url']) : '';
  482. $albums = isset($params['albums'])? get_format_images($params['albums']) : '';
  483. $introProcessAlbums = isset($params['intro_process_albums'])? get_format_images($params['intro_process_albums']) : '';
  484. $introOtherAlbums = isset($params['intro_other_albums'])? get_format_images($params['intro_other_albums']) : '';
  485. $introAlbums = isset($params['intro_albums'])? get_format_images($params['intro_albums']) : '';
  486. $discount = isset($params['discount']) ? floatval($params['discount']) : 0;
  487. $goodsPublish = ConfigService::make()->getConfigByCode('goods_publish');
  488. $goodsPublish = $goodsPublish==1? $goodsPublish : 2;
  489. $goodsName = isset($params['goods_name']) ? trim($params['goods_name']) : '';
  490. $goodsName = MessageService::make()->filterMessage($goodsName,4);
  491. $data = [
  492. 'user_id' => $userId,
  493. 'merch_id' => $merchId,
  494. 'type' => $type,
  495. 'goods_name' => $goodsName,
  496. 'cate_id' => isset($params['cate_id']) ? intval($params['cate_id']) : 0,
  497. 'price' => isset($params['price']) ? floatval($params['price']) : 0.00,
  498. 'unit' => isset($params['unit']) ? trim($params['unit']) : '项',
  499. 'sku' => isset($params['sku']) ? trim($params['sku']) : '',
  500. 'attr' => isset($params['attr']) ? MessageService::make()->filterMessage($params['attr'],4) : '',
  501. 'weight' => isset($params['weight']) ? trim($params['weight']) : '',
  502. 'stock' => isset($params['stock']) && $params['stock']>0? intval($params['stock']) : 9999,
  503. 'buy_notice' => isset($params['buy_notice']) ? MessageService::make()->filterMessage($params['buy_notice'],4) : '',
  504. 'intro_effect' => isset($params['intro_effect']) ? MessageService::make()->filterMessage($params['intro_effect'],4) : '',
  505. 'intro_people' => isset($params['intro_people']) ? MessageService::make()->filterMessage($params['intro_people'],4) : '',
  506. 'intro_unsuitable' => isset($params['intro_unsuitable']) ? MessageService::make()->filterMessage($params['intro_unsuitable'],4) : '',
  507. 'intro_service' => isset($params['intro_service']) ? MessageService::make()->filterMessage($params['intro_service'],4) : '',
  508. 'intro_process' => isset($params['intro_process']) ? MessageService::make()->filterMessage($params['intro_process'],4) : '',
  509. 'content' => isset($params['content']) ? MessageService::make()->filterMessage($params['content'],4) : '',
  510. 'service_fee' => isset($params['service_fee']) ? floatval($params['service_fee']) : 0.00,
  511. 'is_recommend' => isset($params['is_recommend']) && $params['is_recommend']? intval($params['is_recommend']) : 2,
  512. 'is_home' => isset($params['is_home']) && $params['is_home']? intval($params['is_home']) : 1,
  513. 'sort' => isset($params['sort']) && $params['sort']? intval($params['sort']) : 0,
  514. 'service_type' => isset($params['service_type']) && $params['service_type']? trim($params['service_type']) : '2,3',
  515. 'discount' => $discount>0&&$discount<10?$discount:0,
  516. 'thumb' => $thumb,
  517. 'video' => $video,
  518. 'albums' => $albums,
  519. 'intro_albums' => $introAlbums,
  520. 'intro_process_albums' => $introProcessAlbums,
  521. 'intro_other_albums' => $introOtherAlbums,
  522. 'status' => $checkType==2? $status : ($goodsPublish==1? 0 : 1),
  523. 'mark' => 1,
  524. 'update_time' => time(),
  525. ];
  526. if($id>0){
  527. if($this->model->where(['id'=> $id,'mark'=>1])->update($data)){
  528. $this->error = 1008;
  529. return true;
  530. }
  531. }else{
  532. $data['create_time'] = time();
  533. if($this->model->insertGetId($data)){
  534. $this->error = 1023;
  535. return true;
  536. }
  537. }
  538. $this->error = $id? 1007 : 1024;
  539. return false;
  540. }
  541. /**
  542. * 上下架
  543. * @return bool
  544. */
  545. public function status()
  546. {
  547. $id = request()->post('id', 0);
  548. $status = request()->post('status', 1);
  549. if ($id && !$this->model->where(['id' => $id, 'mark' => 1])->value('id')) {
  550. $this->error = 2981;
  551. return false;
  552. }
  553. if($this->model->where(['id'=> $id,'mark'=>1])->update(['status'=>$status, 'update_time'=> time()])){
  554. $this->error = 1002;
  555. return true;
  556. }
  557. $this->error = 1003;
  558. return true;
  559. }
  560. /**
  561. * 删除
  562. * @return array
  563. */
  564. public function delete()
  565. {
  566. // 参数
  567. $param = request()->all();
  568. $ids = getter($param, "id");
  569. if (empty($ids)) {
  570. return message("请选择操作选项", false);
  571. }
  572. DB::beginTransaction();
  573. $result = parent::delete(); // TODO: Change the autogenerated stub
  574. $code = isset($result['success']) ? $result['success'] : '';
  575. if (!$code) {
  576. DB::rollBack();
  577. return $result;
  578. }
  579. DB::commit();
  580. return $result;
  581. }
  582. }