LiveGoodsServices.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\activity\live;
  13. use app\dao\activity\live\LiveGoodsDao;
  14. use app\services\BaseServices;
  15. use app\services\product\product\StoreProductServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\app\MiniProgramService;
  18. use crmeb\utils\DownloadImage;
  19. use crmeb\utils\Str;
  20. use think\facade\Log;
  21. /**
  22. * Class LiveGoodsServices
  23. * @package app\services\activity\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. if (!$product_ids) throw new AdminException(100100);
  46. /** @var StoreProductServices $product */
  47. $productServices = app()->make(StoreProductServices::class);
  48. $products = $productServices->getColumn([['id', 'IN', $product_ids], ['is_del', '=', 0], ['is_show', '=', 1]], 'id,image,store_name,price,price as cost_price,stock', 'id');
  49. if (count($product_ids) != count($products)) {
  50. throw new AdminException(400091);
  51. }
  52. $checkGoods = $this->dao->getCount([['product_id', 'IN', $product_ids], ['is_del', '=', 0], ['audit_status', '<>', 3]]);
  53. if ($checkGoods > 0) {
  54. throw new AdminException(100022);
  55. }
  56. return array_merge($products);
  57. }
  58. /**
  59. * 添加直播商品
  60. * @param array $goods_info
  61. * @return bool
  62. * @throws \Exception
  63. */
  64. public function add(array $goods_info)
  65. {
  66. if (!$goods_info) throw new AdminException(100100);
  67. $product_ids = array_column($goods_info, 'id');
  68. $this->create($product_ids);
  69. $miniUpload = MiniProgramService::materialTemporaryService();
  70. /** @var DownloadImage $download */
  71. $download = app()->make(DownloadImage::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(100022);
  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. /** @var DownloadImage $downloadImage */
  115. $downloadImage = app()->make(DownloadImage::class);
  116. foreach ($liveGoods as $good) {
  117. $path = root_path() . 'public' . $downloadImage->thumb(true)->downloadImage($good['cover_img'])['path'];
  118. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  119. @unlink($path);
  120. $res = MiniProgramService::addGoods($coverImgUrl, $good['name'], $good['price_type'], $good['url'], floatval($good['price']));
  121. $data['goods_id'] = $res['goodsId'];
  122. $data['audit_id'] = $res['auditId'];
  123. $data['audit_status'] = 1;
  124. if (!$this->dao->update($good['id'], $data, 'id')) {
  125. throw new AdminException(100039);
  126. }
  127. }
  128. }
  129. return true;
  130. }
  131. public function wxCreate($goods)
  132. {
  133. if ($goods['goods_id'])
  134. throw new AdminException(400427);
  135. $goods = $goods->toArray();
  136. /** @var DownloadImage $downloadImage */
  137. $downloadImage = app()->make(DownloadImage::class);
  138. $path = root_path() . 'public' . $downloadImage->thumb(true)->downloadImage($goods['cover_img'])['path'];
  139. $url = 'pages/goods_details/index?id=' . $goods['product_id'];
  140. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  141. @unlink($path);
  142. return MiniProgramService::addGoods($coverImgUrl, $goods['name'], 1, $url, floatval($goods['price']));
  143. }
  144. public function isShow(int $id, $is_show)
  145. {
  146. $goods = $this->dao->get(['id' => $id, 'audit_status' => 2]);
  147. if (!$goods) {
  148. throw new AdminException(400428);
  149. }
  150. $this->dao->update($id, ['is_show' => $is_show]);
  151. return true;
  152. }
  153. /**
  154. * 重新提交审核
  155. * @param int $id
  156. * @return mixed
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function audit(int $id)
  162. {
  163. $goods = $this->dao->get($id);
  164. if (!$goods) {
  165. throw new AdminException(100026);
  166. }
  167. if ($goods['audit_status'] != 0) {
  168. throw new AdminException(400429);
  169. }
  170. if (!$this->dao->update($id, ['audit_status' => 1])) {
  171. throw new AdminException(100007);
  172. }
  173. return MiniProgramService::auditGoods((int)$goods['good_id']);
  174. }
  175. /**
  176. * 撤回审核
  177. * @param int $id
  178. * @return bool
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\DbException
  181. * @throws \think\db\exception\ModelNotFoundException
  182. */
  183. public function resetAudit(int $id)
  184. {
  185. $goods = $this->dao->get($id);
  186. if (!$goods) {
  187. throw new AdminException(100026);
  188. }
  189. if ($goods['audit_status'] == 0) {
  190. return true;
  191. }
  192. if ($goods['audit_status'] != 1) {
  193. throw new AdminException(400430);
  194. }
  195. if (!$this->dao->update($id, ['audit_status' => 0])) {
  196. throw new AdminException(100007);
  197. }
  198. return MiniProgramService::resetauditGoods((int)$goods['good_id'], $goods['audit_id']);
  199. }
  200. /**
  201. * 删除商品
  202. * @param int $id
  203. * @return bool
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\DbException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. */
  208. public function delete(int $id)
  209. {
  210. $goods = $this->dao->get(['id' => $id, 'is_del' => 0]);
  211. if ($goods) {
  212. if (in_array($goods['audit_status'], [0, 1])) {
  213. throw new AdminException(400431);
  214. }
  215. if (!$this->dao->update($id, ['is_del' => 1])) {
  216. throw new AdminException(100008);
  217. }
  218. if (MiniProgramService::deleteGoods((int)$goods->goods_id)) {
  219. /** @var LiveRoomGoodsServices $liveRoomGoods */
  220. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  221. $liveRoomGoods->delete(['live_goods_id' => $id]);
  222. }
  223. }
  224. return true;
  225. }
  226. /**
  227. * 同步直播商品审核状态
  228. * @return bool
  229. */
  230. public function syncGoodStatus()
  231. {
  232. $goodsIds = $this->dao->goodsStatusAll();
  233. if (!count($goodsIds)) return true;
  234. $res = MiniProgramService::getGooodsInfo(array_keys($goodsIds));
  235. foreach ($res as $item) {
  236. if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
  237. $data = ['audit_status' => $item['audit_status']];
  238. //TODO 同步商品审核状态
  239. $this->dao->update((int)$goodsIds[$item['goods_id']]['id'], $data);
  240. }
  241. }
  242. return true;
  243. }
  244. }