BroadcastGoodsRepository.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. 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]),
  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]),
  84. Elm::number('price', '价格')->min(0.01)->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. if ($goods->status == 0){
  163. $goods->save($data);
  164. $status = request()->merchant()->is_bro_goods == 1 ? 0 : 1;
  165. if ($status == 1) {
  166. $res = $this->wxCreate($goods);
  167. $goods->goods_id = $res->goodsId;
  168. $goods->audit_id = $res->auditId;
  169. $goods->save();
  170. }else{
  171. SwooleTaskService::admin('notice', [
  172. 'type' => 'new_goods',
  173. 'data' => [
  174. 'title' => '新直播商品申请',
  175. 'message' => '您有1个新的直播商品审核,请及时处理!',
  176. 'id' => $goods->broadcast_goods_id
  177. ]
  178. ]);
  179. }
  180. }else{
  181. if($goods->goods_id){
  182. $this->wxUpdate($goods->goods_id,$data);
  183. }
  184. $data['status'] = 0;
  185. $data['error_msg'] = '';
  186. $this->change($id,$data);
  187. }
  188. return $goods;
  189. }
  190. public function change($id, array $data)
  191. {
  192. return $this->dao->update($id, $data);
  193. }
  194. public function applyForm($id)
  195. {
  196. return Elm::createForm(Route::buildUrl('systemBroadcastGoodsApply', compact('id'))->build(), [
  197. Elm::radio('status', '审核状态', 1)->options([['value' => -1, 'label' => '未通过'], ['value' => 1, 'label' => '通过']])->control([
  198. ['value' => -1, 'rule' => [
  199. Elm::textarea('msg', '未通过原因', '信息有误,请完善')->required()
  200. ]]
  201. ]),
  202. ])->setTitle('审核直播商品');
  203. }
  204. public function apply($id, $status, $msg = '')
  205. {
  206. $goods = $this->dao->get($id);
  207. Db::transaction(function () use ($msg, $status, $goods) {
  208. $goods->status = $status;
  209. if ($status == -1)
  210. $goods->error_msg = $msg;
  211. else {
  212. $res = $this->wxCreate($goods);
  213. $goods->goods_id = $res->goodsId;
  214. $goods->audit_id = $res->auditId;
  215. $goods->status = 1;
  216. }
  217. $goods->save();
  218. SwooleTaskService::merchant('notice', [
  219. 'type' => 'goods_status_' . ($status == -1 ? 'fail' : 'success'),
  220. 'data' => [
  221. 'title' => '直播商品审核通知',
  222. 'message' => $status == -1 ? '您的直播商品审核未通过!' : '您的直播商品审核已通过',
  223. 'id' => $goods->broadcast_goods_id
  224. ]
  225. ], $goods->mer_id);
  226. });
  227. }
  228. public function wxCreate($goods)
  229. {
  230. if ($goods['goods_id'])
  231. throw new ValidateException('商品已创建');
  232. $goods = $goods->toArray();
  233. $miniProgramService = MiniProgramService::create();
  234. $path = './public' . app()->make(DownloadImageService::class)->downloadImage($goods['cover_img'])['path'];
  235. $data = [
  236. 'name' => $goods['name'],
  237. 'priceType' => 1,
  238. 'price' => floatval($goods['price']),
  239. 'url' => 'pages/goods_details/index?source=1:' . $goods['broadcast_goods_id'] . ':' . $goods['product_id'] . '&id=' . $goods['product_id'],
  240. 'coverImgUrl' => $miniProgramService->material()->uploadImage($path)->media_id,
  241. ];
  242. @unlink($path);
  243. try {
  244. return $miniProgramService->miniBroadcast()->create($data);
  245. } catch (Exception $e) {
  246. throw new ValidateException($e->getMessage());
  247. }
  248. }
  249. public function wxUpdate($id,$data)
  250. {
  251. $miniProgramService = MiniProgramService::create();
  252. $path = './public' . app()->make(DownloadImageService::class)->downloadImage($data['cover_img'])['path'];
  253. $params = [
  254. "goodsId" => $id,
  255. 'name' => $data['name'],
  256. 'priceType' => 1,
  257. 'price' => floatval($data['price']),
  258. 'coverImgUrl' => $miniProgramService->material()->uploadImage($path)->media_id,
  259. ];
  260. @unlink($path);
  261. try {
  262. return $miniProgramService->miniBroadcast()->update($params);
  263. } catch (Exception $e) {
  264. throw new ValidateException($e->getMessage());
  265. }
  266. }
  267. public function isShow($id, $isShow, bool $admin = false)
  268. {
  269. return $this->dao->update($id, [($admin ? 'is_show' : 'is_mer_show') => $isShow]);
  270. }
  271. public function mark($id, $mark)
  272. {
  273. return $this->dao->update($id, compact('mark'));
  274. }
  275. public function delete($id)
  276. {
  277. $goods = $this->dao->get($id);
  278. $this->dao->delete($id);
  279. if ($goods->goods_id)
  280. MiniProgramService::create()->miniBroadcast()->delete($goods->goods_id);
  281. }
  282. public function syncGoodStatus()
  283. {
  284. $goodsIds = $this->dao->goodsStatusAll();
  285. if (!count($goodsIds)) return;
  286. $res = MiniProgramService::create()->miniBroadcast()->getGoodsWarehouse(array_keys($goodsIds))->toArray();
  287. foreach ($res['goods'] as $item) {
  288. if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
  289. $data = ['audit_status' => $item['audit_status']];
  290. if (in_array($item['audit_status'], [2, 3])) {
  291. $data['status'] = $item['audit_status'] == 3 ? -1 : 2;
  292. if (-1 == $data['status']) {
  293. $data['error_msg'] = '微信审核未通过';
  294. }
  295. }
  296. //TODO 同步商品审核状态
  297. $this->dao->updateGoods($item['goods_id'], $data);
  298. }
  299. }
  300. }
  301. public function merDelete($id)
  302. {
  303. $goods = $this->dao->get($id);
  304. if ($goods && ($goods->status == -1 || $goods->status == 0)) {
  305. return $this->dao->merDelete($id);
  306. }
  307. throw new ValidateException('状态有误,删除失败');
  308. }
  309. }