123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113 |
- <?php
- /**
- * 商户Model
- * Created by PhpStorm.
- * User: haoren
- * Date: 2020/09/25
- * Time: 09:40
- */
- namespace JinDouYun\Model\Merchant;
- use JinDouYun\Cache\MerchantCache;
- use JinDouYun\Dao\Enterprise\DUserBindEnterprise;
- use JinDouYun\Dao\Shop\DShopBindStaff;
- use JinDouYun\Dao\UserCenter\DUserCenter;
- use JinDouYun\Model\Goods\MGoods;
- use JinDouYun\Model\Shop\MShop;
- use JinDouYun\Model\Stock\MWarehouse;
- use JinDouYun\Model\SysAreaChina\MSysAreaChina;
- use JinDouYun\Model\UserCenter\MUserCenterRegister;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Merchant\DMerchantApply;
- use JinDouYun\Dao\Merchant\DMerchantSetting;
- use JinDouYun\Dao\Shop\DShop;
- use JinDouYun\Model\MBaseModel;
- class MMerchant extends MBaseModel
- {
- private $objDMerchantApply;
- private $objDMerchantSetting;
- private $objDShop;
- private $userCenterId;
- private $enterpriseId;
- private $cutTable = 250000;
- public function __construct($enterpriseId, $userCenterId = false)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->objDMerchantApply = new DMerchantApply();
- $this->objDMerchantSetting = new DMerchantSetting();
- $this->objDShop = new DShop();
- $this->objDShop->setTable('qianniao_shop_' . ceil($this->enterpriseId / $this->cutTable));
- }
- /**
- * 入驻添加
- * @param $params
- * @return ResultWrapper
- */
- public function addApply($params)
- {
- $dbResult = $this->objDMerchantApply->get(['userCenterId' => $params['userCenterId'], 'deleteStatus' => StatusCode::$standard]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- if (!empty($dbResult)) {
- return ResultWrapper::fail('您已经申请过了', ErrorCode::$paramError);
- }
- //商户申请资料
- $insert = $params;
- if (isset($params['info'])) {
- $insert['info'] = json_encode($params['info']);
- }
- $insert['enterpriseId'] = $this->enterpriseId;
- $insert['auditStatus'] = StatusCode::$auditStatus['auditing'];
- $insert['enabledStatus'] = StatusCode::$standard;
- $insert['deleteStatus'] = StatusCode::$standard;
- $insert['createTime'] = time();
- $insert['updateTime'] = time();
- $dbResult = $this->objDMerchantApply->insert($insert);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 入驻审核
- * @param $update
- * @param $where
- * @return ResultWrapper
- * @throws \Exception
- */
- public function auditApply($update, $where)
- {
- //查询入驻
- $dbResult = $this->objDMerchantApply->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- $apply = $dbResult;
- unset($dbResult);
- if (empty($apply)) {
- return ResultWrapper::fail('入驻信息为空', ErrorCode::$paramError);
- }
- //判断审核
- if (isset($update['auditStatus']) && $update['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
- //审核通过
- //修改入驻状态
- $update['auditStatus'] = StatusCode::$auditStatus['auditPass'];
- $update['auditTime'] = time();
- //创建关联数据
- $params = array_merge($apply, $update);
- $modelResult = self::createMerchantBindData($params);
- if (!$modelResult->isSuccess()) {
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $update['extend'] = $modelResult->getData();
- }
- $dbResult = $this->objDMerchantApply->update($update, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 创建商户关联创建数据
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function createMerchantBindData($params)
- {
- //1.查询user表
- $objDUserCenter = new DUserCenter();
- $dbResult = $objDUserCenter->get(['id' => $params['userCenterId']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$paramError);
- }
- $user = $dbResult;
- unset($dbResult);
- if (empty($user)) {
- return ResultWrapper::fail('userCenter数据为空', ErrorCode::$paramError);
- }
- //1.1判断user数据 修改userCenter表客户来源状态 改为后台客户 增加默认密码
- $password = 88888888;
- $return = '当前登录手机号已经注册后台, 已成功绑定商户';
- if ($user['isCustomer'] == StatusCode::$customerType['customer']) {
- $userUpdate = [
- 'password' => password_hash($password, PASSWORD_DEFAULT),
- 'isCustomer' => StatusCode::$customerType['user'],
- 'updateTime' => time()
- ];
- $dbResult = $objDUserCenter->update($userUpdate, ['id' => $params['userCenterId']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$paramError);
- }
- $return = '当前登录手机号未注册, 已自动注册, 商户绑定成功, 默认密码:' . $password;
- }
- //3.增加企业绑定user表数据 把该userCenterId绑定为商户
- $objDUserBindEnterprise = new DUserBindEnterprise();
- $enterpriseBindData = [
- 'userCenterId' => $params['userCenterId'],
- 'enterpriseId' => $this->enterpriseId,
- 'merchantId' => $params['id'],
- 'shopId' => 0,
- 'roleType' => StatusCode::$roleType['merchant'],
- 'updateTime' => time(),
- 'createTime' => time(),
- ];
- $dbResult = $objDUserBindEnterprise->get(['enterpriseId' => $enterpriseBindData['enterpriseId'], 'userCenterId' => $enterpriseBindData['userCenterId'], 'roleType' => $enterpriseBindData['roleType']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDUserBindEnterprise->error(), ErrorCode::$dberror);
- }
- $bind = $dbResult;
- unset($dbResult);
- if (empty($bind)) {
- $dbResult = $objDUserBindEnterprise->insert($enterpriseBindData);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDUserBindEnterprise->error(), ErrorCode::$dberror);
- }
- }
- return ResultWrapper::success($return);
- }
- /**
- * 新增商户
- * @param $params
- * @return ResultWrapper
- */
- public function addMerchant($params)
- {
- $insertData = [
- 'enterpriseId' => $this->enterpriseId,
- 'name' => $params['name'],
- 'contactName' => $params['contactName'],
- 'contactMobile' => $params['contactMobile'],
- ];
- isset($params['desc']) && $insertData['desc'] = !empty($params['desc']) ? $params['desc'] : '';
- isset($params['category']) && $insertData['category'] = !empty($params['category']) ? $params['category'] : '';
- isset($params['provinceCode']) && $insertData['provinceCode'] = !empty($params['provinceCode']) ? $params['provinceCode'] : '';
- isset($params['cityCode']) && $insertData['cityCode'] = !empty($params['cityCode']) ? $params['cityCode'] : '';
- isset($params['districtCode']) && $insertData['districtCode'] = !empty($params['districtCode']) ? $params['districtCode'] : '';
- isset($params['address']) && $insertData['address'] = !empty($params['address']) ? $params['address'] : '';
- isset($params['note']) && $insertData['note'] = !empty($params['note']) ? $params['note'] : '';
- isset($params['settleType']) && $insertData['settleType'] = !empty($params['settleType']) ? $params['settleType'] : '';
- isset($params['settle']) && $insertData['settle'] = !empty($params['settle']) ? json_encode($params['settle']) : '';
- isset($params['auditType']) && $insertData['auditType'] = !empty($params['auditType']) ? $params['auditType'] : '';
- isset($params['audit']) && $insertData['audit'] = !empty($params['audit']) ? json_encode($params['audit']) : '';
- isset($params['settingType']) && $insertData['settingType'] = !empty($params['settingType']) ? $params['settingType'] : '';
- isset($params['setting']) && $insertData['setting'] = !empty($params['setting']) ? json_encode($params['setting']) : '';
- $insertData['createTime'] = time();
- $insertData['deleteStatus'] = StatusCode::$standard;
- $insertData['enabledStatus'] = StatusCode::$standard;
- $insertData['auditStatus'] = StatusCode::$auditStatus['auditPass'];
- $insertData['auditTime'] = time();
- $password = '88888888';
- if (!isset($params['userCenterMobile'])) {
- return ResultWrapper::fail('userCenterMobile参数错误', ErrorCode::$paramError);
- }
- $objMUserCenterRegister = new MUserCenterRegister();
- $modelResult = $objMUserCenterRegister->mobileBindStatus($params['userCenterMobile'], $this->enterpriseId, StatusCode::$roleType['merchant']);
- if (!$modelResult->isSuccess()) {
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $userCenterId = $modelResult->getData();
- unset($modelResult);
- if (!empty($userCenterId)) {
- return ResultWrapper::fail('登录手机号已经是商户', ErrorCode::$paramError);
- }
- //1.创建后台账户
- $objDUserCenter = new DUserCenter();
- $dbResult = $objDUserCenter->get(['mobile' => $params['userCenterMobile']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$dberror);
- }
- $user = $dbResult;
- unset($dbResult);
- if (empty($user)) {
- $userInsert = [
- 'mobile' => $params['userCenterMobile'],
- 'password' => password_hash($password, PASSWORD_DEFAULT),
- 'deleteStatus' => StatusCode::$standard,
- 'source' => StatusCode::$source['manage'],
- 'isCustomer' => StatusCode::$customerType['user'],
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $objDUserCenter->insert($userInsert);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$dberror);
- }
- $insertData['userCenterId'] = $dbResult;
- $return = '该登录手机号未注册, 已自动注册, 密码:' . $password;
- } else {
- $insertData['userCenterId'] = $user['id'];
- $return = '该登录手机号已经注册, 已成功绑定商户';
- }
- //2.创建商户
- $dbResult = $this->objDMerchantApply->insert($insertData);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- $insertData['id'] = $dbResult;
- //3.创建角色绑定数据
- $modelResult = self::createMerchantBindData($insertData);
- if (!$modelResult->isSuccess()) {
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- return ResultWrapper::success($return);
- }
- /**
- * 修改商户
- * @param $update
- * @param $where
- * @return ResultWrapper
- */
- public function updateMerchant($update, $where = [])
- {
- $dbResult = $this->objDMerchantApply->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$paramError);
- }
- $merchant = $dbResult;
- if (empty($merchant)) {
- return ResultWrapper::fail('修改数据不存在', ErrorCode::$paramError);
- }
- $where['enterpriseId'] = $this->enterpriseId;
- unset($update['enterpriseId']);
- $update['updateTime'] = time();
- if (isset($update['deleteStatus']) && $update['deleteStatus'] == StatusCode::$delete) {
- $update['deleteTime'] = time();
- }
- if (isset($update['auditStatus']) && $update['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
- $update['auditTime'] = time();
- }
- if (isset($update['enabledStatus'])) {
- if($merchant['enabledStatus'] == StatusCode::$delete){
- $update['enabledStatus'] = StatusCode::$standard;
- }else{
- $update['enabledStatus'] = StatusCode::$delete;
- }
- }
- $updateShopData = [];
- if(isset($update['name']) && !empty($update['name']) && $merchant['name'] != $update['name']){
- $updateShopData['name'] = $update['name'];
- }
- //获取仓库
- if(isset($update['warehouseId']) && !empty($update['warehouseId'])){
- if(is_array($update['warehouseId'])){
- $warehouseId = $update['warehouseId'];
- $update['warehouseId'] = implode(',',$update['warehouseId']);
- }else{
- $warehouseId = [$update['warehouseId']];
- }
- $objMWarehouse = new MWarehouse($this->enterpriseId);
- $modelResult = $objMWarehouse->selectWarehouse(['id' => $warehouseId, 'deleteStatus' => StatusCode::$standard]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $warehouseResult = $modelResult->getData();
- unset($modelResult);
- if(empty($warehouseResult)){
- return ResultWrapper::fail('仓库查询失败', ErrorCode::$paramError);
- }
- $selectWarehouseId = array_column($warehouseResult, 'id');
- foreach($warehouseId as $value){
- if(!in_array($value,$selectWarehouseId)){
- return ResultWrapper::fail($value.'仓库查询失败', ErrorCode::$paramError);
- }
- }
- $updateShopData['warehouseId'] = $update['warehouseId'];
- }
- if(!empty($updateShopData)){
- $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
- $modelResult = $objMShop->updateShopData($updateShopData, ['enterpriseId' => $this->enterpriseId, 'merchantId' => $where['id'], 'deleteStatus' => 5]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- }
- unset($update['warehouseId']);
- unset($update['userCenterMobile']);
- $dbResult = $this->objDMerchantApply->update($update, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 修改申请
- * @param $update
- * @param $where
- * @return ResultWrapper
- */
- public function updateApply($update, $where = [])
- {
- $dbResult = $this->objDMerchantApply->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$paramError);
- }
- if (empty($dbResult)) {
- return ResultWrapper::fail('修改数据不存在', ErrorCode::$paramError);
- }
- if ($dbResult['auditStatus'] == StatusCode::$auditStatus['auditPass']) {
- return ResultWrapper::fail('审核通过不可以修改', ErrorCode::$paramError);
- }
- $where['enterpriseId'] = $this->enterpriseId;
- unset($update['enterpriseId']);
- $update['updateTime'] = time();
- $update['auditStatus'] = StatusCode::$auditStatus['auditing'];
- if (isset($update['info']) && !empty($update['info'])) {
- $update = json_encode($update['info']);
- } else {
- unset($update['info']);
- }
- $update['auditFailReason'] = '';
- $dbResult = $this->objDMerchantApply->update($update, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 商户列表
- * @param $params
- * @return ResultWrapper
- */
- public function getAllMerchant($params = [])
- {
- $buildSql = self::buildSqlBySelectParams($params);
- $returnData = $this->objDMerchantApply->query($buildSql['sql']);
- if ($returnData === false){
- return ResultWrapper::fail($this->objDMerchantApply->error(),ErrorCode::$dberror);
- }
- $merchantIds = [];
- foreach ($returnData as &$value) {
- !empty($value['settle']) && $value['settle'] = json_decode($value['settle'], true);
- !empty($value['audit']) && $value['audit'] = json_decode($value['audit'], true);
- !empty($value['setting']) && $value['setting'] = json_decode($value['setting'], true);
- !empty($value['info']) && $value['info'] = json_decode($value['info'], true);
- $value['materielNum'] = 0;
- $value['orderNum'] = 0;
- $value['subPayMoney'] = 0;
- $merchantIds[] = $value['id'];
- }
- unset($value);
- $dbResult = $this->objDMerchantApply->query($buildSql['countSql']);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- $count = $dbResult;
- unset($dbResult);
- $objMShop = new MShop($this->enterpriseId,$this->userCenterId);
- $shopMapResult = $objMShop->getShopIdByMerchantIds($merchantIds);
- if (!$shopMapResult->isSuccess()){
- return ResultWrapper::fail($shopMapResult->getData(),$shopMapResult->getErrorCode());
- }
- $shopMap = $shopMapResult->getData();
- foreach ($returnData as &$value){
- $value['shopId'] = isset($shopMap[$value['id']]) ? $shopMap[$value['id']]['id'] : 0;
- }
- unset($value);
- $return = [
- 'data' => self::formatMerchant($returnData),
- 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
- ];
- return ResultWrapper::success($return);
- }
-
-
- /**
- * 商户列表不分页
- * @param $params
- * @return ResultWrapper
- */
- public function getAllMerchantList()
- {
- $where = [
- 'enterpriseId' => $this->enterpriseId,
- 'deleteStatus' => StatusCode::$standard,
- 'enabledStatus' => StatusCode::$standard,
- 'auditStatus' => StatusCode::$auditStatus['auditPass']
- ];
- $dbResult = $this->objDMerchantApply->select($where,'*',"createTime desc");
- if ($dbResult === false){
- return ResultWrapper::fail($this->objDMerchantApply->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
-
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/12/30
- * Time: 10:24 上午
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getAll($params): ResultWrapper
- {
- $buildSql = self::buildSqlBySelectParams($params);
- $returnData = $this->objDMerchantApply->query($buildSql['sql']);
- if ($returnData === false){
- return ResultWrapper::fail($this->objDMerchantApply->error(),ErrorCode::$dberror);
- }
- $merchantIds = [];
- foreach ($returnData as $val){
- $merchantIds[] = $val['id'];
- }
- $objMSysAreaChina = new MSysAreaChina();
- $objMShop = new MShop($this->enterpriseId,$this->userCenterId);
- $shopMapResult = $objMShop->getShopIdByMerchantIds($merchantIds);
- if (!$shopMapResult->isSuccess()){
- return ResultWrapper::fail($shopMapResult->getData(),$shopMapResult->getErrorCode());
- }
- $shopMap = $shopMapResult->getData();
- $objMGoods = new MGoods($this->enterpriseId,true,$this->userCenterId);
- foreach ($returnData as &$value) {
- $value['shopId'] = isset($shopMap[$value['id']]) ? $shopMap[$value['id']]['id'] : 0;
- $value['logo'] = isset($shopMap[$value['id']]) ? $shopMap[$value['id']]['logo'] : '';
- !empty($value['settle']) && $value['settle'] = json_decode($value['settle'], true);
- !empty($value['audit']) && $value['audit'] = json_decode($value['audit'], true);
- !empty($value['setting']) && $value['setting'] = json_decode($value['setting'], true);
- !empty($value['info']) && $value['info'] = json_decode($value['info'], true);
- if (!empty($value['provinceCode']) && !empty($value['cityCode']) && !empty($value['districtCode'])) {
- $areaName = $objMSysAreaChina->getNameByCode([$value['provinceCode'], $value['cityCode'], $value['districtCode']]);
- $value['area']['provinceName'] = isset($areaName[$value['provinceCode']]) ? $areaName[$value['provinceCode']] : '';
- $value['area']['cityName'] = isset($areaName[$value['cityCode']]) ? $areaName[$value['cityCode']] : '';
- $value['area']['districtName'] = isset($areaName[$value['districtCode']]) ? $areaName[$value['districtCode']] : '';
- $value['area']['address'] = $value['address'];
- }
- $inSaleNumResult = $objMGoods->getInSaleNum($value['shopId']);
- if (!$inSaleNumResult->isSuccess()){
- return ResultWrapper::fail($inSaleNumResult->getData(),$inSaleNumResult->getErrorCode());
- }
- $goodsResult = $objMGoods->search([
- 'shopId' => $value['shopId'],
- 'offset' => 0,
- 'limit' => 3,
- 'areaCode' => $params['areaCode']
- ]);
- if (!$goodsResult->isSuccess()){
- return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode());
- }
- $goodsLists = $goodsResult->getData();
- $value['goodsLists'] = $goodsLists['data'];
- $value['inSaleGoodsNum'] = (int)$inSaleNumResult->getData();
- $value['distance'] = 1000;//单位m
- }
- $count = $this->objDMerchantApply->query($buildSql['countSql']);
- if ($count === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- $return = [
- 'data' => $returnData,
- 'total' => isset($count[0]['total']) ? $count[0]['total'] : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/12/30
- * Time: 10:21 上午
- * @param array $params
- * @return string[]
- */
- public function buildSqlBySelectParams(array $params): array
- {
- $fields = ' * ';
- $countField = ' COUNT(id) AS total ';
- $tableName = $this->objDMerchantApply->get_Table();
- $whereSql = ' WHERE enterpriseId = ' . $this->enterpriseId;
- if (isset($params['keyword'])) {
- $whereSql .= ' and name like "%' . $params['keyword'] . '%" ';
- }
- if (isset($params['deleteStatus'])) {
- $whereSql .= ' and deleteStatus = ' . $params['deleteStatus'];
- }
- if (isset($params['auditStatus'])) {
- $whereSql .= ' and auditStatus = ' . $params['auditStatus'];
- }
- if (isset($params['enabledStatus'])) {
- $whereSql .= ' and enabledStatus = ' . $params['enabledStatus'];
- }
- if (isset($params['starExpireTime']) && isset($params['endExpireTime'])) {
- $whereSql .= ' and expireTime between ' . $params['starExpireTime'] . ' and ' . $params['endExpireTime'];
- }
- if (isset($params['starCreateTime']) && isset($params['endCreateTime'])) {
- $whereSql .= ' and createTime between ' . $params['starCreateTime'] . ' and ' . $params['endCreateTime'];
- }
- if (isset($params['starAuditTime']) && isset($params['endAuditTime'])) {
- $whereSql .= ' and auditTime between ' . $params['starAuditTime'] . ' and ' . $params['endAuditTime'];
- }
- if (isset($params['starDeleteTime']) && isset($params['endDeleteTime'])) {
- $whereSql .= ' and deleteTime between ' . $params['starDeleteTime'] . ' and ' . $params['endDeleteTime'];
- }
- if (isset($params['search']) && !empty($params['search'])){
- $search = '"%'.$params['search'].'%"';
- $whereSql .= ' AND name LIKE '.$search;
- }
- $orderSql = ' order by createTime desc';
- $limitSql = ' limit ' . $params['offset'] . ',' . $params['limit'];
- $sql = 'SELECT '.$fields.' FROM ' . $tableName . $whereSql . $orderSql . $limitSql;
- $countSql = 'SELECT '.$countField.' FROM '. $tableName . $whereSql;
- return [
- 'sql' => $sql,
- 'countSql' => $countSql
- ];
- }
- /**
- * 商户详情
- * @param $params
- * @return ResultWrapper
- */
- public function getInfoMerchant($params)
- {
- $params['deleteStatus'] = StatusCode::$standard;
- $params['enterpriseId'] = $this->enterpriseId;
- $dbResult = $this->objDMerchantApply->get($params);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- if (empty($dbResult)) {
- return ResultWrapper::success($dbResult);
- }
- !empty($dbResult['settle']) && $dbResult['settle'] = json_decode($dbResult['settle'], true);
- !empty($dbResult['audit']) && $dbResult['audit'] = json_decode($dbResult['audit'], true);
- !empty($dbResult['setting']) && $dbResult['setting'] = json_decode($dbResult['setting'], true);
- !empty($dbResult['info']) && $dbResult['info'] = json_decode($dbResult['info'], true);
- $objMSysAreaChina = new MSysAreaChina();
- if (!empty($dbResult['provinceCode']) && !empty($dbResult['cityCode']) && !empty($dbResult['districtCode'])) {
- $areaName = $objMSysAreaChina->getNameByCode([$dbResult['provinceCode'], $dbResult['cityCode'], $dbResult['districtCode']]);
- $dbResult['area']['provinceName'] = isset($areaName[$dbResult['provinceCode']]) ? $areaName[$dbResult['provinceCode']] : '';
- $dbResult['area']['cityName'] = isset($areaName[$dbResult['cityCode']]) ? $areaName[$dbResult['cityCode']] : '';
- $dbResult['area']['districtName'] = isset($areaName[$dbResult['districtCode']]) ? $areaName[$dbResult['districtCode']] : '';
- $dbResult['area']['address'] = $dbResult['address'];
- }
- $objDUserCenter = new DUserCenter();
- $user = $objDUserCenter->get(['id' => $dbResult['userCenterId']]);
- if ($user === false) {
- return ResultWrapper::fail($objDUserCenter->error(), ErrorCode::$dberror);
- }
- if (empty($user)) {
- return ResultWrapper::fail('userCenter数据查询失败', ErrorCode::$dberror);
- }
- $dbResult['mobile'] = $user['mobile'];
- $dbResult = self::formatMerchant($dbResult);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 格式化商户
- * @param $params
- * @return array|mixed
- */
- public function formatMerchant($params)
- {
- if(isset($params['id'])){
- $data = [$params];
- }else{
- $data = $params;
- }
- $merchantIds = [];
- foreach ($data as $value) {
- $merchantIds[] = $value['id'];
- }
- $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
- $modelResult = $objMShop->getShopData(['enterpriseId' => $this->enterpriseId, 'merchantId' => $merchantIds]);
- $shopResult = [];
- if($modelResult->isSuccess()){
- $shopResult = $modelResult->getData();
- }
- $shopData = [];
- $warehouseIds = [];
- foreach($shopResult as $value){
- if(!empty($value['warehouseId'])) {
- if (strpos($value['warehouseId'], ',')) {
- $warehouseIds = array_merge($warehouseIds, explode(',', $value['warehouseId']));
- } else {
- $warehouseIds[] = $value['warehouseId'];
- }
- }
- $shopData[$value['merchantId']] = $value;
- }
- //查出仓库信息
- if(!empty($merchantIds)){
- $objMWarehouse = new MWarehouse($this->enterpriseId);
- $warehouseResult = $objMWarehouse->getNameByIds($warehouseIds);
- if ($warehouseResult->isSuccess()) {
- $warehouseData = $warehouseResult->getData();
- }
- }
- foreach($data as &$value){
- $value['warehouseData'] = [];
- $value['warehouseId'] = isset($shopData[$value['id']]) ? $shopData[$value['id']]['warehouseId'] : '';
- if (!empty($value['warehouseId'])) {
- if(strpos($value['warehouseId'],',')){
- $wid = explode(',', $value['warehouseId']);
- }else{
- $wid = [$value['warehouseId']];
- }
- foreach($wid as $v){
- if(isset($warehouseData[$v])){
- $value['warehouseData'][$v] = [
- 'warehouseId' => $v,
- 'warehouseCode' => createCode(StatusCode::$code['warehouse']['prefix'], $v, StatusCode::$code['warehouse']['length']),
- 'warehouseName' => $warehouseData[$v]['warehouseName']
- ];
- }
- }
- }
- }
- unset($value);
- if(isset($params['id'])){
- $return = array_shift($data);
- }else{
- $return = $data;
- }
- return $return;
- }
- /**
- * 获取商户数据
- * @param array $where
- * @return ResultWrapper
- */
- public function getMerchantData($where = []){
- $dbResult = $this->objDMerchantApply->get($where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取商户数据
- * @param array $where
- * @return ResultWrapper
- */
- public function selectMerchantData($where = []){
- $dbResult = $this->objDMerchantApply->select($where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDMerchantApply->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 保存设置
- * @param $data
- * @param $where
- * @return ResultWrapper
- */
- public function saveSetting($data, $where)
- {
- $where['enterpriseId'] = $this->enterpriseId;
- $setting['setting'] = json_encode($data);
- $setting['updateTime'] = time();
- $dbResult = $this->objDMerchantSetting->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
- }
- $settingResult = $dbResult;
- unset($dbResult);
- if ($where['type'] == StatusCode::$standard) {
- }
- if (empty($settingResult)) {
- //新增
- $setting['createTime'] = time();
- $setting['enterpriseId'] = $this->enterpriseId;
- $setting['type'] = $where['type'];
- $dbResult = $this->objDMerchantSetting->insert($setting);
- } else {
- //修改
- $dbResult = $this->objDMerchantSetting->update($setting, $where);
- }
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取设置
- * @param array $where
- * @return ResultWrapper
- */
- public function getSetting($where = [])
- {
- $where['enterpriseId'] = $this->enterpriseId;
- $dbResult = $this->objDMerchantSetting->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
- }
- if (empty($dbResult)) {
- return ResultWrapper::success($dbResult);
- }
- $dbResult = json_decode($dbResult['setting'], true);
- return ResultWrapper::success($dbResult);
- }
- /**
- * Doc: (des="获取配置")
- * User: XMing
- * Date: 2020/12/8
- * Time: 5:41 下午
- * @param $fields
- * @return ResultWrapper
- */
- public function getSettingField($fields): ResultWrapper
- {
- $where['enterpriseId'] = $this->enterpriseId;
- $dbResult = $this->objDMerchantSetting->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantSetting->error(), ErrorCode::$dberror);
- }
- if (empty($dbResult)) {
- return ResultWrapper::success([]);
- }
- $dbResult = json_decode($dbResult['setting'], true);
- if (empty($fields)){
- return ResultWrapper::success($dbResult);
- }
- if (is_string($fields)){
- return ResultWrapper::success([$fields=>isset($dbResult[$fields]) ? $dbResult[$fields] : '']);
- }
- if (is_array($fields)){
- $map = [];
- foreach ($fields as $field){
- $map[$field] = isset($dbResult[$field]) ? $dbResult[$field] : '';
- }
- return ResultWrapper::success($map);
- }
- }
- /**
- * Doc: (des="通过商铺id获取商户的信息")
- * User: XMing
- * Date: 2020/12/7
- * Time: 5:25 下午
- * @param int $shopId 商铺id
- * @return ResultWrapper
- */
- public function getMerchByShopId(int $shopId): ResultWrapper
- {
- $fields = ' s.*,
- a.name as merchantName,a.balance,a.waitAuditWithdraw,a.auditWithdraw,
- a.withdraw,a.totalMoney,a.orderNum,a.auditOrderNum,a.finishOrderNum,a.notOrderNum,a.passOrderNum ';
- $sql = 'SELECT ' . $fields . ' FROM qianniao_shop_1 AS s
- LEFT JOIN qianniao_merchant_apply as a
- ON s.merchantId = a.id
- WHERE a.deleteStatus = ' . StatusCode::$standard . ' AND
- s.deleteStatus = ' . StatusCode::$standard . '
- AND s.id = ' . $shopId;
- $shop = $this->objDShop->query($sql);
- if ($shop === false) {
- return ResultWrapper::fail($this->objDShop->error, ErrorCode::$dberror);
- }
- if (empty($shop)) {
- return ResultWrapper::fail('未获取到指定的商铺信息', ErrorCode::$paramError);
- }
- $shop = array_shift($shop);
- $shop['openTime'] = !empty($shop['openTime']) ? json_decode($shop['openTime'],true) : (object)[];
- return ResultWrapper::success($shop);
- }
- /**
- * Doc: (des="更新商户余额")
- * User: XMing
- * Date: 2020/12/7
- * Time: 6:58 下午
- * @param int $id
- * @param array $update
- * @return ResultWrapper
- */
- public function update(int $id, array $update): ResultWrapper
- {
- $dbResult = $this->objDMerchantApply->update($update, $id);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error, ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="获取商户结算数据")
- * User: XMing
- * Date: 2020/12/8
- * Time: 11:48 上午
- * @param $id
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getWithdraw($id): ResultWrapper
- {
- if (empty($id)) {
- return ResultWrapper::fail('id参数错误', ErrorCode::$paramError);
- }
- $merchResult = self::getMerchByShopId($id);
- if (!$merchResult->isSuccess()) {
- return ResultWrapper::fail($merchResult->getData(), $merchResult->getErrorCode());
- }
- $merch = $merchResult->getData();
- $merchId = $merch['merchantId'];
- $result = $this->objDMerchantApply->get($merchId);
- if ($result === false) {
- return ResultWrapper::fail($this->objDMerchantApply->error, ErrorCode::$dberror);
- }
- $objMMerchantWithdraw = new MMerchantWithdraw($this->enterpriseId,$this->userCenterId);
- $takePriceResult = self::getSettingField('take_price');
- if (!$takePriceResult->isSuccess()){
- return ResultWrapper::fail($takePriceResult->getData(),$takePriceResult->getErrorCode());
- }
- $takePrice = $takePriceResult->getData()['take_price'];
- $return = [
- 'merchId' => $result['id'],
- 'name' => $result['name'],
- 'balance' => [
- 'balance' => [
- 'balance' => $result['balance'],
- 'num' => $result['orderNum'],
- ],
- 'total' => [
- 'total' => $result['balance'],
- 'num' => 0,
- ],
- 'ret' => [
- 'total' => 0,
- 'num' => 0,
- ],
- ],
- 'audit' => [
- 'waitAuditWithdraw' => [
- 'waitAuditWithdraw' => $result['waitAuditWithdraw'],
- 'num' => $result['auditOrderNum'],
- ],//提现等待审核
- 'estimate' => [
- 'estimate' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditing'],$id,'nowMoney'),//预计单款金额
- 'num' => $result['auditOrderNum'],
- ],//预计打款
- 'fee' => [
- 'fee' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditing'],$id,'fee'),
- 'rate' => $takePrice,
- ],//预计抽成
- ],
- 'finish' => [
- 'withdraw' => [
- 'withdraw' => $result['withdraw'],
- 'num' => $result['finishOrderNum']
- ],//总已结算金额
- 'not' => [
- 'not' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditNotPass'],$id,'money'),
- 'num' => $result['notOrderNum'],
- ],//审核未通过金额
- 'pass' => [
- 'pass' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditIng'],$id,'money'),//审核通过金额
- 'num' => $result['passOrderNum']
- ],//审核已通过金额
- 'finish' => [
- 'finish' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditPass'],$id,'nowMoney'),//已打款金额
- 'fee' => $objMMerchantWithdraw->statistics(StatusCode::$auditStatus['auditPass'],$id,'fee'),//抽成
- ],//已打款金额
- ],
- ];
- return ResultWrapper::success($return);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/12/30
- * Time: 5:07 下午
- * @return ResultWrapper
- */
- public function statistics(): ResultWrapper
- {
- $ret = [
- 'auditData' => [
- 'apply' => [
- 'wait' => 1,
- 'auditing' => 0
- ],
- 'withdrawal' => [
- 'wait' => 1,
- 'auditing' => 0
- ],
- 'goods' => [
- 'wait' => 1,
- 'auditing' => 0
- ],
- ],
- 'merchant' => [
- 'merch' => [
- 'subNum' => 2,
- 'enable' => 1,
- 'disEnable' => 1,
- 'expire' => 1,
- ],
- 'goods' => [
- 'num' => 1,
- 'inSaleNum' => 1,
- 'outSaleNUm' => 0,
- ],
- ],
- 'payData' => [
- 'subPayMoney' => 10000,
- 'settle' => 9000,
- 'inSettle' => 4500,
- 'finishSettle' => 4500
- ],
- 'orderData' => [
- 'retOrder' => 2,
- 'inRetOrder' => 1,
- 'finishRetOrder' => 1,
- 'subOrder' => 3,
- 'waitPay' => 1,
- 'waitRec' => 2
- ],
- ];
- return ResultWrapper::success($ret);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2021/1/29
- * Time: 11:51 上午
- * @param int $shopId
- * @return ResultWrapper
- * @throws \Exception
- */
- public function get(int $shopId): ResultWrapper
- {
- $shopResult = self::getMerchByShopId($shopId);
- if (!$shopResult->isSuccess()){
- return ResultWrapper::fail($shopResult->getData(),$shopResult->getErrorCode());
- }
- $shop = $shopResult->getData();
- $objMGoods = new MGoods($this->enterpriseId,false,$this->userCenterId);
- $inSaleNumResult = $objMGoods->getInSaleNum($shop['id']);
- if (!$inSaleNumResult->isSuccess()){
- return ResultWrapper::fail($inSaleNumResult->getData(),$inSaleNumResult->getErrorCode());
- }
- $shop['inSaleGoodsNum'] = (int)$inSaleNumResult->getData();
- return ResultWrapper::success($shop);
- }
- /**
- * 查询商户数据
- * @param $merchantIds
- * @return array
- */
- public function getMerchant($merchantIds)
- {
- if(!is_array($merchantIds)){
- $merchantIdArray = [$merchantIds];
- }else{
- $merchantIdArray = $merchantIds;
- }
- $return = [];
- $empty = [];
- $objMerchantCache = new MerchantCache();
- foreach($merchantIdArray as $merchantId){
- $cache = $objMerchantCache->getMerchant($merchantId);
- if($cache){
- $return[$merchantId] = $cache;
- }else{
- $empty[] = $merchantId;
- }
- }
- if(!empty($empty)){
- $dbResult = $this->objDMerchantApply->select(['id' => $empty]);
- if($dbResult !== false && !empty($dbResult)){
- foreach($dbResult as $value){
- $addCache = [
- 'userCenterId' => $value['userCenterId'],
- 'name' => $value['name'],
- 'contactName' => $value['contactName'],
- 'contactMobile' => $value['contactMobile'],
- ];
- $objMerchantCache->addMerchant($value['id'], $addCache);
- $return[$value['id']] = $addCache;
- }
- }
- }
- //$objMerchantCache->delMerchant();
- if(!is_array($merchantIds)){
- $return = array_shift($return);
- }
- return $return;
- }
- }
|