| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace bizGhs\merchant\services;
- use bizGhs\apply\classes\ApplyClass;
- use bizGhs\custom\services\CustomService;
- use bizGhs\merchant\classes\MerchantInitClass;
- use bizHd\agent\classes\AgentAssetClass;
- use bizHd\auth\services\AuthService;
- use bizHd\base\services\BaseService;
- use bizGhs\merchant\classes\MerchantAssetClass;
- use bizGhs\merchant\classes\MerchantClass;
- use bizGhs\merchant\classes\MerchantExtendClass;
- use bizHd\message\services\TMessageService;
- use bizHd\saas\services\SetAuthService;
- use bizHd\saas\services\SetMealService;
- use bizHd\user\services\UserService;
- use bizHd\wx\services\WxMenuService;
- use common\components\business;
- use common\components\dict;
- use common\components\stringUtil;
- use common\components\util;
- use common\services\xhTMessageService;
- use common\services\xhWxMenuService;
- use Yii;
- class MerchantService extends BaseService
- {
- public static $baseFile = '\bizGhs\merchant\classes\MerchantClass';
- //初始化商家信息 ssh 2020.2.11
- public static function initGhsBaseInfo($applyData, $main, $applyThis = false)
- {
- $applyId = $applyData['id'] ?? 0;
- $adminId = $applyData['adminId'] ?? 0;
- $mainId = $main->id ?? 0;
- unset($applyData['id']);
- //生成商家
- $applyData['accountStyle'] = 0;
- $applyData['status'] = 1;
- $applyData['lat'] = $applyData['lat'] ?? (string)0;
- $applyData['long'] = $applyData['long'] ?? (string)0;
- $applyData['precision'] = (string)0;
- $applyData['wxToken'] = stringUtil::buildWxToken(0);
- $applyData['photoList'] = $applyData['photoList'] ?? '';
- $applyData['name'] = $applyData['name'] ?? '';
- $applyData['province'] = $applyData['province'] ?? '';
- $applyData['city'] = $applyData['city'] ?? '';
- $applyData['dist'] = $applyData['dist'] ?? '';
- $applyData['address'] = $applyData['address'] ?? '';
- $applyData['agentLevel'] = 1;
- $applyData['parentId'] = $applyData['introSjId'] ?? 0;
- $deadline = time() + 365 * 86400;
- $applyData['deadline'] = date("Y-m-d H:i:s", $deadline);
- $applyData['parentId'] = $applyData['introSjId'] ?? 0;
- $applyData['parentShopId'] = $applyData['introShopId'] ?? 0;
- $sjData = $applyData;
- $sjData['style'] = dict::getDict('ptStyle', 'ghs');
- $sj = self::add($sjData, true);
- $sjId = $sj->id;
- $sjName = $sj->name ?? 0;
- $mobile = $applyData['mobile'] ?? '';
- $ptStyle = $applyData['style'] ?? dict::getDict('ptStyle', 'hd');
- if (!empty($applyId) && $applyThis) {
- ApplyClass::updateById($applyId, ['sjId' => $sjId]);
- }
- //生成资产
- $assetData = [
- 'balance' => 0,
- 'totalVisit' => 0,
- 'totalUser' => 0,
- 'totalMember' => 0,
- 'totalFans' => 0,
- 'totalIntegral' => 0,
- 'totalPoint' => 0,
- 'totalGoods' => 0,
- 'totalRecharge' => 0,
- 'totalOrder' => 0,
- 'totalDeal' => 0,
- 'sjId' => $sjId,
- 'totalIncome' => 0,
- 'totalExpend' => 0,
- ];
- MerchantAssetClass::add($assetData);
- //生成商家拓展信息
- $extendData = [
- 'sjId' => $sjId,
- 'wxAppId' => isset($applyData['wxAppId']) ? $applyData['wxAppId'] : '',
- 'webTitle' => isset($applyData['name']) ? $applyData['name'] : '',
- 'webKeyword' => isset($applyData['name']) ? $applyData['name'] : '',
- 'webDescription' => isset($applyData['name']) ? $applyData['name'] : '',
- 'wxMenuInit' => 0,
- 'wxTMessageInit' => 0,
- 'payment' => 0,//支付功能关闭
- 'recharge' => 0,//充值功能关闭
- 'giveCoupon' => 1,//默认赠送代金劵
- 'giveCouponPrice' => 5,//代金劵金额5元
- 'couponMeetPrice' => 180,//满足金额
- 'couponValidity' => 30,//代金劵有效期30天
- 'alipayAccount' => isset($applyData['alipayAccount']) ? $applyData['alipayAccount'] : '',//提现的支付宝帐号
- 'alipayAccountName' => isset($applyData['alipayAccountName']) ? $applyData['alipayAccountName'] : '',//提现支付宝帐号姓名
- 'gradeList' => '1_2000_0.9I2_5000_0.88I3_10000_0.85I4_20000_0.8',//1 级会员需要满 2000 积分 购物享9折;2 级会员需要满 5000 积分 购物享88折
- ];
- MerchantExtendClass::add($extendData);
- MerchantInitClass::add(['sjId' => $sjId]);
- //创建门店
- $shopData = [
- 'shopName' => '首店',
- 'sjId' => $sjId,
- 'merchantName' => $sjName,
- 'telephone' => $mobile,
- 'default' => 1,
- 'mainId' => $mainId,
- 'adminId' => $adminId,
- 'parentId' => $applyData['introSjId'] ?? 0,
- 'parentShopId' => $applyData['introShopId'] ?? 0,
- 'mobile' => $mobile,
- 'introduction' => '欢迎光临',
- 'province' => $applyData['province'] ?? '',
- 'city' => $applyData['city'] ?? '',
- 'dist' => $applyData['dist'] ?? '',
- 'address' => $applyData['address'] ?? '',
- 'floor' => $applyData['floor'] ?? '',
- 'fullAddress' => $applyData['fullAddress'] ?? '',
- 'open' => 1,
- 'lat' => $applyData['lat'] ?? '',
- 'long' => $applyData['long'] ?? '',
- 'img' => ['/example/shop/1.jpg'],
- 'ptStyle' => $ptStyle,
- ];
- $shopRespond = \bizGhs\shop\classes\ShopClass::addShop($shopData);
- $shopId = $shopRespond->id;
- //变更状态和设置商家的默认门店
- $sjData = [];
- $sjData['defaultShopId'] = $shopId;
- $sjStatus = MerchantClass::STATUS_HAS_CHECK;
- $sjData['status'] = $sjStatus;
- MerchantClass::updateById($sjId, $sjData);
- //建立供货商和客户关系
- if ($applyThis) {
- CustomService::shareBuildTrade($applyData, $shopRespond);
- }
- return ['sj' => $sj, 'shop' => $shopRespond];
- }
- //商家在业务端的权限 ssh 2019.11.25
- public static function getAuthIdList($sjId, $endId)
- {
- $merchant = self::getById($sjId);
- $setId = $merchant['setId'];
- if (empty($setId)) {
- util::fail('没有找到商家的套餐');
- }
- //业务端总共有哪些权限
- $idList = AuthService::getListId($endId);
- //套餐在业务端有哪些权限
- $setIdList = SetAuthService::getAuthIdList($setId, $endId);
- //有*号表示有所有的权限
- $commonIdList = in_array('*', $setIdList) ? $idList : array_intersect($idList, $setIdList);
- return $commonIdList;
- }
- //获取默认门店
- public static function getDefaultShopId($merchant)
- {
- return MerchantClass::getDefaultShopId($merchant);
- }
- //商家基本信息 ssh 2019.12.8
- public static function getInfo($merchant, $extend)
- {
- $merchant['extend'] = $extend;
- return $merchant;
- }
- //常用链接 ssh 2019.12.19
- public static function getCommonUrlList()
- {
- $list = MerchantClass::$commonUrl;
- return array_values($list);
- }
- //取指定常用链接 ssh 2019.12.28
- public static function getCommonUrl($id)
- {
- $list = MerchantClass::$commonUrl;
- return isset($list[$id]) ? $list[$id] : [];
- }
- //取平台下的商家 ssh 2019.12.20
- public static function getSaasMerchant($where)
- {
- $data = MerchantClass::getList('*', $where, 'addTime DESC');
- $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
- if (empty($list)) {
- return $data;
- }
- //管理员
- $adminIdList = array_column($list, 'adminId');
- $admin = AdminService::getAdminByIds($adminIdList, null, 'id');
- //商家资产
- $sjIdList = array_column($list, 'id');
- $asset = MerchantAssetService::getAssetBySjIds($sjIdList);
- //套餐
- $setIdList = array_column($list, 'setId');
- $set = SetMealService::getSetByIds($setIdList);
- //拓展
- $ext = MerchantExtendService::getExtendByMerchantIds($sjIdList);
- foreach ($list as $key => $val) {
- $adminId = $val['adminId'];
- $sjId = $val['id'];
- $setId = $val['setId'];
- $val['adminName'] = isset($admin[$adminId]['name']) ? $admin[$adminId]['name'] : '';
- $val['merchantAsset'] = isset($asset[$sjId]) ? $asset[$sjId] : [];
- $val['setMealName'] = isset($set[$setId]) ? $set[$setId]['name'] : '';
- $val['extend'] = isset($ext[$sjId]) ? $ext[$sjId] : [];
- $list[$key] = business::formatMerchantLogo($val);
- }
- $data['list'] = $list;
- return $data;
- }
- //根据id批量取商家 ssh 2019.12.20
- public static function getMerchantByIds($ids)
- {
- return MerchantClass::getMerchantByIds($ids);
- }
- //获取商家 ssh 2019.12.30
- public static function getMerchantById($id)
- {
- return MerchantClass::getMerchantById($id);
- }
- public static function getCurrentSetId($id)
- {
- return MerchantClass::getCurrentSetId($id);
- }
- //开店公众号授权之后初始化 ssh 2020.2.7
- public static function openShopInit($data)
- {
- $sjId = $data['sjId'];
- $merchant = MerchantClass::getMerchantById($sjId);
- if (empty($merchant)) {
- return false;
- }
- $extend = MerchantExtendClass::getBySjId($sjId);
- if (isset($extend['init']) && $extend['init'] == 1) {
- return true;
- }
- //事务处理
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- //初始化微信菜单
- $hasMenu = WxMenuService::getByCondition(['sjId' => $sjId]);
- if (empty($hasMenu)) {
- xhWxMenuService::applyInit($merchant);
- }
- //微信客户初始化
- $hasUser = UserService::getByCondition(['sjId' => $sjId]);
- if (empty($hasUser)) {
- $return = UserService::syncWxAllUser($sjId);
- $totalNum = isset($return['total']) ? $return['total'] : 0;
- MerchantAssetClass::updateByCondition(['sjId' => $sjId], ['totalFans' => $totalNum, 'totalUser' => $totalNum]);
- }
- //模板消息
- $hasMessage = TMessageService::getByCondition(['sjId' => $sjId]);
- if (empty($hasMessage)) {
- xhTMessageService::init($merchant);
- }
- //完成初始化标识
- MerchantExtendClass::updateByCondition(['sjId' => $sjId], ['init' => 1]);
- //代理资产
- AgentAssetClass::add(['sjId' => $sjId]);
- $transaction->commit();
- return true;
- } catch (Exception $e) {
- $transaction->rollBack();
- return false;
- }
- }
- //获取商家的h5商城二维码 ssh 2020.4.30
- public static function getH5MallQrCode($id)
- {
- return MerchantClass::getH5MallQrCode($id);
- }
- }
|