LiveGoodsServices.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\live;
  13. use app\dao\live\LiveGoodsDao;
  14. use app\services\BaseServices;
  15. use app\services\product\product\StoreProductServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\MiniProgramService;
  18. use crmeb\services\DownloadImageService;
  19. use crmeb\utils\Str;
  20. use think\facade\Log;
  21. /**
  22. * Class LiveGoodsServices
  23. * @package app\services\live
  24. */
  25. class LiveGoodsServices extends BaseServices
  26. {
  27. /**
  28. * LiveGoodsServices constructor.
  29. * @param LiveGoodsDao $dao
  30. */
  31. public function __construct(LiveGoodsDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. public function getList(array $where)
  36. {
  37. [$page, $limit] = $this->getPageValue();
  38. $where['is_del'] = 0;
  39. $list = $this->dao->getList($where, '*', ['product'], $page, $limit);
  40. $count = $this->dao->count($where);
  41. return compact('count', 'list');
  42. }
  43. public function create(array $product_ids)
  44. {
  45. /** @var StoreProductServices $product */
  46. $productServices = app()->make(StoreProductServices::class);
  47. $products = $productServices->getColumn([['id', 'IN', $product_ids], ['is_del', '=', 0], ['is_show', '=', 1]], 'id,image,store_name,price,price as cost_price,stock', 'id');
  48. if (count($product_ids) != count($products)) {
  49. throw new AdminException('已选商品中包括已下架或移入回收站商品');
  50. }
  51. $checkGoods = $this->dao->getCount([['product_id', 'IN', $product_ids], ['is_del', '=', 0], ['audit_status', '<>', 3]]);
  52. if ($checkGoods > 0) {
  53. throw new AdminException('其中有商品已经添加');
  54. }
  55. return array_merge($products);
  56. }
  57. /**
  58. * @param array $data
  59. * @return bool|mixed
  60. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function add(array $goods_info)
  66. {
  67. $product_ids = array_column($goods_info, 'id');
  68. $this->create($product_ids);
  69. $miniUpload = MiniProgramService::materialTemporaryService();
  70. /** @var DownloadImageService $download */
  71. $download = app()->make(DownloadImageService::class);
  72. $dataAll = $data = [];
  73. $time = time();
  74. foreach ($goods_info as $product) {
  75. $data = [
  76. 'product_id' => $product['id'],
  77. 'name' => Str::substrUTf8($product['store_name'], 12, 'UTF-8', ''),
  78. 'cover_img' => $product['image'] ?? '',
  79. 'price_type' => 1,
  80. 'cost_price' => $product['cost_price'] ?? 0.00,
  81. 'price' => $product['price'] ?? 0.00,
  82. 'url' => 'pages/goods_details/index?id=' . $product['id'],
  83. 'sort' => $product['sort'] ?? 0,
  84. 'add_time' => $time
  85. ];
  86. try {
  87. $path = root_path() . 'public' . $download->thumb(true)->downloadImage($data['cover_img'])['path'];
  88. $coverImgUrl = $miniUpload->uploadImage($path)->media_id;
  89. @unlink($path);
  90. } catch (\Throwable $e) {
  91. Log::error('添加直播商品图片错误,原因:' . $e->getMessage());
  92. $coverImgUrl = $data['cover_img'];
  93. }
  94. $res = MiniProgramService::addGoods($coverImgUrl, $data['name'], $data['price_type'], $data['url'], floatval($data['price']));
  95. $data['goods_id'] = $res['goodsId'];
  96. $data['audit_id'] = $res['auditId'];
  97. $data['audit_status'] = 1;
  98. $dataAll[] = $data;
  99. }
  100. if (!$goods = $this->dao->saveAll($dataAll)) {
  101. throw new AdminException('添加商品失败');
  102. }
  103. return true;
  104. }
  105. /**
  106. * 同步商品
  107. * @return bool
  108. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  109. */
  110. public function syncGoods()
  111. {
  112. $liveGoods = $this->dao->getColumn([['goods_id', '>', 0]], '*', 'id');
  113. if ($liveGoods) {
  114. foreach ($liveGoods as $good) {
  115. $path = root_path() . 'public' . app()->make(DownloadImageService::class)->thumb(true)->downloadImage($good['cover_img'])['path'];
  116. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  117. @unlink($path);
  118. $res = MiniProgramService::addGoods($coverImgUrl, $good['name'], $good['price_type'], $good['url'], floatval($good['price']));
  119. $data['goods_id'] = $res['goodsId'];
  120. $data['audit_id'] = $res['auditId'];
  121. $data['audit_status'] = 1;
  122. if (!$this->dao->update($good['id'], $data, 'id')) {
  123. throw new AdminException('同步失败');
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. public function wxCreate($goods)
  130. {
  131. if ($goods['goods_id'])
  132. throw new AdminException('商品已创建');
  133. $goods = $goods->toArray();
  134. $path = root_path() . 'public' . app()->make(DownloadImageService::class)->thumb(true)->downloadImage($goods['cover_img'])['path'];
  135. $url = 'pages/goods_details/index?id=' . $goods['product_id'];
  136. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  137. @unlink($path);
  138. return MiniProgramService::addGoods($coverImgUrl, $goods['name'], 1, $url, floatval($goods['price']));
  139. }
  140. public function isShow(int $id, $is_show)
  141. {
  142. $goods = $this->dao->get(['id' => $id, 'audit_status' => 2]);
  143. if (!$goods) {
  144. throw new AdminException('审核中或审核失败不允许此操作');
  145. }
  146. $this->dao->update($id, ['is_show' => $is_show]);
  147. return $is_show == 1 ? '显示成功' : '隐藏成功';
  148. }
  149. /**
  150. * 重新提交审核
  151. * @param int $id
  152. * @return mixed
  153. * @throws \think\db\exception\DataNotFoundException
  154. * @throws \think\db\exception\DbException
  155. * @throws \think\db\exception\ModelNotFoundException
  156. */
  157. public function audit(int $id)
  158. {
  159. $goods = $this->dao->get($id);
  160. if (!$goods) {
  161. throw new AdminException('数据不存在');
  162. }
  163. if ($goods['audit_status'] != 0) {
  164. throw new AdminException('在审核中或已经审核通过');
  165. }
  166. if (!$this->dao->update($id, ['audit_status' => 1])) {
  167. throw new AdminException('修改审核状态失败');
  168. }
  169. return MiniProgramService::auditGoods((int)$goods['good_id']);
  170. }
  171. /**
  172. * 撤回审核
  173. * @param int $id
  174. * @return bool
  175. * @throws \think\db\exception\DataNotFoundException
  176. * @throws \think\db\exception\DbException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. */
  179. public function resetAudit(int $id)
  180. {
  181. $goods = $this->dao->get($id);
  182. if (!$goods) {
  183. throw new AdminException('数据不存在');
  184. }
  185. if ($goods['audit_status'] == 0) {
  186. return true;
  187. }
  188. if ($goods['audit_status'] != 1) {
  189. throw new AdminException('审核通过或失败');
  190. }
  191. if (!$this->dao->update($id, ['audit_status' => 0])) {
  192. throw new AdminException('修改审核状态失败');
  193. }
  194. return MiniProgramService::resetauditGoods((int)$goods['good_id'], $goods['audit_id']);
  195. }
  196. /**
  197. * 删除商品
  198. * @param int $id
  199. * @return bool
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\DbException
  202. * @throws \think\db\exception\ModelNotFoundException
  203. */
  204. public function delete(int $id)
  205. {
  206. $goods = $this->dao->get(['id' => $id, 'is_del' => 0]);
  207. if ($goods) {
  208. if (in_array($goods['audit_status'], [0, 1])) {
  209. throw new AdminException('商品审核中,无法删除');
  210. }
  211. if (!$this->dao->update($id, ['is_del' => 1])) {
  212. throw new AdminException('删除失败');
  213. }
  214. if (MiniProgramService::deleteGoods((int)$goods->goods_id)) {
  215. /** @var LiveRoomGoodsServices $liveRoomGoods */
  216. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  217. $liveRoomGoods->delete(['live_goods_id' => $id]);
  218. }
  219. }
  220. return true;
  221. }
  222. /**
  223. * 同步直播商品审核状态
  224. * @return bool
  225. */
  226. public function syncGoodStatus()
  227. {
  228. $goodsIds = $this->dao->goodsStatusAll();
  229. if (!count($goodsIds)) return true;
  230. $res = MiniProgramService::getGooodsInfo(array_keys($goodsIds));
  231. foreach ($res as $item) {
  232. if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
  233. $data = ['audit_status' => $item['audit_status']];
  234. //TODO 同步商品审核状态
  235. $this->dao->update((int)$goodsIds[$item['goods_id']]['id'], $data);
  236. }
  237. }
  238. return true;
  239. }
  240. }