MShopPartner.Class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /**
  3. * 商铺合作管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/01
  7. * Time: 10:00
  8. */
  9. namespace JinDouYun\Model\ShopPartner;
  10. use Exception;
  11. use JinDouYun\Model\MBaseModel;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\StatusCode;
  14. use Mall\Framework\Core\ResultWrapper;
  15. use JinDouYun\Dao\ShopPartner\DShopPartner;
  16. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  17. use JinDouYun\Model\Shop\MShop;
  18. use JinDouYun\Model\Department\MStaff;
  19. use JinDouYun\Model\Enterprise\MEnterprise;
  20. class MShopPartner extends MBaseModel
  21. {
  22. private $objDShopPartner;
  23. private $cutTable = 250000;
  24. private $enterpriseId;
  25. private $userCenterId;
  26. public function __construct($enterpriseId, $userCenterId)
  27. {
  28. $this->enterpriseId = $enterpriseId;
  29. $this->userCenterId = $userCenterId;
  30. parent::__construct($enterpriseId, $userCenterId);
  31. $this->objDShopPartner = new DShopPartner('default');
  32. $tableName = $this->objDShopPartner->getTableName($this->objDShopPartner->get_Table(), $this->enterpriseId, $this->cutTable);
  33. $this->objDShopPartner->setTable($tableName);
  34. }
  35. /**
  36. * 商铺合作添加
  37. * @param $params
  38. * @return ResultWrapper
  39. * @throws Exception
  40. */
  41. public function addShopPartner($params)
  42. {
  43. #777 查询商铺id 是否存在
  44. $params['createTime'] = $params['updateTime'];
  45. //获取负责人信息
  46. $modelResult = self::getManagerList($params['userCenterId']);
  47. if(!$modelResult->isSuccess()){
  48. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  49. }
  50. $userData = $modelResult->getData();
  51. unset($modelResult);
  52. if(empty($userData)){
  53. return ResultWrapper::fail('负责人id参数错误', ErrorCode::$dberror);
  54. }
  55. foreach($userData as $value){
  56. if($value['userCenterId'] == $params['userCenterId']){
  57. $params['userName'] = $value['name'];
  58. }
  59. }
  60. $dbResult = $this->objDShopPartner->insert($params);
  61. if($dbResult === false){
  62. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  63. }else{
  64. //更改商铺的合伙人
  65. $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
  66. if(!empty($params['shopId'])) {
  67. $shopIds = explode(',', $params['shopId']);
  68. $modelResult = $objMShop->updateShopPartnerId(['id'=>$shopIds, 'shopPartnerId'=>$dbResult]);
  69. if(!$modelResult->isSuccess()){
  70. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  71. }
  72. }
  73. return ResultWrapper::success($dbResult);
  74. }
  75. }
  76. /**
  77. * 商铺合作添加时 获取企业下的管理员信息
  78. * @param bool $userCenterId
  79. * @return ResultWrapper
  80. * @throws Exception
  81. */
  82. public function getManagerList($userCenterId = false)
  83. {
  84. $objMEnterprise = new MEnterprise();
  85. $modelResult = $objMEnterprise->getAdminsByEnterpriseId($this->enterpriseId, $userCenterId);
  86. if(!$modelResult->isSuccess()){
  87. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  88. }
  89. return ResultWrapper::success(array_values($modelResult->getData()));
  90. }
  91. /**
  92. * 商铺合作删除
  93. * @param $where
  94. * @return ResultWrapper
  95. */
  96. public function deleteShopPartner($where)
  97. {
  98. $params = [
  99. 'deleteStatus' => StatusCode::$delete,
  100. 'updateTime' => time(),
  101. ];
  102. $dbResult = $this->objDShopPartner->update($params, $where);
  103. if($dbResult === false){
  104. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  105. }else{
  106. return ResultWrapper::success($dbResult);
  107. }
  108. }
  109. /**
  110. * 商铺合作启用/禁用
  111. * @param $where
  112. * @return ResultWrapper
  113. * @throws Exception
  114. */
  115. public function enableShopPartner($where)
  116. {
  117. $params['updateTime'] = time();
  118. $params['enableStatus'] = $where['enableStatus'];
  119. unset($where['enableStatus']);
  120. $dbResult = $this->objDShopPartner->update($params, ['id' => $where['partnerId']]);
  121. if($dbResult === false){
  122. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  123. }else{
  124. return ResultWrapper::success($dbResult);
  125. }
  126. }
  127. /**
  128. * 商铺合作修改
  129. * @param $params
  130. * @param $where
  131. * @return ResultWrapper
  132. * @throws Exception
  133. */
  134. public function updateShopPartner($params, $where)
  135. {
  136. //先把之前的商铺合伙人置空
  137. $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
  138. $oldShopId = $this->objDShopPartner->get_field('shopId',$where);
  139. if(!empty($oldShopId)) {
  140. $oldShopId = explode(',', $oldShopId);
  141. $objMShop->updateShopPartnerId(['id'=>$oldShopId, 'shopPartnerId'=>'']);
  142. }
  143. $dbResult = $this->objDShopPartner->update($params,$where);
  144. if($dbResult === false){
  145. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  146. }else{
  147. //更改商铺的合伙人
  148. $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
  149. if(!empty($params['shopId'])) {
  150. $newShopIds = explode(',', $params['shopId']);
  151. $objMShop->updateShopPartnerId(['id'=>$newShopIds, 'shopPartnerId'=>$where['id']]);
  152. }
  153. return ResultWrapper::success($dbResult);
  154. }
  155. }
  156. public function updateShopIds($shopIds, $id) {
  157. $dbResult = $this->objDShopPartner->update(['shopId'=>$shopIds],$id);
  158. if($dbResult === false){
  159. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  160. }else{
  161. return ResultWrapper::success($dbResult);
  162. }
  163. }
  164. /**
  165. * 商铺合作列表
  166. * @param $selectParams
  167. * @return ResultWrapper
  168. */
  169. public function getAllShopPartner($selectParams)
  170. {
  171. $limit = $selectParams['limit'];
  172. unset($selectParams['limit']);
  173. $offset = $selectParams['offset'];
  174. unset($selectParams['offset']);
  175. $selectParams['deleteStatus'] = StatusCode::$standard;
  176. $selectParams['enterpriseId'] = $this->enterpriseId;
  177. $dbResult = $this->objDShopPartner->select($selectParams, '*', 'createTime desc', $limit, $offset);
  178. if ($dbResult === false) {
  179. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  180. }
  181. $modelResult = self::format($dbResult);
  182. if(!$modelResult->isSuccess()){
  183. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  184. }
  185. $dbResult = $modelResult->getData();
  186. $total = $this->objDShopPartner->count($selectParams);
  187. $return = [
  188. 'data' => $dbResult,
  189. 'total' => ($total) ? intval($total) : 0,
  190. ];
  191. if($return === false){
  192. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  193. }else{
  194. return ResultWrapper::success($return);
  195. }
  196. }
  197. /**
  198. * 商铺合作搜索
  199. * @param $params
  200. * @return ResultWrapper
  201. */
  202. public function search($params)
  203. {
  204. $limit = $params['limit'];
  205. $offset = $params['offset'];
  206. $condition = 'deleteStatus = '.StatusCode::$standard . ' AND enterpriseId = '.$this->enterpriseId;
  207. if( $params['name'] ){
  208. $condition .= " AND name like '%".$params['name']."%'";
  209. }
  210. if( $params['mobile'] ){
  211. $condition .= " AND mobile like '%".$params['mobile']."%'";
  212. }
  213. $dbResult = $this->objDShopPartner->select($condition, '*', 'createTime desc', $limit, $offset);
  214. if ($dbResult === false) {
  215. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  216. }
  217. $modelResult = self::format($dbResult);
  218. if(!$modelResult->isSuccess()){
  219. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  220. }
  221. $dbResult = $modelResult->getData();
  222. $total = $this->objDShopPartner->count($condition);
  223. $return = [
  224. 'data' => $dbResult,
  225. 'total' => ($total) ? intval($total) : 0,
  226. ];
  227. if($return === false){
  228. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  229. }else{
  230. return ResultWrapper::success($return);
  231. }
  232. }
  233. public function format($data) {
  234. $objMSysAreaChina = new MSysAreaChina();
  235. $userCenterIds = [];
  236. $shopIds = [];
  237. foreach ($data as &$partner) {
  238. if (isset($partner['provinceCode']) && isset($partner['cityCode']) && isset($partner['districtCode'])) {
  239. $areaName = $objMSysAreaChina->getNameByCode([
  240. $partner['provinceCode'],
  241. $partner['cityCode'],
  242. $partner['districtCode']
  243. ]);
  244. $partner['area']['provinceName'] = isset($areaName[$partner['provinceCode']]) ? $areaName[$partner['provinceCode']] : '';
  245. $partner['area']['cityName'] = isset($areaName[$partner['cityCode']]) ? $areaName[$partner['cityCode']] : '';
  246. $partner['area']['districtName'] = isset($areaName[$partner['districtCode']]) ? $areaName[$partner['districtCode']] : '';
  247. $partner['area']['address'] = $partner['address'];
  248. if(!empty($partner['shopId'])){
  249. $shopIds = array_merge($shopIds, explode(',',$partner['shopId']));
  250. }
  251. if(empty($partner['userName'])){
  252. $userCenterIds[] = $partner['userCenterId'];
  253. }
  254. }
  255. }
  256. $shopData = [];
  257. if( isset($shopIds) && !empty($shopIds) ) {
  258. //查出合伙人对应的商铺名称
  259. $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
  260. $shopData = $objMShop->getShopName($shopIds);
  261. }
  262. //负责人账号
  263. $formatUserData = [];
  264. if(!empty($userCenterIds)){
  265. $modelResult = self::getManagerList($userCenterIds);
  266. if(!$modelResult->isSuccess()){
  267. return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode());
  268. }
  269. $userData = $modelResult->getData();
  270. if(empty($userData)){
  271. return ResultWrapper::fail('负责人账号查询失败', ErrorCode::$paramError);
  272. }
  273. foreach($userData as $value){
  274. $formatUserData[$value['userCenterId']] = $value;
  275. }
  276. }
  277. unset($partner);
  278. foreach ($data as $key =>$partner) {
  279. $data[$key]['shopName'] = '';
  280. $data[$key]['staffMobile'] = isset($formatUserData[$partner['userCenterId']]) && !empty($formatUserData[$partner['userCenterId']]) ? $formatUserData[$partner['userCenterId']]['userMobile'] : '';
  281. if(empty($partner['userName']) && isset($formatUserData[$partner['userCenterId']])){
  282. $data[$key]['userName'] = $formatUserData[$partner['userCenterId']]['name'];
  283. //修改数据库userName字段
  284. $update = [
  285. 'userName' => $formatUserData[$partner['userCenterId']]['name'],
  286. 'updateTime' => time(),
  287. ];
  288. $dbResult = $this->objDShopPartner->update($update, ['id' => $partner['id']]);
  289. if($dbResult === false){
  290. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  291. }
  292. }
  293. if(!empty($partner['shopId'])) {
  294. $shopIds = explode(',', $partner['shopId']);
  295. foreach ($shopIds as $shopId) {
  296. $data[$key]['shopName'] .= isset($shopData[$shopId]) ? ','.$shopData[$shopId]['name'] : '';
  297. }
  298. $data[$key]['shopName'] = ltrim($data[$key]['shopName'], ',');
  299. }
  300. }
  301. return ResultWrapper::success($data);
  302. }
  303. /**
  304. * 商铺合作详情
  305. * @param $params
  306. * @return ResultWrapper
  307. */
  308. public function getShopPartnerInfo($params)
  309. {
  310. $dbResult = $this->objDShopPartner->get($params);
  311. if($dbResult === false){
  312. return ResultWrapper::fail($this->objDShopPartner->error(), ErrorCode::$dberror);
  313. }
  314. $modelResult = self::format([$dbResult]);
  315. if(!$modelResult->isSuccess()){
  316. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  317. }
  318. $dbResult = $modelResult->getData();
  319. $dbResult = array_shift($dbResult);
  320. return ResultWrapper::success($dbResult);
  321. }
  322. /**
  323. * 根据合伙人id查商铺Id 一个合伙人管理一个店铺,一个店铺可以有多个合伙人
  324. * @param $partnerId
  325. * @return ResultWrapper
  326. */
  327. public function getShopIdsByPartnerId($partnerId) {
  328. $dbResult = $this->objDShopPartner->get_field('shopId', $partnerId);
  329. return isset($dbResult) ? explode(',',$dbResult) : [];
  330. }
  331. /**
  332. * 获取合伙人名称数组
  333. * @param $shopPartnerIds
  334. * @return array
  335. */
  336. public function getNameByShopPartnerIds($shopPartnerIds) {
  337. $result = [];
  338. $dbResult = $this->objDShopPartner->select(array_filter($shopPartnerIds, function($v) {
  339. return !empty($v) && $v !== 0;
  340. }), 'id,name', 'createTime desc');
  341. if($dbResult === false) {
  342. return $result;
  343. }
  344. foreach ($dbResult as $shopPartner) {
  345. $result[$shopPartner['id']] = $shopPartner['name'];
  346. }
  347. return $result;
  348. }
  349. }