MerchantService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace bizGhs\merchant\services;
  3. use bizGhs\apply\classes\ApplyClass;
  4. use bizGhs\custom\services\CustomService;
  5. use bizGhs\merchant\classes\MerchantInitClass;
  6. use bizHd\agent\classes\AgentAssetClass;
  7. use bizHd\auth\services\AuthService;
  8. use bizHd\base\services\BaseService;
  9. use bizGhs\merchant\classes\MerchantAssetClass;
  10. use bizGhs\merchant\classes\MerchantClass;
  11. use bizGhs\merchant\classes\MerchantExtendClass;
  12. use bizHd\message\services\TMessageService;
  13. use bizHd\saas\services\SetAuthService;
  14. use bizHd\saas\services\SetMealService;
  15. use bizHd\user\services\UserService;
  16. use bizHd\wx\services\WxMenuService;
  17. use common\components\business;
  18. use common\components\dict;
  19. use common\components\stringUtil;
  20. use common\components\util;
  21. use common\services\xhTMessageService;
  22. use common\services\xhWxMenuService;
  23. use Yii;
  24. class MerchantService extends BaseService
  25. {
  26. public static $baseFile = '\bizGhs\merchant\classes\MerchantClass';
  27. //初始化商家信息 ssh 2020.2.11
  28. public static function initGhsBaseInfo($applyData, $main, $applyThis = false)
  29. {
  30. $applyId = $applyData['id'] ?? 0;
  31. $adminId = $applyData['adminId'] ?? 0;
  32. $mainId = $main->id ?? 0;
  33. unset($applyData['id']);
  34. //生成商家
  35. $applyData['accountStyle'] = 0;
  36. $applyData['status'] = 1;
  37. $applyData['lat'] = $applyData['lat'] ?? (string)0;
  38. $applyData['long'] = $applyData['long'] ?? (string)0;
  39. $applyData['precision'] = (string)0;
  40. $applyData['wxToken'] = stringUtil::buildWxToken(0);
  41. $applyData['photoList'] = $applyData['photoList'] ?? '';
  42. $applyData['name'] = $applyData['name'] ?? '';
  43. $applyData['province'] = $applyData['province'] ?? '';
  44. $applyData['city'] = $applyData['city'] ?? '';
  45. $applyData['dist'] = $applyData['dist'] ?? '';
  46. $applyData['address'] = $applyData['address'] ?? '';
  47. $applyData['agentLevel'] = 1;
  48. $applyData['parentId'] = $applyData['introSjId'] ?? 0;
  49. $deadline = time() + 365 * 86400;
  50. $applyData['deadline'] = date("Y-m-d H:i:s", $deadline);
  51. $applyData['parentId'] = $applyData['introSjId'] ?? 0;
  52. $applyData['parentShopId'] = $applyData['introShopId'] ?? 0;
  53. $sjData = $applyData;
  54. $sjData['style'] = dict::getDict('ptStyle', 'ghs');
  55. $sj = self::add($sjData, true);
  56. $sjId = $sj->id;
  57. $sjName = $sj->name ?? 0;
  58. $mobile = $applyData['mobile'] ?? '';
  59. $ptStyle = $applyData['style'] ?? dict::getDict('ptStyle', 'hd');
  60. if (!empty($applyId) && $applyThis) {
  61. ApplyClass::updateById($applyId, ['sjId' => $sjId]);
  62. }
  63. //生成资产
  64. $assetData = [
  65. 'balance' => 0,
  66. 'totalVisit' => 0,
  67. 'totalUser' => 0,
  68. 'totalMember' => 0,
  69. 'totalFans' => 0,
  70. 'totalIntegral' => 0,
  71. 'totalPoint' => 0,
  72. 'totalGoods' => 0,
  73. 'totalRecharge' => 0,
  74. 'totalOrder' => 0,
  75. 'totalDeal' => 0,
  76. 'sjId' => $sjId,
  77. 'totalIncome' => 0,
  78. 'totalExpend' => 0,
  79. ];
  80. MerchantAssetClass::add($assetData);
  81. //生成商家拓展信息
  82. $extendData = [
  83. 'sjId' => $sjId,
  84. 'wxAppId' => isset($applyData['wxAppId']) ? $applyData['wxAppId'] : '',
  85. 'webTitle' => isset($applyData['name']) ? $applyData['name'] : '',
  86. 'webKeyword' => isset($applyData['name']) ? $applyData['name'] : '',
  87. 'webDescription' => isset($applyData['name']) ? $applyData['name'] : '',
  88. 'wxMenuInit' => 0,
  89. 'wxTMessageInit' => 0,
  90. 'payment' => 0,//支付功能关闭
  91. 'recharge' => 0,//充值功能关闭
  92. 'giveCoupon' => 1,//默认赠送代金劵
  93. 'giveCouponPrice' => 5,//代金劵金额5元
  94. 'couponMeetPrice' => 180,//满足金额
  95. 'couponValidity' => 30,//代金劵有效期30天
  96. 'alipayAccount' => isset($applyData['alipayAccount']) ? $applyData['alipayAccount'] : '',//提现的支付宝帐号
  97. 'alipayAccountName' => isset($applyData['alipayAccountName']) ? $applyData['alipayAccountName'] : '',//提现支付宝帐号姓名
  98. 'gradeList' => '1_2000_0.9I2_5000_0.88I3_10000_0.85I4_20000_0.8',//1 级会员需要满 2000 积分 购物享9折;2 级会员需要满 5000 积分 购物享88折
  99. ];
  100. MerchantExtendClass::add($extendData);
  101. MerchantInitClass::add(['sjId' => $sjId]);
  102. //创建门店
  103. $shopData = [
  104. 'shopName' => '首店',
  105. 'sjId' => $sjId,
  106. 'merchantName' => $sjName,
  107. 'telephone' => $mobile,
  108. 'default' => 1,
  109. 'mainId' => $mainId,
  110. 'adminId' => $adminId,
  111. 'parentId' => $applyData['introSjId'] ?? 0,
  112. 'parentShopId' => $applyData['introShopId'] ?? 0,
  113. 'mobile' => $mobile,
  114. 'introduction' => '欢迎光临',
  115. 'province' => $applyData['province'] ?? '',
  116. 'city' => $applyData['city'] ?? '',
  117. 'dist' => $applyData['dist'] ?? '',
  118. 'address' => $applyData['address'] ?? '',
  119. 'floor' => $applyData['floor'] ?? '',
  120. 'fullAddress' => $applyData['fullAddress'] ?? '',
  121. 'open' => 1,
  122. 'lat' => $applyData['lat'] ?? '',
  123. 'long' => $applyData['long'] ?? '',
  124. 'img' => ['/example/shop/1.jpg'],
  125. 'ptStyle' => $ptStyle,
  126. ];
  127. $shopRespond = \bizGhs\shop\classes\ShopClass::addShop($shopData);
  128. $shopId = $shopRespond->id;
  129. //变更状态和设置商家的默认门店
  130. $sjData = [];
  131. $sjData['defaultShopId'] = $shopId;
  132. $sjStatus = MerchantClass::STATUS_HAS_CHECK;
  133. $sjData['status'] = $sjStatus;
  134. MerchantClass::updateById($sjId, $sjData);
  135. //建立供货商和客户关系
  136. if ($applyThis) {
  137. CustomService::shareBuildTrade($applyData, $shopRespond);
  138. }
  139. return ['sj' => $sj, 'shop' => $shopRespond];
  140. }
  141. //商家在业务端的权限 ssh 2019.11.25
  142. public static function getAuthIdList($sjId, $endId)
  143. {
  144. $merchant = self::getById($sjId);
  145. $setId = $merchant['setId'];
  146. if (empty($setId)) {
  147. util::fail('没有找到商家的套餐');
  148. }
  149. //业务端总共有哪些权限
  150. $idList = AuthService::getListId($endId);
  151. //套餐在业务端有哪些权限
  152. $setIdList = SetAuthService::getAuthIdList($setId, $endId);
  153. //有*号表示有所有的权限
  154. $commonIdList = in_array('*', $setIdList) ? $idList : array_intersect($idList, $setIdList);
  155. return $commonIdList;
  156. }
  157. //获取默认门店
  158. public static function getDefaultShopId($merchant)
  159. {
  160. return MerchantClass::getDefaultShopId($merchant);
  161. }
  162. //商家基本信息 ssh 2019.12.8
  163. public static function getInfo($merchant, $extend)
  164. {
  165. $merchant['extend'] = $extend;
  166. return $merchant;
  167. }
  168. //常用链接 ssh 2019.12.19
  169. public static function getCommonUrlList()
  170. {
  171. $list = MerchantClass::$commonUrl;
  172. return array_values($list);
  173. }
  174. //取指定常用链接 ssh 2019.12.28
  175. public static function getCommonUrl($id)
  176. {
  177. $list = MerchantClass::$commonUrl;
  178. return isset($list[$id]) ? $list[$id] : [];
  179. }
  180. //取平台下的商家 ssh 2019.12.20
  181. public static function getSaasMerchant($where)
  182. {
  183. $data = MerchantClass::getList('*', $where, 'addTime DESC');
  184. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  185. if (empty($list)) {
  186. return $data;
  187. }
  188. //管理员
  189. $adminIdList = array_column($list, 'adminId');
  190. $admin = AdminService::getAdminByIds($adminIdList, null, 'id');
  191. //商家资产
  192. $sjIdList = array_column($list, 'id');
  193. $asset = MerchantAssetService::getAssetBySjIds($sjIdList);
  194. //套餐
  195. $setIdList = array_column($list, 'setId');
  196. $set = SetMealService::getSetByIds($setIdList);
  197. //拓展
  198. $ext = MerchantExtendService::getExtendByMerchantIds($sjIdList);
  199. foreach ($list as $key => $val) {
  200. $adminId = $val['adminId'];
  201. $sjId = $val['id'];
  202. $setId = $val['setId'];
  203. $val['adminName'] = isset($admin[$adminId]['name']) ? $admin[$adminId]['name'] : '';
  204. $val['merchantAsset'] = isset($asset[$sjId]) ? $asset[$sjId] : [];
  205. $val['setMealName'] = isset($set[$setId]) ? $set[$setId]['name'] : '';
  206. $val['extend'] = isset($ext[$sjId]) ? $ext[$sjId] : [];
  207. $list[$key] = business::formatMerchantLogo($val);
  208. }
  209. $data['list'] = $list;
  210. return $data;
  211. }
  212. //根据id批量取商家 ssh 2019.12.20
  213. public static function getMerchantByIds($ids)
  214. {
  215. return MerchantClass::getMerchantByIds($ids);
  216. }
  217. //获取商家 ssh 2019.12.30
  218. public static function getMerchantById($id)
  219. {
  220. return MerchantClass::getMerchantById($id);
  221. }
  222. public static function getCurrentSetId($id)
  223. {
  224. return MerchantClass::getCurrentSetId($id);
  225. }
  226. //开店公众号授权之后初始化 ssh 2020.2.7
  227. public static function openShopInit($data)
  228. {
  229. $sjId = $data['sjId'];
  230. $merchant = MerchantClass::getMerchantById($sjId);
  231. if (empty($merchant)) {
  232. return false;
  233. }
  234. $extend = MerchantExtendClass::getBySjId($sjId);
  235. if (isset($extend['init']) && $extend['init'] == 1) {
  236. return true;
  237. }
  238. //事务处理
  239. $connection = Yii::$app->db;
  240. $transaction = $connection->beginTransaction();
  241. try {
  242. //初始化微信菜单
  243. $hasMenu = WxMenuService::getByCondition(['sjId' => $sjId]);
  244. if (empty($hasMenu)) {
  245. xhWxMenuService::applyInit($merchant);
  246. }
  247. //微信客户初始化
  248. $hasUser = UserService::getByCondition(['sjId' => $sjId]);
  249. if (empty($hasUser)) {
  250. $return = UserService::syncWxAllUser($sjId);
  251. $totalNum = isset($return['total']) ? $return['total'] : 0;
  252. MerchantAssetClass::updateByCondition(['sjId' => $sjId], ['totalFans' => $totalNum, 'totalUser' => $totalNum]);
  253. }
  254. //模板消息
  255. $hasMessage = TMessageService::getByCondition(['sjId' => $sjId]);
  256. if (empty($hasMessage)) {
  257. xhTMessageService::init($merchant);
  258. }
  259. //完成初始化标识
  260. MerchantExtendClass::updateByCondition(['sjId' => $sjId], ['init' => 1]);
  261. //代理资产
  262. AgentAssetClass::add(['sjId' => $sjId]);
  263. $transaction->commit();
  264. return true;
  265. } catch (Exception $e) {
  266. $transaction->rollBack();
  267. return false;
  268. }
  269. }
  270. //获取商家的h5商城二维码 ssh 2020.4.30
  271. public static function getH5MallQrCode($id)
  272. {
  273. return MerchantClass::getH5MallQrCode($id);
  274. }
  275. }