MEnterprise.Class.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. <?php
  2. /**
  3. * 企业管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/04
  7. * Time: 11:50
  8. */
  9. namespace JinDouYun\Model\Enterprise;
  10. use Exception;
  11. use Jindouyun\Cache\EnterpriseCache;
  12. use Jindouyun\Cache\RoleAclCache;
  13. use JinDouYun\Dao\Commission\DCommissionSetting;
  14. use JinDouYun\Dao\Department\DStaff;
  15. use JinDouYun\Dao\Merchant\DMerchantApply;
  16. use JinDouYun\Dao\Shop\DShop;
  17. use JinDouYun\Dao\Shop\DShopBindStaff;
  18. use JinDouYun\Dao\Shop\DShopClock;
  19. use JinDouYun\Dao\Shop\DShopKey;
  20. use JinDouYun\Dao\Shop\DShopRostering;
  21. use JinDouYun\Dao\Shop\DShopSubscribe;
  22. use JinDouYun\Model\Commission\MCommissionGrade;
  23. use JinDouYun\Model\Department\MDepartment;
  24. use JinDouYun\Model\Department\MRole;
  25. use JinDouYun\Model\GoodsCategory\MGoodsCategory;
  26. use JinDouYun\Model\GoodsManage\MGoodsBrand;
  27. use JinDouYun\Model\Merchant\MMerchant;
  28. use JinDouYun\Model\Shop\MShop;
  29. use JinDouYun\Model\Shop\MShopRostering;
  30. use JinDouYun\Model\Shop\MShopSubscribe;
  31. use JinDouYun\Model\ShopPartner\MShopPartner;
  32. use JinDouYun\Model\Stock\MWarehouse;
  33. use JinDouYun\Model\System\MPaymentSetting;
  34. use Mall\Framework\Core\ErrorCode;
  35. use Mall\Framework\Core\StatusCode;
  36. use Mall\Framework\Core\ResultWrapper;
  37. use JinDouYun\Dao\Enterprise\DEnterprise;
  38. use JinDouYun\Dao\UserCenter\DUserCenter;
  39. use JinDouYun\Dao\Enterprise\DUserBindEnterprise;
  40. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  41. use JinDouYun\Model\Department\MStaff;
  42. class MEnterprise
  43. {
  44. private $objDEnterprise;
  45. private $objDUserBindEnterprise;
  46. private $objDUserCenter;
  47. private $cutTable = 500000;
  48. public function __construct()
  49. {
  50. $this->objDUserCenter = new DUserCenter('default');
  51. $this->objDEnterprise = new DEnterprise('default');
  52. $this->objDUserBindEnterprise = new DUserBindEnterprise('default');
  53. }
  54. /**
  55. * 分表
  56. * @param $id
  57. * @throws Exception
  58. */
  59. public function enterpriseSubTable()
  60. {
  61. //企业表不考虑分表
  62. $this->objDEnterprise->setTable('qianniao_enterprise_1');
  63. // $tableName = $this->objDEnterprise->getTableName('qianniao_enterprise_', $id, $this->cutTable);
  64. // $this->objDEnterprise->setTable($tableName);
  65. }
  66. /**
  67. * 生成appid
  68. * @param $userCenterId
  69. * @return array
  70. */
  71. public function createAppid($userCenterId)
  72. {
  73. $returnData = [];
  74. //1 + 时间戳转日期(1107)4位 + 用户id(2位) + 毫秒时间提取(4位)
  75. $appid = '1'.date('md').substr(strval($userCenterId+100),-2).substr(microtime(),2, 4);
  76. $returnData['appid'] = intval($appid);
  77. $returnData['appSecret'] = md5($returnData['appid'].'qiannaio');
  78. $returnData['token'] = md5(md5($returnData['appid'].$returnData['appSecret'].'qianniao'));
  79. return $returnData;
  80. }
  81. /**
  82. * 增
  83. */
  84. /**
  85. * 企业添加
  86. * @param $params
  87. * @return ResultWrapper
  88. * @throws Exception
  89. */
  90. public function addEnterprise($params, $authorization)
  91. {
  92. //调用分表
  93. self::enterpriseSubTable();
  94. #777 查询商铺id 是否存在
  95. $params['createTime'] = $params['updateTime'];
  96. $params['expireTime'] = strtotime('+30 day');
  97. //增加加密字段
  98. $appidData = $this->createAppid($params['userCenterId']);
  99. $params = array_merge($params, $appidData);
  100. $enterpriseId = $this->objDEnterprise->insert($params);
  101. if($enterpriseId === false){
  102. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  103. }
  104. //增加用户公司关系表
  105. $UserBindEnterpriseData = [
  106. 'userCenterId' => $params['userCenterId'],
  107. 'roleType' => StatusCode::$roleType['admin'],
  108. 'enterpriseId' => $enterpriseId,
  109. 'updateTime' => time(),
  110. 'createTime' => time(),
  111. ];
  112. $dbResult = $this->objDUserBindEnterprise->insert($UserBindEnterpriseData);
  113. if($dbResult === false){
  114. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  115. }
  116. $returnData = [
  117. 'enterpriseId' => $enterpriseId,
  118. 'token' => $appidData['token'],
  119. ];
  120. //调用自动创建队列
  121. $data = [
  122. 'userCenterId' => $params['userCenterId'],
  123. 'enterpriseId' => $enterpriseId,
  124. 'Authorization' => $authorization,
  125. 'PROJECT_DOMAIN' => PROJECT_DOMAIN
  126. ];
  127. self::autoCreate($data, 'MEnterprise');
  128. return ResultWrapper::success( array_merge($params,$returnData) );
  129. }
  130. /**
  131. * 删
  132. */
  133. /**
  134. * 企业删除
  135. * @param $params
  136. * @return ResultWrapper
  137. * @throws Exception
  138. */
  139. public function deleteEnterprise($params)
  140. {
  141. //#777 校验公司id数据是否属于该用户id下的公司
  142. //调用分表
  143. self::enterpriseSubTable();
  144. $updateData = [
  145. 'deleteStatus' => StatusCode::$delete,
  146. 'updateTime' => time(),
  147. ];
  148. $dbResult = $this->objDEnterprise->update($updateData, ['id' => $params['enterpriseId']]);
  149. if($dbResult === false){
  150. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  151. }
  152. //删除用户企业关联表里的数据
  153. $dbReturn = $this->objDUserBindEnterprise->delete(['userCenterId' => $params['userCenterId'], 'enterpriseId' => $params['enterpriseId']]);
  154. if($dbReturn === false){
  155. return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
  156. }
  157. return ResultWrapper::success($dbResult);
  158. }
  159. /**
  160. * 改
  161. */
  162. /**
  163. * 企业修改
  164. * @param $updateData
  165. * @param $params
  166. * @return ResultWrapper
  167. * @throws Exception
  168. */
  169. public function updateEnterprise($updateData, $params)
  170. {
  171. //#777 校验公司id数据是否属于该用户id下的公司
  172. //调用分表
  173. self::enterpriseSubTable();
  174. unset($updateData['userCenterId']);
  175. $updateData['updateTime'] = time();
  176. $dbResult = $this->objDEnterprise->update($updateData, $params);
  177. if($dbResult === false){
  178. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  179. }else{
  180. return ResultWrapper::success($dbResult);
  181. }
  182. }
  183. /**
  184. * 用户修改密码
  185. * @param $params
  186. * @param $where
  187. * @return ResultWrapper
  188. */
  189. public function updateUserCenterData($params, $where)
  190. {
  191. $dbResult = $this->objDUserCenter->get($where);
  192. if($dbResult === false){
  193. return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
  194. }
  195. if(empty($dbResult)){
  196. return ResultWrapper::fail('用户信息为空', ErrorCode::$dberror);
  197. }
  198. $userCenterData = $dbResult;
  199. unset($dbResult);
  200. if(password_verify($params['oldPassword'], $userCenterData['password']) === false){
  201. return ResultWrapper::fail('旧密码错误', ErrorCode::$paramError);
  202. }
  203. $updateData = [
  204. 'password' => password_hash($params['newPassword'], PASSWORD_DEFAULT ),
  205. 'updateTime' => time(),
  206. ];
  207. $dbResult = $this->objDUserCenter->update($updateData, $where);
  208. if($dbResult === false){
  209. return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
  210. }
  211. return ResultWrapper::success($dbResult);
  212. }
  213. /**
  214. * 查出当前登录用户下的企业列表
  215. * @param $userCenterId
  216. * @return ResultWrapper
  217. * @throws Exception
  218. */
  219. public function getAllEnterprise($userCenterId, $source = 1)
  220. {
  221. if(empty($userCenterId)){
  222. return ResultWrapper::fail('userCenterId参数为空', ErrorCode::$paramError);
  223. }
  224. switch ($source){
  225. case 1://管理员,员工
  226. $roleType = StatusCode::$roleType['admin'] . "," .StatusCode::$roleType['staff'];
  227. break;
  228. case 2://供应商
  229. $roleType = StatusCode::$roleType['supplier'];
  230. break;
  231. case 3://商户
  232. $roleType = StatusCode::$roleType['merchant'];
  233. break;
  234. default:
  235. return ResultWrapper::fail('获取企业列表source参数错误', ErrorCode::$paramError);
  236. }
  237. //企业表不考虑分表
  238. $this->objDEnterprise->setTable('qianniao_enterprise_1');
  239. $sql = "select b.userCenterId,b.enterpriseId,b.roleType,b.shopId,e.enterpriseName,e.logo,e.appid,e.token,e.expireTime,e.createTime,e.scope,e.isoem from " . $this->objDUserBindEnterprise->get_Table() . " b left join " . $this->objDEnterprise->get_Table() . " e on b.enterpriseId = e.id where b.userCenterId = ".$userCenterId." and b.roleType in (".$roleType.") and e.deleteStatus = " . StatusCode::$standard . " order by e.expireTime desc";
  240. $bindData = $this->objDUserBindEnterprise->query($sql);
  241. if ($bindData === false) {
  242. return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
  243. }
  244. $return = [];
  245. $objDMerchantApply = new DMerchantApply();
  246. foreach($bindData as $value){
  247. $value['merchantData'] = [];
  248. switch ($value['roleType']){
  249. case StatusCode::$roleType['admin']:
  250. //管理员
  251. $value['isInEnterprise'] = StatusCode::$standard;
  252. break;
  253. case StatusCode::$roleType['staff']:
  254. //员工
  255. if(empty($value['shopId'])){
  256. $value['isInEnterprise'] = StatusCode::$standard;
  257. }
  258. break;
  259. case StatusCode::$roleType['merchant']:
  260. //商户
  261. $value['isInEnterprise'] = StatusCode::$delete;
  262. //查询商户数据
  263. $where =
  264. [
  265. 'userCenterId' => $value['userCenterId'],
  266. 'enterpriseId' => $value['enterpriseId'],
  267. 'deleteStatus' => StatusCode::$standard,
  268. 'auditStatus' => StatusCode::$auditStatus['auditPass'],
  269. ];
  270. $dbResult = $objDMerchantApply->get($where, 'id,`name`,`desc`,category,contactName,contactMobile,expireTime,enabledStatus');
  271. if($dbResult === false){
  272. return ResultWrapper::fail($objDMerchantApply->error(), ErrorCode::$dberror);
  273. }
  274. if($dbResult['enabledStatus'] == StatusCode::$delete){
  275. return ResultWrapper::fail('该商户已禁用', ErrorCode::$accountfail);
  276. }
  277. $value['merchantData'] = $dbResult;
  278. unset($dbResult);
  279. break;
  280. default:
  281. $value['isInEnterprise'] = StatusCode::$delete;
  282. break;
  283. }
  284. if(!isset($return[$value['enterpriseId']])){
  285. $value['roleType'] = [$value['roleType']];
  286. $return[$value['enterpriseId']] = $value;
  287. }else{
  288. $return[$value['enterpriseId']]['roleType'][] = $value['roleType'];
  289. }
  290. }
  291. return ResultWrapper::success(self::format(array_values($return)));
  292. }
  293. /**
  294. * 企业详情
  295. * @param $params
  296. * @return ResultWrapper
  297. * @throws Exception
  298. */
  299. public function getEnterpriseInfo($params)
  300. {
  301. //调用分表
  302. self::enterpriseSubTable();
  303. $dbResult = $this->objDEnterprise->get(['id' => $params['enterpriseId']]);
  304. if($dbResult === false){
  305. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  306. }else{
  307. $objMSysAreaChina = new MSysAreaChina();
  308. if (isset($dbResult['provinceCode']) && isset($dbResult['cityCode']) && isset($dbResult['districtCode'])) {
  309. $areaName = $objMSysAreaChina->getNameByCode([
  310. $dbResult['provinceCode'],
  311. $dbResult['cityCode'],
  312. $dbResult['districtCode']
  313. ]);
  314. $dbResult['area']['provinceName'] = isset($areaName[$dbResult['provinceCode']]) ? $areaName[$dbResult['provinceCode']] : '';
  315. $dbResult['area']['cityName'] = isset($areaName[$dbResult['cityCode']]) ? $areaName[$dbResult['cityCode']] : '';
  316. $dbResult['area']['districtName'] = isset($areaName[$dbResult['districtCode']]) ? $areaName[$dbResult['districtCode']] : '';
  317. $dbResult['area']['address'] = $dbResult['address'];
  318. }
  319. return ResultWrapper::success($dbResult);
  320. }
  321. }
  322. /**
  323. * 根据企业id获取企业数据
  324. * @param $enterpriseId
  325. * @return ResultWrapper
  326. * @throws Exception
  327. */
  328. public function getEnterpriseDataByEnterpriseId($enterpriseId)
  329. {
  330. //调用分表
  331. self::enterpriseSubTable();
  332. $dbResult = $this->objDEnterprise->get(['id' => $enterpriseId, 'deleteStatus' => StatusCode::$standard]);
  333. if($dbResult === false){
  334. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  335. }
  336. return ResultWrapper::success($dbResult);
  337. }
  338. /**
  339. * 根据用户id获取企业信息
  340. * @param $userId
  341. * @return array|bool|ResultWrapper
  342. * @throws Exception
  343. */
  344. public function getEnterpriseByUserId($userId)
  345. {
  346. //调用分表
  347. self::enterpriseSubTable();
  348. $sql = "select e.token,b.roleType from qianniao_user_bind_enterprise b LEFT JOIN ".$this->objDEnterprise->get_Table()." e on b.enterpriseId = e.id where b.userCenterId = ".$userId;
  349. $bindData = $this->objDUserBindEnterprise->query($sql);
  350. if($bindData === false){
  351. return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
  352. }
  353. return ResultWrapper::success($bindData);
  354. }
  355. /**
  356. * 根据企业id获取企业的管理员信息
  357. * @param $enterpriseId
  358. * @param array $userCenterId
  359. * @return ResultWrapper
  360. * @throws Exception
  361. */
  362. public function getAdminsByEnterpriseId($enterpriseId, $userCenterId = [])
  363. {
  364. empty($enterpriseId) && ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  365. $whereSql = '';
  366. if(!empty($userCenterId) && $userCenterId != false){
  367. if(is_array($userCenterId)){
  368. if(count($userCenterId) == 1){
  369. $whereSql .= ' AND u.id in('. array_shift($userCenterId).')';
  370. }else{
  371. $whereSql .= ' AND u.id in('.implode(',', $userCenterId).')';
  372. }
  373. }else{
  374. $whereSql .= ' AND u.id in('.$userCenterId.')';
  375. }
  376. }
  377. $sql = 'SELECT b.*,u.mobile AS userMobile FROM qianniao_user_bind_enterprise b LEFT JOIN qianniao_user_center u ON b.userCenterId = u.id WHERE b.enterpriseId = '.$enterpriseId.' AND b.roleType BETWEEN '.StatusCode::$roleType['admin'].' AND '.StatusCode::$roleType['staff'].' AND u.deleteStatus ='.StatusCode::$standard.$whereSql;
  378. $dbResult = $this->objDUserBindEnterprise->query($sql);
  379. if($dbResult === false){
  380. return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
  381. }
  382. $createAdminId = 0;
  383. foreach($dbResult as &$value){
  384. if($value['roleType'] == StatusCode::$roleType['admin']){
  385. if(!$createAdminId){
  386. //企业创建者NAME
  387. //调用分表
  388. $createAdminId = $value['userCenterId'];
  389. self::enterpriseSubTable();
  390. $enterpriseDbResult = $this->objDEnterprise->get(['id' => $enterpriseId, 'userCenterId' => $createAdminId], 'contact');
  391. if($enterpriseDbResult === false){
  392. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  393. }
  394. if(empty($enterpriseDbResult)){
  395. return ResultWrapper::fail('企业信息查询失败', ErrorCode::$paramError);
  396. }
  397. $value['name'] = $enterpriseDbResult['contact'];
  398. }else{
  399. return ResultWrapper::fail('角色id错误', ErrorCode::$paramError);//一个企业不可能有多个创建者
  400. }
  401. }else{
  402. $value['name'] = '';
  403. $userCenterIds[] = $value['userCenterId'];
  404. }
  405. }
  406. unset($value);
  407. if(empty($userCenterIds)){
  408. return ResultWrapper::success($dbResult);
  409. }
  410. //职工name
  411. $objMStaff = new MStaff($enterpriseId);
  412. $modelResult = $objMStaff->getStaffByUserCenterIds($userCenterIds);
  413. if(!$modelResult->isSuccess()){
  414. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  415. }
  416. $staffData = $modelResult->getData();
  417. $newStaffData = [];
  418. if(!empty($staffData)){
  419. foreach ($staffData as $key => $value){
  420. $newStaffData[$value['userCenterId']] = $value;
  421. }
  422. }
  423. foreach($dbResult as $key => $value){
  424. if(isset($newStaffData[$value['userCenterId']])){
  425. $dbResult[$key]['name'] = $newStaffData[$value['userCenterId']]['staffName'];
  426. }else if($value['roleType'] == StatusCode::$roleType['staff']){
  427. unset($dbResult[$key]);
  428. }
  429. }
  430. return ResultWrapper::success($dbResult);
  431. }
  432. /**
  433. * 格式化企业列表数据 过期状态
  434. * @param $data
  435. * @return mixed
  436. */
  437. public function format($data)
  438. {
  439. $objEnterpriseCache = new EnterpriseCache();
  440. foreach($data as &$v){
  441. $v['expireStatus'] = true;
  442. if($v['expireTime'] > time()){
  443. $v['expireStatus'] = false;
  444. }
  445. //映射redis缓存里的企业生产日期设置
  446. $cacheResult = $objEnterpriseCache->getEnterpriseData($v['enterpriseId'], 'shelfLifeSetUp');
  447. $v['shelfLifeSetUp'] = $cacheResult ? $cacheResult : StatusCode::$delete;
  448. }
  449. return $data;
  450. }
  451. /**
  452. * 根据用户token获取用户信息(无用)
  453. * @param $params
  454. * @return ResultWrapper
  455. * @throws Exception
  456. */
  457. public function getStaffByToken($params)
  458. {
  459. //调用分表
  460. self::enterpriseSubTable();
  461. //1.查询是否有角色绑定
  462. $where = [
  463. 'enterpriseId' => $params['enterpriseId'],
  464. // 'shopId' => $params['shopId'],
  465. 'userCenterId' => $params['userCenterId'],
  466. ];
  467. $dbResult = $this->objDUserBindEnterprise->get($where);
  468. if($dbResult === false){
  469. return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
  470. }
  471. $bind = $dbResult;
  472. unset($dbResult);
  473. $returnData = [
  474. 'staffId' => '',
  475. 'name' => '',
  476. 'userCenterId' => $params['userCenterId']
  477. ];
  478. if(empty($bind)){
  479. return ResultWrapper::success($returnData);
  480. }
  481. if($bind['roleType'] == StatusCode::$roleType['merchant']){
  482. //商户
  483. $objMMerchant = new MMerchant($params['enterpriseId'], false);
  484. $modelResult = $objMMerchant->getMerchantData(['id' => $bind['merchantId']]);
  485. if(!$modelResult->isSuccess()){
  486. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  487. }
  488. $merchant = $modelResult->getData();
  489. unset($modelResult);
  490. $returnData['name'] = $merchant['contactName'];
  491. }else{
  492. //员工
  493. $objMStaff = new MStaff($params['enterpriseId']);
  494. $modelResult = $objMStaff->getStaffData(['userCenterId' => $params['userCenterId'], 'deleteStatus' => StatusCode::$standard]);
  495. if(!$modelResult->isSuccess()){
  496. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  497. }
  498. $staff = $modelResult->getData();
  499. unset($modelResult);
  500. if(empty($staff)){
  501. return ResultWrapper::success($returnData);
  502. }
  503. $returnData['staffId'] = $staff['id'];
  504. $returnData['name'] = $staff['staffName'];
  505. }
  506. return ResultWrapper::success($returnData);
  507. }
  508. /**
  509. * 判断用户id是否是某企业下的
  510. * @param $userCenterId
  511. * @param $enterpriseId
  512. * @param bool $roleType
  513. * @return ResultWrapper
  514. */
  515. public function isUserCenterBindEnterprise($userCenterId, $enterpriseId, $roleType = false)
  516. {
  517. $where = [
  518. 'userCenterId' => $userCenterId,
  519. 'enterpriseId' => $enterpriseId,
  520. ];
  521. $roleType && $where['roleType'] = $roleType;
  522. $dbResult = $this->objDUserBindEnterprise->select($where);
  523. if($dbResult === false){
  524. return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
  525. }
  526. if(empty($dbResult)){
  527. return ResultWrapper::fail('账号不存在', ErrorCode::$paramError);
  528. }
  529. return ResultWrapper::success($dbResult);
  530. }
  531. /**
  532. * 自动创建
  533. * @param $data
  534. * @param $className
  535. */
  536. protected static function autoCreate($data, $className)
  537. {
  538. $i = 1;
  539. do {
  540. $postData = [
  541. 'topicName' => 'MyJob',
  542. 'topicClass' => "Jobs\Model\MTopic\Enterprise\\".$className,
  543. 'topicMethon' => 'autoCreate',
  544. 'topicMethonParams' => [
  545. 'data' => $data
  546. ],
  547. ];
  548. $url = QIANNIAO_QUEUE . '/CAddJob/add';
  549. $result = request($url, $postData);
  550. $i++;
  551. } while ($result['httpcode'] != 200 && $i <= 3);
  552. }
  553. /**
  554. * 创建企业自动创建数据
  555. * @param $userCenterId
  556. * @param $enterpriseId
  557. * @return ResultWrapper
  558. * @throws Exception
  559. */
  560. public function autoCreateData($userCenterId, $enterpriseId)
  561. {
  562. self::enterpriseSubTable();
  563. //查询企业数据
  564. $dbResult = $this->objDEnterprise->get(['id'=> $enterpriseId]);
  565. if($dbResult === false){
  566. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  567. }
  568. $enterpriseData = $dbResult;
  569. unset($dbResult);
  570. //新建合伙人
  571. // $objMShopPartner = new MShopPartner($enterpriseId, $userCenterId);
  572. // $insertShopPartnerData = [
  573. // "logo" => $enterpriseData['logo'],
  574. // "name" => $enterpriseData['enterpriseName'],//合伙名称
  575. // "model" => 1,
  576. // "provinceCode" => $enterpriseData['provinceCode'],//省编码
  577. // "cityCode" => $enterpriseData['cityCode'],//市编码
  578. // "districtCode" => $enterpriseData['districtCode'],//区编码
  579. // "address" => $enterpriseData['address'], //详细地址
  580. // "userCenterId" => $userCenterId,//负责人id
  581. // "userName" => $enterpriseData['contact'],//负责人名称
  582. // "mobile" => $enterpriseData['mobile'], //负责人电话
  583. // "enterpriseId" => $enterpriseId,
  584. // 'updateTime' => time(),
  585. // 'createTime' => time(),
  586. // ];
  587. // $modelResult = $objMShopPartner->addShopPartner($insertShopPartnerData);
  588. // if(!$modelResult->isSuccess()){
  589. // return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  590. // }
  591. // $shopPartnerId = $modelResult->getData();
  592. // unset($modelResult);
  593. //新建仓库
  594. $objMWarehouse = new MWarehouse($enterpriseId);
  595. $insertWarehouseData = [
  596. "warehouseName" => '默认仓库',
  597. "contactName" => $enterpriseData['contact'],
  598. "contactMobile" => $enterpriseData['mobile'],
  599. "provinceCode" => $enterpriseData['provinceCode'],
  600. "cityCode" => $enterpriseData['cityCode'],
  601. "districtCode" => $enterpriseData['districtCode'],
  602. "contactAddress" => $enterpriseData['address'],
  603. "costType" => 1,
  604. "deleteStatus" => StatusCode::$standard,
  605. "enableStatus" => StatusCode::$standard,
  606. 'updateTime' => time(),
  607. 'createTime' => time(),
  608. ];
  609. $modelResult = $objMWarehouse->addWarehouse($insertWarehouseData);
  610. if(!$modelResult->isSuccess()){
  611. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  612. }
  613. $warehouseId = $modelResult->getData();
  614. unset($modelResult);
  615. //新建商铺
  616. $objMShop = new MShop($enterpriseId, $userCenterId);
  617. $insertShopData = [
  618. 'logo' => $enterpriseData['logo'],//logo
  619. 'shopType' => 1,//商铺类型
  620. 'isMaster' => 5,//自动创建
  621. 'costType' => 1,//成本计算方式 1移动加权 2个别计价
  622. 'name' => '默认门店',//名称
  623. 'warehouseId' => $warehouseId,
  624. 'openTime' => '{"end": "", "start": "", "isAllDay": 1}',//营业时间
  625. 'contactName' => $enterpriseData['contact'],//联系人
  626. 'mobile' => $enterpriseData['mobile'],//联系电话
  627. // 'shopPartnerId' => $shopPartnerId,//合伙人Id
  628. "provinceCode" => $enterpriseData['provinceCode'],//省编码
  629. "cityCode" => $enterpriseData['cityCode'],//市编码
  630. "districtCode" => $enterpriseData['districtCode'],//区编码6
  631. "address" => $enterpriseData['address'], //详细地址
  632. 'salesArea' => '["0-0-0"]',
  633. 'deleteStatus' => StatusCode::$standard,
  634. 'enableStatus' => StatusCode::$standard,
  635. 'createTime' => time(),
  636. 'updateTime' => time(),
  637. ];
  638. $modelResult = $objMShop->addShop($insertShopData);
  639. if(!$modelResult->isSuccess()){
  640. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  641. }
  642. $shopId = $modelResult->getData();
  643. unset($modelResult);
  644. //新建分类
  645. $objMGoodsCategory = new MGoodsCategory($userCenterId, $enterpriseId);
  646. $insertGoodsCategory = [
  647. 'sort' => 0,//排序
  648. 'title' => '默认分类',//分类名称
  649. 'adImage' => $enterpriseData['logo'],//广告图片
  650. 'images' => $enterpriseData['logo'],//图片
  651. 'link' => '',//分类链路
  652. 'pid' => 0,
  653. 'enableStatus' => StatusCode::$standard
  654. ];
  655. $modelResult = $objMGoodsCategory->addCategory($insertGoodsCategory);
  656. if(!$modelResult->isSuccess()){
  657. return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode());
  658. }
  659. $goodsCategoryId = $modelResult->getData();
  660. unset($modelResult);
  661. //新建品牌
  662. $objMGoodsBrand = new MGoodsBrand($userCenterId,$enterpriseId);
  663. $insertGoodsBrand = [
  664. 'title' => '默认品牌',//名称
  665. 'images' => $enterpriseData['logo'],//图片
  666. 'enableStatus' => StatusCode::$standard,
  667. 'createTime' => time(),
  668. 'updateTime' => time(),
  669. ];
  670. $modelResult = $objMGoodsBrand->addBrand($insertGoodsBrand);
  671. if(!$modelResult->isSuccess()){
  672. return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode());
  673. }
  674. $goodsBrandId = $modelResult->getData();
  675. unset($modelResult);
  676. //新建部门
  677. $objMDepartment = new MDepartment($enterpriseId);
  678. $insertDepartment = [
  679. "departmentName" => '总裁办',
  680. "pid" => 0,
  681. "order" => 0,
  682. "desc" => '',
  683. "extend" => [],
  684. "updateTime" => time(),
  685. ];
  686. $insertDepartment['extend']['departmentPidPath'] = '';
  687. $insertDepartment['extend'] = json_encode($insertDepartment['extend']);
  688. $modelResult = $objMDepartment->addDepartment($insertDepartment);
  689. if(!$modelResult->isSuccess()){
  690. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  691. }
  692. $departmentId = $modelResult->getData();
  693. unset($modelResult);
  694. //新建岗位
  695. $objMRole = new MRole($enterpriseId);
  696. $insertRole = [
  697. "roleName" => '总裁',
  698. "isAdministrator" => 5,
  699. "pid" => 0,
  700. "updateTime" => time(),
  701. "acl" => null,
  702. ];
  703. $insertRole['acl'] = !empty($insertRole['acl']) ? json_encode($insertRole['acl']) : NULL;
  704. $modelResult = $objMRole->addRole($insertRole);
  705. if(!$modelResult->isSuccess()){
  706. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  707. }
  708. $roleId = $modelResult->getData();
  709. unset($modelResult);
  710. //添加职工
  711. $insertStaff = [
  712. "userCenterId" => $userCenterId, // int(10) DEFAULT NULL COMMENT '用户中心id',
  713. "departmentId" => $departmentId, //int(10) DEFAULT NULL COMMENT '部门id',
  714. "staffName" => $enterpriseData['contact'], //varchar(30) DEFAULT NULL COMMENT '员工姓名',
  715. "roleId" => $roleId, //int(10) DEFAULT NULL COMMENT '角色id',
  716. "mobile" => $enterpriseData['mobile'], //varchar(11) DEFAULT NULL COMMENT '手机号',
  717. "deleteStatus" => StatusCode::$standard, //tinyint(3) NOT NULL DEFAULT '5' COMMENT '删除状态 4:删除 5:正常',
  718. "createTime" => time(), //int(10) DEFAULT NULL COMMENT '创建时间',
  719. "updateTime" => time(), //int(10) DEFAULT NULL COMMENT '修改时间',
  720. "extend" => '{"rolePidPath": "1", "departmentPidPath": "1"}',
  721. "dataField" => '{"shopIds": [], "staffIds": [], "dataField": 3, "shopNames": [], "staffNames": []}', //json DEFAULT NULL COMMENT '数据域',
  722. ];
  723. $objDStaff = new DStaff();
  724. $objDStaff->setTable($objDStaff->get_Table().'_'.$enterpriseId);
  725. $dbResult = $objDStaff->insert($insertStaff);
  726. if($dbResult === false){
  727. return ResultWrapper::fail($objDStaff->error, ErrorCode::$dberror);
  728. }
  729. $staffId = $dbResult;
  730. unset($dbResult);
  731. // 添加排班表
  732. $objMShopRostering = new DShopRostering();
  733. $objMShopRostering->setTable($objMShopRostering->get_Table().'_'.$enterpriseId);
  734. //添加预约表
  735. $objMShopSubscribe = new DShopSubscribe();
  736. $objMShopSubscribe->setTable($objMShopSubscribe->get_Table().'_'.$enterpriseId);
  737. //添加打卡记录表
  738. // $objDShopClock = new DShopClock();
  739. // $objDShopClock->setTable($objDShopClock->get_Table().'_'.$enterpriseId);
  740. //添加默认支付方式
  741. $objMPaymentSetting = new MPaymentSetting($userCenterId, $enterpriseId);
  742. $updateEnable['id'] = 3;
  743. $updateEnable['enableStatus'] = StatusCode::$standard;
  744. $modelResult = $objMPaymentSetting->updateEnableStatus($updateEnable);
  745. if(!$modelResult->isSuccess()){
  746. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  747. }
  748. unset($modelResult);
  749. $objDCommissionSetting = new DCommissionSetting();
  750. $objDCommissionSetting->setTable('qianniao_commission_setting');
  751. //自动新增分销
  752. $commissionSettingDate = [
  753. 'enterpriseId'=>$enterpriseId,
  754. 'self'=>StatusCode::$delete
  755. ];
  756. $insert = $objDCommissionSetting->insert($commissionSettingDate);
  757. if($insert === false){
  758. return ResultWrapper::fail($objDCommissionSetting->error(), ErrorCode::$dberror);
  759. }
  760. return ResultWrapper::success('自动创建成功');
  761. }
  762. /**
  763. * 通过企业toke获取企业id
  764. * @param string $token 企业token
  765. * @return ResultWrapper
  766. */
  767. public function getEnterpriseIdByToken($token)
  768. {
  769. $this->objDEnterprise->setTable('qianniao_enterprise_1');
  770. $dbResult = $this->objDEnterprise->get_field('id', ['token'=>$token]);
  771. if($dbResult === false){
  772. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  773. }
  774. if(empty($dbResult)){
  775. return ResultWrapper::success(0);
  776. }
  777. return ResultWrapper::success($dbResult);
  778. }
  779. /**
  780. * 列表
  781. * @param $where
  782. * @return mixed
  783. */
  784. public function list($where)
  785. {
  786. $where['deleteStatus'] = 5;
  787. $where['iosAuditStatus'] = 5;
  788. $this->objDEnterprise->setTable('qianniao_enterprise_1');
  789. $dbResult = $this->objDEnterprise->select($where, '*', 'id DESC');
  790. $count = $this->objDEnterprise->count($where);
  791. if ($dbResult === false) {
  792. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  793. }
  794. $return = [
  795. 'data' => $dbResult,
  796. 'total' => $count,
  797. ];
  798. if($return === false){
  799. return ResultWrapper::fail($this->objDEnterprise->error(), ErrorCode::$dberror);
  800. }else{
  801. return ResultWrapper::success($return);
  802. }
  803. }
  804. }