LiveGoodsServices.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. @unlink($path);
  93. $coverImgUrl = $data['cover_img'];
  94. }
  95. $res = MiniProgramService::addGoods($coverImgUrl, $data['name'], $data['price_type'], $data['url'], floatval($data['price']));
  96. $data['goods_id'] = $res['goodsId'];
  97. $data['audit_id'] = $res['auditId'];
  98. $data['audit_status'] = 1;
  99. $dataAll[] = $data;
  100. }
  101. if (!$goods = $this->dao->saveAll($dataAll)) {
  102. throw new AdminException(100022);
  103. }
  104. return true;
  105. }
  106. /**
  107. * 同步商品
  108. * @return bool
  109. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  110. */
  111. public function syncGoods()
  112. {
  113. $liveGoods = $this->dao->getColumn([['goods_id', '>', 0]], '*', 'id');
  114. if ($liveGoods) {
  115. /** @var DownloadImage $downloadImage */
  116. $downloadImage = app()->make(DownloadImage::class);
  117. foreach ($liveGoods as $good) {
  118. $path = root_path() . 'public' . $downloadImage->thumb(true)->downloadImage($good['cover_img'])['path'];
  119. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  120. @unlink($path);
  121. $res = MiniProgramService::addGoods($coverImgUrl, $good['name'], $good['price_type'], $good['url'], floatval($good['price']));
  122. $data['goods_id'] = $res['goodsId'];
  123. $data['audit_id'] = $res['auditId'];
  124. $data['audit_status'] = 1;
  125. if (!$this->dao->update($good['id'], $data, 'id')) {
  126. throw new AdminException(100039);
  127. }
  128. }
  129. }
  130. return true;
  131. }
  132. public function wxCreate($goods)
  133. {
  134. if ($goods['goods_id'])
  135. throw new AdminException(400427);
  136. $goods = $goods->toArray();
  137. /** @var DownloadImage $downloadImage */
  138. $downloadImage = app()->make(DownloadImage::class);
  139. $path = root_path() . 'public' . $downloadImage->thumb(true)->downloadImage($goods['cover_img'])['path'];
  140. $url = 'pages/goods_details/index?id=' . $goods['product_id'];
  141. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  142. @unlink($path);
  143. return MiniProgramService::addGoods($coverImgUrl, $goods['name'], 1, $url, floatval($goods['price']));
  144. }
  145. public function isShow(int $id, $is_show)
  146. {
  147. $goods = $this->dao->get(['id' => $id, 'audit_status' => 2]);
  148. if (!$goods) {
  149. throw new AdminException(400428);
  150. }
  151. $this->dao->update($id, ['is_show' => $is_show]);
  152. return true;
  153. }
  154. /**
  155. * 重新提交审核
  156. * @param int $id
  157. * @return mixed
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function audit(int $id)
  163. {
  164. $goods = $this->dao->get($id);
  165. if (!$goods) {
  166. throw new AdminException(100026);
  167. }
  168. if ($goods['audit_status'] != 0) {
  169. throw new AdminException(400429);
  170. }
  171. if (!$this->dao->update($id, ['audit_status' => 1])) {
  172. throw new AdminException(100007);
  173. }
  174. return MiniProgramService::auditGoods((int)$goods['good_id']);
  175. }
  176. /**
  177. * 撤回审核
  178. * @param int $id
  179. * @return bool
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function resetAudit(int $id)
  185. {
  186. $goods = $this->dao->get($id);
  187. if (!$goods) {
  188. throw new AdminException(100026);
  189. }
  190. if ($goods['audit_status'] == 0) {
  191. return true;
  192. }
  193. if ($goods['audit_status'] != 1) {
  194. throw new AdminException(400430);
  195. }
  196. if (!$this->dao->update($id, ['audit_status' => 0])) {
  197. throw new AdminException(100007);
  198. }
  199. return MiniProgramService::resetauditGoods((int)$goods['good_id'], $goods['audit_id']);
  200. }
  201. /**
  202. * 删除商品
  203. * @param int $id
  204. * @return bool
  205. * @throws \think\db\exception\DataNotFoundException
  206. * @throws \think\db\exception\DbException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. */
  209. public function delete(int $id)
  210. {
  211. $goods = $this->dao->get(['id' => $id, 'is_del' => 0]);
  212. if ($goods) {
  213. if (in_array($goods['audit_status'], [0, 1])) {
  214. throw new AdminException(400431);
  215. }
  216. if (!$this->dao->update($id, ['is_del' => 1])) {
  217. throw new AdminException(100008);
  218. }
  219. if (MiniProgramService::deleteGoods((int)$goods->goods_id)) {
  220. /** @var LiveRoomGoodsServices $liveRoomGoods */
  221. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  222. $liveRoomGoods->delete(['live_goods_id' => $id]);
  223. }
  224. }
  225. return true;
  226. }
  227. /**
  228. * 同步直播商品审核状态
  229. * @return bool
  230. */
  231. public function syncGoodStatus()
  232. {
  233. $goodsIds = $this->dao->goodsStatusAll();
  234. if (!count($goodsIds)) return true;
  235. $res = MiniProgramService::getGooodsInfo(array_keys($goodsIds));
  236. foreach ($res as $item) {
  237. if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
  238. $data = ['audit_status' => $item['audit_status']];
  239. //TODO 同步商品审核状态
  240. $this->dao->update((int)$goodsIds[$item['goods_id']]['id'], $data);
  241. }
  242. }
  243. return true;
  244. }
  245. }