Shop.Class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * 商铺管理模块
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/10/31
  7. * Time: 15:02
  8. */
  9. namespace JinDouYun\Controller\Shop;
  10. use JinDouYun\Model\Customer\MCustomer;
  11. use JinDouYun\Model\Enterprise\MEnterprise;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use Mall\Framework\Core\StatusCode;
  15. use JinDouYun\Cache\ShopCache;
  16. use JinDouYun\Controller\BaseController;
  17. use JinDouYun\Model\Shop\MShop;
  18. use JinDouYun\Cache\TempSaveCache;
  19. class Shop extends BaseController
  20. {
  21. private $objMShop;
  22. private $objShopCache;
  23. private $objTempSaveCache;
  24. private $objMCustomer;
  25. public function __construct($isCheckAcl = true, $isMustLogin = true)
  26. {
  27. parent::__construct($isCheckAcl, $isMustLogin);
  28. $this->objMShop = new MShop($this->onlineEnterpriseId, $this->onlineUserId);
  29. $this->objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
  30. $this->objShopCache = new ShopCache();
  31. $this->objTempSaveCache = new TempSaveCache();
  32. }
  33. /**
  34. * 添加和编辑商铺管理公共字段处理方法
  35. *
  36. * @return array
  37. */
  38. public function commonFieldFilter(){
  39. $params = $this->request->getRawJson();
  40. if( empty($params) ){
  41. $this->sendOutput('参数为空', ErrorCode::$paramError );
  42. }
  43. $shopData = [
  44. 'logo' => isset($params['logo']) ? $params['logo'] : '',
  45. 'shopType' => isset($params['shopType']) ? $params['shopType'] : '',
  46. 'name' => isset($params['name']) ? $params['name'] : '',
  47. 'openTime' => isset($params['openTime']) ? $params['openTime'] : '',
  48. 'contactName' => isset($params['contactName']) ? $params['contactName'] : '',//联系人
  49. 'mobile' => isset($params['mobile']) ? $params['mobile'] : '',//联系电话
  50. //'shopPartnerId' => isset($params['shopPartnerId']) ? $params['shopPartnerId'] : '',//合伙人Id
  51. //'partnerName' => isset($params['partnerName']) ? $params['partnerName'] : '',//负责人名称
  52. 'salesArea' => isset($params['salesArea']) ? $params['salesArea'] : '',//接json值
  53. //'isAllCountry' => isset($params['isAllCountry']) ? $params['isAllCountry'] : 2,//是否是全国销售 1是全国 2部分
  54. ];
  55. //非暂存则验空
  56. if (!isset($params['tempSave']) || $params['tempSave'] == false) {
  57. foreach($shopData as $key => $value){
  58. if(empty($value) && $value !== 0){
  59. $this->sendOutput($key.'参数错误', ErrorCode::$paramError );
  60. }
  61. }
  62. }
  63. //处理是否全国销售
  64. /*$shopData['salesArea'] = json_decode($shopData['salesArea'],true);
  65. if ($shopData['isAllCountry']==1 && !in_array('100000-0-0', $shopData['salesArea'])) {
  66. $shopData['salesArea'][] = '100000-0-0';
  67. }
  68. if ($shopData['isAllCountry']==2 && in_array('100000-0-0', $shopData['salesArea'])) {
  69. $pos = array_search('100000-0-0', $shopData['salesArea']);
  70. unset($shopData['salesArea'][$pos]);
  71. }
  72. $shopData['salesArea'] = json_encode($shopData['salesArea']);
  73. unset($shopData['isAllCountry']);*/
  74. $shopData['tempSave']= isset($params['tempSave']) ? $params['tempSave'] : false;
  75. $shopData['area']= isset($params['area']) ? $params['area'] : [];
  76. $shopData['provinceCode'] = isset($params['provinceCode']) ? $params['provinceCode'] : '';//商铺地址
  77. $shopData['cityCode'] = isset($params['cityCode']) ? $params['cityCode'] : '';
  78. $shopData['districtCode'] = isset($params['districtCode']) ? $params['districtCode'] : '';
  79. $shopData['address'] = isset($params['address']) ? $params['address'] : '';
  80. $shopData['warehouseId'] = isset($params['warehouseId']) ? $params['warehouseId'] : '';
  81. $shopData['longitude'] = isset($params['longitude']) ? $params['longitude'] : 0;//商铺坐标
  82. $shopData['latitude'] = isset($params['latitude']) ? $params['latitude'] : 0;
  83. $shopData['recommend_user_id'] = isset($params['recommend_user_id']) ? $params['recommend_user_id'] : 0;
  84. $shopData['customerId'] = isset($params['customerId']) ? $params['customerId'] : 0;
  85. $shopData['deleteStatus']= StatusCode::$standard;
  86. $shopData['enableStatus']= isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$standard;
  87. $shopData['createTime'] = time();
  88. $shopData['updateTime'] = time();
  89. $shopData['modelType'] = getArrayItem($params,'modelType',0);
  90. $shopData['describe'] = isset($params['describe']) ? $params['describe'] : '';
  91. return $shopData;
  92. }
  93. /**
  94. * 添加商铺
  95. * @throws \Exception
  96. */
  97. public function addShop()
  98. {
  99. $dbResult = $this->objMShop->checkAllowAddShop($this->onlineEnterpriseId);
  100. if (!$dbResult->isSuccess()){
  101. parent::sendOutput($dbResult->getData(),$dbResult->getErrorCode());
  102. }
  103. $shopData = $this->commonFieldFilter();
  104. if ($shopData['tempSave']) {
  105. $this->objTempSaveCache->saveShop($this->onlineEnterpriseId, $this->onlineUserId, $shopData);
  106. parent::sendOutput('暂存成功');
  107. }
  108. unset($shopData['tempSave']);
  109. unset($shopData['area']);
  110. $shopData['openTime'] = json_encode($shopData['openTime']);
  111. $shopData['salesArea'] = json_encode($shopData['salesArea']);
  112. $result = $this->objMShop ->addShop($shopData);
  113. if($result->isSuccess()){
  114. //清除redis 企业和商铺关联key
  115. $this->objShopCache->cleanUserRelationEnterprise($this->onlineEnterpriseId);
  116. //删除暂存数据
  117. $this->objTempSaveCache->delShop($this->onlineEnterpriseId, $this->onlineUserId);
  118. parent::sendOutput($result->getData());
  119. }else{
  120. parent::sendOutput($result->getData(), $result->getErrorCode());
  121. }
  122. }
  123. /**
  124. * 获取暂存信息
  125. */
  126. public function getTempData()
  127. {
  128. $salesArea = $this->objMShop->getSalesAreaInEnterprise();
  129. $result = $this->objTempSaveCache->getShop($this->onlineEnterpriseId, $this->onlineUserId);
  130. if (!empty($result['salesArea'])) {
  131. foreach ($result['salesArea'] as $key => &$area) {
  132. if (in_array($area, $salesArea)) {
  133. unset($result['salesArea'][$key]);
  134. }
  135. }
  136. }
  137. $this->sendOutput($result);
  138. }
  139. /**
  140. * 获取指定商铺信息
  141. */
  142. public function getShopInfo()
  143. {
  144. $params['id'] = $this->request->param('request_id');
  145. if ( !$params['id'] ) {
  146. $this->sendOutput('参数错误', ErrorCode::$paramError );
  147. }
  148. $result = $this->objMShop->getShopInfo($params);
  149. if($result->isSuccess()){
  150. $this->sendOutput($result->getData());
  151. }else{
  152. $this->sendOutput($result->getData(), $result->getErrorCode());
  153. }
  154. }
  155. /**
  156. * 编辑商铺
  157. */
  158. public function editShop()
  159. {
  160. $shopId = $this->request->param('request_id');
  161. if(empty($shopId)){
  162. $this->sendOutput('参数错误', ErrorCode::$paramError);
  163. }
  164. $shopData = $this->commonFieldFilter();
  165. $shopData['id'] = $shopId;
  166. unset($shopData['createTime']);
  167. unset($shopData['tempSave']);
  168. unset($shopData['area']);
  169. $shopData['openTime'] = json_encode($shopData['openTime']);
  170. $shopData['salesArea'] = json_encode($shopData['salesArea']);
  171. $shopData['warehouseId'] = array_unique($shopData['warehouseId']);
  172. $result = $this->objMShop->editShop($shopData);
  173. if($result->isSuccess()){
  174. parent::sendOutput($result->getData());
  175. }else{
  176. parent::sendOutput($result->getData(), $result->getErrorCode());
  177. }
  178. }
  179. /**
  180. * 删除商铺
  181. */
  182. public function delShop()
  183. {
  184. $params['id'] = $this->request->param('request_id');
  185. if(!$params['id']){
  186. $this->sendOutput('参数错误', ErrorCode::$paramError);
  187. }
  188. $result = $this->objMShop->delShop($params);
  189. if($result->isSuccess()){
  190. parent::sendOutput($result->getData());
  191. }else{
  192. parent::sendOutput($result->getData(), $result->getErrorCode());
  193. }
  194. }
  195. /**
  196. * 商铺启用和禁用
  197. */
  198. public function updateShopStatus()
  199. {
  200. $paramsData = $this->request->getRawJson();
  201. if (empty($paramsData)) {
  202. $this->sendOutput('参数为空', ErrorCode::$paramError);
  203. }
  204. $params = [
  205. 'id' => isset($paramsData['id']) ? $paramsData['id'] : '',
  206. 'enableStatus' => isset($paramsData['enableStatus']) ? $paramsData['enableStatus'] : '',
  207. ];
  208. foreach ($params as $key => $value) {
  209. if (empty($value) && $value !== 0) {
  210. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  211. }
  212. }
  213. $result = $this->objMShop->updateShopStatus($params);
  214. if($result->isSuccess()){
  215. parent::sendOutput($result->getData());
  216. }else{
  217. parent::sendOutput($result->getData(), $result->getErrorCode());
  218. }
  219. }
  220. /**
  221. * 后台所有商铺列表
  222. */
  223. public function getAllShop()
  224. {
  225. $params = $this->request->getRawJson();
  226. if( empty($params) ){
  227. $this->sendOutput('参数为空', ErrorCode::$paramError );
  228. }
  229. if(isset($params['enableStatus']) && !empty($params['enableStatus'])) {
  230. $selectParams['enableStatus'] = $params['enableStatus'];
  231. }
  232. $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
  233. $selectParams['limit'] = $pageParams['limit'];
  234. $selectParams['offset'] = $pageParams['offset'];
  235. isset($params['action']) && $selectParams['action'] = $params['action'];
  236. isset($params['materielId']) && $selectParams['materielId'] = $params['materielId'];
  237. $result = $this->objMShop->getAllShop($selectParams);
  238. if($result->isSuccess()){
  239. $returnData = $result->getData();
  240. $pageData = [
  241. 'pageIndex' => $params['page'],
  242. 'pageSize' => $params['pageSize'],
  243. 'pageTotal' => $returnData['total'],
  244. ];
  245. //使用redis存入企业id下所有的商铺id
  246. $this->objShopCache->cacheEnterpriseAndShop($this->onlineEnterpriseId, $returnData['data']);
  247. parent::sendOutput($returnData['data'], 0, $pageData);
  248. }else{
  249. parent::sendOutput($result->getData(), $result->getErrorCode());
  250. }
  251. }
  252. /**
  253. * 后台商铺搜索
  254. */
  255. public function search()
  256. {
  257. $params = $this->request->getRawJson();
  258. if( empty($params) ){
  259. $this->sendOutput('参数为空', ErrorCode::$paramError );
  260. }
  261. $selectParams = [
  262. 'name' => isset($params['name']) ? $params['name'] : '',
  263. 'shopPartnerId' => isset($params['shopPartnerId']) ? $params['shopPartnerId'] : '',
  264. 'shopType' => isset($params['shopType']) ? $params['shopType'] : '',
  265. 'enableStatus' => isset($params['enableStatus']) ? $params['enableStatus'] : '',
  266. ];
  267. $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
  268. $selectParams['limit'] = $pageParams['limit'];
  269. $selectParams['offset'] = $pageParams['offset'];
  270. $result = $this->objMShop->search($selectParams);
  271. if($result->isSuccess()){
  272. $returnData = $result->getData();
  273. $pageData = [
  274. 'pageIndex' => $params['page'],
  275. 'pageSize' => $params['pageSize'],
  276. 'pageTotal' => $returnData['total'],
  277. ];
  278. parent::sendOutput($returnData['data'], 0, $pageData);
  279. }else{
  280. parent::sendOutput($result->getData(), $result->getErrorCode());
  281. }
  282. }
  283. /**
  284. * 更换负责人
  285. */
  286. public function changeManagerForShop() {
  287. $params = $this->request->getRawJson();
  288. if(empty($params)){
  289. $this->sendOutput('参数为空', ErrorCode::$paramError );
  290. }
  291. $shopData = [
  292. 'id' => isset($params['id']) ? $params['id'] : '',
  293. 'shopPartnerId' => isset($params['shopPartnerId']) ? $params['shopPartnerId'] : ''
  294. ];
  295. foreach($shopData as $key => $value){
  296. if(empty($value) && $value !== 0){
  297. $this->sendOutput($key.'参数错误', ErrorCode::$paramError );
  298. }
  299. }
  300. $result = $this->objMShop->changeManagerForShop($shopData);
  301. if($result->isSuccess()){
  302. parent::sendOutput($result->getData());
  303. }else{
  304. parent::sendOutput($result->getData(), $result->getErrorCode());
  305. }
  306. }
  307. /**
  308. * 获取当前企业下已有的销售区域
  309. */
  310. public function getSalesAreaInEnterprise() {
  311. $result = $this->objMShop->getSalesAreaInEnterprise();
  312. parent::sendOutput($result, 0, ['pageTotal'=>0]);
  313. }
  314. /**
  315. * 获取员工所在商铺列表
  316. */
  317. public function getShopByStaff() {
  318. $params = $this->request->getRawJson();
  319. if( empty($params) ){
  320. $this->sendOutput('参数为空', ErrorCode::$paramError );
  321. }
  322. $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
  323. $selectParams['limit'] = $pageParams['limit'];
  324. $selectParams['offset'] = $pageParams['offset'];
  325. $result = $this->objMShop->getShopByStaff($selectParams);
  326. if(!$result->isSuccess()){
  327. parent::sendOutput($result->getData(), $result->getErrorCode());
  328. }
  329. $returnData = $result->getData();
  330. $pageData = [
  331. 'pageIndex' => $params['page'],
  332. 'pageSize' => $params['pageSize'],
  333. 'pageTotal' => $returnData['total'],
  334. ];
  335. parent::sendOutput($returnData['data'], 0, $pageData);
  336. }
  337. /**
  338. * 格式化商铺token
  339. * @return ResultWrapper
  340. */
  341. public function formatShopToken()
  342. {
  343. $result = $this->objMShop->formatShopToken();
  344. if(!$result->isSuccess()){
  345. parent::sendOutput($result->getData(), $result->getErrorCode());
  346. }
  347. parent::sendOutput($result->getData());
  348. }
  349. /**
  350. * 设置总店
  351. * @return ResultWrapper
  352. */
  353. public function setTopShop()
  354. {
  355. $id = $this->request->param('request_id');
  356. if(empty($id)){
  357. parent::sendOutput('id参数错误', ErrorCode::$paramError);
  358. }
  359. $result = $this->objMShop->setTopShop(['id' => $id]);
  360. if(!$result->isSuccess()){
  361. parent::sendOutput($result->getData(), $result->getErrorCode());
  362. }
  363. parent::sendOutput($result->getData());
  364. }
  365. /**
  366. * 设置店长
  367. * @throws \Exception
  368. */
  369. public function setStaffType()
  370. {
  371. $params = $this->request->getRawJson();
  372. if (empty($params)) {
  373. $this->sendOutput('参数为空', ErrorCode::$paramError);
  374. }
  375. $data = [
  376. "id" => isset($params['id']) ? $params['id'] : '',
  377. "shopId" => isset($params['shopId']) ? $params['shopId'] : '',
  378. ];
  379. foreach($data as $key => $value){
  380. if(empty($value)){
  381. parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
  382. }
  383. }
  384. $data['enterpriseId'] = $this->onlineEnterpriseId;
  385. $modelResult = $this->objMShop->setStaffType($data);
  386. if(!$modelResult->isSuccess()){
  387. parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
  388. }
  389. parent::sendOutput($modelResult->getData());
  390. }
  391. /**
  392. * 删除店长
  393. * @throws \Exception
  394. */
  395. public function delManager()
  396. {
  397. $params = $this->request->getRawJson();
  398. if (empty($params)) {
  399. $this->sendOutput('参数为空', ErrorCode::$paramError);
  400. }
  401. $data = [
  402. "shopId" => isset($params['shopId']) ? $params['shopId'] : '',
  403. ];
  404. foreach($data as $key => $value){
  405. if(empty($value)){
  406. parent::sendOutput($key.'参数错误', ErrorCode::$paramError);
  407. }
  408. }
  409. $data['enterpriseId'] = $this->onlineEnterpriseId;
  410. $modelResult = $this->objMShop->delManager($data);
  411. if(!$modelResult->isSuccess()){
  412. parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
  413. }
  414. parent::sendOutput($modelResult->getData());
  415. }
  416. /**
  417. * 获取指定店铺绑定的所有仓库数据
  418. */
  419. public function getShopBindWarehouse()
  420. {
  421. $shopId = $this->request->param('request_id');
  422. if(empty($shopId)){
  423. parent::sendOutput('shopId参数错误', ErrorCode::$paramError);
  424. }
  425. $result = $this->objMShop->getShopBindWarehouse($shopId);
  426. if(!$result->isSuccess()){
  427. parent::sendOutput($result->getData(), $result->getErrorCode());
  428. }
  429. parent::sendOutput($result->getData());
  430. }
  431. }