GoodsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 App\Services\SupplyService;
  23. use BN\Red;
  24. use Illuminate\Support\Facades\DB;
  25. /**
  26. * 商品管理-服务类
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * Class GoodsService
  30. * @package App\Services\Api
  31. */
  32. class GoodsService extends BaseService
  33. {
  34. // 静态对象
  35. protected static $instance = null;
  36. /**
  37. * 构造函数
  38. * @author laravel开发员
  39. * @since 2020/11/11
  40. * GoodsService constructor.
  41. */
  42. public function __construct()
  43. {
  44. $this->model = new GoodsModel();
  45. }
  46. /**
  47. * 静态入口
  48. * @return static|null
  49. */
  50. public static function make()
  51. {
  52. if (!self::$instance) {
  53. self::$instance = (new static());
  54. }
  55. return self::$instance;
  56. }
  57. /**
  58. * 商品列表
  59. * @param $params
  60. * @param int $pageSize
  61. * @param int $userId
  62. * @return array
  63. */
  64. public function getDataList($params, $pageSize=12, $userId=0)
  65. {
  66. $list = $this->model->from('goods as a')
  67. ->where(['a.status'=>1,'a.mark'=>1])
  68. ->where('a.cost_price','>',0)
  69. ->where(function ($query) use($params){
  70. $supplyType = isset($params['supply_type'])? intval($params['supply_type']) : 0;
  71. if($supplyType>0){
  72. $query->where('a.supply_type',$supplyType);
  73. }
  74. })
  75. ->where(function ($query) use($params){
  76. $keyword = isset($params['kw'])? $params['kw'] : '';
  77. if($keyword){
  78. $query->where('a.goods_name','like',"%{$keyword}%")
  79. ->orWhere('a.spu_name','like',"%{$keyword}%")
  80. ->orWhere('a.tag','like',"%{$keyword}%");
  81. }
  82. })
  83. ->select(['a.*'])
  84. ->orderBy('a.sales','desc')
  85. ->orderBy('a.create_time','desc')
  86. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  87. $list = $list? $list->toArray() :[];
  88. if($list) {
  89. $locale = RedisService::get("caches:locale:lang_{$userId}");
  90. $locale = $locale ? $locale : session('locale_lang');
  91. $locale = $locale ? $locale : 'zh-cn';
  92. $supplyList = config('goods.supplyList');
  93. foreach ($list['data'] as &$item) {
  94. $item['supply_name'] = isset($supplyList[$item['supply_type']])? $supplyList[$item['supply_type']] : '';
  95. }
  96. unset($item);
  97. }else{
  98. $this->updateGoods();
  99. }
  100. return [
  101. 'total'=> isset($list['total'])? $list['total'] : 9,
  102. 'pageSize'=> $pageSize,
  103. 'list'=> isset($list['data'])? $list['data'] : [],
  104. ];
  105. }
  106. /**
  107. * 更新商品SKU数据到本地
  108. * @param int $pageSize
  109. * @param $params 参数
  110. * @return array|false
  111. */
  112. public function updateGoodsSku($pageSize=50,$params=[])
  113. {
  114. $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
  115. if(RedisService::get($cacheKey)){
  116. $this->error = 1047;
  117. return false;
  118. }
  119. $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
  120. $params = [
  121. 'limit'=> $pageSize>0?$pageSize : 50,
  122. 'page'=> 1,
  123. 'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  124. 'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0, // 渠道商
  125. 'title'=> isset($params['title'])? $params['title'] : '', // 标题关键词
  126. 'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '', // 分类ID
  127. 'begin_time'=> $lastTime? $lastTime : '', // 开始时间
  128. ];
  129. $goods = [];
  130. $updated = 0;
  131. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  132. if($datas && $datas['list']){
  133. foreach ($datas['list'] as &$item){
  134. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  135. if($goodsId && !$this->checkGoods($goodsId)){
  136. $goods[] = [
  137. 'goods_id'=> $goodsId,
  138. 'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
  139. 'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
  140. 'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
  141. 'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
  142. 'status'=> isset($item['status'])? intval($item['status']) : 1,
  143. 'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
  144. 'last_update_at'=> isset($item['time'])? $item['time'] : date('Y-m-d H:i:s'),
  145. 'create_time'=> time(),
  146. ];
  147. }else{
  148. $updated++;
  149. }
  150. }
  151. unset($item);
  152. }
  153. if($goods){
  154. RedisService::set($cacheKey, $goods, rand(5, 10));
  155. $this->model->insert($goods);
  156. }
  157. return ['count'=> count($goods),'updated'=> $updated];
  158. }
  159. /**
  160. *
  161. * @param int $pageSize
  162. * @param $params 参数
  163. * @return array|false
  164. */
  165. public function updateGoods($pageSize=50,$params=[])
  166. {
  167. $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
  168. if(RedisService::get($cacheKey)){
  169. $this->error = 1047;
  170. return false;
  171. }
  172. $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
  173. $params = [
  174. 'limit'=> $pageSize>0?$pageSize : 50,
  175. 'page'=> 1,
  176. 'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  177. 'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0, // 渠道商
  178. 'title'=> isset($params['title'])? $params['title'] : '', // 标题关键词
  179. 'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '', // 分类ID
  180. 'begin_time'=> $lastTime? $lastTime : '', // 开始时间
  181. ];
  182. $goods = [];
  183. $updated = 0;
  184. $error =0;
  185. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  186. if($datas && $datas['list']){
  187. foreach ($datas['list'] as &$item){
  188. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  189. if($goodsId && !$this->checkGoods($goodsId)){
  190. $info = $this->getApiInfo($goodsId);
  191. if($info){
  192. $goods[] = [
  193. 'goods_id'=> $goodsId,
  194. 'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
  195. 'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
  196. 'spu_name'=> isset($info['spu_name'])? $item['spu_name'] : '',
  197. 'main_img'=> isset($info['main_img'])? $item['main_img'] : '',
  198. 'detail_img'=> isset($info['detail_img'])? $item['detail_img'] : '',
  199. 'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
  200. 'brand_name'=> isset($info['brand_name'])? $info['brand_name'] : '',
  201. 'limit_num'=> isset($info['limit_num'])? intval($info['limit_num']) : 0,
  202. 'lowest_num'=> isset($info['lowest_num'])? intval($info['lowest_num']) : 1,
  203. 'cost_price'=> isset($info['cost_price'])? floatval($info['cost_price']) : 0,
  204. 'retail_price'=> isset($info['retail_price'])? floatval($info['retail_price']) : 0,
  205. 'profit'=> isset($info['profit'])? floatval($info['profit']) : 0,
  206. 'sku_list'=> isset($info['sku_list'])? json_encode($info['sku_list'], 256) : '',
  207. 'sku_total'=> isset($info['sku_total'])? intval($info['sku_total']) : 0,
  208. 'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
  209. 'status'=> isset($info['status'])? intval($info['status']) : 1,
  210. 'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
  211. 'last_update_at'=> isset($info['update_time'])? $info['update_time'] : (isset($item['time']) && $item['time']? $item['time'] : date('Y-m-d H:i:s')),
  212. 'create_time'=> time(),
  213. ];
  214. }else{
  215. $error++;
  216. }
  217. }else{
  218. $updated++;
  219. }
  220. }
  221. unset($item);
  222. }
  223. if($goods){
  224. RedisService::set($cacheKey, $goods, rand(5, 10));
  225. $this->model->insert($goods);
  226. }
  227. return ['count'=> count($goods),'updated'=> $updated,'errorCount'=> $error];
  228. }
  229. /**
  230. * 验证
  231. * @param $goodsId
  232. * @return bool
  233. */
  234. public function checkGoods($goodsId)
  235. {
  236. $cacheKey ="caches:goods:check_{$goodsId}";
  237. if(RedisService::get($cacheKey) || RedisService::exists($cacheKey)){
  238. return true;
  239. }
  240. $data = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->value('id');
  241. RedisService::set($cacheKey, $data, rand(30,60));
  242. return $data;
  243. }
  244. /**
  245. * 接口商品详情
  246. * @param $goodsId 商品ID
  247. * @param int $isReal 是否实时数据,0-是,1-否
  248. * @param int $type 数据类型:0-详情,1-仅SKU数据
  249. * @return array|false|mixed|string
  250. */
  251. public function getApiInfo($goodsId, $isReal=0, $type=0)
  252. {
  253. $cacheKey ="caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
  254. $info = RedisService::get($cacheKey);
  255. if(empty($info)){
  256. $params = [
  257. 'goods_id'=> $goodsId,
  258. 'is_real'=> $isReal,
  259. 'type'=> $type
  260. ];
  261. $info = SupplyService::make()->getApiData('getGoodsDetail',$params);
  262. if($info){
  263. RedisService::set($cacheKey, $info, rand(5,10));
  264. }
  265. }
  266. return $info;
  267. }
  268. public function apiCategory($num)
  269. {
  270. }
  271. }