MerchantService.php 11 KB

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