MShop.Class.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. <?php
  2. /**
  3. * 商铺管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/31
  7. * Time: 15:02
  8. */
  9. namespace JinDouYun\Model\Shop;
  10. use Exception;
  11. use JinDouYun\Controller\Common\Logger;
  12. use JinDouYun\Dao\Department\DStaff;
  13. use JinDouYun\Dao\GoodsManage\DGoodsBasic;
  14. use JinDouYun\Dao\UserCenter\DUserCenter;
  15. use JinDouYun\Model\Common\Location;
  16. use JinDouYun\Model\Customer\MMemberBalanceDetail;
  17. use JinDouYun\Model\System\MBasicSetup;
  18. use Mall\Framework\Core\ErrorCode;
  19. use Mall\Framework\Core\StatusCode;
  20. use Mall\Framework\Core\ResultWrapper;
  21. use JinDouYun\Dao\Shop\DShop;
  22. use JinDouYun\Dao\Shop\DEnterpriseBindShop;
  23. use Jindouyun\Cache\EnterpriseCache;
  24. use JinDouYun\Cache\RoleAclCache;
  25. use JinDouYun\Cache\ShopCache;
  26. use JinDouYun\Cache\SysAreaChinaCache;
  27. use JinDouYun\Cache\GoodsBasicRelevant;
  28. use JinDouYun\Model\ShopPartner\MShopPartner;
  29. use JinDouYun\Model\Stock\MWarehouse;
  30. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  31. use JinDouYun\Model\Customer\MCustomer;
  32. use JinDouYun\Model\Enterprise\MEnterprise;
  33. use JinDouYun\Model\Department\MStaff;
  34. use JinDouYun\Model\MBaseModel;
  35. use Monolog\Handler\IFTTTHandler;
  36. class MShop extends MBaseModel
  37. {
  38. private $objDShop;
  39. private $objDEnterpriseBindShop;
  40. private $objSysAreaChinaCache;
  41. private $cutTable = 250000;
  42. private $enterpriseId;
  43. private $userCenterId;
  44. private $objMShopPartner;
  45. private $objRoleAclCache;
  46. private $objShopCache;
  47. private $objMEnterprise;
  48. private $objMStaff;
  49. private $objMMemberBalanceDetail;
  50. public function __construct($enterpriseId, $userCenterId = '')
  51. {
  52. $this->enterpriseId = $enterpriseId;
  53. $this->userCenterId = $userCenterId;
  54. parent::__construct($this->enterpriseId, $this->userCenterId);
  55. $this->objMShopPartner = new MShopPartner($this->enterpriseId, $this->userCenterId);
  56. $this->objDShop = new DShop('default');
  57. $this->objDEnterpriseBindShop = new DEnterpriseBindShop('default');
  58. $this->objSysAreaChinaCache = new SysAreaChinaCache();
  59. $this->objRoleAclCache = new RoleAclCache();
  60. $this->objShopCache = new ShopCache();
  61. $this->objMEnterprise = new MEnterprise();
  62. $this->objMStaff = new MStaff($enterpriseId,$userCenterId);
  63. $this->objMMemberBalanceDetail = new MMemberBalanceDetail($enterpriseId,$userCenterId);
  64. $tableName = $this->objDShop->getTableName('qianniao_shop', $this->enterpriseId, $this->cutTable);
  65. $this->objDShop->setTable($tableName);
  66. $this->objDShop->setSearchIndex('shop_search')->setType('shop');
  67. }
  68. /**
  69. * Doc: (des="添加店铺")
  70. * User: XMing
  71. * Date: 2020/7/15
  72. * Time: 10:42 上午
  73. * @throws Exception
  74. * @param array $params
  75. * @return ResultWrapper
  76. */
  77. public function addShop(array $params)
  78. {
  79. $params['enterpriseId'] = $this->enterpriseId;
  80. $objMEnterprise = new MEnterprise();
  81. $modelResult = $objMEnterprise->createAppid($this->userCenterId);
  82. $params['token'] = $modelResult['token'];
  83. //获取仓库
  84. if(isset($params['warehouseId']) && !empty($params['warehouseId'])){
  85. if(is_array($params['warehouseId'])){
  86. $warehouseId = $params['warehouseId'];
  87. $params['warehouseId'] = implode(',',$params['warehouseId']);
  88. }else{
  89. $warehouseId = [$params['warehouseId']];
  90. }
  91. $objMWarehouse = new MWarehouse($this->enterpriseId);
  92. $modelResult = $objMWarehouse->selectWarehouse(['id' => $warehouseId, 'deleteStatus' => StatusCode::$standard]);
  93. if(!$modelResult->isSuccess()){
  94. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  95. }
  96. $warehouseResult = $modelResult->getData();
  97. unset($modelResult);
  98. if(empty($warehouseResult)){
  99. return ResultWrapper::fail('仓库查询失败', ErrorCode::$paramError);
  100. }
  101. $selectWarehouseId = array_column($warehouseResult, 'id');
  102. foreach($warehouseId as $value){
  103. if(!in_array($value,$selectWarehouseId)){
  104. return ResultWrapper::fail($value.'仓库查询失败', ErrorCode::$paramError);
  105. }
  106. }
  107. }
  108. $beginStatus = $this->objDShop->beginTransaction();
  109. $ShopId = $this->objDShop->insert($params);
  110. if ($ShopId === false) {
  111. $this->objDShop->rollBack();
  112. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  113. }
  114. //缓存token
  115. $this->objShopCache->setShopToken($this->enterpriseId, $ShopId, $params['token']);
  116. unset($params['token']);
  117. //缓存企业下的销售区域
  118. self::cacheSalesAreaInEnterprise();
  119. //增加企业商铺关联表
  120. $EnterpriseBindShopData = [
  121. 'enterpriseId' => $params['enterpriseId'],
  122. 'shopId' => $ShopId,
  123. 'updateTime' => time(),
  124. 'createTime' => time(),
  125. ];
  126. $dbResult = $this->objDEnterpriseBindShop->insert($EnterpriseBindShopData);
  127. if($dbResult === false){
  128. $this->objDShop->rollBack();
  129. return ResultWrapper::fail($this->objDEnterpriseBindShop->error(),ErrorCode::$dberror);
  130. }
  131. if($params['recommend_user_id']>0)
  132. {
  133. $objMBasicSetup = new MBasicSetup($this->enterpriseId);
  134. $yhtjmd = $objMBasicSetup->getBasicField('yhtjmd')->getData()['yhtjmd'];
  135. $memberBalanceDetailData = [
  136. 'customerId' => $params['customerId'],
  137. 'type' => 5,
  138. 'userCenterId' => $params['recommend_user_id'],
  139. 'money' => $yhtjmd,
  140. 'purpose' => '推荐门店获得',
  141. 'orderIds' =>'',
  142. 'remark' => '推荐门店获得'.$yhtjmd,
  143. 'financeType' => '推荐门店获得',
  144. 'accountId' => isset($params['accountId']) ? $params['accountId']: 0,
  145. 'accountName' => isset($params['accountName']) ? $params['accountName']: '',
  146. 'accountNumber' => isset($params['accountNumber']) ? $params['accountNumber']: '',
  147. 'explain' => isset($params['explain']) ? $params['explain']: '',
  148. ];
  149. $result = $this->objMMemberBalanceDetail->addMemberBalanceDetail($memberBalanceDetailData,$isbackstage=true);
  150. if($result) {
  151. $this->objDShop->update(['is_send' => 1], ['id' => $ShopId]);
  152. }
  153. }
  154. /*//自动创建仓库
  155. $wareHouseParam = [
  156. 'warehouseName' => $params['name'] . '仓库',
  157. 'shopId' => $ShopId,
  158. 'shopName' => $params['name'],
  159. 'contactMobile' => $params['mobile'],
  160. 'provinceCode' => $params['provinceCode'],
  161. 'cityCode' => $params['cityCode'],
  162. 'districtCode' => $params['districtCode'],
  163. 'contactAddress' => $params['address'],
  164. 'updateTime' => time(),
  165. 'deleteStatus' => StatusCode::$standard,
  166. 'enableStatus' => StatusCode::$standard,
  167. ];
  168. //添加仓库
  169. $objMWarehouse = new MWarehouse($this->enterpriseId);
  170. $warehouseResult = $objMWarehouse->addWarehouse($wareHouseParam);
  171. if(!$warehouseResult->isSuccess()){
  172. $this->objDShop->rollBack();
  173. return ResultWrapper::fail($warehouseResult->getData(),$warehouseResult->getErrorCode());
  174. }
  175. $warehouseId =(int) $warehouseResult->getData();
  176. $this->objDShop->update(['warehouseId' => $warehouseId], $ShopId);
  177. $objGoodsBasicRelevant = new GoodsBasicRelevant($this->enterpriseId);
  178. $cacheResult = $objGoodsBasicRelevant->cacheShopIdRelationName($ShopId,
  179. [
  180. 'name' => $params['name'],
  181. 'warehouseId' => $warehouseId,
  182. 'warehouseName' => $wareHouseParam['warehouseName'],
  183. 'costType' => $params['costType']
  184. ]);
  185. if (!$cacheResult->isSuccess()) {
  186. $this->objDShop->rollBack();
  187. return ResultWrapper::fail($cacheResult->getData(), $cacheResult->getErrorCode());
  188. }
  189. $this->objRoleAclCache->cacheShopIdAndWarehouseId($this->enterpriseId, $ShopId, $warehouseId);
  190. //修改商铺数据
  191. $update = [
  192. 'warehouseId' => $warehouseId,
  193. 'updateTime' => time(),
  194. ];
  195. $result = $this->objDShop->update($update, ['id' => $ShopId]);
  196. if($result === false){
  197. $this->objDShop->rollBack();
  198. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  199. }*/
  200. //更新ES
  201. $beginStatus && $this->objDShop->commit();
  202. // self::updateEsData($params, $ShopId, true);
  203. return ResultWrapper::success($ShopId);
  204. }
  205. private function createEsDocumentId($shopId)
  206. {
  207. return 'EnterpriseId_' . $this->enterpriseId . '_ShopId_' . $shopId;
  208. }
  209. public function getShopTable() {
  210. return $this->objDShop->get_Table();
  211. }
  212. public function updateEsData($params, $shopId, $insert = false)
  213. {
  214. $objMSysAreaChina = new MSysAreaChina();
  215. if (isset($params['provinceCode']) && isset($params['cityCode']) && isset($params['districtCode'])) {
  216. $areaName = $objMSysAreaChina->getNameByCode([
  217. $params['provinceCode'],
  218. $params['cityCode'],
  219. $params['districtCode']
  220. ]);
  221. $params['provinceName'] = isset($areaName[$params['provinceCode']]) ? $areaName[$params['provinceCode']] : '';
  222. $params['cityName'] = isset($areaName[$params['cityCode']]) ? $areaName[$params['cityCode']] : '';
  223. $params['districtName'] = isset($areaName[$params['districtCode']]) ? $areaName[$params['districtCode']] : '';
  224. }
  225. if (!empty($params['latitude']) && !empty($params['longitude'])) {
  226. $params['shoplocation'] = [
  227. 'lat' => $params['latitude'],
  228. 'lon' => $params['longitude']
  229. ];
  230. }
  231. unset($params['latitude']);
  232. unset($params['longitude']);
  233. unset($params['openTime']);
  234. if(isset($params['salesArea']) && !empty($params['salesArea'])){
  235. $params['salesArea'] = json_decode($params['salesArea'], true);
  236. $params['salesArea'] = implode(',', $params['salesArea']);
  237. }
  238. $params['id'] = $shopId;
  239. $params['enterpriseId'] = $this->enterpriseId;
  240. $_id = self::createEsDocumentId($shopId);
  241. $result = $this->objDShop->addUpSearchIndexDocument($params, $_id);
  242. //print_r($result);
  243. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  244. //echo "es操作成功";die;
  245. }
  246. //echo "es操作失败";
  247. }
  248. /**
  249. * 获取指定商铺信息
  250. * @param $params
  251. * @return ResultWrapper
  252. * @throws Exception
  253. */
  254. public function getShopInfo($params)
  255. {
  256. $dbResult = $this->objDShop->get($params);
  257. if ($dbResult === false) {
  258. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  259. } else {
  260. $dbResult = $this->format([$dbResult]);
  261. $dbResult = array_shift($dbResult);
  262. return ResultWrapper::success($dbResult);
  263. }
  264. }
  265. /**
  266. * 编辑商铺
  267. * @param int|array $params 修改商铺的数据
  268. * @return ResultWrapper
  269. * @throws Exception
  270. */
  271. public function editShop($params)
  272. {
  273. if (empty($params['id'])) {
  274. return ResultWrapper::fail('没有指定要修改的商铺id', ErrorCode::$paramError);
  275. }
  276. $shopInfo = $this->objDShop->get(['id' => $params['id']], 'id,warehouseId');
  277. if ($shopInfo === false) {
  278. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$paramError);
  279. }
  280. $updateShopId = $params['id'];
  281. unset($params['id']);
  282. $shopPartnerId = isset($params['shopPartnerId']) ? $params['shopPartnerId'] : '';
  283. unset($params['shopPartnerId']);
  284. unset($params['partnerName']);
  285. //获取仓库
  286. if(isset($params['warehouseId']) && !empty($params['warehouseId'])){
  287. if(is_array($params['warehouseId'])){
  288. $warehouseId = $params['warehouseId'];
  289. $params['warehouseId'] = implode(',',$params['warehouseId']);
  290. }else{
  291. $warehouseId = [$params['warehouseId']];
  292. }
  293. $objMWarehouse = new MWarehouse($this->enterpriseId);
  294. $modelResult = $objMWarehouse->selectWarehouse(['id' => $warehouseId, 'deleteStatus' => StatusCode::$standard]);
  295. if(!$modelResult->isSuccess()){
  296. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  297. }
  298. $warehouseResult = $modelResult->getData();
  299. unset($modelResult);
  300. if(empty($warehouseResult)){
  301. return ResultWrapper::fail('仓库查询失败', ErrorCode::$paramError);
  302. }
  303. $selectWarehouseId = array_column($warehouseResult, 'id');
  304. foreach($warehouseId as $value){
  305. if(!in_array($value,$selectWarehouseId)){
  306. return ResultWrapper::fail($value.'仓库查询失败', ErrorCode::$paramError);
  307. }
  308. }
  309. }
  310. $dbResult = $this->objDShop->update($params, $updateShopId);
  311. if ($dbResult === false) {
  312. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  313. }
  314. if ($shopPartnerId) {
  315. self::changeManagerForShop(['id'=>$updateShopId,'shopPartnerId'=>$shopPartnerId]);
  316. }
  317. //查出企业的店铺下所有的销售区域,缓存到redis
  318. self::cacheSalesAreaInEnterprise();
  319. $objGoodsBasicRelevant = new GoodsBasicRelevant($this->enterpriseId);
  320. $objGoodsBasicRelevant->cacheShopIdRelationName($updateShopId,
  321. [
  322. 'name' => $params['name'],
  323. 'warehouseId' => isset($params['warehouseId']) ? $params['warehouseId'] :$shopInfo['warehouseId'],
  324. 'warehouseName' => isset($params['warehouseName']) ? $params['warehouseName'] : $params['name'] . '仓库',
  325. 'costType' => $params['costType']
  326. ]);
  327. //更新ES
  328. //self::updateEsData($params, $updateShopId);
  329. return ResultWrapper::success($dbResult);
  330. }
  331. /**
  332. * 更换负责人
  333. * @param int|array $params 修改商铺的数据
  334. *
  335. * @return ResultWrapper
  336. * @throws Exception
  337. */
  338. public function changeManagerForShop($params)
  339. {
  340. if (empty($params['id'])) {
  341. return ResultWrapper::fail('没有指定要修改的商铺id', ErrorCode::$paramError);
  342. }
  343. $shopPartnerId = isset($params['shopPartnerId']) ? $params['shopPartnerId'] : '';
  344. //修改原来负责人的商铺数据
  345. $shopPartnerInfo = $this->objMShopPartner->getShopPartnerInfo($shopPartnerId);
  346. if($shopPartnerInfo->isSuccess() == false) {
  347. return ResultWrapper::fail($shopPartnerInfo->getData(), $shopPartnerInfo->getErrorCode());
  348. }
  349. $shopPartnerInfo = $shopPartnerInfo->getData();
  350. $shopIds = explode(',', $shopPartnerInfo['shopId']);
  351. foreach($shopIds as $k => $shopId) {
  352. if($shopId == $params['id']) {
  353. unset($shopIds[$k]);
  354. }
  355. }
  356. $result = $this->objMShopPartner->updateShopIds(trim(implode(',',$shopIds),','),$shopPartnerId);
  357. if($result->isSuccess() == false) {
  358. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  359. }
  360. //更改商铺的合伙人
  361. $result = self::updateShopPartnerId(['id'=>$params['id'], 'shopPartnerId'=>$shopPartnerId]);
  362. if($result->isSuccess() == false) {
  363. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  364. }
  365. //更改合伙人的商铺
  366. //查出该合伙人管理的所有商铺
  367. $shopIds = $this->objMShopPartner->getShopIdsByPartnerId($shopPartnerId);
  368. $dbResult = $this->objMShopPartner->updateShopIds(trim(implode(',', array_filter(array_merge($shopIds, [$params['id']]))),","), $shopPartnerId);
  369. if ($dbResult === false) {
  370. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  371. } else {
  372. return ResultWrapper::success($dbResult);
  373. }
  374. }
  375. /**
  376. * 删除商铺
  377. * @param array $params
  378. * @return ResultWrapper
  379. */
  380. public function delShop($params)
  381. {
  382. if (empty($params)) {
  383. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  384. }
  385. $dbResult = $this->objDShop->update(['deleteStatus' => StatusCode::$delete], $params);
  386. if ($dbResult === false) {
  387. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  388. } else {
  389. //在ES删除
  390. //$_id = self::createEsDocumentId($params['id']);
  391. //$this->objDShop->esdeleteTypeDocument($_id);
  392. return ResultWrapper::success($dbResult);
  393. }
  394. }
  395. /**
  396. * 商铺启用和禁用
  397. * @param array $params
  398. * @return ResultWrapper
  399. */
  400. public function updateShopStatus($params)
  401. {
  402. $dbResult = $this->objDShop->update(['enableStatus' => $params['enableStatus']], $params['id']);
  403. if ($dbResult === false) {
  404. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  405. } else {
  406. /*$_id = self::createEsDocumentId($params['id']);
  407. $result = $this->objDShop->esupdateTypeFieldVaule(['enableStatus' => $params['enableStatus']], $_id);*/
  408. return ResultWrapper::success($dbResult);
  409. }
  410. }
  411. public function updateShopPartnerId($params)
  412. {
  413. $dbResult = $this->objDShop->update(['shopPartnerId' => $params['shopPartnerId']], $params['id']);
  414. if ($dbResult === false) {
  415. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  416. } else {
  417. return ResultWrapper::success($dbResult);
  418. }
  419. }
  420. /*
  421. * 处理搜索条件
  422. */
  423. public function search($params)
  424. {
  425. $limit = $params['limit'];
  426. unset($params['limit']);
  427. $offset = $params['offset'];
  428. unset($params['offset']);
  429. $condition = 'deleteStatus = ' . StatusCode::$standard.' AND enterpriseId='.$this->enterpriseId;
  430. if ($params['name']) {
  431. $condition .= " AND name like '%" . $params['name'] . "%'";
  432. }
  433. if ($params['shopPartnerId']) {
  434. $condition .= " AND shopPartnerId = " . $params['shopPartnerId'];
  435. }
  436. if ($params['shopType']) {
  437. $condition .= " AND shopType = " . $params['shopType'];
  438. }
  439. if ($params['enableStatus']) {
  440. $condition .= " AND enableStatus = " . $params['enableStatus'];
  441. }
  442. $dbResult = $this->objDShop->select($condition, '*', 'createTime desc', $limit, $offset);
  443. if ($dbResult === false) {
  444. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  445. }
  446. $dbResult = self::format($dbResult);
  447. $return = [
  448. 'data' => $dbResult,
  449. 'total' => $this->getShopTotal($condition)
  450. ];
  451. return ResultWrapper::success($return);
  452. }
  453. /**
  454. * 获取所有商铺数据
  455. * @param array $selectParams 过滤条件
  456. * @param string $fields 查询的字段
  457. * @return ResultWrapper
  458. * @throws Exception
  459. */
  460. public function getAllShop($selectParams, $fields = '*')
  461. {
  462. $fields = ' * ';
  463. $countField = ' COUNT(id) as total ';
  464. $where = '';
  465. if (isset($selectParams['enableStatus']) && !empty($selectParams['enableStatus'])){
  466. $where .= ' AND enableStatus = '.$selectParams['enableStatus'];
  467. }
  468. if (isset($selectParams['action']) && $selectParams['action'] == 1){
  469. //基础资料详情页面用来过滤禁止销售店铺
  470. if (isset($selectParamsp['materielId']) && !empty($selectParams['materielId'])){
  471. $objDGoodsBasic = new DGoodsBasic();
  472. $objDGoodsBasic->setTable('qianniao_goods_basic_'.$this->enterpriseId);
  473. $row = $objDGoodsBasic->get($selectParams['materielId']);
  474. if ($row === false){
  475. return ResultWrapper::fail($objDGoodsBasic->error(),ErrorCode::$dberror);
  476. }
  477. $noSaleShopIds = '';
  478. if (isset($row['noSalesShop']) && !empty($row['noSalesShop'])){
  479. $noSaleShopIds = trim(explode($row['noSalesShop'],','),',');
  480. }
  481. if (!empty($noSaleShopIds)){
  482. $where .= ' AND id NOT IN('.$noSaleShopIds.')';
  483. }
  484. }
  485. }
  486. $sql = 'SELECT '.$fields.' FROM qianniao_shop_1 WHERE enterpriseId = '.$this->enterpriseId.' AND deleteStatus = '.StatusCode::$standard.' AND merchantId IS NULL '.$where;
  487. $countSql = 'SELECT '.$countField.' FROM qianniao_shop_1 WHERE enterpriseId = '.$this->enterpriseId.' AND deleteStatus = '.StatusCode::$standard.' AND merchantId IS NULL '.$where;
  488. if (isset($selectParams['limit']) && !empty($selectParams['limit'])){
  489. $sql .= ' LIMIT '.$selectParams['offset'].','.$selectParams['limit'];
  490. }
  491. $dbResult = $this->objDShop->query($sql);
  492. if ($dbResult === false) {
  493. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  494. }
  495. $dbResult = self::format($dbResult);
  496. $count = $this->objDShop->query($countSql);
  497. if ($count === false){
  498. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  499. }
  500. $return = [
  501. 'data' => $dbResult,
  502. 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
  503. ];
  504. return ResultWrapper::success($return);
  505. }
  506. /**
  507. * 获取商品总数
  508. * @param $where mixed
  509. * @return int
  510. */
  511. public function getShopTotal($where)
  512. {
  513. $total = $this->objDShop->count($where);
  514. return ($total) ? intval($total) : 0;
  515. }
  516. /**
  517. * 处理开业时间 和省市区
  518. * @param $data
  519. * @return mixed
  520. */
  521. public function format($data)
  522. {
  523. $objMSysAreaChina = new MSysAreaChina();
  524. $managerIds = [];
  525. $warehouseIds = [];
  526. foreach ($data as $key => $shop) {
  527. $data[$key]['openTime'] = isset($shop['openTime']) ? (array)json_decode($shop['openTime'], true) : '';
  528. $data[$key]['shopCode'] = createCode(StatusCode::$code['shop']['prefix'], $shop['id'], StatusCode::$code['shop']['length']);
  529. if (isset($shop['provinceCode']) && isset($shop['cityCode']) && isset($shop['districtCode'])) {
  530. $areaName = $objMSysAreaChina->getNameByCode([
  531. $shop['provinceCode'],
  532. $shop['cityCode'],
  533. $shop['districtCode']
  534. ]);
  535. $data[$key]['area']['provinceName'] = isset($areaName[$shop['provinceCode']]) ? $areaName[$shop['provinceCode']] : '';
  536. $data[$key]['area']['cityName'] = isset($areaName[$shop['cityCode']]) ? $areaName[$shop['cityCode']] : '';
  537. $data[$key]['area']['districtName'] = isset($areaName[$shop['districtCode']]) ? $areaName[$shop['districtCode']] : '';
  538. $data[$key]['area']['address'] = $shop['address'];
  539. }
  540. if(!empty($shop['managerId'])) {
  541. $managerIds[] = $shop['managerId'];
  542. }
  543. if(!empty($shop['warehouseId'])) {
  544. if (strpos($shop['warehouseId'], ',')) {
  545. $warehouseIds = array_merge($warehouseIds, explode(',', $shop['warehouseId']));
  546. } else {
  547. $warehouseIds[] = $shop['warehouseId'];
  548. }
  549. }
  550. if (!isset($shop['meters'])){
  551. $data[$key]['meters'] = null;
  552. }
  553. }
  554. $staff = [];
  555. if (!empty($managerIds)) {
  556. //查出商铺的所有店长
  557. $where = [
  558. 'shopId' => 0,
  559. 'userCenterId' => $managerIds,
  560. 'deleteStatus' => StatusCode::$standard
  561. ];
  562. $modelResult = $this->objMStaff->getAllStaffData($where);
  563. if(!$modelResult->isSuccess()){
  564. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  565. }
  566. $staffResult = $modelResult->getData();
  567. unset($modelResult);
  568. foreach($staffResult as $value){
  569. $staff[$value['userCenterId']] = $value;
  570. }
  571. }
  572. $warehouseData = [];
  573. if (isset($warehouseIds) && $warehouseIds) {
  574. //查出仓库信息
  575. $objMWarehouse = new MWarehouse($this->enterpriseId);
  576. $warehouseResult = $objMWarehouse->getNameByIds($warehouseIds);
  577. if ($warehouseResult->isSuccess()) {
  578. $warehouseData = $warehouseResult->getData();
  579. }
  580. }
  581. foreach ($data as &$shop) {
  582. $shop['warehouseData'] = [];
  583. $shop['managerName'] = isset($staff[$shop['managerId']]) ? $staff[$shop['managerId']]['staffName'] : '';//店长名称
  584. $shop['salesArea'] = !empty($shop['salesArea']) ? json_decode($shop['salesArea'], true) : [];
  585. /*if (in_array('100000-0-0', $shop['salesArea'])) {
  586. $pos = array_search('100000-0-0', $shop['salesArea']);
  587. unset($shop['salesArea'][$pos]);
  588. $shop['isAllCountry'] = 1;
  589. }else {
  590. $shop['isAllCountry'] = 2;
  591. }*/
  592. if (!empty($shop['warehouseId'])) {
  593. if(strpos($shop['warehouseId'],',')){
  594. $wid = explode(',', $shop['warehouseId']);
  595. }else{
  596. $wid = [$shop['warehouseId']];
  597. }
  598. foreach($wid as $v){
  599. if(isset($warehouseData[$v])){
  600. $shop['warehouseData'][$v] = [
  601. 'warehouseId' => $v,
  602. 'warehouseCode' => createCode(StatusCode::$code['warehouse']['prefix'], $v, StatusCode::$code['warehouse']['length']),
  603. 'warehouseName' => $warehouseData[$v]['warehouseName']
  604. ];
  605. }
  606. }
  607. }
  608. }
  609. //$data['warehouseData'] = array_values($data['warehouseData']);
  610. return $data;
  611. }
  612. /**
  613. * Doc: (des="")
  614. * User: XMing
  615. * Date: 2021/3/4
  616. * Time: 12:10 下午
  617. * @param array $shopIds
  618. * @param string $field
  619. * @return ResultWrapper
  620. */
  621. public function getShopByIds(array $shopIds,string $field = "*"): ResultWrapper
  622. {
  623. if (empty($shopIds)){
  624. return ResultWrapper::success([]);
  625. }
  626. $dbResult = $this->objDShop->select(array_values($shopIds), $field);
  627. if ($dbResult === false) {
  628. return ResultWrapper::fail($this->objDShop->error(),ErrorCode::$dberror);
  629. }
  630. return ResultWrapper::success($dbResult);
  631. }
  632. /**
  633. * 获取指定商铺信息
  634. * @param $shopIds
  635. * @param string $field
  636. * @return array
  637. * @deprecated
  638. */
  639. public function getShopName($shopIds, $field = "*")
  640. {
  641. $return = [];
  642. if (!$shopIds) {
  643. return $return;
  644. }
  645. $dbResult = $this->objDShop->select(array_values($shopIds), $field);
  646. if ($dbResult === false) {
  647. return $return;
  648. }
  649. foreach ($dbResult as $shop) {
  650. $return[$shop['id']] = $shop;
  651. }
  652. return $return;
  653. }
  654. /**
  655. * 单个商铺Id查询商铺名称
  656. * @param $shopId
  657. * @return string
  658. */
  659. public function getShopNameById($shopId)
  660. {
  661. $return = '';
  662. if (empty($shopId)) {
  663. return $return;
  664. }
  665. $dbResult = $this->objDShop->get($shopId, 'name');
  666. if ($dbResult === false || empty($dbResult)) {
  667. return $return;
  668. }
  669. return isset($dbResult['name']) ? $dbResult['name'] : $return;
  670. }
  671. /**
  672. * 会员所在地区销售商品的店铺
  673. * @param $userCenterId
  674. * @param $areaCode
  675. * @return ResultWrapper
  676. * @throws Exception
  677. */
  678. public function getShopIdsByArea($userCenterId, $areaCode='')
  679. {
  680. $defaultDSL = [
  681. 'sort' => [
  682. 'createTime' => [
  683. 'order' => 'desc'
  684. ],
  685. ],
  686. ];
  687. $dsl = [];
  688. $dsl['query']['bool']['must'][] = [
  689. 'term' => ['enterpriseId' => $this->enterpriseId],
  690. ];
  691. $dsl['query']['bool']['must'][] = [
  692. 'term' => ['enableStatus' => StatusCode::$standard],
  693. ];
  694. if (!empty($areaCode)) {
  695. $dsl['query']['bool']['must'][] = [
  696. 'term' => [
  697. 'salesArea' => $areaCode
  698. ],
  699. ];
  700. } elseif ($userCenterId){
  701. //查出当前用户的省市区
  702. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  703. $customerData = $objMCustomer->getCustomerInfoByUserCenterId($userCenterId);
  704. if ($customerData->isSuccess()) {
  705. $customerInfo = $customerData->getData();
  706. $provinceCode = isset($customerInfo['provinceCode']) ? $customerInfo['provinceCode'] : 0;
  707. $cityCode = isset($customerInfo['cityCode']) ? $customerInfo['cityCode'] : 0;
  708. $districtCode = isset($customerInfo['districtCode']) ? $customerInfo['districtCode'] : 0;
  709. //V($provinceCode.'-'.$cityCode.'-'.$districtCode);
  710. $dsl['query']['bool']['must'][] = [
  711. 'term' => [
  712. 'salesArea' => $provinceCode . '-' . $cityCode . '-' . $districtCode
  713. ],
  714. ];
  715. /*
  716. * 处理全国销售
  717. * $dsl['query']['bool']['should'][] = [
  718. 'term' => [
  719. 'salesArea' => '100000-0-0'
  720. ],
  721. ];
  722. $dsl['query']['bool']['minimum_should_match'] = 1;*/
  723. }
  724. }else {
  725. return ResultWrapper::success([]);
  726. }
  727. //V($dsl);
  728. $dsl = array_merge($defaultDSL, $dsl);
  729. $result = $this->objDShop->getSearchQueryDsl($dsl);
  730. if (isset($result['status']) && $result['status'] == 400) {
  731. file_put_contents('/www/wwwroot/logs/api.junhailan.com/elasticsearch.log',date('Y-m-d H:i:s').'获取商铺失败,错误原因'.var_export($result,true).PHP_EOL,FILE_APPEND);
  732. if ($result['error']['reason'] == 'all shards failed') {
  733. return ResultWrapper::success([]);
  734. }
  735. return ResultWrapper::fail('获取商铺数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
  736. }
  737. if (!isset($result['hits']) || $result['hits']['total'] == 0) {
  738. return ResultWrapper::success([]);
  739. }
  740. $dbResult = $result['hits']['hits'];
  741. $list = [];
  742. foreach ($dbResult as $key => &$value) {
  743. $data = [];
  744. $data = $value['_source'];
  745. //$data['id'] = $value['_id'];
  746. $list[] = $data['id'];
  747. }
  748. return ResultWrapper::success($list);
  749. }
  750. /**
  751. * 根据省市区获取商铺
  752. * @param $params
  753. * @return ResultWrapper
  754. * @throws Exception
  755. */
  756. public function getShopIdByArea($params)
  757. {
  758. $defaultDSL = [
  759. 'sort' => [
  760. 'createTime' => [
  761. 'order' => 'desc'
  762. ],
  763. ],
  764. ];
  765. $dsl = [];
  766. $dsl['query']['bool']['must'][] = [
  767. 'term' => ['enterpriseId' => $this->enterpriseId],
  768. ];
  769. $dsl['query']['bool']['must'][] = [
  770. 'term' => ['enableStatus' => StatusCode::$standard],
  771. ];
  772. $provinceCode = isset($params['provinceCode']) ? $params['provinceCode'] : 0;
  773. $cityCode = isset($params['cityCode']) ? $params['cityCode'] : 0;
  774. $districtCode = isset($params['districtCode']) ? $params['districtCode'] : 0;
  775. $dsl['query']['bool']['must'][] = [
  776. 'term' => [
  777. 'salesArea' => $provinceCode . '-' . $cityCode . '-' . $districtCode
  778. ]
  779. ];
  780. $dsl = array_merge($defaultDSL, $dsl);
  781. $result = $this->objDShop->getSearchQueryDsl($dsl);
  782. if (isset($result['status']) && $result['status'] == 400) {
  783. return '';
  784. /*if($result['error']['reason'] == 'all shards failed') {
  785. return ResultWrapper::success('');
  786. }
  787. return ResultWrapper::fail('获取商铺数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);*/
  788. }
  789. if (!isset($result['hits']) || $result['hits']['total'] == 0) {
  790. return '';
  791. }
  792. $dbResult = $result['hits']['hits'];
  793. $list = [];
  794. foreach ($dbResult as $key => &$value) {
  795. $data = [];
  796. $data = $value['_source'];
  797. $list[] = $data['id'];
  798. }
  799. return array_shift($list);
  800. }
  801. /**
  802. * 获取当前企业下的销售区域
  803. */
  804. public function getSalesAreaInEnterprise()
  805. {
  806. $salesArea = $this->objShopCache->getEnterpriseSalesArea($this->enterpriseId);
  807. return !empty($salesArea) ? array_values(json_decode($salesArea, true)) : [];
  808. }
  809. /**
  810. * sql查询企业下的所有商铺销售区域
  811. */
  812. public function cacheSalesAreaInEnterprise() {
  813. $selectParams['enterpriseId'] = $this->enterpriseId;
  814. $selectParams['deleteStatus'] = StatusCode::$standard;
  815. $dbResult = $this->objDShop->select($selectParams, 'salesArea');
  816. if ($dbResult === false) {
  817. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  818. }
  819. $allAreaByEnterprise = [];
  820. foreach ($dbResult as $area) {
  821. $salesArea = !empty($area['salesArea']) ? json_decode($area['salesArea'], true) : [];
  822. if($salesArea) {
  823. $allAreaByEnterprise = array_merge($allAreaByEnterprise, $salesArea);
  824. }
  825. }
  826. $this->objShopCache->cacheEnterpriseSalesArea($this->enterpriseId, $allAreaByEnterprise);
  827. }
  828. /**
  829. * 获取企业下商铺总数
  830. * @param $enterpriseId
  831. * @return ResultWrapper
  832. */
  833. public function getShopTotalByEnterpriseId($enterpriseId)
  834. {
  835. if(empty($enterpriseId)) return ResultWrapper::fail('参数为空', ErrorCode::$paramError);
  836. $cacheEnterpriseCache = new EnterpriseCache();
  837. $cacheResult = $cacheEnterpriseCache->delShopTotalByEnterpriseId($enterpriseId);
  838. unset($cacheResult);
  839. $cacheResult = $cacheEnterpriseCache->getShopTotalByEnterpriseId($enterpriseId);
  840. if($cacheResult){
  841. return ResultWrapper::success($cacheResult);
  842. }
  843. $dbResult = $this->objDShop->count(['enterpriseId' => $enterpriseId, 'deleteStatus' => StatusCode::$standard]);
  844. if($dbResult === false){
  845. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  846. }
  847. if(empty($dbResult)){
  848. $returnData = 0;
  849. }else{
  850. $returnData = $dbResult;
  851. }
  852. unset($cacheResult);
  853. $cacheResult = $cacheEnterpriseCache->setShopTotalByEnterpriseId($enterpriseId, $returnData);
  854. return ResultWrapper::success($returnData);
  855. }
  856. /**
  857. * @param $enterpriseId
  858. * @return ResultWrapper
  859. * @throws Exception
  860. */
  861. public function checkAllowAddShop($enterpriseId)
  862. {
  863. $enterpriseResult = $this->objMEnterprise->getEnterpriseDataByEnterpriseId($enterpriseId);
  864. if (!$enterpriseResult->isSuccess()){
  865. return ResultWrapper::fail($enterpriseResult->getData(),$enterpriseResult->getErrorCode());
  866. }
  867. $enterprise = $enterpriseResult->getData();
  868. if ($enterprise['scope'] == StatusCode::$standard){
  869. //多店铺,不限制创建数量
  870. return ResultWrapper::success($enterprise['scope']);
  871. }
  872. //单店铺,查询已创建数量
  873. $shopResult = self::getShopTotalByEnterpriseId($enterpriseId);
  874. if (!$shopResult->isSuccess()){
  875. return ResultWrapper::fail($shopResult->getData(),$shopResult->getErrorCode());
  876. }
  877. $shopNum =(int) $shopResult->getData();
  878. if ($shopNum == 0){
  879. return ResultWrapper::success($enterprise['scope']);
  880. }
  881. return ResultWrapper::fail('您已经创建过店铺了',ErrorCode::$paramError);
  882. }
  883. /**
  884. * @param int $enterpriseId
  885. * @param string $fields
  886. * @return ResultWrapper
  887. */
  888. public function getScopeShopInfo(int $enterpriseId, string $fields)
  889. {
  890. $dbResult = $this->objDShop->get(['enterpriseId' => $enterpriseId,'deleteStatus'=>StatusCode::$standard],$fields);
  891. if ($dbResult === false){
  892. return ResultWrapper::fail($this->objDShop->error(),ErrorCode::$dberror);
  893. }
  894. return ResultWrapper::success($dbResult);
  895. }
  896. /**
  897. * 根据shopIds获取商铺数据
  898. * @param $shopIds
  899. * @return ResultWrapper
  900. */
  901. public function getShopDataByShopIds($shopIds)
  902. {
  903. if(empty($shopIds)) return ResultWrapper::fail('shopIds参数错误', ErrorCode::$paramError);
  904. $dbResult = $this->objDShop->select(['id' => $shopIds]);
  905. if($dbResult === false){
  906. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  907. }
  908. if(empty($dbResult)){
  909. return ResultWrapper::fail('商铺不存在', ErrorCode::$dberror);
  910. }
  911. $objGoodsBasicRelevant = new GoodsBasicRelevant($this->enterpriseId);
  912. $formatData = [];
  913. foreach($dbResult as $value){
  914. if(isset($value['warehouseId']) && !empty($value['warehouseId'])){
  915. $value['warehouseName'] = '';
  916. if(!isset($value['warehouseName']) || empty($value['warehouseName'])){
  917. $value['warehouseName'] = $objGoodsBasicRelevant->getNameByWarehouseId($value['warehouseId']);
  918. }
  919. }
  920. $formatData[$value['id']] = $value;
  921. }
  922. return ResultWrapper::success($formatData);
  923. }
  924. /**
  925. * 获取员工所在商铺列表
  926. * @param $params
  927. * @return ResultWrapper
  928. */
  929. public function getShopByStaff($params)
  930. {
  931. //1.判断当前登录人角色 区分是否返回所有商铺列表
  932. $modelResult = $this->objMEnterprise->getEnterpriseDataByEnterpriseId($this->enterpriseId);
  933. if(!$modelResult->isSuccess()){
  934. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  935. }
  936. $enterprise = $modelResult->getData();
  937. unset($modelResult);
  938. //如果是管理员返回所有商铺列表
  939. if($enterprise['userCenterId'] == $this->userCenterId){
  940. $modelResult = self::getAllShop($params);
  941. if(!$modelResult->isSuccess()){
  942. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  943. }
  944. return ResultWrapper::success($modelResult->getData());
  945. }
  946. //如果不是管理员 则返回该用户绑定的商铺列表
  947. $sql = 'select * from qianniao_user_bind_enterprise where userCenterId = '.$this->userCenterId.' and enterpriseId = '.$this->enterpriseId;
  948. $dbResult = $this->objDShop->query($sql);
  949. if($dbResult === false){
  950. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  951. }
  952. $shopIds = [];
  953. foreach($dbResult as $value){
  954. !empty($value['shopId']) && $shopIds[] = $value['shopId'];
  955. }
  956. unset($dbResult);
  957. $whereSql = ' WHERE deleteStatus = '.StatusCode::$standard.' and enterpriseId = '.$this->enterpriseId;
  958. if (!empty($shopIds)){
  959. $whereSql .= ' AND id in ('.implode(',',$shopIds).') ';
  960. }
  961. $sql = 'select * from qianniao_shop_1 '.$whereSql.' order by createTime ASC limit '.$params['offset'].','.$params['limit'];
  962. $dbResult = $this->objDShop->query($sql);
  963. if($dbResult === false){
  964. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  965. }
  966. $data = $dbResult;
  967. unset($dbResult);
  968. $countSql = 'select count(*) as count from qianniao_shop_1 '.$whereSql;
  969. $dbResult = $this->objDShop->query($countSql);
  970. if($dbResult === false){
  971. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  972. }
  973. $count = array_shift($dbResult)['count'];
  974. $return = [
  975. 'data' => self::format($data),
  976. 'total' => $count
  977. ];
  978. return ResultWrapper::success($return);
  979. }
  980. /**
  981. * 格式化商铺token
  982. * @return ResultWrapper
  983. */
  984. public function formatShopToken()
  985. {
  986. $dbResult = $this->objDShop->select();
  987. if($dbResult === false){
  988. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  989. }
  990. $objMEnterprise = new MEnterprise();
  991. foreach($dbResult as $value){
  992. $data = $objMEnterprise->createAppid($value['enterpriseId']);
  993. $this->objDShop->update(['token' => $data['token']], ['id' => $value['id']]);
  994. $this->objShopCache->setShopToken($value['enterpriseId'], $value['id'], $data['token']);
  995. }
  996. return ResultWrapper::success('操作成功');
  997. }
  998. /**
  999. * 设置总店
  1000. * @param $params
  1001. * @return ResultWrapper
  1002. */
  1003. public function setTopShop($params)
  1004. {
  1005. $dbResult = $this->objDShop->get(['id' => $params['id'], 'deleteStatus' => StatusCode::$standard]);
  1006. if($dbResult === false){
  1007. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1008. }
  1009. if(empty($dbResult)){
  1010. return ResultWrapper::fail('商铺不存在', ErrorCode::$dberror);
  1011. }
  1012. if($dbResult['isMaster'] == StatusCode::$standard){
  1013. return ResultWrapper::fail('已经是总店了', ErrorCode::$dberror);
  1014. }
  1015. $dbResult = $this->objDShop->update(['enterpriseId' => $this->enterpriseId, 'isMaster' => StatusCode::$delete, 'updateTime' => time()]);
  1016. if($dbResult === false){
  1017. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1018. }
  1019. $dbResult = $this->objDShop->update(['isMaster' => StatusCode::$standard, 'updateTime' => time()], ['id' => $params['id']]);
  1020. if($dbResult === false){
  1021. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1022. }
  1023. return ResultWrapper::success($dbResult);
  1024. }
  1025. /**
  1026. * 设置店长
  1027. * @param $params
  1028. * @return ResultWrapper
  1029. */
  1030. public function setStaffType($params)
  1031. {
  1032. //1.判断员工是否存在
  1033. $modelResult = $this->objMStaff->getStaffData(['id' => $params['id'], 'deleteStatus' => StatusCode::$standard]);
  1034. if(!$modelResult->isSuccess()){
  1035. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1036. }
  1037. $staff = $modelResult->getData();
  1038. $userCenterId = getArrayItem($staff,'userCenterId',0);
  1039. unset($modelResult);
  1040. if(empty($staff)){
  1041. return ResultWrapper::fail('员工不存在', ErrorCode::$paramError);
  1042. }
  1043. //2.判断该店铺是否已经有店长
  1044. $dbResult = $this->objDShop->get(['id' => $params['shopId'], 'deleteStatus' => StatusCode::$standard]);
  1045. if($dbResult === false){
  1046. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1047. }
  1048. $shop = $dbResult;
  1049. unset($dbResult);
  1050. if(empty($shop)){
  1051. return ResultWrapper::fail('商铺不存在', ErrorCode::$paramError);
  1052. }
  1053. if(!empty($shop['managerId'])){
  1054. return ResultWrapper::fail('该店铺已有店长', ErrorCode::$paramError);
  1055. }
  1056. //3.如果该店铺下没有这个员工的话 把该员工添加到该商铺
  1057. $modelResult = $this->objMStaff->getStaffData(['userCenterId' => $userCenterId, 'shopId' => $params['shopId'], 'deleteStatus' => StatusCode::$standard]);
  1058. if(!$modelResult->isSuccess()){
  1059. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1060. }
  1061. $staffBindShop = $modelResult->getData();
  1062. unset($modelResult);
  1063. if(empty($staffBindShop)){
  1064. $objDUserCenter = new DUserCenter();
  1065. $dbResult = $objDUserCenter->get(['id' => $staff['userCenterId']]);
  1066. if($dbResult === false){
  1067. return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$dberror);
  1068. }
  1069. $staff['mobile'] = $dbResult['mobile'];
  1070. $staff['shopId'] = $params['shopId'];
  1071. unset($staff['id']);
  1072. unset($dbResult);
  1073. $modelResult = $this->objMStaff->addStaff($staff);
  1074. if(!$modelResult->isSuccess()){
  1075. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1076. }
  1077. }
  1078. //4.添加店长
  1079. $update = [
  1080. 'managerId' => $userCenterId,
  1081. 'updateTime' => time(),
  1082. ];
  1083. $dbResult = $this->objDShop->update($update, ['id' => $params['shopId']]);
  1084. if($dbResult === false){
  1085. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1086. }
  1087. return ResultWrapper::success($userCenterId);
  1088. }
  1089. /**
  1090. * 删除店长
  1091. */
  1092. public function delManager($params)
  1093. {
  1094. $where = [
  1095. 'id' => $params['shopId'],
  1096. 'enterpriseId' => $params['enterpriseId']
  1097. ];
  1098. $dbResult = $this->objDShop->get($where);
  1099. if($dbResult === false){
  1100. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1101. }
  1102. $shop = $dbResult;
  1103. unset($dbResult);
  1104. if(empty($shop)){
  1105. return ResultWrapper::fail('商铺不存在', ErrorCode::$paramError);
  1106. }
  1107. if(empty($shop['managerId'])){
  1108. return ResultWrapper::fail('店长不存在', ErrorCode::$paramError);
  1109. }
  1110. //1.删除员工 userCenter与店铺绑定关系
  1111. $delWhere = [
  1112. 'userCenterId' => $shop['managerId'],
  1113. 'shopId' => $shop['id']
  1114. ];
  1115. $modelResult = $this->objMStaff->deleteStaff($delWhere);
  1116. if(!$modelResult->isSuccess()){
  1117. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1118. }
  1119. $update = [
  1120. 'managerId' => NULL,
  1121. 'updateTime' => time(),
  1122. ];
  1123. $dbResult = $this->objDShop->update($update,$where);
  1124. if($dbResult === false){
  1125. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1126. }
  1127. return ResultWrapper::success($dbResult);
  1128. }
  1129. /**
  1130. * Doc: (des="组装距离计算sql")
  1131. * User: XMing
  1132. * Date: 2020/12/10
  1133. * Time: 10:15 上午
  1134. * @param string $lon1 用户当前经度
  1135. * @param string $lat1 用户当前纬度
  1136. * @param string $lon2 数据库经度的字段名
  1137. * @param string $lat2 数据库纬度的字段名
  1138. * @return string
  1139. */
  1140. public static function buildDistanceSql($lon1='',$lat1='',$lon2='longitude',$lat2='latitude'): string
  1141. {
  1142. $PI = PI;
  1143. $sql = "ROUND(6378.138 * 2 * ASIN( SQRT(POW(SIN( ({$lat1} * $PI / 180 - {$lat2} * $PI / 180 ) / 2 ),2) + COS({$lat1} * $PI / 180) * COS({$lat2} * $PI / 180) * POW(SIN( ({$lon1} * $PI / 180 - {$lon2} * $PI / 180 ) / 2) ,2)) ) * 1000) / 1000 ";
  1144. return $sql;
  1145. }
  1146. /**
  1147. * Doc: (des="获取附近商家列表-按照距离排序-sql")
  1148. * User: XMing
  1149. * Date: 2020/12/10
  1150. * Time: 10:23 上午
  1151. * @param array $data
  1152. * @param $lon1
  1153. * @param $lat1
  1154. * @return ResultWrapper
  1155. */
  1156. public function nearbyLists(array $data,$lon1,$lat1): ResultWrapper
  1157. {
  1158. //$lon1 = '108.978695';
  1159. //$lat1 = '34.321583';
  1160. $map = [];
  1161. $noLocalMap = [];
  1162. foreach ($data as $value){
  1163. if (!empty($value['longitude']) && !empty($value['latitude'])){
  1164. $map[] = $value;
  1165. continue;
  1166. }
  1167. $noLocalMap[] = $value;
  1168. }
  1169. $objLocation = new Location();
  1170. foreach ($map as &$value){
  1171. $sub = $objLocation->get_distance_by_geo($lat1,$lon1,$value['latitude'],$value['longitude']);
  1172. $value['sub'] = $sub;
  1173. }
  1174. unset($value);
  1175. $subColl = array_column($map,'sub');
  1176. array_multisort($subColl,SORT_ASC,$map);
  1177. foreach ($map as &$value){
  1178. $value['meters'] = Location::toMeters($value['sub']);
  1179. }
  1180. unset($value);
  1181. foreach ($noLocalMap as &$value){
  1182. $value['meters'] = null;
  1183. }
  1184. unset($value);
  1185. $map = array_merge($map,$noLocalMap);
  1186. return ResultWrapper::success($map);
  1187. }
  1188. /**
  1189. * Doc: (des="根据企业id获取企业下总店")
  1190. * User: XMing
  1191. * Date: 2020/12/17
  1192. * Time: 4:50 下午
  1193. * @param int $enterpriseId
  1194. * @return ResultWrapper
  1195. */
  1196. public function getCenterShopByEnterprise(int $enterpriseId): ResultWrapper
  1197. {
  1198. $shop = $this->objDShop->get(['enterpriseId' => $enterpriseId,'isMaster' => StatusCode::$standard],'id,name');
  1199. if ($shop === false){
  1200. return ResultWrapper::fail($this->objDShop->error(),ErrorCode::$dberror);
  1201. }
  1202. return ResultWrapper::success($shop);
  1203. }
  1204. /**
  1205. * Doc: (des="")
  1206. * User: XMing
  1207. * Date: 2020/12/30
  1208. * Time: 5:03 下午
  1209. * @param $ids
  1210. * @return ResultWrapper
  1211. */
  1212. public function getShopIdByMerchantIds($ids): ResultWrapper
  1213. {
  1214. if (empty($ids)){
  1215. return ResultWrapper::success([]);
  1216. }
  1217. $lists = $this->objDShop->select(['merchantId'=>$ids],'*');
  1218. if ($lists === false){
  1219. return ResultWrapper::fail($this->objDShop->error(),ErrorCode::$dberror);
  1220. }
  1221. $map = [];
  1222. foreach ($lists as $list) {
  1223. $map[$list['merchantId']] = $list;
  1224. }
  1225. return ResultWrapper::success($map);
  1226. }
  1227. /**
  1228. * Doc: (des="")
  1229. * User: XMing
  1230. * Date: 2021/1/22
  1231. * Time: 6:31 下午
  1232. * @param int $id
  1233. * @return ResultWrapper
  1234. */
  1235. public function getShopById(int $id): ResultWrapper
  1236. {
  1237. $result = $this->objDShop->get($id);
  1238. if ($result === false){
  1239. return ResultWrapper::fail($this->objDShop->error(),ErrorCode::$dberror);
  1240. }
  1241. return ResultWrapper::success($result);
  1242. }
  1243. /**
  1244. * Doc: (des="")
  1245. * User: XMing
  1246. * Date: 2021/1/28
  1247. * Time: 3:58 下午
  1248. * @param array $selectParams
  1249. * @return ResultWrapper
  1250. * @throws Exception
  1251. */
  1252. public function getAll(array $selectParams): ResultWrapper
  1253. {
  1254. //获取用户的客户类型
  1255. $selectParams['modelType'] = StatusCode::$modelType['B2C'];
  1256. if (!empty($this->userCenterId)){
  1257. $objMCustomer = new MCustomer($this->enterpriseId,$this->userCenterId);
  1258. $customerResult = $objMCustomer->getCustomerInfoByUserCenterId($this->userCenterId);
  1259. if (!$customerResult->isSuccess()){
  1260. return ResultWrapper::fail($customerResult->getData(),$customerResult->getErrorCode());
  1261. }
  1262. $customer = $customerResult->getData();
  1263. $modelType = $customer['modelType'];
  1264. if (empty($modelType)){
  1265. $modelType = StatusCode::$modelType['B2C'];
  1266. }
  1267. $selectParams['modelType'] = $modelType;
  1268. }
  1269. $buildSql = self::buildSqlByParams($selectParams);
  1270. $dbResult = $this->objDShop->query($buildSql['sql']);
  1271. if ($dbResult === false) {
  1272. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1273. }
  1274. //按照距离进行排序
  1275. if ( isset($selectParams['longitude']) && !empty($selectParams['longitude']) &&
  1276. isset($selectParams['latitude']) && !empty($selectParams['latitude']) ){
  1277. $nearResult = self::nearbyLists($dbResult,$selectParams['longitude'],$selectParams['latitude']);
  1278. if (!$nearResult->isSuccess()){
  1279. return ResultWrapper::fail($nearResult->getData(),$nearResult->getErrorCode());
  1280. }
  1281. $dbResult = $nearResult->getData();
  1282. }
  1283. $dbResult = self::format($dbResult);
  1284. $count = $this->objDShop->query($buildSql['countSql']);
  1285. $return = [
  1286. 'data' => $dbResult,
  1287. 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0
  1288. ];
  1289. return ResultWrapper::success($return);
  1290. }
  1291. /**
  1292. * Doc: (des="")
  1293. * User: XMing
  1294. * Date: 2021/1/30
  1295. * Time: 12:18 下午
  1296. * @param array $selectParams
  1297. * @return array
  1298. */
  1299. public function buildSqlByParams(array $selectParams): array
  1300. {
  1301. $tableName = 'qianniao_shop_1';
  1302. $fields = ' * ';
  1303. $countField = ' COUNT(id) as total ';
  1304. $whereSql = '';
  1305. if (isset($selectParams['modelType']) && !empty($selectParams['modelType'])){
  1306. $modelType = explode(',',$selectParams['modelType']);
  1307. $str = '';
  1308. foreach ($modelType as $type){
  1309. if (!empty($str)){
  1310. $str .= ' OR ';
  1311. }
  1312. $str .= ' FIND_IN_SET('.$type.',`modelType`) ';
  1313. }
  1314. $whereSql .= ' AND ('.$str .')';
  1315. }
  1316. $countSql = 'SELECT '.$countField.' FROM '.$tableName.' WHERE enterpriseId = '.$this->enterpriseId.' and deleteStatus = '.StatusCode::$standard.' AND merchantId IS NULL and enableStatus = '.StatusCode::$standard.' AND extends IS NULL '.$whereSql;
  1317. $whereSql .= ' ORDER BY createTime DESC ';
  1318. if (isset($selectParams['limit']) && !empty($selectParams['limit'])){
  1319. $whereSql .= ' LIMIT '.$selectParams['offset'].','.$selectParams['limit'];
  1320. }
  1321. $sql = 'SELECT '.$fields.' FROM '.$tableName.' WHERE enterpriseId = '.$this->enterpriseId.' and deleteStatus = '.StatusCode::$standard.' AND merchantId IS NULL and enableStatus = '.StatusCode::$standard.' AND extends IS NULL '.$whereSql;
  1322. Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$sql);
  1323. return [
  1324. 'countSql' => $countSql,
  1325. 'sql' => $sql
  1326. ];
  1327. }
  1328. /**
  1329. * 获取商铺绑定仓库
  1330. */
  1331. public function getShopBindWarehouse($shopId)
  1332. {
  1333. $dbResult = $this->objDShop->get(['id' => $shopId]);
  1334. if($dbResult === false){
  1335. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1336. }
  1337. $shop = $dbResult;
  1338. if(empty($shop)){
  1339. return ResultWrapper::fail('查询商铺失败', ErrorCode::$paramError);
  1340. }
  1341. $warehouseId = [];
  1342. $warehouseData = [];
  1343. if(!empty($shop['warehouseId'])){
  1344. $warehouseIds = explode(',',$shop['warehouseId']);
  1345. $objMWarehouse = new MWarehouse($this->enterpriseId);
  1346. $modelResult = $objMWarehouse->selectWarehouse(['id' => $warehouseIds, 'deleteStatus' => StatusCode::$standard]);
  1347. if(!$modelResult->isSuccess()){
  1348. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1349. }
  1350. $warehouse = $modelResult->getData();
  1351. unset($modelResult);
  1352. $warehouseId = [];
  1353. $warehouseData = [];
  1354. foreach($warehouse as $value){
  1355. $warehouseId[] = $value['id'];
  1356. $warehouseData[$value['id']] = $value;
  1357. }
  1358. }
  1359. $return['shopId'] = $shopId;
  1360. $return['warehouseId'] = $warehouseId;
  1361. $return['warehouseData'] = $warehouseData;
  1362. return ResultWrapper::success($return);
  1363. }
  1364. /**
  1365. * 获取店铺绑定的仓库id
  1366. */
  1367. public function getWarehouseIdsByShopId($shopId)
  1368. {
  1369. $warehouseIds = [];
  1370. $dbResult = $this->objDShop->get(['id' => $shopId]);
  1371. if($dbResult === false){
  1372. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1373. }
  1374. if(empty($dbResult)){
  1375. return ResultWrapper::success($warehouseIds);
  1376. }
  1377. if(!empty($dbResult['warehouseId'])){
  1378. $warehouseIds = explode(',',$dbResult['warehouseId']);
  1379. }
  1380. return ResultWrapper::success($warehouseIds);
  1381. }
  1382. /**
  1383. * 获取商铺信息
  1384. */
  1385. public function getShopData($where)
  1386. {
  1387. $dbResult = $this->objDShop->select($where);
  1388. if($dbResult === false){
  1389. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1390. }
  1391. return ResultWrapper::success($dbResult);
  1392. }
  1393. /**
  1394. * 修改商铺
  1395. * @param $update
  1396. * @param $where
  1397. * @return ResultWrapper
  1398. */
  1399. public function updateShopData($update, $where)
  1400. {
  1401. $dbResult = $this->objDShop->update($update, $where);
  1402. if($dbResult === false){
  1403. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1404. }
  1405. return ResultWrapper::success($dbResult);
  1406. }
  1407. /**
  1408. * 多个店铺Id查询店铺名称
  1409. * @param $departmentIds
  1410. * @return ResultWrapper
  1411. */
  1412. public function getShopNameByIds(array $ids)
  1413. {
  1414. //查询所有 id 店铺名称
  1415. $where = [
  1416. 'id' => array_values($ids),
  1417. 'deleteStatus' => StatusCode::$standard,
  1418. ];
  1419. $fields = 'id,name';
  1420. $shopData = $this->objDShop->select($where, $fields);
  1421. if($shopData === false){
  1422. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1423. }
  1424. return $shopData;
  1425. }
  1426. // 根据实体企业id和token获取商铺id
  1427. public function getShopid($enterpriseId,$token)
  1428. {
  1429. //查询所有 id 店铺名称
  1430. $where = [
  1431. 'enterpriseId' =>$enterpriseId,
  1432. 'token' =>$token,
  1433. ];
  1434. $fields = 'id,name';
  1435. $shopData = $this->objDShop->select($where, $fields);
  1436. if($shopData === false){
  1437. return ResultWrapper::fail($this->objDShop->error(), ErrorCode::$dberror);
  1438. }
  1439. return $shopData;
  1440. }
  1441. }