MMerchant.Class.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. <?php
  2. /**
  3. * 商户Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2020/09/25
  7. * Time: 09:40
  8. */
  9. namespace JinDouYun\Model\Merchant;
  10. use JinDouYun\Cache\MerchantCache;
  11. use JinDouYun\Dao\Enterprise\DUserBindEnterprise;
  12. use JinDouYun\Dao\Shop\DShopBindStaff;
  13. use JinDouYun\Dao\UserCenter\DUserCenter;
  14. use JinDouYun\Model\Goods\MGoods;
  15. use JinDouYun\Model\Shop\MShop;
  16. use JinDouYun\Model\Stock\MWarehouse;
  17. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  18. use JinDouYun\Model\UserCenter\MUserCenterRegister;
  19. use Mall\Framework\Core\ErrorCode;
  20. use Mall\Framework\Core\StatusCode;
  21. use Mall\Framework\Core\ResultWrapper;
  22. use JinDouYun\Dao\Merchant\DMerchantApply;
  23. use JinDouYun\Dao\Merchant\DMerchantSetting;
  24. use JinDouYun\Dao\Shop\DShop;
  25. use JinDouYun\Model\MBaseModel;
  26. class MMerchant extends MBaseModel
  27. {
  28. private $objDMerchantApply;
  29. private $objDMerchantSetting;
  30. private $objDShop;
  31. private $userCenterId;
  32. private $enterpriseId;
  33. private $cutTable = 250000;
  34. public function __construct($enterpriseId, $userCenterId = false)
  35. {
  36. $this->enterpriseId = $enterpriseId;
  37. $this->userCenterId = $userCenterId;
  38. $this->objDMerchantApply = new DMerchantApply();
  39. $this->objDMerchantSetting = new DMerchantSetting();
  40. $this->objDShop = new DShop();
  41. $this->objDShop->setTable('qianniao_shop_' . ceil($this->enterpriseId / $this->cutTable));
  42. }
  43. /**
  44. * 入驻添加
  45. * @param $params
  46. * @return ResultWrapper
  47. */
  48. public function addApply($params)
  49. {
  50. $dbResult = $this->objDMerchantApply->get(['userCenterId' => $params['userCenterId'], 'deleteStatus' => StatusCode::$standard]);
  51. if ($dbResult === false) {
  52. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  53. }
  54. if (!empty($dbResult)) {
  55. return ResultWrapper::fail('您已经申请过了', ErrorCode::$paramError);
  56. }
  57. //商户申请资料
  58. $insert = $params;
  59. if (isset($params['info'])) {
  60. $insert['info'] = json_encode($params['info']);
  61. }
  62. $insert['enterpriseId'] = $this->enterpriseId;
  63. $insert['auditStatus'] = StatusCode::$auditStatus['auditing'];
  64. $insert['enabledStatus'] = StatusCode::$standard;
  65. $insert['deleteStatus'] = StatusCode::$standard;
  66. $insert['createTime'] = time();
  67. $insert['updateTime'] = time();
  68. $dbResult = $this->objDMerchantApply->insert($insert);
  69. if ($dbResult === false) {
  70. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  71. }
  72. return ResultWrapper::success($dbResult);
  73. }
  74. /**
  75. * 入驻审核
  76. * @param $update
  77. * @param $where
  78. * @return ResultWrapper
  79. * @throws \Exception
  80. */
  81. public function auditApply($update, $where)
  82. {
  83. //查询入驻
  84. $dbResult = $this->objDMerchantApply->get($where);
  85. if ($dbResult === false) {
  86. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  87. }
  88. $apply = $dbResult;
  89. unset($dbResult);
  90. if (empty($apply)) {
  91. return ResultWrapper::fail('入驻信息为空', ErrorCode::$paramError);
  92. }
  93. //判断审核
  94. if (isset($update['auditStatus']) && $update['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
  95. //审核通过
  96. //修改入驻状态
  97. $update['auditStatus'] = StatusCode::$auditStatus['auditPass'];
  98. $update['auditTime'] = time();
  99. //创建关联数据
  100. $params = array_merge($apply, $update);
  101. $modelResult = self::createMerchantBindData($params);
  102. if (!$modelResult->isSuccess()) {
  103. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  104. }
  105. $update['extend'] = $modelResult->getData();
  106. }
  107. $dbResult = $this->objDMerchantApply->update($update, $where);
  108. if ($dbResult === false) {
  109. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  110. }
  111. return ResultWrapper::success($dbResult);
  112. }
  113. /**
  114. * 创建商户关联创建数据
  115. * @param $params
  116. * @return ResultWrapper
  117. * @throws \Exception
  118. */
  119. public function createMerchantBindData($params)
  120. {
  121. //1.查询user表
  122. $objDUserCenter = new DUserCenter();
  123. $dbResult = $objDUserCenter->get(['id' => $params['userCenterId']]);
  124. if ($dbResult === false) {
  125. return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$paramError);
  126. }
  127. $user = $dbResult;
  128. unset($dbResult);
  129. if (empty($user)) {
  130. return ResultWrapper::fail('userCenter数据为空', ErrorCode::$paramError);
  131. }
  132. //1.1判断user数据 修改userCenter表客户来源状态 改为后台客户 增加默认密码
  133. $password = 88888888;
  134. $return = '当前登录手机号已经注册后台, 已成功绑定商户';
  135. if ($user['isCustomer'] == StatusCode::$customerType['customer']) {
  136. $userUpdate = [
  137. 'password' => password_hash($password, PASSWORD_DEFAULT),
  138. 'isCustomer' => StatusCode::$customerType['user'],
  139. 'updateTime' => time()
  140. ];
  141. $dbResult = $objDUserCenter->update($userUpdate, ['id' => $params['userCenterId']]);
  142. if ($dbResult === false) {
  143. return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$paramError);
  144. }
  145. $return = '当前登录手机号未注册, 已自动注册, 商户绑定成功, 默认密码:' . $password;
  146. }
  147. //3.增加企业绑定user表数据 把该userCenterId绑定为商户
  148. $objDUserBindEnterprise = new DUserBindEnterprise();
  149. $enterpriseBindData = [
  150. 'userCenterId' => $params['userCenterId'],
  151. 'enterpriseId' => $this->enterpriseId,
  152. 'merchantId' => $params['id'],
  153. 'shopId' => 0,
  154. 'roleType' => StatusCode::$roleType['merchant'],
  155. 'updateTime' => time(),
  156. 'createTime' => time(),
  157. ];
  158. $dbResult = $objDUserBindEnterprise->get(['enterpriseId' => $enterpriseBindData['enterpriseId'], 'userCenterId' => $enterpriseBindData['userCenterId'], 'roleType' => $enterpriseBindData['roleType']]);
  159. if ($dbResult === false) {
  160. return ResultWrapper::fail($objDUserBindEnterprise->error(), ErrorCode::$dberror);
  161. }
  162. $bind = $dbResult;
  163. unset($dbResult);
  164. if (empty($bind)) {
  165. $dbResult = $objDUserBindEnterprise->insert($enterpriseBindData);
  166. if ($dbResult === false) {
  167. return ResultWrapper::fail($objDUserBindEnterprise->error(), ErrorCode::$dberror);
  168. }
  169. }
  170. return ResultWrapper::success($return);
  171. }
  172. /**
  173. * 新增商户
  174. * @param $params
  175. * @return ResultWrapper
  176. */
  177. public function addMerchant($params)
  178. {
  179. $insertData = [
  180. 'enterpriseId' => $this->enterpriseId,
  181. 'name' => $params['name'],
  182. 'contactName' => $params['contactName'],
  183. 'contactMobile' => $params['contactMobile'],
  184. ];
  185. isset($params['desc']) && $insertData['desc'] = !empty($params['desc']) ? $params['desc'] : '';
  186. isset($params['category']) && $insertData['category'] = !empty($params['category']) ? $params['category'] : '';
  187. isset($params['provinceCode']) && $insertData['provinceCode'] = !empty($params['provinceCode']) ? $params['provinceCode'] : '';
  188. isset($params['cityCode']) && $insertData['cityCode'] = !empty($params['cityCode']) ? $params['cityCode'] : '';
  189. isset($params['districtCode']) && $insertData['districtCode'] = !empty($params['districtCode']) ? $params['districtCode'] : '';
  190. isset($params['address']) && $insertData['address'] = !empty($params['address']) ? $params['address'] : '';
  191. isset($params['note']) && $insertData['note'] = !empty($params['note']) ? $params['note'] : '';
  192. isset($params['settleType']) && $insertData['settleType'] = !empty($params['settleType']) ? $params['settleType'] : '';
  193. isset($params['settle']) && $insertData['settle'] = !empty($params['settle']) ? json_encode($params['settle']) : '';
  194. isset($params['auditType']) && $insertData['auditType'] = !empty($params['auditType']) ? $params['auditType'] : '';
  195. isset($params['audit']) && $insertData['audit'] = !empty($params['audit']) ? json_encode($params['audit']) : '';
  196. isset($params['settingType']) && $insertData['settingType'] = !empty($params['settingType']) ? $params['settingType'] : '';
  197. isset($params['setting']) && $insertData['setting'] = !empty($params['setting']) ? json_encode($params['setting']) : '';
  198. $insertData['createTime'] = time();
  199. $insertData['deleteStatus'] = StatusCode::$standard;
  200. $insertData['enabledStatus'] = StatusCode::$standard;
  201. $insertData['auditStatus'] = StatusCode::$auditStatus['auditPass'];
  202. $insertData['auditTime'] = time();
  203. $password = '88888888';
  204. if (!isset($params['userCenterMobile'])) {
  205. return ResultWrapper::fail('userCenterMobile参数错误', ErrorCode::$paramError);
  206. }
  207. $objMUserCenterRegister = new MUserCenterRegister();
  208. $modelResult = $objMUserCenterRegister->mobileBindStatus($params['userCenterMobile'], $this->enterpriseId, StatusCode::$roleType['merchant']);
  209. if (!$modelResult->isSuccess()) {
  210. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  211. }
  212. $userCenterId = $modelResult->getData();
  213. unset($modelResult);
  214. if (!empty($userCenterId)) {
  215. return ResultWrapper::fail('登录手机号已经是商户', ErrorCode::$paramError);
  216. }
  217. //1.创建后台账户
  218. $objDUserCenter = new DUserCenter();
  219. $dbResult = $objDUserCenter->get(['mobile' => $params['userCenterMobile']]);
  220. if ($dbResult === false) {
  221. return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$dberror);
  222. }
  223. $user = $dbResult;
  224. unset($dbResult);
  225. if (empty($user)) {
  226. $userInsert = [
  227. 'mobile' => $params['userCenterMobile'],
  228. 'password' => password_hash($password, PASSWORD_DEFAULT),
  229. 'deleteStatus' => StatusCode::$standard,
  230. 'source' => StatusCode::$source['manage'],
  231. 'isCustomer' => StatusCode::$customerType['user'],
  232. 'createTime' => time(),
  233. 'updateTime' => time(),
  234. ];
  235. $dbResult = $objDUserCenter->insert($userInsert);
  236. if ($dbResult === false) {
  237. return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$dberror);
  238. }
  239. $insertData['userCenterId'] = $dbResult;
  240. $return = '该登录手机号未注册, 已自动注册, 密码:' . $password;
  241. } else {
  242. $insertData['userCenterId'] = $user['id'];
  243. $return = '该登录手机号已经注册, 已成功绑定商户';
  244. }
  245. //2.创建商户
  246. $dbResult = $this->objDMerchantApply->insert($insertData);
  247. if ($dbResult === false) {
  248. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  249. }
  250. $insertData['id'] = $dbResult;
  251. //3.创建角色绑定数据
  252. $modelResult = self::createMerchantBindData($insertData);
  253. if (!$modelResult->isSuccess()) {
  254. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  255. }
  256. return ResultWrapper::success($return);
  257. }
  258. /**
  259. * 修改商户
  260. * @param $update
  261. * @param $where
  262. * @return ResultWrapper
  263. */
  264. public function updateMerchant($update, $where = [])
  265. {
  266. $dbResult = $this->objDMerchantApply->get($where);
  267. if ($dbResult === false) {
  268. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$paramError);
  269. }
  270. $merchant = $dbResult;
  271. if (empty($merchant)) {
  272. return ResultWrapper::fail('修改数据不存在', ErrorCode::$paramError);
  273. }
  274. $where['enterpriseId'] = $this->enterpriseId;
  275. unset($update['enterpriseId']);
  276. $update['updateTime'] = time();
  277. if (isset($update['deleteStatus']) && $update['deleteStatus'] == StatusCode::$delete) {
  278. $update['deleteTime'] = time();
  279. }
  280. if (isset($update['auditStatus']) && $update['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
  281. $update['auditTime'] = time();
  282. }
  283. if (isset($update['enabledStatus'])) {
  284. if($merchant['enabledStatus'] == StatusCode::$delete){
  285. $update['enabledStatus'] = StatusCode::$standard;
  286. }else{
  287. $update['enabledStatus'] = StatusCode::$delete;
  288. }
  289. }
  290. $updateShopData = [];
  291. if(isset($update['name']) && !empty($update['name']) && $merchant['name'] != $update['name']){
  292. $updateShopData['name'] = $update['name'];
  293. }
  294. //获取仓库
  295. if(isset($update['warehouseId']) && !empty($update['warehouseId'])){
  296. if(is_array($update['warehouseId'])){
  297. $warehouseId = $update['warehouseId'];
  298. $update['warehouseId'] = implode(',',$update['warehouseId']);
  299. }else{
  300. $warehouseId = [$update['warehouseId']];
  301. }
  302. $objMWarehouse = new MWarehouse($this->enterpriseId);
  303. $modelResult = $objMWarehouse->selectWarehouse(['id' => $warehouseId, 'deleteStatus' => StatusCode::$standard]);
  304. if(!$modelResult->isSuccess()){
  305. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  306. }
  307. $warehouseResult = $modelResult->getData();
  308. unset($modelResult);
  309. if(empty($warehouseResult)){
  310. return ResultWrapper::fail('仓库查询失败', ErrorCode::$paramError);
  311. }
  312. $selectWarehouseId = array_column($warehouseResult, 'id');
  313. foreach($warehouseId as $value){
  314. if(!in_array($value,$selectWarehouseId)){
  315. return ResultWrapper::fail($value.'仓库查询失败', ErrorCode::$paramError);
  316. }
  317. }
  318. $updateShopData['warehouseId'] = $update['warehouseId'];
  319. }
  320. if(!empty($updateShopData)){
  321. $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
  322. $modelResult = $objMShop->updateShopData($updateShopData, ['enterpriseId' => $this->enterpriseId, 'merchantId' => $where['id'], 'deleteStatus' => 5]);
  323. if(!$modelResult->isSuccess()){
  324. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  325. }
  326. }
  327. unset($update['warehouseId']);
  328. unset($update['userCenterMobile']);
  329. $dbResult = $this->objDMerchantApply->update($update, $where);
  330. if ($dbResult === false) {
  331. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  332. }
  333. return ResultWrapper::success($dbResult);
  334. }
  335. /**
  336. * 修改申请
  337. * @param $update
  338. * @param $where
  339. * @return ResultWrapper
  340. */
  341. public function updateApply($update, $where = [])
  342. {
  343. $dbResult = $this->objDMerchantApply->get($where);
  344. if ($dbResult === false) {
  345. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$paramError);
  346. }
  347. if (empty($dbResult)) {
  348. return ResultWrapper::fail('修改数据不存在', ErrorCode::$paramError);
  349. }
  350. if ($dbResult['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
  351. return ResultWrapper::fail('审核通过不可以修改', ErrorCode::$paramError);
  352. }
  353. $where['enterpriseId'] = $this->enterpriseId;
  354. unset($update['enterpriseId']);
  355. $update['updateTime'] = time();
  356. $update['auditStatus'] = StatusCode::$auditStatus['auditing'];
  357. if (isset($update['info']) && !empty($update['info'])) {
  358. $update = json_encode($update['info']);
  359. } else {
  360. unset($update['info']);
  361. }
  362. $update['auditFailReason'] = '';
  363. $dbResult = $this->objDMerchantApply->update($update, $where);
  364. if ($dbResult === false) {
  365. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  366. }
  367. return ResultWrapper::success($dbResult);
  368. }
  369. /**
  370. * 商户列表
  371. * @param $params
  372. * @return ResultWrapper
  373. */
  374. public function getAllMerchant($params = [])
  375. {
  376. $buildSql = self::buildSqlBySelectParams($params);
  377. $returnData = $this->objDMerchantApply->query($buildSql['sql']);
  378. if ($returnData === false){
  379. return ResultWrapper::fail($this->objDMerchantApply->error(),ErrorCode::$dberror);
  380. }
  381. $merchantIds = [];
  382. foreach ($returnData as &$value) {
  383. !empty($value['settle']) && $value['settle'] = json_decode($value['settle'], true);
  384. !empty($value['audit']) && $value['audit'] = json_decode($value['audit'], true);
  385. !empty($value['setting']) && $value['setting'] = json_decode($value['setting'], true);
  386. !empty($value['info']) && $value['info'] = json_decode($value['info'], true);
  387. $value['materielNum'] = 0;
  388. $value['orderNum'] = 0;
  389. $value['subPayMoney'] = 0;
  390. $merchantIds[] = $value['id'];
  391. }
  392. unset($value);
  393. $dbResult = $this->objDMerchantApply->query($buildSql['countSql']);
  394. if ($dbResult === false) {
  395. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  396. }
  397. $count = $dbResult;
  398. unset($dbResult);
  399. $objMShop = new MShop($this->enterpriseId,$this->userCenterId);
  400. $shopMapResult = $objMShop->getShopIdByMerchantIds($merchantIds);
  401. if (!$shopMapResult->isSuccess()){
  402. return ResultWrapper::fail($shopMapResult->getData(),$shopMapResult->getErrorCode());
  403. }
  404. $shopMap = $shopMapResult->getData();
  405. foreach ($returnData as &$value){
  406. $value['shopId'] = isset($shopMap[$value['id']]) ? $shopMap[$value['id']]['id'] : 0;
  407. }
  408. unset($value);
  409. $return = [
  410. 'data' => self::formatMerchant($returnData),
  411. 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
  412. ];
  413. return ResultWrapper::success($return);
  414. }
  415. /**
  416. * 商户列表不分页
  417. * @param $params
  418. * @return ResultWrapper
  419. */
  420. public function getAllMerchantList()
  421. {
  422. $where = [
  423. 'enterpriseId' => $this->enterpriseId,
  424. 'deleteStatus' => StatusCode::$standard,
  425. 'enabledStatus' => StatusCode::$standard,
  426. 'auditStatus' => StatusCode::$auditStatus['auditPass']
  427. ];
  428. $dbResult = $this->objDMerchantApply->select($where,'*',"createTime desc");
  429. if ($dbResult === false){
  430. return ResultWrapper::fail($this->objDMerchantApply->error(),ErrorCode::$dberror);
  431. }
  432. return ResultWrapper::success($dbResult);
  433. }
  434. /**
  435. * Doc: (des="")
  436. * User: XMing
  437. * Date: 2020/12/30
  438. * Time: 10:24 上午
  439. * @param $params
  440. * @return ResultWrapper
  441. * @throws \Exception
  442. */
  443. public function getAll($params): ResultWrapper
  444. {
  445. $buildSql = self::buildSqlBySelectParams($params);
  446. $returnData = $this->objDMerchantApply->query($buildSql['sql']);
  447. if ($returnData === false){
  448. return ResultWrapper::fail($this->objDMerchantApply->error(),ErrorCode::$dberror);
  449. }
  450. $merchantIds = [];
  451. foreach ($returnData as $val){
  452. $merchantIds[] = $val['id'];
  453. }
  454. $objMSysAreaChina = new MSysAreaChina();
  455. $objMShop = new MShop($this->enterpriseId,$this->userCenterId);
  456. $shopMapResult = $objMShop->getShopIdByMerchantIds($merchantIds);
  457. if (!$shopMapResult->isSuccess()){
  458. return ResultWrapper::fail($shopMapResult->getData(),$shopMapResult->getErrorCode());
  459. }
  460. $shopMap = $shopMapResult->getData();
  461. $objMGoods = new MGoods($this->enterpriseId,true,$this->userCenterId);
  462. foreach ($returnData as &$value) {
  463. $value['shopId'] = isset($shopMap[$value['id']]) ? $shopMap[$value['id']]['id'] : 0;
  464. $value['logo'] = isset($shopMap[$value['id']]) ? $shopMap[$value['id']]['logo'] : '';
  465. !empty($value['settle']) && $value['settle'] = json_decode($value['settle'], true);
  466. !empty($value['audit']) && $value['audit'] = json_decode($value['audit'], true);
  467. !empty($value['setting']) && $value['setting'] = json_decode($value['setting'], true);
  468. !empty($value['info']) && $value['info'] = json_decode($value['info'], true);
  469. if (!empty($value['provinceCode']) && !empty($value['cityCode']) && !empty($value['districtCode'])) {
  470. $areaName = $objMSysAreaChina->getNameByCode([$value['provinceCode'], $value['cityCode'], $value['districtCode']]);
  471. $value['area']['provinceName'] = isset($areaName[$value['provinceCode']]) ? $areaName[$value['provinceCode']] : '';
  472. $value['area']['cityName'] = isset($areaName[$value['cityCode']]) ? $areaName[$value['cityCode']] : '';
  473. $value['area']['districtName'] = isset($areaName[$value['districtCode']]) ? $areaName[$value['districtCode']] : '';
  474. $value['area']['address'] = $value['address'];
  475. }
  476. $inSaleNumResult = $objMGoods->getInSaleNum($value['shopId']);
  477. if (!$inSaleNumResult->isSuccess()){
  478. return ResultWrapper::fail($inSaleNumResult->getData(),$inSaleNumResult->getErrorCode());
  479. }
  480. $goodsResult = $objMGoods->search([
  481. 'shopId' => $value['shopId'],
  482. 'offset' => 0,
  483. 'limit' => 3,
  484. 'areaCode' => $params['areaCode']
  485. ]);
  486. if (!$goodsResult->isSuccess()){
  487. return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode());
  488. }
  489. $goodsLists = $goodsResult->getData();
  490. $value['goodsLists'] = $goodsLists['data'];
  491. $value['inSaleGoodsNum'] = (int)$inSaleNumResult->getData();
  492. $value['distance'] = 1000;//单位m
  493. }
  494. $count = $this->objDMerchantApply->query($buildSql['countSql']);
  495. if ($count === false) {
  496. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  497. }
  498. $return = [
  499. 'data' => $returnData,
  500. 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
  501. ];
  502. return ResultWrapper::success($return);
  503. }
  504. /**
  505. * Doc: (des="")
  506. * User: XMing
  507. * Date: 2020/12/30
  508. * Time: 10:21 上午
  509. * @param array $params
  510. * @return string[]
  511. */
  512. public function buildSqlBySelectParams(array $params): array
  513. {
  514. $fields = ' * ';
  515. $countField = ' COUNT(id) AS total ';
  516. $tableName = $this->objDMerchantApply->get_Table();
  517. $whereSql = ' WHERE enterpriseId = ' . $this->enterpriseId;
  518. if (isset($params['keyword'])) {
  519. $whereSql .= ' and name like "%' . $params['keyword'] . '%" ';
  520. }
  521. if (isset($params['deleteStatus'])) {
  522. $whereSql .= ' and deleteStatus = ' . $params['deleteStatus'];
  523. }
  524. if (isset($params['auditStatus'])) {
  525. $whereSql .= ' and auditStatus = ' . $params['auditStatus'];
  526. }
  527. if (isset($params['enabledStatus'])) {
  528. $whereSql .= ' and enabledStatus = ' . $params['enabledStatus'];
  529. }
  530. if (isset($params['starExpireTime']) && isset($params['endExpireTime'])) {
  531. $whereSql .= ' and expireTime between ' . $params['starExpireTime'] . ' and ' . $params['endExpireTime'];
  532. }
  533. if (isset($params['starCreateTime']) && isset($params['endCreateTime'])) {
  534. $whereSql .= ' and createTime between ' . $params['starCreateTime'] . ' and ' . $params['endCreateTime'];
  535. }
  536. if (isset($params['starAuditTime']) && isset($params['endAuditTime'])) {
  537. $whereSql .= ' and auditTime between ' . $params['starAuditTime'] . ' and ' . $params['endAuditTime'];
  538. }
  539. if (isset($params['starDeleteTime']) && isset($params['endDeleteTime'])) {
  540. $whereSql .= ' and deleteTime between ' . $params['starDeleteTime'] . ' and ' . $params['endDeleteTime'];
  541. }
  542. if (isset($params['search']) && !empty($params['search'])){
  543. $search = '"%'.$params['search'].'%"';
  544. $whereSql .= ' AND name LIKE '.$search;
  545. }
  546. $orderSql = ' order by createTime desc';
  547. $limitSql = ' limit ' . $params['offset'] . ',' . $params['limit'];
  548. $sql = 'SELECT '.$fields.' FROM ' . $tableName . $whereSql . $orderSql . $limitSql;
  549. $countSql = 'SELECT '.$countField.' FROM '. $tableName . $whereSql;
  550. return [
  551. 'sql' => $sql,
  552. 'countSql' => $countSql
  553. ];
  554. }
  555. /**
  556. * 商户详情
  557. * @param $params
  558. * @return ResultWrapper
  559. */
  560. public function getInfoMerchant($params)
  561. {
  562. $params['deleteStatus'] = StatusCode::$standard;
  563. $params['enterpriseId'] = $this->enterpriseId;
  564. $dbResult = $this->objDMerchantApply->get($params);
  565. if ($dbResult === false) {
  566. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  567. }
  568. if (empty($dbResult)) {
  569. return ResultWrapper::success($dbResult);
  570. }
  571. !empty($dbResult['settle']) && $dbResult['settle'] = json_decode($dbResult['settle'], true);
  572. !empty($dbResult['audit']) && $dbResult['audit'] = json_decode($dbResult['audit'], true);
  573. !empty($dbResult['setting']) && $dbResult['setting'] = json_decode($dbResult['setting'], true);
  574. !empty($dbResult['info']) && $dbResult['info'] = json_decode($dbResult['info'], true);
  575. $objMSysAreaChina = new MSysAreaChina();
  576. if (!empty($dbResult['provinceCode']) && !empty($dbResult['cityCode']) && !empty($dbResult['districtCode'])) {
  577. $areaName = $objMSysAreaChina->getNameByCode([$dbResult['provinceCode'], $dbResult['cityCode'], $dbResult['districtCode']]);
  578. $dbResult['area']['provinceName'] = isset($areaName[$dbResult['provinceCode']]) ? $areaName[$dbResult['provinceCode']] : '';
  579. $dbResult['area']['cityName'] = isset($areaName[$dbResult['cityCode']]) ? $areaName[$dbResult['cityCode']] : '';
  580. $dbResult['area']['districtName'] = isset($areaName[$dbResult['districtCode']]) ? $areaName[$dbResult['districtCode']] : '';
  581. $dbResult['area']['address'] = $dbResult['address'];
  582. }
  583. $objDUserCenter = new DUserCenter();
  584. $user = $objDUserCenter->get(['id' => $dbResult['userCenterId']]);
  585. if ($user === false) {
  586. return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$dberror);
  587. }
  588. if (empty($user)) {
  589. return ResultWrapper::fail('userCenter数据查询失败', ErrorCode::$dberror);
  590. }
  591. $dbResult['mobile'] = $user['mobile'];
  592. $dbResult = self::formatMerchant($dbResult);
  593. return ResultWrapper::success($dbResult);
  594. }
  595. /**
  596. * 格式化商户
  597. * @param $params
  598. * @return array|mixed
  599. */
  600. public function formatMerchant($params)
  601. {
  602. if(isset($params['id'])){
  603. $data = [$params];
  604. }else{
  605. $data = $params;
  606. }
  607. $merchantIds = [];
  608. foreach ($data as $value) {
  609. $merchantIds[] = $value['id'];
  610. }
  611. $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
  612. $modelResult = $objMShop->getShopData(['enterpriseId' => $this->enterpriseId, 'merchantId' => $merchantIds]);
  613. $shopResult = [];
  614. if($modelResult->isSuccess()){
  615. $shopResult = $modelResult->getData();
  616. }
  617. $shopData = [];
  618. $warehouseIds = [];
  619. foreach($shopResult as $value){
  620. if(!empty($value['warehouseId'])) {
  621. if (strpos($value['warehouseId'], ',')) {
  622. $warehouseIds = array_merge($warehouseIds, explode(',', $value['warehouseId']));
  623. } else {
  624. $warehouseIds[] = $value['warehouseId'];
  625. }
  626. }
  627. $shopData[$value['merchantId']] = $value;
  628. }
  629. //查出仓库信息
  630. if(!empty($merchantIds)){
  631. $objMWarehouse = new MWarehouse($this->enterpriseId);
  632. $warehouseResult = $objMWarehouse->getNameByIds($warehouseIds);
  633. if ($warehouseResult->isSuccess()) {
  634. $warehouseData = $warehouseResult->getData();
  635. }
  636. }
  637. foreach($data as &$value){
  638. $value['warehouseData'] = [];
  639. $value['warehouseId'] = isset($shopData[$value['id']]) ? $shopData[$value['id']]['warehouseId'] : '';
  640. if (!empty($value['warehouseId'])) {
  641. if(strpos($value['warehouseId'],',')){
  642. $wid = explode(',', $value['warehouseId']);
  643. }else{
  644. $wid = [$value['warehouseId']];
  645. }
  646. foreach($wid as $v){
  647. if(isset($warehouseData[$v])){
  648. $value['warehouseData'][$v] = [
  649. 'warehouseId' => $v,
  650. 'warehouseCode' => createCode(StatusCode::$code['warehouse']['prefix'], $v, StatusCode::$code['warehouse']['length']),
  651. 'warehouseName' => $warehouseData[$v]['warehouseName']
  652. ];
  653. }
  654. }
  655. }
  656. }
  657. unset($value);
  658. if(isset($params['id'])){
  659. $return = array_shift($data);
  660. }else{
  661. $return = $data;
  662. }
  663. return $return;
  664. }
  665. /**
  666. * 获取商户数据
  667. * @param array $where
  668. * @return ResultWrapper
  669. */
  670. public function getMerchantData($where = []){
  671. $dbResult = $this->objDMerchantApply->get($where);
  672. if($dbResult === false){
  673. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  674. }
  675. return ResultWrapper::success($dbResult);
  676. }
  677. /**
  678. * 获取商户数据
  679. * @param array $where
  680. * @return ResultWrapper
  681. */
  682. public function selectMerchantData($where = []){
  683. $dbResult = $this->objDMerchantApply->select($where);
  684. if($dbResult === false){
  685. return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
  686. }
  687. return ResultWrapper::success($dbResult);
  688. }
  689. /**
  690. * 保存设置
  691. * @param $data
  692. * @param $where
  693. * @return ResultWrapper
  694. */
  695. public function saveSetting($data, $where)
  696. {
  697. $where['enterpriseId'] = $this->enterpriseId;
  698. $setting['setting'] = json_encode($data);
  699. $setting['updateTime'] = time();
  700. $dbResult = $this->objDMerchantSetting->get($where);
  701. if ($dbResult === false) {
  702. return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
  703. }
  704. $settingResult = $dbResult;
  705. unset($dbResult);
  706. if ($where['type'] == StatusCode::$standard) {
  707. }
  708. if (empty($settingResult)) {
  709. //新增
  710. $setting['createTime'] = time();
  711. $setting['enterpriseId'] = $this->enterpriseId;
  712. $setting['type'] = $where['type'];
  713. $dbResult = $this->objDMerchantSetting->insert($setting);
  714. } else {
  715. //修改
  716. $dbResult = $this->objDMerchantSetting->update($setting, $where);
  717. }
  718. if ($dbResult === false) {
  719. return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
  720. }
  721. return ResultWrapper::success($dbResult);
  722. }
  723. /**
  724. * 获取设置
  725. * @param array $where
  726. * @return ResultWrapper
  727. */
  728. public function getSetting($where = [])
  729. {
  730. $where['enterpriseId'] = $this->enterpriseId;
  731. $dbResult = $this->objDMerchantSetting->get($where);
  732. if ($dbResult === false) {
  733. return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
  734. }
  735. if (empty($dbResult)) {
  736. return ResultWrapper::success($dbResult);
  737. }
  738. $dbResult = json_decode($dbResult['setting'], true);
  739. return ResultWrapper::success($dbResult);
  740. }
  741. /**
  742. * Doc: (des="获取配置")
  743. * User: XMing
  744. * Date: 2020/12/8
  745. * Time: 5:41 下午
  746. * @param $fields
  747. * @return ResultWrapper
  748. */
  749. public function getSettingField($fields): ResultWrapper
  750. {
  751. $where['enterpriseId'] = $this->enterpriseId;
  752. $dbResult = $this->objDMerchantSetting->get($where);
  753. if ($dbResult === false) {
  754. return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
  755. }
  756. if (empty($dbResult)) {
  757. return ResultWrapper::success([]);
  758. }
  759. $dbResult = json_decode($dbResult['setting'], true);
  760. if (empty($fields)){
  761. return ResultWrapper::success($dbResult);
  762. }
  763. if (is_string($fields)){
  764. return ResultWrapper::success([$fields=>isset($dbResult[$fields]) ? $dbResult[$fields] : '']);
  765. }
  766. if (is_array($fields)){
  767. $map = [];
  768. foreach ($fields as $field){
  769. $map[$field] = isset($dbResult[$field]) ? $dbResult[$field] : '';
  770. }
  771. return ResultWrapper::success($map);
  772. }
  773. }
  774. /**
  775. * Doc: (des="通过商铺id获取商户的信息")
  776. * User: XMing
  777. * Date: 2020/12/7
  778. * Time: 5:25 下午
  779. * @param int $shopId 商铺id
  780. * @return ResultWrapper
  781. */
  782. public function getMerchByShopId(int $shopId): ResultWrapper
  783. {
  784. $fields = ' s.*,
  785. a.name as merchantName,a.balance,a.waitAuditWithdraw,a.auditWithdraw,
  786. a.withdraw,a.totalMoney,a.orderNum,a.auditOrderNum,a.finishOrderNum,a.notOrderNum,a.passOrderNum ';
  787. $sql = 'SELECT ' . $fields . ' FROM qianniao_shop_1 AS s
  788. LEFT JOIN qianniao_merchant_apply as a
  789. ON s.merchantId = a.id
  790. WHERE a.deleteStatus = ' . StatusCode::$standard . ' AND
  791. s.deleteStatus = ' . StatusCode::$standard . '
  792. AND s.id = ' . $shopId;
  793. $shop = $this->objDShop->query($sql);
  794. if ($shop === false) {
  795. return ResultWrapper::fail($this->objDShop->error, ErrorCode::$dberror);
  796. }
  797. if (empty($shop)) {
  798. return ResultWrapper::fail('未获取到指定的商铺信息', ErrorCode::$paramError);
  799. }
  800. $shop = array_shift($shop);
  801. $shop['openTime'] = !empty($shop['openTime']) ? json_decode($shop['openTime'],true) : (object)[];
  802. return ResultWrapper::success($shop);
  803. }
  804. /**
  805. * Doc: (des="更新商户余额")
  806. * User: XMing
  807. * Date: 2020/12/7
  808. * Time: 6:58 下午
  809. * @param int $id
  810. * @param array $update
  811. * @return ResultWrapper
  812. */
  813. public function update(int $id, array $update): ResultWrapper
  814. {
  815. $dbResult = $this->objDMerchantApply->update($update, $id);
  816. if ($dbResult === false) {
  817. return ResultWrapper::fail($this->objDMerchantApply->error, ErrorCode::$dberror);
  818. }
  819. return ResultWrapper::success(true);
  820. }
  821. /**
  822. * Doc: (des="获取商户结算数据")
  823. * User: XMing
  824. * Date: 2020/12/8
  825. * Time: 11:48 上午
  826. * @param $id
  827. * @return ResultWrapper
  828. * @throws \Exception
  829. */
  830. public function getWithdraw($id): ResultWrapper
  831. {
  832. if (empty($id)) {
  833. return ResultWrapper::fail('id参数错误', ErrorCode::$paramError);
  834. }
  835. $merchResult = self::getMerchByShopId($id);
  836. if (!$merchResult->isSuccess()) {
  837. return ResultWrapper::fail($merchResult->getData(), $merchResult->getErrorCode());
  838. }
  839. $merch = $merchResult->getData();
  840. $merchId = $merch['merchantId'];
  841. $result = $this->objDMerchantApply->get($merchId);
  842. if ($result === false) {
  843. return ResultWrapper::fail($this->objDMerchantApply->error, ErrorCode::$dberror);
  844. }
  845. $objMMerchantWithdraw = new MMerchantWithdraw($this->enterpriseId,$this->userCenterId);
  846. $takePriceResult = self::getSettingField('take_price');
  847. if (!$takePriceResult->isSuccess()){
  848. return ResultWrapper::fail($takePriceResult->getData(),$takePriceResult->getErrorCode());
  849. }
  850. $takePrice = $takePriceResult->getData()['take_price'];
  851. $return = [
  852. 'merchId' => $result['id'],
  853. 'name' => $result['name'],
  854. 'balance' => [
  855. 'balance' => [
  856. 'balance' => $result['balance'],
  857. 'num' => $result['orderNum'],
  858. ],
  859. 'total' => [
  860. 'total' => $result['balance'],
  861. 'num' => 0,
  862. ],
  863. 'ret' => [
  864. 'total' => 0,
  865. 'num' => 0,
  866. ],
  867. ],
  868. 'audit' => [
  869. 'waitAuditWithdraw' => [
  870. 'waitAuditWithdraw' => $result['waitAuditWithdraw'],
  871. 'num' => $result['auditOrderNum'],
  872. ],//提现等待审核
  873. 'estimate' => [
  874. 'estimate' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditing'],$id,'nowMoney'),//预计单款金额
  875. 'num' => $result['auditOrderNum'],
  876. ],//预计打款
  877. 'fee' => [
  878. 'fee' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditing'],$id,'fee'),
  879. 'rate' => $takePrice,
  880. ],//预计抽成
  881. ],
  882. 'finish' => [
  883. 'withdraw' => [
  884. 'withdraw' => $result['withdraw'],
  885. 'num' => $result['finishOrderNum']
  886. ],//总已结算金额
  887. 'not' => [
  888. 'not' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditNotPass'],$id,'money'),
  889. 'num' => $result['notOrderNum'],
  890. ],//审核未通过金额
  891. 'pass' => [
  892. 'pass' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditIng'],$id,'money'),//审核通过金额
  893. 'num' => $result['passOrderNum']
  894. ],//审核已通过金额
  895. 'finish' => [
  896. 'finish' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditPass'],$id,'nowMoney'),//已打款金额
  897. 'fee' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditPass'],$id,'fee'),//抽成
  898. ],//已打款金额
  899. ],
  900. ];
  901. return ResultWrapper::success($return);
  902. }
  903. /**
  904. * Doc: (des="")
  905. * User: XMing
  906. * Date: 2020/12/30
  907. * Time: 5:07 下午
  908. * @return ResultWrapper
  909. */
  910. public function statistics(): ResultWrapper
  911. {
  912. $ret = [
  913. 'auditData' => [
  914. 'apply' => [
  915. 'wait' => 1,
  916. 'auditing' => 0
  917. ],
  918. 'withdrawal' => [
  919. 'wait' => 1,
  920. 'auditing' => 0
  921. ],
  922. 'goods' => [
  923. 'wait' => 1,
  924. 'auditing' => 0
  925. ],
  926. ],
  927. 'merchant' => [
  928. 'merch' => [
  929. 'subNum' => 2,
  930. 'enable' => 1,
  931. 'disEnable' => 1,
  932. 'expire' => 1,
  933. ],
  934. 'goods' => [
  935. 'num' => 1,
  936. 'inSaleNum' => 1,
  937. 'outSaleNUm' => 0,
  938. ],
  939. ],
  940. 'payData' => [
  941. 'subPayMoney' => 10000,
  942. 'settle' => 9000,
  943. 'inSettle' => 4500,
  944. 'finishSettle' => 4500
  945. ],
  946. 'orderData' => [
  947. 'retOrder' => 2,
  948. 'inRetOrder' => 1,
  949. 'finishRetOrder' => 1,
  950. 'subOrder' => 3,
  951. 'waitPay' => 1,
  952. 'waitRec' => 2
  953. ],
  954. ];
  955. return ResultWrapper::success($ret);
  956. }
  957. /**
  958. * Doc: (des="")
  959. * User: XMing
  960. * Date: 2021/1/29
  961. * Time: 11:51 上午
  962. * @param int $shopId
  963. * @return ResultWrapper
  964. * @throws \Exception
  965. */
  966. public function get(int $shopId): ResultWrapper
  967. {
  968. $shopResult = self::getMerchByShopId($shopId);
  969. if (!$shopResult->isSuccess()){
  970. return ResultWrapper::fail($shopResult->getData(),$shopResult->getErrorCode());
  971. }
  972. $shop = $shopResult->getData();
  973. $objMGoods = new MGoods($this->enterpriseId,false,$this->userCenterId);
  974. $inSaleNumResult = $objMGoods->getInSaleNum($shop['id']);
  975. if (!$inSaleNumResult->isSuccess()){
  976. return ResultWrapper::fail($inSaleNumResult->getData(),$inSaleNumResult->getErrorCode());
  977. }
  978. $shop['inSaleGoodsNum'] = (int)$inSaleNumResult->getData();
  979. return ResultWrapper::success($shop);
  980. }
  981. /**
  982. * 查询商户数据
  983. * @param $merchantIds
  984. * @return array
  985. */
  986. public function getMerchant($merchantIds)
  987. {
  988. if(!is_array($merchantIds)){
  989. $merchantIdArray = [$merchantIds];
  990. }else{
  991. $merchantIdArray = $merchantIds;
  992. }
  993. $return = [];
  994. $empty = [];
  995. $objMerchantCache = new MerchantCache();
  996. foreach($merchantIdArray as $merchantId){
  997. $cache = $objMerchantCache->getMerchant($merchantId);
  998. if($cache){
  999. $return[$merchantId] = $cache;
  1000. }else{
  1001. $empty[] = $merchantId;
  1002. }
  1003. }
  1004. if(!empty($empty)){
  1005. $dbResult = $this->objDMerchantApply->select(['id' => $empty]);
  1006. if($dbResult !== false && !empty($dbResult)){
  1007. foreach($dbResult as $value){
  1008. $addCache = [
  1009. 'userCenterId' => $value['userCenterId'],
  1010. 'name' => $value['name'],
  1011. 'contactName' => $value['contactName'],
  1012. 'contactMobile' => $value['contactMobile'],
  1013. ];
  1014. $objMerchantCache->addMerchant($value['id'], $addCache);
  1015. $return[$value['id']] = $addCache;
  1016. }
  1017. }
  1018. }
  1019. //$objMerchantCache->delMerchant();
  1020. if(!is_array($merchantIds)){
  1021. $return = array_shift($return);
  1022. }
  1023. return $return;
  1024. }
  1025. }