12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439 |
- <?php
- /**
- * 员工管理Model
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/10/31
- * Time: 18:00
- */
- namespace JinDouYun\Model\Department;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Dao\Customer\DMemberBalanceDetail;
- use JinDouYun\Dao\Department\DStaffComment;
- use JinDouYun\Dao\Department\DStaffLadder;
- use JinDouYun\Dao\Department\DStaffLadderRecord;
- use JinDouYun\Dao\Department\DStaffReflect;
- use JinDouYun\Dao\Department\DStaffStock;
- use JinDouYun\Dao\Reward\DRewardRule;
- use JinDouYun\Dao\Shop\DShopProject;
- use JinDouYun\Dao\Shop\DShopSubscribe;
- use JinDouYun\Model\MBaseModel;
- use JinDouYun\Model\System\MBasicSetup;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use Jindouyun\Cache\EnterpriseCache;
- use JinDouYun\Cache\RoleAclCache;
- use JinDouYun\Dao\Department\DStaff;
- use JinDouYun\Dao\UserCenter\DUserCenter;
- use JinDouYun\Dao\Enterprise\DUserBindEnterprise;
- use JinDouYun\Dao\Shop\DShopBindStaff;
- use JinDouYun\Dao\Department\DStaffRewardDesc;
- use JinDouYun\Model\Shop\MShop;
- use JinDouYun\Model\Department\MRole;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Model\Department\MDepartment;
- use JinDouYun\Model\Enterprise\MEnterprise;
- use JinDouYun\Model\UserCenter\MUserCenterRegister;
- class MStaff extends MBaseModel
- {
- private $objDStaff;
- private $objDUserCenter;
- private $objDStaffRewardDesc;
- private $objDUserBindEnterprise;
- private $objDShopBindStaff;
- private $enterpriseId;
- private $userCenterId;
- private $objMUserCenterRegister;
- private $Mbais;
- private $DCustomer;
- private $DSubscribe;
- private $objMenber;
- private $objLadder;
- private $objStock;
- public function __construct($enterpriseId = '', $userCenterId = '')
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->objDStaff = new DStaff('default');
- $this->objDUserCenter = new DUserCenter('default');
- $this->objDStaffRewardDesc = new DUserCenter('default');
- $this->objDUserBindEnterprise = new DUserBindEnterprise('default');
- $this->objMUserCenterRegister = new MUserCenterRegister();
- $this->objDShopBindStaff = new DShopBindStaff('default');
- $this->objDShopBindStaff->setTable('qianniao_shop_bind_staff_' . $enterpriseId);
- $this->objDStaffRewardDesc->setTable('qianniao_staff_reward_desc_' . $enterpriseId);
- $this->objDStaff->setTable('qianniao_staff_' . $enterpriseId);
- $this->Mbais = new MBasicSetup($enterpriseId);
- $this->DCustomer = new DCustomer('default');
- $this->DCustomer->setTable('qianniao_customer_' . $enterpriseId);
- $this->DSubscribe = new DShopSubscribe('default');
- $this->DSubscribe->setTable('qianniao_shop_subscribe_' . $enterpriseId);
- $this->objMenber = new DMemberBalanceDetail('default');
- $this->objMenber->setTable('qianniao_member_balance_detail_'.$enterpriseId);
- $this->objLadder = new DStaffLadder('default');
- $this->objStock = new DStaffStock('default');
- }
- /**
- * 员工添加
- * @param $params
- * @return ResultWrapper
- */
- public function addStaff($params)
- {
- //查询userCenter
- $dbResult = $this->objDUserCenter->get(['mobile' => $params['mobile']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
- }
- $user = $dbResult;
- unset($dbResult);
- //判断手机号是否存在
- if (!empty($user)) {
- $selectWhere = [
- 'userCenterId' => $user['id']
- ];
- //判断是否有shopId 每个商铺只能添加一个userCenter
- if ($params['shopId']) {
- $selectWhere['shopId'] = $params['shopId'];
- }
- $selectWhere['deleteStatus'] = StatusCode::$standard;
- $result = $this->objDStaff->get($selectWhere);
- if ($result === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- if (!empty($result)) {
- return ResultWrapper::fail('该手机号的员工已存在', ErrorCode::$mobileishaved);
- }
- }
- $params['deleteStatus'] = StatusCode::$standard;
- $params['createTime'] = time();
- $params['updateTime'] = time();
- $beginStatus = $this->objDUserCenter->beginTransaction();
- //3.未注册手机号进行注册
- $password = '88888888';
- if (empty($user)) {
- //添加userCenter
- $userCenterAddData['mobile'] = $params['mobile'];
- $userCenterAddData['password'] = password_hash($password, PASSWORD_DEFAULT);
- $userCenterAddData['deleteStatus'] = StatusCode::$standard;
- $userCenterAddData['source'] = StatusCode::$source['manage'];
- $userCenterAddData['isCustomer'] = StatusCode::$customerType['user'];
- $userCenterAddData['createTime'] = time();
- $userCenterAddData['updateTime'] = time();
- //添加userCenter
- $dbResult = $this->objDUserCenter->insert($userCenterAddData);
- if ($dbResult === false) {
- $this->objDUserCenter->rollBack();
- return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
- }
- $params['userCenterId'] = $dbResult;
- $returnData['userCenterId'] = $dbResult;
- $returnData['title'] = '该手机号未注册, 已自动创建账号, 初始密码' . $password;
- unset($dbResult);
- } else {
- if ($user['isCustomer'] == StatusCode::$standard) {
- $updateUser = [
- 'password' => password_hash($password, PASSWORD_DEFAULT),
- 'updateTime' => time(),
- 'isCustomer' => StatusCode::$delete,//4:后台用户(优先级高,有密码) 5:小程序用户(优先级低, 无密码)
- ];
- $dbResult = $this->objDUserCenter->update($updateUser, ['id' => $user['id']]);
- if ($dbResult === false) {
- $this->objDUserCenter->rollBack();
- return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
- }
- $returnData['title'] = '该手机号未注册, 已自动创建账号, 初始密码' . $password;
- } else {
- $returnData['title'] = '该手机号号已注册, 绑定成功';
- }
- $params['userCenterId'] = $user['id'];
- $returnData['userCenterId'] = $user['id'];
- }
- //查询绑定表
- $where = [
- 'userCenterId' => $params['userCenterId'],
- 'enterpriseId' => $this->enterpriseId,
- 'roleType' => StatusCode::$roleType['staff'],
- ];
- if (isset($params['shopId']) && !empty($params['shopId'])) {
- $where['shopId'] = $params['shopId'];
- }
- $result = $this->objDUserBindEnterprise->get($where);
- if ($result === false) {
- $this->objDUserCenter->rollBack();
- return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
- }
- if (empty($result)) {
- $bindData['userCenterId'] = $params['userCenterId'];
- $bindData['enterpriseId'] = $this->enterpriseId;
- $bindData['roleType'] = StatusCode::$roleType['staff'];
- $bindData['updateTime'] = time();
- $bindData['createTime'] = time();
- if (isset($params['shopId']) && !empty($params['shopId'])) {
- $bindData['shopId'] = $params['shopId'];
- }
- $dbResult = $this->objDUserBindEnterprise->insert($bindData);
- if ($dbResult === false) {
- $this->objDUserCenter->rollBack();
- return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- }
- //新增员工表
- $dbResult = $this->objDStaff->insert($params);
- if ($dbResult === false) {
- $this->objDUserCenter->rollBack();
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $beginStatus && $this->objDUserCenter->commit();
- $objRoleAclCache = new RoleAclCache();
- $objRoleAclCache->cacheStaffIdAndUserCenterId($this->enterpriseId, $params['userCenterId'], $dbResult);
- $objRoleAclCache->cacheUserCenterIdAndStaffId($this->enterpriseId, $dbResult, $params['userCenterId']);
- return ResultWrapper::success($returnData);
- }
- /**
- * 员工删除
- * @param array $where
- * @return ResultWrapper
- */
- public function deleteStaff($where = [])
- {
- $where['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDStaff->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $staff = $dbResult;
- unset($dbResult);
- if (empty($staff)) {
- return ResultWrapper::fail('员工不存在', ErrorCode::$paramError);
- }
- //查询是否有员工角色
- $dbResult = $this->objDUserBindEnterprise->select(['userCenterId' => $staff['userCenterId'], 'enterpriseId' => $this->enterpriseId, 'roleType' => StatusCode::$roleType['staff']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$paramError);
- }
- $bind = $dbResult;
- unset($dbResult);
- if (empty($bind)) {
- return ResultWrapper::fail('员工不存在', ErrorCode::$paramError);
- }
- //删除员工绑定企业表的关系
- $bindWhere = [
- 'userCenterId' => $staff['userCenterId'],
- 'enterpriseId' => $this->enterpriseId,
- 'roleType' => StatusCode::$roleType['staff'],
- 'shopId' => 0,
- ];
- if (isset($where['shopId'])) $bindWhere['shopId'] = $where['shopId'];
- $dbResult = $this->objDUserBindEnterprise->delete($bindWhere);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $update = [
- 'deleteStatus' => StatusCode::$delete,
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDStaff->update($update, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取员工信息
- * @param array $where
- * @return ResultWrapper
- */
- public function getStaffData($where = [])
- {
- $dbResult = $this->objDStaff->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取员工信息
- * @param array $where
- * @return ResultWrapper
- */
- public function getAllStaffData($where = [])
- {
- $dbResult = $this->objDStaff->select($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取员工信息
- * @param array $where
- * @return ResultWrapper
- */
- public function getAllStaffDatas($where = [])
- {
- $dbResult = $this->objDStaff->select($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(self::formatList($dbResult));
- }
- /**
- * 员工修改
- * @param $params
- * @param $idWhere
- * @return ResultWrapper
- */
- public function updateStaff($params, $idWhere)
- {
- unset($params['userCenterId']);
- $dbResult = $this->objDStaff->get($idWhere);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $staff = $dbResult;
- unset($dbResult);
- if (empty($staff)) {
- return ResultWrapper::fail('员工不存在', ErrorCode::$paramError);
- }
- //编辑员工不支持编辑手机号 (例:修改员工手机号 编辑了18888888888) 修改手机号需要本人登录修改
- /* if(isset($params['mobile']) && !empty($params['mobile'])){
- $dbResult = $this->objDUserCenter->get(['id' => $staff['userCenterId']]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$paramError);
- }
- $user = $dbResult;
- unset($dbResult);
- if($user['mobile'] != $params['mobile']){
- $dbResult = $this->objDUserCenter->update(['mobile' => $params['mobile']], ['id' => $staff['userCenterId']]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$paramError);
- }
- }
- }*/
- unset($params['mobile']);
- $dbResult = $this->objDStaff->update($params, $idWhere);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 修改密码
- * @param $params
- * @return ResultWrapper
- */
- public function updatePassword($params)
- {
- $dbResult = $this->objDUserCenter->get(['id' => $params['id']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
- }
- $isPassword = password_verify($params['oldPassword'], $dbResult['password']);
- if (!$isPassword) {
- return ResultWrapper::fail('旧密码错误', ErrorCode::$paramError);
- }
- $dbResult = $this->objDUserCenter->update(['password' => $params['password']], ['id' => $params['id']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDUserCenter->error(), ErrorCode::$dberror);
- } else {
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 员工列表
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllStaff($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $this->objDStaff->setTable('qianniao_department_role_' . $this->enterpriseId);
- $this->objDStaff->setTable('qianniao_staff_' . $this->enterpriseId);
- $fields = 's.*,r.signId,b.staffName as spread_name';
- $sql = 'SELECT ' . $fields . ' FROM qianniao_staff_' . $this->enterpriseId . ' AS s
- LEFT JOIN qianniao_department_role_' . $this->enterpriseId . ' AS r ON r.id = s.roleId
- LEFT JOIN qianniao_staff_' . $this->enterpriseId . ' AS b ON s.spread_uid = b.id
- WHERE s.deleteStatus = ' . StatusCode::$standard . ' AND s.is_shareholder = 0';
- if (isset($selectParams['staffName'])) {
- $sql .= ' AND s.staffName LIKE "%' . $selectParams['staffName'] . '%"' . ' or s.mobile LIKE "%' . $selectParams['staffName'] . '%"';
- }
- if (isset($selectParams['roleId'])) {
- $sql .= ' AND s.roleId =' . $selectParams['roleId'];
- }
- if (isset($selectParams['departmentId'])) {
- $sql .= ' AND s.departmentId =' . $selectParams['departmentId'];
- }
- if (isset($selectParams['signId']) && !empty($selectParams['signId'])) {
- $sql .= ' AND r.signId =' . $selectParams['signId'];
- }
- if (isset($selectParams['shopId']) && !empty($selectParams['shopId'])) {
- $sql .= ' AND s.shopId =' . $selectParams['shopId'];
- }
- $total = count((array)$this->objDStaff->query($sql));
- $sql .= ' ORDER BY createTime DESC LIMiT ' . $offset . ',' . $limit;
- $dbResult = $this->objDStaff->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $db = new DStaffComment('default');
- foreach ($dbResult as &$item)
- {
- $sql = "SELECT AVG(score) as score FROM qianniao_staff_comment WHERE uid = ".$item['id'].' and en_id = '.$this->enterpriseId;
- $score = $db->query($sql);
- $score = $score[0]['score'];
- if ($score){
- $item['evaluate'] = $score;
- }
- if ($item['spread_uid']){
- $staff = $this->objDStaff->get(['id' => $item['spread_uid']]);
- $item['spread_name'] = $staff['staffName'];
- }
- }
- $return = [
- 'data' => self::formatList($dbResult),
- 'total' => ($total) ? intval($total) : 0,
- ];
- if ($return === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- } else {
- return ResultWrapper::success($return);
- }
- }
- /**
- * 员工详情
- * @param $params
- * @return ResultWrapper
- */
- public function getStaffInfo($params)
- {
- if (empty($params)) {
- return ResultWrapper::success($params);
- }
- $dbResult = $this->objDStaff->get($params);
- $dbResult['sold'] = $this->DSubscribe->count(['uid' => $dbResult['id'], 'paid' => 1]);
- $db = new DStaffComment('default');
- $sql = "SELECT AVG(score) as score FROM qianniao_staff_comment WHERE uid = ".$dbResult['id'].' and en_id = '.$this->enterpriseId;
- $score = $db->query($sql);
- $dbResult['evaluate'] = $score[0]['score'];
- if ($score){
- $item['evaluate'] = $score;
- }
- $dbs = new DShopSubscribe('default');
- $dbs->setTable('qianniao_shop_subscribe_'.$this->enterpriseId);
- $firstDay = strtotime(date('Y-m-01', time()));
- $lastDay = strtotime(date('Y-m-01', time()). ' 1 month -1 day');
- $count = $dbs->get([['paid', '=', 1], ['createTime', '>=', $firstDay], ['createTime', '<=', $lastDay], ['status', '=', 1], ['uid' ,'=', $dbResult['id']]], 'sum(to_price) as to_price')['to_price'];
- $dbResult['order_count'] = $count;
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- if ($dbResult) {
- return ResultWrapper::success(self::formatInfo($dbResult));
- }
- }
- /**
- * 根据userCenterId获取员工详情
- * @param $userCenterId
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getStaffDataByUserCenterId($userCenterId)
- {
- //获取当前用户的身份信息
- $dbResult = $this->objDUserBindEnterprise->get(['userCenterId' => $userCenterId, 'enterpriseId' => $this->enterpriseId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDUserBindEnterprise->error(), ErrorCode::$dberror);
- }
- $userCenterBind = $dbResult;
- unset($dbResult);
- $objMEnterprise = new MEnterprise();
- $modelResult = $objMEnterprise->getEnterpriseDataByEnterpriseId($this->enterpriseId);
- if (!$modelResult->isSuccess()) {
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $enterpriseData = $modelResult->getData();
- unset($modelResult);
- $returnData = [];
- if ($userCenterBind['roleType'] == StatusCode::$roleType['admin']) {
- //管理员
- //管理员名称和信息就在企业信息里
- $returnData = [
- 'enterpriseId' => $this->enterpriseId,
- 'enterpriseLogo' => $enterpriseData['logo'],
- 'enterpriseName' => $enterpriseData['enterpriseName'],
- 'userName' => $enterpriseData['contact'],
- 'departmentName' => '企业管理员',
- 'roleName' => '企业管理员'
- ];
- } elseif ($userCenterBind['roleType'] == StatusCode::$roleType['staff']) {
- //员工
- $dbResult = $this->objDStaff->get(['userCenterId' => $userCenterId]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $result = $dbResult;
- unset($dbResult);
- $returnData = [
- 'enterpriseId' => $this->enterpriseId,
- 'enterpriseLogo' => $enterpriseData['logo'],
- 'enterpriseName' => $enterpriseData['enterpriseName'],
- 'userName' => $result['staffName'],
- 'departmentName' => isset($result['departmentName']) ? $result['departmentName'] : '',
- 'roleName' => isset($result['roleName']) ? $result['roleName'] : ''
- ];
- } elseif ($userCenterBind['roleType'] == StatusCode::$roleType['customer']) {
- //客户
- $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
- $modelResult = $objMCustomer->getCustomerDataByUserCenterIdAndEnterpriseId($userCenterId, $this->enterpriseId);
- if (!$modelResult->isSuccess()) {
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $result = $modelResult->getData();
- unset($modelResult);
- $returnData = [
- 'enterpriseId' => $this->enterpriseId,
- 'enterpriseLogo' => $enterpriseData['logo'],
- 'enterpriseName' => $enterpriseData['enterpriseName'],
- 'userName' => $result['name'],
- 'departmentName' => '企业客户',
- 'roleName' => '企业客户'
- ];
- }
- return ResultWrapper::success($returnData);
- }
- /**
- * 格式化员工详情
- * @param $data
- * @return mixed
- */
- public function formatInfo($data)
- {
- if (empty($data)) return $data;
- //取出部门id 角色id
- $departmentId = $data['departmentId'];
- $roleId = $data['roleId'];
- //查询部门 id 名称
- $MDepartment = new MDepartment($this->enterpriseId);
- $departmentData = $MDepartment->departmentDataByDepartmentId($departmentId);
- $data['departmentName'] = isset($departmentData['departmentName']) ? $departmentData['departmentName'] : '';
- //查询角色 id 名称
- $MRole = new MRole($this->enterpriseId);
- $roleData = $MRole->roleDataByRoleId($roleId);
- $data['roleName'] = isset($roleData['roleName']) ? $roleData['roleName'] : '';
- $data['extend'] = json_decode($data['extend'], true);
- $data['dataField'] = json_decode($data['dataField'], true);
- $data['dataField']['shopNames'] = [];
- if (isset($data['dataField']['shopIds'])) {
- $objMShop = new MShop($this->enterpriseId, $this->userCenterId);
- $shops = $objMShop->getShopName($data['dataField']['shopIds'], 'id,name');
- foreach ($shops as $shop) {
- $data['dataField']['shopNames'][] = $shop['name'];
- }
- }
- $data['dataField']['staffNames'] = [];
- if (isset($data['dataField']['staffIds'])) {
- $staffs = self::getStaffDataByStaffIds($data['dataField']['staffIds']);
- foreach ($staffs as $staff) {
- $data['dataField']['staffNames'][] = $staff['staffName'];
- }
- }
- //总收益
- $data['rewardData'] = [
- 'rewardTotal' => [
- 'total' => $data['rewardTotal'],
- 'order' => $data['rewardOrder'],
- 'goods' => $data['rewardGoods'],
- ]
- ];
- //查询月收益
- $startTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
- $endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
- $sql = 'select `source`,sum(amount) as total from qianniao_staff_reward_desc_' . $this->enterpriseId . ' where staffId = ' . $data['id'] . ' and type = 5 and createTime between ' . $startTime . ' and ' . $endTime . ' group by `source`';
- $dbResult = $this->objDStaffRewardDesc->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaffRewardDesc->error(), ErrorCode::$dberror);
- }
- $reward = [
- 1 => 0,
- 2 => 0,
- ];
- foreach ($dbResult as $value) {
- empty($value['total']) && $value['total'] = 0;
- $reward[$value['source']] = isset($reward[$value['source']]) ? $reward[$value['source']] += $value['total'] : $value['total'];
- }
- unset($dbResult);
- $data['rewardData']['rewardMonth'] = [
- 'total' => bcadd($reward[1], $reward[2]),
- 'order' => $reward[1],
- 'goods' => $reward[2]
- ];
- //查询日收益
- $startTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
- $endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
- $sql = 'select `source`,sum(amount) as total from qianniao_staff_reward_desc_' . $this->enterpriseId . ' where staffId = ' . $data['id'] . ' and type = 5 and createTime between ' . $startTime . ' and ' . $endTime . ' group by `source`';
- $dbResult = $this->objDStaffRewardDesc->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaffRewardDesc->error(), ErrorCode::$dberror);
- }
- $reward = [
- 1 => 0,
- 2 => 0,
- ];
- foreach ($dbResult as $value) {
- empty($value['total']) && $value['total'] = 0;
- $reward[$value['source']] = isset($reward[$value['source']]) ? $reward[$value['source']] += $value['total'] : $value['total'];
- }
- $data['rewardData']['rewardDay'] = [
- 'total' => bcadd($reward[1], $reward[2]),
- 'order' => $reward[1],
- 'goods' => $reward[2]
- ];
- $data['staffCode'] = createCode(StatusCode::$code['staff']['prefix'], $data['id'], StatusCode::$code['staff']['length']);
- $data['rule'] = empty($data['rule']) ? (object)[] : json_decode($data['rule'], true);
- return $data;
- }
- /**
- * 格式化员工列表数据
- * @param $data
- * @return mixed
- */
- public function formatList(array $data)
- {
- $departmentIds = [];
- $roleIds = [];
- $userCenterIds = [];
- $shopIds = [];
- //取出部门id 角色id 商品id
- foreach ($data as $key => $value) {
- if (!in_array($value['departmentId'], $departmentIds)) {
- $departmentIds[] = $value['departmentId'];
- }
- if (!in_array($value['roleId'], $roleIds)) {
- $roleIds[] = $value['roleId'];
- }
- if (!in_array($value['shopId'], $shopIds)) {
- $shopIds[] = $value['shopId'];
- }
- $userCenterIds[] = $value['userCenterId'];
- }
- //查询所有部门 id 名称
- $MDepartment = new MDepartment($this->enterpriseId);
- $departmentData = $MDepartment->departmentDataByDepartmentIds($departmentIds);
- //映射 key = id
- $departmentNames = [];
- foreach ($departmentData as $key => $v) {
- $departmentNames[$v['id']] = $v['departmentName'];
- }
- //查询所有店铺id名称
- $MShop = new MShop($this->enterpriseId);
- $shopData = $MShop->getShopNameByIds($shopIds);
- //映射 key==id、
- $shopNames = [];
- foreach ($shopData as $key => $v) {
- $shopNames[$v['id']] = $v['name'];
- }
- //查询所有角色 id 名称
- $MRole = new MRole($this->enterpriseId);
- $roleData = $MRole->roleDataByRoleIds($roleIds);
- //映射 key = id
- $roleNames = [];
- foreach ($roleData as $key => $v) {
- $roleNames[$v['id']] = $v['roleName'];
- }
- //手机号
- $userData = [];
- if (!empty($userCenterIds)) {
- $objDUserCenter = new DUserCenter();
- $dbResult = $objDUserCenter->select(['id' => $userCenterIds]);
- if ($dbResult) {
- foreach ($dbResult as $value) {
- $userData[$value['id']] = $value['mobile'];
- }
- }
- }
- foreach ($data as &$v) {
- $v['departmentName'] = isset($departmentNames[$v['departmentId']]) ? $departmentNames[$v['departmentId']] : '';
- $v['roleName'] = isset($roleNames[$v['roleId']]) ? $roleNames[$v['roleId']] : '';
- $v['shopName'] = isset($shopNames[$v['shopId']]) ? $shopNames[$v['shopId']] : '';
- $v['extend'] = json_decode($v['extend'], true);
- $v['dataField'] = json_decode($v['dataField'], true);
- $v['staffCode'] = createCode(StatusCode::$code['staff']['prefix'], $v['id'], StatusCode::$code['staff']['length']);
- $v['rule'] = empty($v['rule']) ? (object)[] : json_decode($v['rule'], true);
- $v['mobile'] = isset($userData[$v['userCenterId']]) ? $userData[$v['userCenterId']] : '';
- }
- return $data;
- }
- /**
- * 根据员工ids获取员工id和员工名称
- * @param array $ids
- * @return bool|ResultWrapper
- */
- public function getStaffDataByStaffIds(array $ids)
- {
- //查询所有 id 员工名称
- $where = [
- 'id' => array_values($ids),
- 'deleteStatus' => StatusCode::$standard,
- ];
- $fields = 'id,staffName,mobile';
- $departmentData = $this->objDStaff->select($where, $fields);
- if ($departmentData === false) {
- return [];
- }
- return $departmentData;
- }
- /**
- * 单个业务员Id查询名称和电话
- * @param $staffId
- * @return string
- */
- public function getNameById($staffId)
- {
- $return = [];
- if (!$staffId) {
- return $return;
- }
- $sql = 'select b.mobile as userMobile, a.* from qianniao_staff_' . $this->enterpriseId . ' as a left join qianniao_user_center as b on a.userCenterId = b.id where a.id =' . $staffId;
- $dbResult = $this->objDStaff->query($sql);
- if ($dbResult === false || empty($dbResult)) {
- return $return;
- }
- $return = $dbResult[0];
- return $return;
- }
- /**
- * 根据userCenterId获取员工名称
- * @param $userCenterId
- * @return ResultWrapper|string
- */
- public function getStaffInfoByUserCenterId($userCenterId)
- {
- $dbResult = $this->objDStaff->get(['userCenterId' => $userCenterId], 'id,staffName');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 根据多个userCenterId获取员工信息
- * @param $userCenterId
- * @return ResultWrapper
- */
- public function getStaffByUserCenterIds($userCenterId)
- {
- if (empty($userCenterId)) {
- return ResultWrapper::success($userCenterId);
- }
- $dbResult = $this->objDStaff->select(['userCenterId' => $userCenterId, 'deleteStatus' => StatusCode::$standard], 'userCenterId,staffName,mobile');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取当前登录员工信息
- * @param $userId
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getStaffByToken($userId)
- {
- //获取redis以userCenterId为key的数据
- //如果有数据 返回读出来的数据
- //如果没有数据 就去数据库里查询 把查询出来的数据保存到redis
- $objMEnterprise = new MEnterprise();
- $enterpriseInfo = $objMEnterprise->getEnterpriseByUserId($userId);
- $staffData = $this->objDStaff->get(['userCenterId' => $userId], 'staffName,mobile,departmentId,roleId');
- if ($staffData === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $userInfo = [
- 'staffName' => $staffData['staffName'],
- 'mobile' => $staffData['mobile'],
- 'departmentId' => $staffData['departmentId'],
- 'roleId' => $staffData['roleId'],
- 'enterpriseInfo' => $enterpriseInfo,
- ];
- return ResultWrapper::success($userInfo);
- }
- /**
- * 根据企业id获取企业下员工总数
- * @param $enterpriseId
- * @return ResultWrapper
- * @throws \Exception
- */
- public function getStaffTotalByEnterpriseId($enterpriseId)
- {
- if (empty($enterpriseId)) return ResultWrapper::fail('参数为空', ErrorCode::$paramError);
- $cacheEnterpriseCache = new EnterpriseCache();
- $cacheResult = $cacheEnterpriseCache->delStaffTotalByEnterpriseId($enterpriseId);
- unset($cacheResult);
- $cacheResult = $cacheEnterpriseCache->getStaffTotalByEnterpriseId($enterpriseId);
- if ($cacheResult) {
- return ResultWrapper::success($cacheResult);
- }
- $this->objDStaff->setTable('qianniao_staff_' . $enterpriseId);
- $dbResult = $this->objDStaff->count(['deleteStatus' => StatusCode::$standard]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- if (empty($dbResult)) {
- $returnData = 0;
- } else {
- $returnData = $dbResult;
- }
- unset($cacheResult);
- $cacheResult = $cacheEnterpriseCache->setStaffTotalByEnterpriseId($enterpriseId, $returnData);
- return ResultWrapper::success($returnData);
- }
- /**
- * 获取商铺下的员工
- * @param $params
- * @return ResultWrapper
- */
- public function getStaffByShopId($params)
- {
- $whereSql = ' and s.deleteStatus = ' . StatusCode::$standard;
- $sql = 'select s.*,u.mobile from qianniao_staff_' . $this->enterpriseId . ' s left join qianniao_user_center u on s.userCenterId = u.id where s.shopId = ' . $params['shopId'] . $whereSql . ' order by s.createTime ASC limit ' . $params['offset'] . ',' . $params['limit'];
- $dbResult = $this->objDStaff->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $data = $dbResult;
- unset($dbResult);
- $countSql = 'select count(*) as count from qianniao_staff_' . $this->enterpriseId . ' s left join qianniao_user_center u on s.userCenterId = u.id where s.shopId = ' . $params['shopId'] . $whereSql;
- $dbResult = $this->objDStaff->query($countSql);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $count = array_shift($dbResult)['count'];
- $return = [
- 'data' => self::formatList($data),
- 'total' => $count
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 提成流水
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllStaffRewardDesc($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $params = $selectParams;
- $selectParams = self::formatSqlWhere($selectParams);
- $dbResult = $this->objDStaffRewardDesc->select($selectParams, '*', 'createTime desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaffRewardDesc->error(), ErrorCode::$dberror);
- }
- $countResult = $this->objDStaffRewardDesc->count($selectParams);
- if ($countResult === false) {
- return ResultWrapper::fail($this->objDStaffRewardDesc->error(), ErrorCode::$dberror);
- }
- unset($params['source']);
- $params['type'] = 4;
- $zc = $this->objDStaffRewardDesc->get(self::formatSqlWhere($params), 'sum(amount) as amount');
- $params['type'] = 5;
- $sr = $this->objDStaffRewardDesc->get(self::formatSqlWhere($params), 'sum(amount) as amount');
- $return = [
- 'data' => self::formatRewardDesc($dbResult),
- 'total' => ($countResult) ? intval($countResult) : 0,
- 'zc' => $zc,
- 'sr' => $sr,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 格式化提成流水
- * @param $data
- * @return mixed
- */
- public function formatRewardDesc($data)
- {
- $staffIds = array_column($data, 'staffId');
- $dbResult = $this->objDStaff->select(['id' => $staffIds]);
- $staff = [];
- if ($dbResult) {
- foreach ($dbResult as $value) {
- $staff[$value['id']] = $value;
- }
- }
- foreach ($data as &$value) {
- $value['staffName'] = isset($staff[$value['id']]) ? $staff[$value['id']]['staffName'] : '';
- }
- unset($value);
- return $data;
- }
- /**
- * 员工修改
- * @param $update
- * @param $where
- * @return ResultWrapper
- */
- public function updateStaffData($update, $where)
- {
- $dbResult = $this->objDStaff->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $staff = $dbResult;
- unset($dbResult);
- if (isset($update['rewardGo']) && !empty($update['rewardGo'])) {
- $update['rewardOut'] = bcadd($staff['rewardOut'], $update['rewardGo']);
- $update['reward'] = bcsub($staff['reward'], $update['rewardGo']);
- if ($update['reward'] < 0) {
- return ResultWrapper::fail('余额不足', ErrorCode::$paramError);
- }
- //增加记录
- $insertRewardDesc = [
- 'userCenterId' => $staff['userCenterId'],
- 'staffId' => $staff['id'],
- 'shopId' => $staff['shopId'],
- 'title' => '发放提成',
- 'amount' => $update['rewardGo'],
- 'changeAmount' => $update['reward'],
- 'type' => StatusCode::$delete,
- 'source' => 4,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDStaffRewardDesc->insert($insertRewardDesc);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaffRewardDesc->error(), ErrorCode::$dberror);
- }
- }
- unset($update['rewardGo']);
- $update['updateTime'] = time();
- $dbResult = $this->objDStaff->update($update, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- public function clockReward($price, $balance, $uid, $staff_id, $shopId, $rewardTotal)
- {
- $insertRewardDesc = [
- 'userCenterId' => $uid,
- 'staffId' => $staff_id,
- 'shopId' => $shopId,
- 'title' => '发放打卡提成',
- 'amount' => $price,
- 'changeAmount' => $balance,
- 'type' => 5,
- 'source' => 4,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDStaffRewardDesc->insert($insertRewardDesc);
- $this->objDStaff->update(['reward' => $balance, 'rewardTotal' => $rewardTotal], $staff_id);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 发放员工提成
- * @param $data
- * @param $shopId
- * @return mixed
- */
- public function service($data, $shopId)
- {
- $this->objDStaff->beginTransaction();
- $commission = $this->Mbais->getBasicField('commission')->getData();
- $Mproject = new DShopProject('default');
- $project = $Mproject->select([['id', 'in', $data['project']]]);
- $price = 0;
- foreach ($project as $item){
- $price += $item['commission'];
- }
- // $price = $data['pay_price'] * ($commission['commission'] / 100);//提成
- $staff = $this->objDStaff->get(['id' => $data['uid']]);
- $spread_staff = $this->objDStaff->get(['id' => $staff['spread_uid']]);
- $balance = $staff['reward'] + $price;
- $rewardTotal = $staff['rewardTotal'] + $price;
- $insertRewardDesc = [
- 'userCenterId' => $staff['userCenterId'],
- 'staffId' => $staff['id'],
- 'shopId' => $shopId,
- 'customerId' => $data['customer_id'],
- 'title' => '服务项目提成',
- 'amount' => $price,
- 'originId' => $data['id'],
- 'changeAmount' => $balance,
- 'type' => 5,
- 'source' => 4,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- if ($spread_staff) {
- $db = new MBasicSetup($this->enterpriseId);
- $staff_push = $db->getBasicField('staff_push')->getData();
- $spread_balance = $spread_staff['reward'] + ($price * ($staff_push['staff_push'] /100));
- $spread_rewardTotal = $spread_staff['rewardTotal'] + ($price * ($staff_push['staff_push'] /100));
- $insert = [
- 'userCenterId' => $spread_staff['userCenterId'],
- 'staffId' => $spread_staff['id'],
- 'shopId' => $shopId,
- 'customerId' => $data['customer_id'],
- 'title' => '推荐员工服务项目提成',
- 'amount' => $price * 0.1,
- 'originId' => $data['id'],
- 'changeAmount' => $spread_balance,
- 'type' => 5,
- 'source' => 4,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $res = $this->objDStaffRewardDesc->insert($insert);
- $this->objDStaff->update(['reward' => $spread_balance, 'rewardTotal' => $spread_rewardTotal], $spread_balance['id']);
- }
- $dbResult = $this->objDStaffRewardDesc->insert($insertRewardDesc);
- $this->objDStaff->update(['reward' => $balance, 'rewardTotal' => $rewardTotal], $staff['id']);
- if ($dbResult === false) {
- $this->objDStaff->rollBack();
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $this->objDStaff->commit();
- return ResultWrapper::success($dbResult);
- }
- /**
- * 发放客户推荐奖
- * @param $data
- * @param $shopId
- * @return bool
- */
- public function recommend($data, $shopId)
- {
- $customer = $this->DCustomer->get(['id' => $data['customer_id']]);
- if ($customer['recommenderId'] > 0 and $customer['recommenderType'] == 3) {
- $this->DCustomer->beginTransaction();
- $recommender = $this->DCustomer->get(['id' => $customer['recommenderId']]); //推荐人
- $count = $this->DSubscribe->count(['customer_id' => $data['customer_id']]);
- $price = $data['pay_price'] - $data['cost_price'];// 利润
- if ($recommender['is_shareholder'] == 1) {
- $shareholder_extension = $this->Mbais->getBasicField('shareholder_extension')->getData();//股东提成
- //推荐人是股东
- if ($count == 1) {
- //如果是第一单提成30%
- $commission = $price * 0.3; //提成
- $before = $recommender['memberBalance'];
- $after = $recommender['memberBalance'] + $commission;
- }elseif ($count > 1){
- //如果是第一单提成30%
- $commission = $price * ($shareholder_extension['shareholder_extension']/100); //提成
- $before = $recommender['memberBalance'];
- $after = $recommender['memberBalance'] + $commission;
- }
- } else {
- //推荐人不是股东
- $customer_extension = $this->Mbais->getBasicField('customer_extension')->getData();//客户提成
- $commission = $price * ($customer_extension['customer_extension']/100); //提成
- $before = $recommender['memberBalance'];
- $after = $recommender['memberBalance'] + $commission;
- }
- $memberInsert = [
- 'customerId' => $recommender['id'],
- 'userCenterId' => $recommender['userCenterId'],
- 'type' => 5,
- 'purpose' => '转介绍奖励',
- 'money' => $commission,
- 'afterMoney' => $after,
- 'beforeMoney' => $before,
- 'remark' => '用户'.$customer['name'].'消费,转介绍奖励',
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $res = $this->objMenber->insert($memberInsert);
- $res1 = $this->DCustomer->update(['memberBalance' => $after], $recommender['id']);
- if ($res and $res1){
- $this->DCustomer->commit();
- return ResultWrapper::success('成功');
- }
- $this->DCustomer->rollBack();
- return ResultWrapper::fail('发放失败', ErrorCode::$dberror);
- }
- return true;
- }
- /**
- * 发放奖励股权
- * @param $id
- * @return void
- */
- public function stock($id)
- {
- $db = new DStaffLadderRecord('default');
- $start = strtotime( date('Y-m-01', time()));
- $end = strtotime(date('Y-m-t 23:59:59', time()));
- $wherein[] = ['createTime', '>=', $start];
- $wherein[] = ['createTime', '<=', $end];
- $wherein[] = ['uid', '=', $id];
- $wherein[] = ['status', '=', 1];
- $count = $this->DSubscribe->get($wherein, 'sum(to_price) as to_price');
- $where = [
- 'enterpriseId' => $this->enterpriseId,
- ];
- $ladder = $this->objLadder->select($where, '*', 'number ASC');
- if ($ladder){
- foreach ($ladder as &$item){
- $wheres = [
- ['createTime', '>=', $start],
- ['createTime', '<=', $end],
- ['staff_id', '=', $id],
- ['ladder_id', '=', $item['id']],
- ];
- $record = $db->get($wheres);
- if (empty($record)){
- if ($count['to_price'] > $item['number']){
- $staff = $this->objDStaff->get(['id'=> $id]);
- $stock = $staff['cumulative_stock_right'];
- $use_stock = $staff['use_stock'];
- $before = $staff['use_stock'];
- $stock += $item['stock'];//增加累计股权
- $use_stock += $item['stock']; //增加可使用股权
- $insert = [
- 'en_id' => $this->enterpriseId,
- 'staff_id' => $id,
- 'pm' => 1,
- 'number' => $item['stock'],
- 'before' => $before,
- 'after' => $use_stock,
- 'mark' => $item['name'].'达标发放股权奖励',
- ];
- $this->objStock->insert($insert);
- $db->insert([
- 'en_id' =>$this->enterpriseId,
- 'staff_id' => $id,
- 'ladder_id' => $item['id'],
- 'createTime' => time()
- ]);
- $this->objDStaff->update(['cumulative_stock_right' => $stock, 'use_stock' => $use_stock]);
- }
- }
- }
- }
- }
- /**
- * 根据订单增加员工业绩提成
- * @param int $shopId
- * @param array $customer
- * @param array $order
- * @param array $orderGoods
- * @return ResultWrapper
- */
- public function addStaffRewardByOrder($shopId = 0, $customer = [], $order = [], $orderGoods = [])
- {
- if (empty($customer['salesManId'])) {
- return ResultWrapper::success(false);
- }
- //查询员工
- $dbResult = $this->objDStaff->get(['id' => $customer['salesManId'], 'deleteStatus' => StatusCode::$standard]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- $staff = $dbResult;
- unset($dbResult);
- if (empty($staff)) {
- return ResultWrapper::success(false);
- }
- //查询规则
- $objDRewardRule = new DRewardRule();
- $objDRewardRule->setTable('qianniao_reward_rule_' . $this->enterpriseId);
- $dbResult = $objDRewardRule->get(['id' => $staff['rewardRuleId'], 'enableStatus' => StatusCode::$standard, 'deleteStatus' => StatusCode::$standard], '*', 'createTime DESC');
- if ($dbResult === false) {
- return ResultWrapper::fail($objDRewardRule->error(), ErrorCode::$dberror);
- }
- $rule = $dbResult;
- unset($dbResult);
- if (empty($rule)) {
- return ResultWrapper::success(false);
- }
- $ruleGoods = json_decode($rule['goods'], true);
- //匹配商品
- $goodsReward = 0;
- $orderAmount = 0;
- foreach ($orderGoods as $value) {
- if (isset($ruleGoods[$value['skuId']])) {
- //计算商品
- if ($ruleGoods[$value['skuId']]['money'] > 0) {
- $goodsReward = bcadd($goodsReward, bcmul($ruleGoods[$value['skuId']]['money'], $value['buyNum']), 2);
- }
- if ($rule['computed'] == StatusCode::$standard) {
- $orderAmount = bcadd($orderAmount, $value['totalMoney'], 2);
- }
- } else {
- $orderAmount = bcadd($orderAmount, $value['totalMoney'], 2);
- }
- }
- $insertRewardDesc = [];
- if ($goodsReward > 0) {
- //增加记录
- $insertRewardDesc[] = [
- 'userCenterId' => $staff['userCenterId'],
- 'customerId' => $customer['id'],
- 'staffId' => $staff['id'],
- 'shopId' => $shopId,
- 'originId' => $order['id'],
- 'originNo' => $order['no'],
- 'ruleId' => $rule['id'],
- 'title' => '客户购买指定商品增加提成',
- 'amount' => $goodsReward,
- 'changeAmount' => bcadd($staff['reward'], $goodsReward, 2),
- 'type' => StatusCode::$standard,
- 'source' => 2,
- 'createTime' => time() - 1,
- 'updateTime' => time() - 1,
- ];
- }
- //计算订单金额
- $orderReward = 0;
- if ($orderAmount > 0 && $rule['percentage'] > 0) {
- $orderReward = bcmul(bcdiv($orderAmount, 100), $rule['percentage'], 2);
- //增加记录
- $insertRewardDesc[] = [
- 'userCenterId' => $staff['userCenterId'],
- 'customerId' => $customer['id'],
- 'staffId' => $staff['id'],
- 'shopId' => $shopId,
- 'originId' => $order['id'],
- 'originNo' => $order['no'],
- 'ruleId' => $rule['id'],
- 'title' => '客户下单增加提成',
- 'amount' => $orderReward,
- 'changeAmount' => bcadd($staff['reward'], bcadd($orderReward, $goodsReward, 2), 2),
- 'type' => StatusCode::$standard,
- 'source' => 1,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- }
- //累加金额
- $addReward = bcadd($orderReward, $goodsReward, 2);
- //增加员工金额
- if ($addReward > 0) {
- $updateStaff = [
- 'completed' => bcadd($staff['completed'], $order['payAmount'], 2),
- 'reward' => bcadd($staff['reward'], $addReward, 2),
- 'rewardOrder' => bcadd($staff['rewardOrder'], $orderReward, 2),
- 'rewardGoods' => bcadd($staff['rewardGoods'], $goodsReward, 2),
- 'rewardTotal' => bcadd($staff['rewardTotal'], $addReward, 2),
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDStaff->update($updateStaff, ['id' => $staff['id']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaff->error(), ErrorCode::$dberror);
- }
- }
- //保存记录
- if (!empty($insertRewardDesc)) {
- $dbResult = $this->objDStaffRewardDesc->insert($insertRewardDesc, true);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDStaffRewardDesc->error(), ErrorCode::$dberror);
- }
- }
- return ResultWrapper::success('执行成功');
- }
- public function addReflectDetail($params)
- {
- $staff = $this->objDStaff->get(['userCenterId' => $this->userCenterId, 'id' => $params['staffId']]);
- if ($staff['reward'] < $params['money']) return ResultWrapper::fail('余额不足', ErrorCode::$notAllowAccess);
- $before = $staff['reward'];
- $after = $staff['reward'] - $params['money'];
- $insertRewardDesc[] = [
- 'userCenterId' => $staff['userCenterId'],
- 'customerId' => '',
- 'staffId' => $staff['id'],
- 'shopId' => $staff['shopId'],
- 'originId' => 0,
- 'originNo' => '',
- 'ruleId' => '',
- 'title' => '员工提成提现',
- 'amount' => $params['money'],
- 'changeAmount' => $after,
- 'type' => 4,
- 'source' => 5,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDStaffRewardDesc->insert($insertRewardDesc, true);
- $res = $this->objDStaff->update(['reward' => $after]);
- $db = new DStaffReflect('default');
- $res1 = $db->insert($params);
- if ($dbResult === false && $res === false && $res1 === false) {
- return ResultWrapper::fail($this->objDStaffRewardDesc->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success('申请提现成功');
- }
- public function exchange($number, $money, $staff)
- {
- $stock_before = $staff['use_stock'];
- $stock_after = $staff['use_stock'] - $number;
- $used_stock = $staff['used_stock'] + $number;
- $reward_before = $staff['reward'];
- $reward_after = $staff['reward'] + $money;
- $insertRewardDesc[] = [
- 'userCenterId' => $staff['userCenterId'],
- 'customerId' => '',
- 'staffId' => $staff['id'],
- 'shopId' => $staff['shopId'],
- 'originId' => 0,
- 'originNo' => '',
- 'ruleId' => '',
- 'title' => '股权兑换提成',
- 'amount' => $money,
- 'changeAmount' => $reward_after,
- 'type' => 4,
- 'source' => 4,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $stockI = [
- 'en_id' => $this->enterpriseId,
- 'staff_id' => $staff['id'],
- 'pm' => 2,
- 'number' => $number,
- 'before' => $stock_before,
- 'after' => $stock_after,
- 'mark' => '股权提现',
- ];
- $dbResult = $this->objDStaffRewardDesc->insert($insertRewardDesc, true);
- $res = $this->objStock->insert($stockI);
- $res1 = $this->objDStaff->update(['reward' => $reward_after, 'use_stock' => $stock_after, 'used_stock' => $used_stock], $staff['id']);
- if ($res and $dbResult and $res1){
- return true;
- }
- return false;
- }
- }
|