BroadcastGoodsRepository.php 11 KB

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