LiveGoodsServices.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. * @param array $data
  60. * @return bool|mixed
  61. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function add(array $goods_info)
  67. {
  68. if (!$goods_info) throw new AdminException(100100);
  69. $product_ids = array_column($goods_info, 'id');
  70. $this->create($product_ids);
  71. $miniUpload = MiniProgramService::materialTemporaryService();
  72. /** @var DownloadImage $download */
  73. $download = app()->make(DownloadImage::class);
  74. $dataAll = $data = [];
  75. $time = time();
  76. foreach ($goods_info as $product) {
  77. $data = [
  78. 'product_id' => $product['id'],
  79. 'name' => Str::substrUTf8($product['store_name'], 12, 'UTF-8', ''),
  80. 'cover_img' => $product['image'] ?? '',
  81. 'price_type' => 1,
  82. 'cost_price' => $product['cost_price'] ?? 0.00,
  83. 'price' => $product['price'] ?? 0.00,
  84. 'url' => 'pages/goods_details/index?id=' . $product['id'],
  85. 'sort' => $product['sort'] ?? 0,
  86. 'add_time' => $time
  87. ];
  88. try {
  89. $path = root_path() . 'public' . $download->thumb(true)->downloadImage($data['cover_img'])['path'];
  90. $coverImgUrl = $miniUpload->uploadImage($path)->media_id;
  91. @unlink($path);
  92. } catch (\Throwable $e) {
  93. Log::error('添加直播商品图片错误,原因:' . $e->getMessage());
  94. $coverImgUrl = $data['cover_img'];
  95. }
  96. $res = MiniProgramService::addGoods($coverImgUrl, $data['name'], $data['price_type'], $data['url'], floatval($data['price']));
  97. $data['goods_id'] = $res['goodsId'];
  98. $data['audit_id'] = $res['auditId'];
  99. $data['audit_status'] = 1;
  100. $dataAll[] = $data;
  101. }
  102. if (!$goods = $this->dao->saveAll($dataAll)) {
  103. throw new AdminException(100022);
  104. }
  105. return true;
  106. }
  107. /**
  108. * 同步商品
  109. * @return bool
  110. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  111. */
  112. public function syncGoods()
  113. {
  114. $liveGoods = $this->dao->getColumn([['goods_id', '>', 0]], '*', 'id');
  115. if ($liveGoods) {
  116. /** @var DownloadImage $downloadImage */
  117. $downloadImage = app()->make(DownloadImage::class);
  118. foreach ($liveGoods as $good) {
  119. $path = root_path() . 'public' . $downloadImage->thumb(true)->downloadImage($good['cover_img'])['path'];
  120. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  121. @unlink($path);
  122. $res = MiniProgramService::addGoods($coverImgUrl, $good['name'], $good['price_type'], $good['url'], floatval($good['price']));
  123. $data['goods_id'] = $res['goodsId'];
  124. $data['audit_id'] = $res['auditId'];
  125. $data['audit_status'] = 1;
  126. if (!$this->dao->update($good['id'], $data, 'id')) {
  127. throw new AdminException(100039);
  128. }
  129. }
  130. }
  131. return true;
  132. }
  133. public function wxCreate($goods)
  134. {
  135. if ($goods['goods_id'])
  136. throw new AdminException(400427);
  137. $goods = $goods->toArray();
  138. /** @var DownloadImage $downloadImage */
  139. $downloadImage = app()->make(DownloadImage::class);
  140. $path = root_path() . 'public' . $downloadImage->thumb(true)->downloadImage($goods['cover_img'])['path'];
  141. $url = 'pages/goods_details/index?id=' . $goods['product_id'];
  142. $coverImgUrl = MiniProgramService::materialTemporaryService()->uploadImage($path)->media_id;
  143. @unlink($path);
  144. return MiniProgramService::addGoods($coverImgUrl, $goods['name'], 1, $url, floatval($goods['price']));
  145. }
  146. public function isShow(int $id, $is_show)
  147. {
  148. $goods = $this->dao->get(['id' => $id, 'audit_status' => 2]);
  149. if (!$goods) {
  150. throw new AdminException(400428);
  151. }
  152. $this->dao->update($id, ['is_show' => $is_show]);
  153. return true;
  154. }
  155. /**
  156. * 重新提交审核
  157. * @param int $id
  158. * @return mixed
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function audit(int $id)
  164. {
  165. $goods = $this->dao->get($id);
  166. if (!$goods) {
  167. throw new AdminException(100026);
  168. }
  169. if ($goods['audit_status'] != 0) {
  170. throw new AdminException(400429);
  171. }
  172. if (!$this->dao->update($id, ['audit_status' => 1])) {
  173. throw new AdminException(100007);
  174. }
  175. return MiniProgramService::auditGoods((int)$goods['good_id']);
  176. }
  177. /**
  178. * 撤回审核
  179. * @param int $id
  180. * @return bool
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\DbException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. */
  185. public function resetAudit(int $id)
  186. {
  187. $goods = $this->dao->get($id);
  188. if (!$goods) {
  189. throw new AdminException(100026);
  190. }
  191. if ($goods['audit_status'] == 0) {
  192. return true;
  193. }
  194. if ($goods['audit_status'] != 1) {
  195. throw new AdminException(400430);
  196. }
  197. if (!$this->dao->update($id, ['audit_status' => 0])) {
  198. throw new AdminException(100007);
  199. }
  200. return MiniProgramService::resetauditGoods((int)$goods['good_id'], $goods['audit_id']);
  201. }
  202. /**
  203. * 删除商品
  204. * @param int $id
  205. * @return bool
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\DbException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. public function delete(int $id)
  211. {
  212. $goods = $this->dao->get(['id' => $id, 'is_del' => 0]);
  213. if ($goods) {
  214. if (in_array($goods['audit_status'], [0, 1])) {
  215. throw new AdminException(400431);
  216. }
  217. if (!$this->dao->update($id, ['is_del' => 1])) {
  218. throw new AdminException(100008);
  219. }
  220. if (MiniProgramService::deleteGoods((int)$goods->goods_id)) {
  221. /** @var LiveRoomGoodsServices $liveRoomGoods */
  222. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  223. $liveRoomGoods->delete(['live_goods_id' => $id]);
  224. }
  225. }
  226. return true;
  227. }
  228. /**
  229. * 同步直播商品审核状态
  230. * @return bool
  231. */
  232. public function syncGoodStatus()
  233. {
  234. $goodsIds = $this->dao->goodsStatusAll();
  235. if (!count($goodsIds)) return true;
  236. $res = MiniProgramService::getGooodsInfo(array_keys($goodsIds));
  237. foreach ($res as $item) {
  238. if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
  239. $data = ['audit_status' => $item['audit_status']];
  240. //TODO 同步商品审核状态
  241. $this->dao->update((int)$goodsIds[$item['goods_id']]['id'], $data);
  242. }
  243. }
  244. return true;
  245. }
  246. }