BroadcastGoodsRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. namespace app\common\repositories\store\broadcast;
  12. use app\common\dao\store\broadcast\BroadcastGoodsDao;
  13. use app\common\repositories\BaseRepository;
  14. use crmeb\jobs\ApplyBroadcastGoodsJob;
  15. use crmeb\services\DownloadImageService;
  16. use crmeb\services\MiniProgramService;
  17. use crmeb\services\SwooleTaskService;
  18. use Exception;
  19. use FormBuilder\Exception\FormBuilderException;
  20. use FormBuilder\Factory\Elm;
  21. use FormBuilder\Form;
  22. use think\db\exception\DataNotFoundException;
  23. use think\db\exception\DbException;
  24. use think\db\exception\ModelNotFoundException;
  25. use think\exception\ValidateException;
  26. use think\facade\Db;
  27. use think\facade\Queue;
  28. use think\facade\Route;
  29. use think\Model;
  30. /**
  31. * Class BroadcastGoodsRepository
  32. * @package app\common\repositories\store\broadcast
  33. * @author xaboy
  34. * @day 2020/7/30
  35. * @mixin BroadcastGoodsDao
  36. */
  37. class BroadcastGoodsRepository extends BaseRepository
  38. {
  39. /**
  40. * @var BroadcastGoodsDao
  41. */
  42. protected $dao;
  43. public function __construct(BroadcastGoodsDao $dao)
  44. {
  45. $this->dao = $dao;
  46. }
  47. public function getList($merId, array $where, $page, $limit)
  48. {
  49. $where['mer_id'] = $merId;
  50. $query = $this->dao->search($where)->with('product')->order('create_time DESC');
  51. $count = $query->count();
  52. $list = $query->page($page, $limit)->select();
  53. return compact('count', 'list');
  54. }
  55. public function adminList(array $where, $page, $limit)
  56. {
  57. $query = $this->dao->search($where)->with(['merchant' => function ($query) {
  58. $query->field('mer_name,mer_id,is_trader');
  59. },'product'])->order('BroadcastGoods.sort DESC,BroadcastGoods.create_time DESC');
  60. $count = $query->count();
  61. $list = $query->page($page, $limit)->select();
  62. return compact('count', 'list');
  63. }
  64. /**
  65. * @param array $formData
  66. * @return Form
  67. * @throws FormBuilderException
  68. * @author xaboy
  69. * @day 2020/7/30
  70. */
  71. public function createForm(array $formData = [])
  72. {
  73. if (isset($formData['product_id'])) {
  74. $formData['product_id'] = [
  75. 'id' => $formData['product_id'],
  76. 'src' => $formData['cover_img']
  77. ];
  78. }
  79. return Elm::createForm((isset($formData['broadcast_goods_id']) ? Route::buildUrl('merchantBroadcastGoodsUpdate', ['id' => $formData['broadcast_goods_id']]) : Route::buildUrl('merchantBroadcastGoodsCreate'))->build(), [
  80. Elm::frameImage('product_id', '商品', '/' . config('admin.merchant_prefix') . '/setting/storeProduct?field=product_id')->width('60%')->height('536px')->props(['srcKey' => 'src'])->modal(['modal' => false])->appendValidate(Elm::validateObject()->message('请选择商品')),
  81. Elm::input('name', '商品名称')->required(),
  82. Elm::frameImage('cover_img', '商品图', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=cover_img&type=1')
  83. ->info('图片尺寸最大像素 300*300')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false])->required(),
  84. Elm::number('price', '价格')->min(0.01)->info('该价格只做展示,不会影响原商品价格')->required(),
  85. ])->setTitle('创建直播商品')->formData($formData);
  86. }
  87. public function updateForm($id)
  88. {
  89. return $this->createForm($this->dao->get($id)->toArray())->setTitle('编辑直播商品');
  90. }
  91. /**
  92. * @param $merId
  93. * @param array $data
  94. * @return mixed
  95. * @author xaboy
  96. * @day 2020/8/25
  97. */
  98. public function create($merId, array $data)
  99. {
  100. $data['status'] = request()->merchant()->is_bro_goods == 1 ? 0 : 1;
  101. $data['mer_id'] = $merId;
  102. return Db::transaction(function () use ($data) {
  103. $goods = $this->dao->create($data);
  104. if ($data['status'] == 1) {
  105. $res = $this->wxCreate($goods);
  106. $goods->goods_id = $res->goodsId;
  107. $goods->audit_id = $res->auditId;
  108. $goods->save();
  109. } else {
  110. SwooleTaskService::admin('notice', [
  111. 'type' => 'new_goods',
  112. 'data' => [
  113. 'title' => '新直播商品申请',
  114. 'message' => '您有1个新的直播商品审核,请及时处理!',
  115. 'id' => $goods->broadcast_goods_id
  116. ]
  117. ]);
  118. }
  119. return $goods;
  120. });
  121. }
  122. public function batchCreate($merId, array $goodsList)
  123. {
  124. $status = request()->merchant()->is_bro_goods == 1 ? 0 : 1;
  125. $ids = Db::transaction(function () use ($goodsList, $status, $merId) {
  126. $ids = [];
  127. foreach ($goodsList as $goods) {
  128. $goods['status'] = $status;
  129. $goods['mer_id'] = $merId;
  130. $ids[] = $this->dao->create($goods)->broadcast_goods_id;
  131. }
  132. return $ids;
  133. });
  134. foreach ($ids as $id) {
  135. if ($status == 1) {
  136. Queue::push(ApplyBroadcastGoodsJob::class, $id);
  137. } else {
  138. SwooleTaskService::admin('notice', [
  139. 'type' => 'new_goods',
  140. 'data' => [
  141. 'title' => '新直播商品申请',
  142. 'message' => '您有1个新的直播商品审核,请及时处理!',
  143. 'id' => $id
  144. ]
  145. ]);
  146. }
  147. }
  148. }
  149. /**
  150. * @param $id
  151. * @param array $data
  152. * @return array|Model|null
  153. * @throws DataNotFoundException
  154. * @throws DbException
  155. * @throws ModelNotFoundException
  156. * @author xaboy
  157. * @day 2020/8/25
  158. */
  159. public function update($id, array $data)
  160. {
  161. $goods = $this->dao->get($id);
  162. $status = request()->merchant()->is_bro_goods == 1 ? 0 : 1;
  163. if ($goods->status == 0) {
  164. $goods->save($data);
  165. if ($status == 1) {
  166. if ($goods->goods_id) {
  167. $this->wxUpdate($goods->goods_id, $data);
  168. } else {
  169. $res = $this->wxCreate($goods);
  170. $goods->goods_id = $res->goodsId;
  171. $goods->audit_id = $res->auditId;
  172. }
  173. $goods->save();
  174. } else {
  175. SwooleTaskService::admin('notice', [
  176. 'type' => 'new_goods',
  177. 'data' => [
  178. 'title' => '新直播商品申请',
  179. 'message' => '您有1个新的直播商品审核,请及时处理!',
  180. 'id' => $goods->broadcast_goods_id
  181. ]
  182. ]);
  183. }
  184. } else {
  185. if ($status == 1 && $goods->goods_id) {
  186. $this->wxUpdate($goods->goods_id, $data);
  187. }
  188. $data['status'] = $status;
  189. $data['error_msg'] = '';
  190. $this->change($id, $data);
  191. }
  192. return $goods;
  193. }
  194. public function change($id, array $data)
  195. {
  196. return $this->dao->update($id, $data);
  197. }
  198. public function applyForm($id)
  199. {
  200. return Elm::createForm(Route::buildUrl('systemBroadcastGoodsApply', compact('id'))->build(), [
  201. Elm::radio('status', '审核状态', 1)->options([['value' => -1, 'label' => '未通过'], ['value' => 1, 'label' => '通过']])->control([
  202. ['value' => -1, 'rule' => [
  203. Elm::textarea('msg', '未通过原因', '信息有误,请完善')->required()
  204. ]]
  205. ]),
  206. ])->setTitle('审核直播商品');
  207. }
  208. public function apply($id, $status, $msg = '')
  209. {
  210. $goods = $this->dao->get($id);
  211. Db::transaction(function () use ($msg, $status, $goods) {
  212. $goods->status = $status;
  213. if ($status == -1)
  214. $goods->error_msg = $msg;
  215. else {
  216. if ($goods->goods_id) {
  217. $this->wxUpdate($goods->goods_id, $goods);
  218. } else {
  219. $res = $this->wxCreate($goods);
  220. $goods->goods_id = $res->goodsId;
  221. $goods->audit_id = $res->auditId;
  222. }
  223. $goods->status = 1;
  224. }
  225. $goods->save();
  226. SwooleTaskService::merchant('notice', [
  227. 'type' => 'goods_status_' . ($status == -1 ? 'fail' : 'success'),
  228. 'data' => [
  229. 'title' => '直播商品审核通知',
  230. 'message' => $status == -1 ? '您的直播商品审核未通过!' : '您的直播商品审核已通过',
  231. 'id' => $goods->broadcast_goods_id
  232. ]
  233. ], $goods->mer_id);
  234. });
  235. }
  236. public function wxCreate($goods)
  237. {
  238. if ($goods['goods_id'])
  239. throw new ValidateException('商品已创建');
  240. $goods = $goods->toArray();
  241. $miniProgramService = MiniProgramService::create();
  242. $path = './public' . app()->make(DownloadImageService::class)->downloadImage($goods['cover_img'])['path'];
  243. $data = [
  244. 'name' => $goods['name'],
  245. 'priceType' => 1,
  246. 'price' => floatval($goods['price']),
  247. 'url' => 'pages/goods_details/index?source=1:' . $goods['broadcast_goods_id'] . ':' . $goods['product_id'] . '&id=' . $goods['product_id'],
  248. 'coverImgUrl' => $miniProgramService->material()->uploadImage($path)->media_id,
  249. ];
  250. @unlink($path);
  251. try {
  252. return $miniProgramService->miniBroadcast()->create($data);
  253. } catch (Exception $e) {
  254. throw new ValidateException($e->getMessage());
  255. }
  256. }
  257. public function wxUpdate($id,$data)
  258. {
  259. $miniProgramService = MiniProgramService::create();
  260. $path = './public' . app()->make(DownloadImageService::class)->downloadImage($data['cover_img'])['path'];
  261. $params = [
  262. "goodsId" => $id,
  263. 'name' => $data['name'],
  264. 'priceType' => 1,
  265. 'price' => floatval($data['price']),
  266. 'coverImgUrl' => $miniProgramService->material()->uploadImage($path)->media_id,
  267. ];
  268. @unlink($path);
  269. try {
  270. return $miniProgramService->miniBroadcast()->update($params);
  271. } catch (Exception $e) {
  272. throw new ValidateException($e->getMessage());
  273. }
  274. }
  275. public function isShow($id, $isShow, bool $admin = false)
  276. {
  277. return $this->dao->update($id, [($admin ? 'is_show' : 'is_mer_show') => $isShow]);
  278. }
  279. public function mark($id, $mark)
  280. {
  281. return $this->dao->update($id, compact('mark'));
  282. }
  283. public function delete($id)
  284. {
  285. $goods = $this->dao->get($id);
  286. if ($goods->goods_id) {
  287. MiniProgramService::create()->miniBroadcast()->delete($goods->goods_id);
  288. }
  289. app()->make(BroadcastRoomGoodsRepository::class)->deleteGoods($id);
  290. $this->dao->delete($id);
  291. }
  292. public function syncGoodStatus()
  293. {
  294. $goodsIds = $this->dao->goodsStatusAll();
  295. if (!count($goodsIds)) return;
  296. $res = MiniProgramService::create()->miniBroadcast()->getGoodsWarehouse(array_keys($goodsIds))->toArray();
  297. foreach ($res['goods'] as $item) {
  298. if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
  299. $data = ['audit_status' => $item['audit_status']];
  300. if (in_array($item['audit_status'], [2, 3])) {
  301. $data['status'] = $item['audit_status'] == 3 ? -1 : 2;
  302. if (-1 == $data['status']) {
  303. $data['error_msg'] = '微信审核未通过';
  304. }
  305. }
  306. //TODO 同步商品审核状态
  307. $this->dao->updateGoods($item['goods_id'], $data);
  308. }
  309. }
  310. }
  311. }