LiveGoodsServices.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\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\DownloadImageService;
  18. use crmeb\services\wechat\MiniProgram;
  19. use crmeb\services\wechat\OfficialAccount;
  20. use think\facade\Log;
  21. /**
  22. * 直播商品
  23. * Class LiveGoodsServices
  24. * @package app\services\activity\live
  25. * @mixin LiveGoodsDao
  26. */
  27. class LiveGoodsServices extends BaseServices
  28. {
  29. /**
  30. * LiveGoodsServices constructor.
  31. * @param LiveGoodsDao $dao
  32. */
  33. public function __construct(LiveGoodsDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. public function getList(array $where)
  38. {
  39. [$page, $limit] = $this->getPageValue();
  40. $where['is_del'] = 0;
  41. $list = $this->dao->getList($where, '*', ['product'], $page, $limit);
  42. $count = $this->dao->count($where);
  43. return compact('count', 'list');
  44. }
  45. public function create(array $product_ids)
  46. {
  47. /** @var StoreProductServices $product */
  48. $productServices = app()->make(StoreProductServices::class);
  49. $products = $productServices->getColumn([['id', 'IN', $product_ids], ['is_del', '=', 0], ['is_show', '=', 1]], 'id,image,store_name,price,price as cost_price,stock', 'id');
  50. if (count($product_ids) != count($products)) {
  51. throw new AdminException('已选商品中包括已下架或移入回收站商品');
  52. }
  53. $checkGoods = $this->dao->getCount([['product_id', 'IN', $product_ids], ['is_del', '=', 0], ['audit_status', '<>', 3]]);
  54. if ($checkGoods > 0) {
  55. throw new AdminException('其中有商品已经添加');
  56. }
  57. return array_merge($products);
  58. }
  59. /**
  60. * @param array $data
  61. * @return bool|mixed
  62. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function add(array $goods_info)
  68. {
  69. $product_ids = array_column($goods_info, 'id');
  70. $this->create($product_ids);
  71. $miniUpload = OfficialAccount::materialService();
  72. /** @var DownloadImageService $download */
  73. $download = app()->make(DownloadImageService::class);
  74. $dataAll = $data = [];
  75. $time = time();
  76. foreach ($goods_info as $product) {
  77. $data = [
  78. 'product_id' => $product['id'],
  79. 'name' => 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 = MiniProgram::temporaryUpload($path)->media_id;
  91. @unlink($path);
  92. } catch (\Throwable $e) {
  93. Log::error('添加直播商品图片错误,原因:' . $e->getMessage());
  94. $coverImgUrl = $data['cover_img'];
  95. }
  96. $res = MiniProgram::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('添加商品失败');
  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. foreach ($liveGoods as $good) {
  117. $path = root_path() . 'public' . app()->make(DownloadImageService::class)->thumb(true)->downloadImage($good['cover_img'])['path'];
  118. $materialInfo = MiniProgram::temporaryUpload($path);
  119. $coverImgUrl = $materialInfo['media_id'];
  120. @unlink($path);
  121. $res = MiniProgram::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('同步失败');
  127. }
  128. }
  129. }
  130. return true;
  131. }
  132. public function wxCreate($goods)
  133. {
  134. if ($goods['goods_id'])
  135. throw new AdminException('商品已创建');
  136. $goods = $goods->toArray();
  137. $path = root_path() . 'public' . app()->make(DownloadImageService::class)->thumb(true)->downloadImage($goods['cover_img'])['path'];
  138. $url = 'pages/goods_details/index?id=' . $goods['product_id'];
  139. $coverImgUrl = MiniProgram::temporaryUpload($path)->media_id;
  140. @unlink($path);
  141. return MiniProgram::addGoods($coverImgUrl, $goods['name'], 1, $url, floatval($goods['price']));
  142. }
  143. public function isShow(int $id, $is_show)
  144. {
  145. $goods = $this->dao->get(['id' => $id, 'audit_status' => 2]);
  146. if (!$goods) {
  147. throw new AdminException('审核中或审核失败不允许此操作');
  148. }
  149. $this->dao->update($id, ['is_show' => $is_show]);
  150. return $is_show == 1 ? '显示成功' : '隐藏成功';
  151. }
  152. /**
  153. * 重新提交审核
  154. * @param int $id
  155. * @return mixed
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. */
  160. public function audit(int $id)
  161. {
  162. $goods = $this->dao->get($id);
  163. if (!$goods) {
  164. throw new AdminException('数据不存在');
  165. }
  166. if ($goods['audit_status'] != 0) {
  167. throw new AdminException('在审核中或已经审核通过');
  168. }
  169. if (!$this->dao->update($id, ['audit_status' => 1])) {
  170. throw new AdminException('修改审核状态失败');
  171. }
  172. return MiniProgram::auditGoods((int)$goods['good_id']);
  173. }
  174. /**
  175. * 撤回审核
  176. * @param int $id
  177. * @return bool
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public function resetAudit(int $id)
  183. {
  184. $goods = $this->dao->get($id);
  185. if (!$goods) {
  186. throw new AdminException('数据不存在');
  187. }
  188. if ($goods['audit_status'] == 0) {
  189. return true;
  190. }
  191. if ($goods['audit_status'] != 1) {
  192. throw new AdminException('审核通过或失败');
  193. }
  194. if (!$this->dao->update($id, ['audit_status' => 0])) {
  195. throw new AdminException('修改审核状态失败');
  196. }
  197. return MiniProgram::resetauditGoods((int)$goods['good_id'], $goods['audit_id']);
  198. }
  199. /**
  200. * 删除商品
  201. * @param int $id
  202. * @return bool
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\DbException
  205. * @throws \think\db\exception\ModelNotFoundException
  206. */
  207. public function delete(int $id)
  208. {
  209. $goods = $this->dao->get(['id' => $id, 'is_del' => 0]);
  210. if ($goods) {
  211. if (in_array($goods['audit_status'], [0, 1])) {
  212. throw new AdminException('商品审核中,无法删除');
  213. }
  214. if (!$this->dao->update($id, ['is_del' => 1])) {
  215. throw new AdminException('删除失败');
  216. }
  217. if (MiniProgram::deleteGoods((int)$goods->goods_id)) {
  218. /** @var LiveRoomGoodsServices $liveRoomGoods */
  219. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  220. $liveRoomGoods->delete(['live_goods_id' => $id]);
  221. }
  222. }
  223. return true;
  224. }
  225. /**
  226. * 同步直播商品审核状态
  227. * @return bool
  228. */
  229. public function syncGoodStatus()
  230. {
  231. $goodsIds = $this->dao->goodsStatusAll();
  232. if (!count($goodsIds)) return true;
  233. $res = MiniProgram::getGooodsInfo(array_keys($goodsIds));
  234. foreach ($res as $item) {
  235. if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
  236. $data = ['audit_status' => $item['audit_status']];
  237. // 同步商品审核状态
  238. $this->dao->update((int)$goodsIds[$item['goods_id']]['id'], $data);
  239. }
  240. }
  241. return true;
  242. }
  243. }