GoodsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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\Common;
  12. use App\Models\GoodsModel;
  13. use App\Models\MemberModel;
  14. use App\Models\ShopModel;
  15. use App\Models\TradeModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 商品管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * Class GoodsService
  25. * @package App\Services\Common
  26. */
  27. class GoodsService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. * GoodsService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new GoodsModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * 列表数据
  54. * @param $params
  55. * @param int $pageSize
  56. * @return array
  57. */
  58. public function getDataList($params, $pageSize = 15)
  59. {
  60. $where = ['a.mark' => 1];
  61. $status = isset($params['status'])? $params['status'] : 0;
  62. if($status>0){
  63. $where['a.status'] = $status;
  64. }
  65. $isTrade = isset($params['is_trade'])? $params['is_trade'] : 0;
  66. if($isTrade>0){
  67. $where['a.is_trade'] = $isTrade;
  68. }
  69. $list = $this->model->from('goods as a')
  70. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  71. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  72. ->where($where)
  73. ->where(function ($query) use($params){
  74. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  75. if($keyword){
  76. $query->where('a.goods_name','like',"%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%")->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  77. }
  78. })
  79. ->where(function($query) use ($params){
  80. $time = isset($params['time'])? $params['time'] : '';
  81. if($time){
  82. $query->where('last_sell_time','<=', $time);
  83. }
  84. })
  85. ->select(['a.*','b.nickname','b.code as user_code','b.mobile as mobile','c.name as shop_name','c.code as shop_code'])
  86. ->orderBy('a.id','desc')
  87. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  88. $list = $list? $list->toArray() :[];
  89. if($list){
  90. foreach($list['data'] as &$item){
  91. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  92. $item['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : '';
  93. }
  94. }
  95. return [
  96. 'pageSize'=> $pageSize,
  97. 'total'=>isset($list['total'])? $list['total'] : 0,
  98. 'list'=> isset($list['data'])? $list['data'] : []
  99. ];
  100. }
  101. /**
  102. * 添加编辑
  103. * @return array
  104. * @since 2020/11/11
  105. * @author laravel开发员
  106. */
  107. public function edit()
  108. {
  109. // 请求参数
  110. $data = request()->all();
  111. // thumb处理
  112. $thumb = isset($data['thumb'])? trim($data['thumb']) : '';
  113. if ($thumb && strpos($thumb, "temp")) {
  114. $data['thumb'] = save_image($thumb, 'member');
  115. } else if($thumb){
  116. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  117. }
  118. if(!isset($data['code']) || empty($data['code'])){
  119. $data['code'] = isset($data['id'])&&$data['id']? 'G'.$data['id'] :'G'.(GoodsModel::max('id')+1);
  120. }
  121. if(!isset($data['price']) || empty($data['price'])){
  122. $data['price'] = $data['sell_price'];
  123. }
  124. //
  125. // $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  126. // $feeRate = $feeRate? $feeRate : '2.5';
  127. // $data['fee'] = round($data['price'] * $feeRate/100, 0);
  128. return parent::edit($data); // TODO: Change the autogenerated stub
  129. }
  130. /**
  131. * 获取资料详情
  132. * @param $where
  133. * @param array $field
  134. */
  135. public function getInfo($where, array $field = [])
  136. {
  137. $cacheKey = "caches:goods:".(!is_array($where)? $where : md5(json_encode($where)));
  138. $info = RedisService::get($cacheKey);
  139. if($info){
  140. return $info;
  141. }
  142. $field = $field ? $field : '*';
  143. if (is_array($where)) {
  144. $info = $this->model->where($where)->select($field)->first();
  145. } else {
  146. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  147. }
  148. $info = $info ? $info->toArray() : [];
  149. if($info){
  150. $info['thumb'] = $info['thumb'] ? get_image_url($info['thumb']) : '';
  151. RedisService::set($cacheKey, $info, rand(5,10));
  152. }
  153. return $info;
  154. }
  155. /**
  156. * 验证是否抢购有新的商品
  157. * @param $userId
  158. * @return mixed
  159. */
  160. public function checkNewGoods($userId)
  161. {
  162. return $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])
  163. ->where('create_time','>=', strtotime(date('Y-m-d')))
  164. ->count('id');
  165. }
  166. /**
  167. * 拆分
  168. * @param $info
  169. * @param $goods
  170. * @return bool
  171. */
  172. public function split($goodsId)
  173. {
  174. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  175. $splitNum = isset($goods['split_num'])? $goods['split_num'] : 0;
  176. if($splitNum<=0){
  177. return false;
  178. }
  179. $datas = [];
  180. $sumPrice = 0;
  181. for($i=1; $i<= $splitNum; $i++){
  182. if($i < $splitNum){
  183. $price = round($info['price']/$splitNum, 0);
  184. $sumPrice += $price;
  185. }else{
  186. $price = round($info['price'] - $sumPrice, 0);
  187. }
  188. $datas[] = [
  189. 'user_id'=> $goods['user_id'],
  190. 'shop_id'=> $goods['shop_id'],
  191. 'goods_name'=> $goods['goods_name'],
  192. 'code'=> $goods['code'].'-'.$i,
  193. 'source_price'=> round($goods['source_price']/$splitNum, 0),
  194. 'sell_price'=> round($goods['sell_price']/$splitNum, 0),
  195. 'price'=> $price,
  196. 'fee'=> 0,
  197. 'thumb'=> $goods['thumb'],
  198. 'content'=> $goods['content'],
  199. 'split_num'=> $goods['split_num'],
  200. 'split_price'=> $goods['split_price'],
  201. 'last_sell_time'=> time(),
  202. 'create_time'=> time(),
  203. 'update_time'=> time(),
  204. 'mark'=>1,
  205. 'status'=>1,
  206. ];
  207. }
  208. DB::beginTransaction();
  209. if($datas){
  210. if(!$this->model->insert($datas)){
  211. DB::rollBack();
  212. return false;
  213. }
  214. if(!$this->model->where(['id'=> $goods['id'],'mark'=>1])->update(['status'=>2,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){
  215. DB::rollBack();
  216. return false;
  217. }
  218. if($info && !TradeModel::where(['id'=> $info['id'],'mark'=>1])->update(['status'=>-1,'is_split'=>1,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){
  219. DB::rollBack();
  220. return false;
  221. }
  222. }
  223. DB::commit();
  224. return true;
  225. }
  226. /**
  227. * 转场
  228. * @param $goodsId
  229. */
  230. public function change($goodsId)
  231. {
  232. $params = request()->all();
  233. $shopId = isset($params['shop_id'])? $params['shop_id']:0;
  234. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  235. if(empty($goods)){
  236. $this->error = 2061;
  237. return false;
  238. }
  239. $shopInfo = ShopModel::where(['id'=> $shopId,'mark'=>1])->first();
  240. if(empty($shopInfo)){
  241. $this->error = 2062;
  242. return false;
  243. }
  244. if($this->model->where(['id'=> $goodsId])->update(['shop_id'=> $shopId,'update_time'=> time(),'remark'=> '店长转场'])){
  245. $this->error = 1002;
  246. return true;
  247. }
  248. return false;
  249. }
  250. /**
  251. * 转会员
  252. * @param $goodsId
  253. */
  254. public function switchUser($params)
  255. {
  256. $userId = isset($params['user_id'])? $params['user_id']:0;
  257. $ids = isset($params['ids'])? $params['ids']:[];
  258. if(empty($ids)){
  259. $this->error = 2063;
  260. return false;
  261. }
  262. $goodsList = $this->whereIn('id',$ids)->where(['mark'=>1])->first();
  263. if(empty($goodsList)){
  264. $this->error = 2061;
  265. return false;
  266. }
  267. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->first();
  268. if(empty($memberInfo)){
  269. $this->error = 2062;
  270. return false;
  271. }
  272. if($this->model->whereIn('id', $ids)->update(['user_id'=> $userId,'update_time'=> time(),'remark'=> '商品转会员'])){
  273. $this->error = 1002;
  274. return true;
  275. }
  276. return false;
  277. }
  278. /**
  279. * 封存/解封
  280. * @param $goodsId
  281. */
  282. public function lock($goodsId)
  283. {
  284. $params = request()->all();
  285. $status = isset($params['status'])? $params['status'] : 2;
  286. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  287. if(empty($goods)){
  288. $this->error = 2061;
  289. return false;
  290. }
  291. if($this->model->where(['id'=> $goodsId])->update(['status'=> $status,'update_time'=> time(),'remark'=> '店长封存'])){
  292. $this->error = 1002;
  293. return true;
  294. }
  295. return false;
  296. }
  297. /**
  298. * 修改商品信息
  299. * @param $goodsId
  300. * @return bool
  301. */
  302. public function updateData($goodsId)
  303. {
  304. $params = request()->all();
  305. $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first();
  306. if(empty($goods)){
  307. $this->error = 2061;
  308. return false;
  309. }
  310. $data = [
  311. 'thumb'=> isset($params['thumb'])? $params['thumb']: '',
  312. 'goods_name'=> isset($params['goods_name'])? $params['goods_name']: '',
  313. 'update_time'=>time(),
  314. ];
  315. if($this->model->where(['id'=> $goodsId])->update($data)){
  316. $this->error = 1008;
  317. return true;
  318. }
  319. $this->error = 1009;
  320. return false;
  321. }
  322. }