123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- /**
- * 商铺合作管理Model
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/11/01
- * Time: 10:00
- */
- namespace JinDouYun\Model\ShopPartner;
- use Exception;
- use JinDouYun\Model\MBaseModel;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\ShopPartner\DShopPartner;
- use JinDouYun\Model\SysAreaChina\MSysAreaChina;
- use JinDouYun\Model\Shop\MShop;
- use JinDouYun\Model\Department\MStaff;
- use JinDouYun\Model\Enterprise\MEnterprise;
- class MShopPartner extends MBaseModel
- {
- private $objDShopPartner;
- private $cutTable = 250000;
- private $enterpriseId;
- private $userCenterId;
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- parent::__construct($enterpriseId, $userCenterId);
- $this->objDShopPartner = new DShopPartner('default');
- $tableName = $this->objDShopPartner->getTableName($this->objDShopPartner->get_Table(), $this->enterpriseId, $this->cutTable);
- $this->objDShopPartner->setTable($tableName);
- }
- /**
- * 商铺合作添加
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function addShopPartner($params)
- {
- #777 查询商铺id 是否存在
- $params['createTime'] = $params['updateTime'];
- //获取负责人信息
- $modelResult = self::getManagerList($params['userCenterId']);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $userData = $modelResult->getData();
- unset($modelResult);
- if(empty($userData)){
- return ResultWrapper::fail('负责人id参数错误', ErrorCode::$dberror);
- }
- foreach($userData as $value){
- if($value['userCenterId'] == $params['userCenterId']){
- $params['userName'] = $value['name'];
- }
- }
- $dbResult = $this->objDShopPartner->insert($params);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }else{
- //更改商铺的合伙人
- $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
- if(!empty($params['shopId'])) {
- $shopIds = explode(',', $params['shopId']);
- $modelResult = $objMShop->updateShopPartnerId(['id'=>$shopIds, 'shopPartnerId'=>$dbResult]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- }
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 商铺合作添加时 获取企业下的管理员信息
- * @param bool $userCenterId
- * @return ResultWrapper
- * @throws Exception
- */
- public function getManagerList($userCenterId = false)
- {
- $objMEnterprise = new MEnterprise();
- $modelResult = $objMEnterprise->getAdminsByEnterpriseId($this->enterpriseId, $userCenterId);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- return ResultWrapper::success(array_values($modelResult->getData()));
- }
- /**
- * 商铺合作删除
- * @param $where
- * @return ResultWrapper
- */
- public function deleteShopPartner($where)
- {
- $params = [
- 'deleteStatus' => StatusCode::$delete,
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDShopPartner->update($params, $where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 商铺合作启用/禁用
- * @param $where
- * @return ResultWrapper
- * @throws Exception
- */
- public function enableShopPartner($where)
- {
- $params['updateTime'] = time();
- $params['enableStatus'] = $where['enableStatus'];
- unset($where['enableStatus']);
- $dbResult = $this->objDShopPartner->update($params, ['id' => $where['partnerId']]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 商铺合作修改
- * @param $params
- * @param $where
- * @return ResultWrapper
- * @throws Exception
- */
- public function updateShopPartner($params, $where)
- {
- //先把之前的商铺合伙人置空
- $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
- $oldShopId = $this->objDShopPartner->get_field('shopId',$where);
- if(!empty($oldShopId)) {
- $oldShopId = explode(',', $oldShopId);
- $objMShop->updateShopPartnerId(['id'=>$oldShopId, 'shopPartnerId'=>'']);
- }
- $dbResult = $this->objDShopPartner->update($params,$where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }else{
- //更改商铺的合伙人
- $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
- if(!empty($params['shopId'])) {
- $newShopIds = explode(',', $params['shopId']);
- $objMShop->updateShopPartnerId(['id'=>$newShopIds, 'shopPartnerId'=>$where['id']]);
- }
- return ResultWrapper::success($dbResult);
- }
- }
- public function updateShopIds($shopIds, $id) {
- $dbResult = $this->objDShopPartner->update(['shopId'=>$shopIds],$id);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 商铺合作列表
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllShopPartner($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $selectParams['enterpriseId'] = $this->enterpriseId;
- $dbResult = $this->objDShopPartner->select($selectParams, '*', 'createTime desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }
- $modelResult = self::format($dbResult);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $dbResult = $modelResult->getData();
- $total = $this->objDShopPartner->count($selectParams);
- $return = [
- 'data' => $dbResult,
- 'total' => ($total) ? intval($total) : 0,
- ];
- if($return === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($return);
- }
- }
- /**
- * 商铺合作搜索
- * @param $params
- * @return ResultWrapper
- */
- public function search($params)
- {
- $limit = $params['limit'];
- $offset = $params['offset'];
- $condition = 'deleteStatus = '.StatusCode::$standard . ' AND enterpriseId = '.$this->enterpriseId;
- if( $params['name'] ){
- $condition .= " AND name like '%".$params['name']."%'";
- }
- if( $params['mobile'] ){
- $condition .= " AND mobile like '%".$params['mobile']."%'";
- }
- $dbResult = $this->objDShopPartner->select($condition, '*', 'createTime desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }
- $modelResult = self::format($dbResult);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $dbResult = $modelResult->getData();
- $total = $this->objDShopPartner->count($condition);
- $return = [
- 'data' => $dbResult,
- 'total' => ($total) ? intval($total) : 0,
- ];
- if($return === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($return);
- }
- }
- public function format($data) {
- $objMSysAreaChina = new MSysAreaChina();
- $userCenterIds = [];
- $shopIds = [];
- foreach ($data as &$partner) {
- if (isset($partner['provinceCode']) && isset($partner['cityCode']) && isset($partner['districtCode'])) {
- $areaName = $objMSysAreaChina->getNameByCode([
- $partner['provinceCode'],
- $partner['cityCode'],
- $partner['districtCode']
- ]);
- $partner['area']['provinceName'] = isset($areaName[$partner['provinceCode']]) ? $areaName[$partner['provinceCode']] : '';
- $partner['area']['cityName'] = isset($areaName[$partner['cityCode']]) ? $areaName[$partner['cityCode']] : '';
- $partner['area']['districtName'] = isset($areaName[$partner['districtCode']]) ? $areaName[$partner['districtCode']] : '';
- $partner['area']['address'] = $partner['address'];
- if(!empty($partner['shopId'])){
- $shopIds = array_merge($shopIds, explode(',',$partner['shopId']));
- }
- if(empty($partner['userName'])){
- $userCenterIds[] = $partner['userCenterId'];
- }
- }
- }
- $shopData = [];
- if( isset($shopIds) && !empty($shopIds) ) {
- //查出合伙人对应的商铺名称
- $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
- $shopData = $objMShop->getShopName($shopIds);
- }
- //负责人账号
- $formatUserData = [];
- if(!empty($userCenterIds)){
- $modelResult = self::getManagerList($userCenterIds);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode());
- }
- $userData = $modelResult->getData();
- if(empty($userData)){
- return ResultWrapper::fail('负责人账号查询失败', ErrorCode::$paramError);
- }
- foreach($userData as $value){
- $formatUserData[$value['userCenterId']] = $value;
- }
- }
- unset($partner);
- foreach ($data as $key =>$partner) {
- $data[$key]['shopName'] = '';
- $data[$key]['staffMobile'] = isset($formatUserData[$partner['userCenterId']]) && !empty($formatUserData[$partner['userCenterId']]) ? $formatUserData[$partner['userCenterId']]['userMobile'] : '';
- if(empty($partner['userName']) && isset($formatUserData[$partner['userCenterId']])){
- $data[$key]['userName'] = $formatUserData[$partner['userCenterId']]['name'];
- //修改数据库userName字段
- $update = [
- 'userName' => $formatUserData[$partner['userCenterId']]['name'],
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDShopPartner->update($update, ['id' => $partner['id']]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }
- }
- if(!empty($partner['shopId'])) {
- $shopIds = explode(',', $partner['shopId']);
- foreach ($shopIds as $shopId) {
- $data[$key]['shopName'] .= isset($shopData[$shopId]) ? ','.$shopData[$shopId]['name'] : '';
- }
- $data[$key]['shopName'] = ltrim($data[$key]['shopName'], ',');
- }
- }
- return ResultWrapper::success($data);
- }
- /**
- * 商铺合作详情
- * @param $params
- * @return ResultWrapper
- */
- public function getShopPartnerInfo($params)
- {
- $dbResult = $this->objDShopPartner->get($params);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
- }
- $modelResult = self::format([$dbResult]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $dbResult = $modelResult->getData();
- $dbResult = array_shift($dbResult);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 根据合伙人id查商铺Id 一个合伙人管理一个店铺,一个店铺可以有多个合伙人
- * @param $partnerId
- * @return ResultWrapper
- */
- public function getShopIdsByPartnerId($partnerId) {
- $dbResult = $this->objDShopPartner->get_field('shopId', $partnerId);
- return isset($dbResult) ? explode(',',$dbResult) : [];
- }
- /**
- * 获取合伙人名称数组
- * @param $shopPartnerIds
- * @return array
- */
- public function getNameByShopPartnerIds($shopPartnerIds) {
- $result = [];
- $dbResult = $this->objDShopPartner->select(array_filter($shopPartnerIds, function($v) {
- return !empty($v) && $v !== 0;
- }), 'id,name', 'createTime desc');
- if($dbResult === false) {
- return $result;
- }
- foreach ($dbResult as $shopPartner) {
- $result[$shopPartner['id']] = $shopPartner['name'];
- }
- return $result;
- }
- }
|