123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\common\repositories\store\broadcast;
- use app\common\dao\store\broadcast\BroadcastGoodsDao;
- use app\common\repositories\BaseRepository;
- use crmeb\jobs\ApplyBroadcastGoodsJob;
- use crmeb\services\DownloadImageService;
- use crmeb\services\MiniProgramService;
- use crmeb\services\SwooleTaskService;
- use Exception;
- use FormBuilder\Exception\FormBuilderException;
- use FormBuilder\Factory\Elm;
- use FormBuilder\Form;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\Queue;
- use think\facade\Route;
- use think\Model;
- /**
- * Class BroadcastGoodsRepository
- * @package app\common\repositories\store\broadcast
- * @author xaboy
- * @day 2020/7/30
- * @mixin BroadcastGoodsDao
- */
- class BroadcastGoodsRepository extends BaseRepository
- {
- /**
- * @var BroadcastGoodsDao
- */
- protected $dao;
- public function __construct(BroadcastGoodsDao $dao)
- {
- $this->dao = $dao;
- }
- public function getList($merId, array $where, $page, $limit)
- {
- $where['mer_id'] = $merId;
- $query = $this->dao->search($where)->with('product')->order('create_time DESC');
- $count = $query->count();
- $list = $query->page($page, $limit)->select();
- return compact('count', 'list');
- }
- public function adminList(array $where, $page, $limit)
- {
- $query = $this->dao->search($where)->with(['merchant' => function ($query) {
- $query->field('mer_name,mer_id,is_trader');
- },'product'])->order('BroadcastGoods.sort DESC,BroadcastGoods.create_time DESC');
- $count = $query->count();
- $list = $query->page($page, $limit)->select();
- return compact('count', 'list');
- }
- /**
- * @param array $formData
- * @return Form
- * @throws FormBuilderException
- * @author xaboy
- * @day 2020/7/30
- */
- public function createForm(array $formData = [])
- {
- if (isset($formData['product_id'])) {
- $formData['product_id'] = [
- 'id' => $formData['product_id'],
- 'src' => $formData['cover_img']
- ];
- }
- return Elm::createForm((isset($formData['broadcast_goods_id']) ? Route::buildUrl('merchantBroadcastGoodsUpdate', ['id' => $formData['broadcast_goods_id']]) : Route::buildUrl('merchantBroadcastGoodsCreate'))->build(), [
- Elm::frameImage('product_id', '商品', '/' . config('admin.merchant_prefix') . '/setting/storeProduct?field=product_id')->width('60%')->height('536px')->props(['srcKey' => 'src'])->modal(['modal' => false]),
- Elm::input('name', '商品名称')->required(),
- Elm::frameImage('cover_img', '商品图', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=cover_img&type=1')
- ->info('图片尺寸最大像素 300*300')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]),
- Elm::number('price', '价格')->min(0.01)->required(),
- ])->setTitle('创建直播商品')->formData($formData);
- }
- public function updateForm($id)
- {
- return $this->createForm($this->dao->get($id)->toArray())->setTitle('编辑直播商品');
- }
- /**
- * @param $merId
- * @param array $data
- * @return mixed
- * @author xaboy
- * @day 2020/8/25
- */
- public function create($merId, array $data)
- {
- $data['status'] = request()->merchant()->is_bro_goods == 1 ? 0 : 1;
- $data['mer_id'] = $merId;
- return Db::transaction(function () use ($data) {
- $goods = $this->dao->create($data);
- if ($data['status'] == 1) {
- $res = $this->wxCreate($goods);
- $goods->goods_id = $res->goodsId;
- $goods->audit_id = $res->auditId;
- $goods->save();
- } else {
- SwooleTaskService::admin('notice', [
- 'type' => 'new_goods',
- 'data' => [
- 'title' => '新直播商品申请',
- 'message' => '您有1个新的直播商品审核,请及时处理!',
- 'id' => $goods->broadcast_goods_id
- ]
- ]);
- }
- return $goods;
- });
- }
- public function batchCreate($merId, array $goodsList)
- {
- $status = request()->merchant()->is_bro_goods == 1 ? 0 : 1;
- $ids = Db::transaction(function () use ($goodsList, $status, $merId) {
- $ids = [];
- foreach ($goodsList as $goods) {
- $goods['status'] = $status;
- $goods['mer_id'] = $merId;
- $ids[] = $this->dao->create($goods)->broadcast_goods_id;
- }
- return $ids;
- });
- foreach ($ids as $id) {
- if ($status == 1) {
- Queue::push(ApplyBroadcastGoodsJob::class, $id);
- } else {
- SwooleTaskService::admin('notice', [
- 'type' => 'new_goods',
- 'data' => [
- 'title' => '新直播商品申请',
- 'message' => '您有1个新的直播商品审核,请及时处理!',
- 'id' => $id
- ]
- ]);
- }
- }
- }
- /**
- * @param $id
- * @param array $data
- * @return array|Model|null
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author xaboy
- * @day 2020/8/25
- */
- public function update($id, array $data)
- {
- $goods = $this->dao->get($id);
- if ($goods->status == 0){
- $goods->save($data);
- $status = request()->merchant()->is_bro_goods == 1 ? 0 : 1;
- if ($status == 1) {
- $res = $this->wxCreate($goods);
- $goods->goods_id = $res->goodsId;
- $goods->audit_id = $res->auditId;
- $goods->save();
- }else{
- SwooleTaskService::admin('notice', [
- 'type' => 'new_goods',
- 'data' => [
- 'title' => '新直播商品申请',
- 'message' => '您有1个新的直播商品审核,请及时处理!',
- 'id' => $goods->broadcast_goods_id
- ]
- ]);
- }
- }else{
- if($goods->goods_id){
- $this->wxUpdate($goods->goods_id,$data);
- }
- $data['status'] = 0;
- $data['error_msg'] = '';
- $this->change($id,$data);
- }
- return $goods;
- }
- public function change($id, array $data)
- {
- return $this->dao->update($id, $data);
- }
- public function applyForm($id)
- {
- return Elm::createForm(Route::buildUrl('systemBroadcastGoodsApply', compact('id'))->build(), [
- Elm::radio('status', '审核状态', 1)->options([['value' => -1, 'label' => '未通过'], ['value' => 1, 'label' => '通过']])->control([
- ['value' => -1, 'rule' => [
- Elm::textarea('msg', '未通过原因', '信息有误,请完善')->required()
- ]]
- ]),
- ])->setTitle('审核直播商品');
- }
- public function apply($id, $status, $msg = '')
- {
- $goods = $this->dao->get($id);
- Db::transaction(function () use ($msg, $status, $goods) {
- $goods->status = $status;
- if ($status == -1)
- $goods->error_msg = $msg;
- else {
- $res = $this->wxCreate($goods);
- $goods->goods_id = $res->goodsId;
- $goods->audit_id = $res->auditId;
- $goods->status = 1;
- }
- $goods->save();
- SwooleTaskService::merchant('notice', [
- 'type' => 'goods_status_' . ($status == -1 ? 'fail' : 'success'),
- 'data' => [
- 'title' => '直播商品审核通知',
- 'message' => $status == -1 ? '您的直播商品审核未通过!' : '您的直播商品审核已通过',
- 'id' => $goods->broadcast_goods_id
- ]
- ], $goods->mer_id);
- });
- }
- public function wxCreate($goods)
- {
- if ($goods['goods_id'])
- throw new ValidateException('商品已创建');
- $goods = $goods->toArray();
- $miniProgramService = MiniProgramService::create();
- $path = './public' . app()->make(DownloadImageService::class)->downloadImage($goods['cover_img'])['path'];
- $data = [
- 'name' => $goods['name'],
- 'priceType' => 1,
- 'price' => floatval($goods['price']),
- 'url' => 'pages/goods_details/index?source=1:' . $goods['broadcast_goods_id'] . ':' . $goods['product_id'] . '&id=' . $goods['product_id'],
- 'coverImgUrl' => $miniProgramService->material()->uploadImage($path)->media_id,
- ];
- @unlink($path);
- try {
- return $miniProgramService->miniBroadcast()->create($data);
- } catch (Exception $e) {
- throw new ValidateException($e->getMessage());
- }
- }
- public function wxUpdate($id,$data)
- {
- $miniProgramService = MiniProgramService::create();
- $path = './public' . app()->make(DownloadImageService::class)->downloadImage($data['cover_img'])['path'];
- $params = [
- "goodsId" => $id,
- 'name' => $data['name'],
- 'priceType' => 1,
- 'price' => floatval($data['price']),
- 'coverImgUrl' => $miniProgramService->material()->uploadImage($path)->media_id,
- ];
- @unlink($path);
- try {
- return $miniProgramService->miniBroadcast()->update($params);
- } catch (Exception $e) {
- throw new ValidateException($e->getMessage());
- }
- }
- public function isShow($id, $isShow, bool $admin = false)
- {
- return $this->dao->update($id, [($admin ? 'is_show' : 'is_mer_show') => $isShow]);
- }
- public function mark($id, $mark)
- {
- return $this->dao->update($id, compact('mark'));
- }
- public function delete($id)
- {
- $goods = $this->dao->get($id);
- $this->dao->delete($id);
- if ($goods->goods_id)
- MiniProgramService::create()->miniBroadcast()->delete($goods->goods_id);
- }
- public function syncGoodStatus()
- {
- $goodsIds = $this->dao->goodsStatusAll();
- if (!count($goodsIds)) return;
- $res = MiniProgramService::create()->miniBroadcast()->getGoodsWarehouse(array_keys($goodsIds))->toArray();
- foreach ($res['goods'] as $item) {
- if (isset($goodsIds[$item['goods_id']]) && $item['audit_status'] != $goodsIds[$item['goods_id']]) {
- $data = ['audit_status' => $item['audit_status']];
- if (in_array($item['audit_status'], [2, 3])) {
- $data['status'] = $item['audit_status'] == 3 ? -1 : 2;
- if (-1 == $data['status']) {
- $data['error_msg'] = '微信审核未通过';
- }
- }
- //TODO 同步商品审核状态
- $this->dao->updateGoods($item['goods_id'], $data);
- }
- }
- }
- public function merDelete($id)
- {
- $goods = $this->dao->get($id);
- if ($goods && ($goods->status == -1 || $goods->status == 0)) {
- return $this->dao->merDelete($id);
- }
- throw new ValidateException('状态有误,删除失败');
- }
- }
|