enterpriseId = $enterpriseId; $this->userCenterId = $userCenterId; $this->objDRewardRule = new DRewardRule('default'); $this->objDRewardRule->setTable('qianniao_reward_rule_' . $enterpriseId); } /** * 提成规则添加 * @param $params * @return ResultWrapper */ public function addRewardRule($params) { $staffIds = $params['staff']; unset($params['staff']); $params['deleteStatus'] = StatusCode::$standard; $params['enableStatus'] = StatusCode::$standard; $params['createTime'] = time(); $params['updateTime'] = time(); $dbResult = $this->objDRewardRule->insert($params); if($dbResult === false){ return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } $ruleId = $dbResult; unset($dbResult); //修改每一个员工的规则绑定 $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId); $updateStaff = [ 'rewardRuleId' => $ruleId, 'updateTime' => time(), ]; $modelResult = $objMStaff->updateStaff($updateStaff, ['id' => $staffIds]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } return ResultWrapper::success($ruleId); } /** * 提成规则删除 * @param $where * @return ResultWrapper */ public function deleteRewardRule($where) { $params = [ 'deleteStatus' => StatusCode::$delete, 'updateTime' => time(), ]; $dbResult = $this->objDRewardRule->update($params, $where); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } $returnData = $dbResult; return ResultWrapper::success($returnData); } /** * 提成规则禁用 * @param $where * @return ResultWrapper */ public function enableRewardRule($where) { $dbResult = $this->objDRewardRule->get($where); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } $update = [ 'enableStatus' => StatusCode::$standard, 'updateTime' => time(), ]; if($dbResult['enableStatus'] == StatusCode::$standard){ $update['enableStatus'] = StatusCode::$delete; } $dbResult = $this->objDRewardRule->update($update, $where); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } $returnData = $dbResult; return ResultWrapper::success($returnData); } /** * 提成规则修改 * @param $updateData * @param $where * @return ResultWrapper */ public function updateRewardRule($updateData, $where) { $staffIds = $updateData['staff']; unset($updateData['staff']); $dbResult = $this->objDRewardRule->get($where); if($dbResult === false){ return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$paramError); } $rule = $dbResult; unset($dbResult); $dbResult = $this->objDRewardRule->update($updateData, $where); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } //修改每一个员工的规则绑定 $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId); $updateStaff = [ 'rewardRuleId' => $where['id'], 'updateTime' => time(), ]; $modelResult = $objMStaff->updateStaff($updateStaff, ['id' => $staffIds]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } return ResultWrapper::success($dbResult); } /** * 提成规则列表 * @param $selectParams * @return ResultWrapper * @throws Exception */ public function getAllRewardRule($selectParams) { $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); $selectParams['deleteStatus'] = StatusCode::$standard; isset($selectParams['search']) && $selectParams['search'] = ['title' => $selectParams['search']]; $selectParams = self::formatSqlWhere($selectParams); $dbResult = $this->objDRewardRule->select($selectParams,'*', 'createTime desc', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } $countResult = $this->objDRewardRule->count($selectParams); if ($countResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } $return = [ 'data' => self::formatRewardRule($dbResult), 'total' => ($countResult) ? intval($countResult) : 0, ]; return ResultWrapper::success($return); } /** * 提成规则详情 * @param $where * @return ResultWrapper * @throws Exception */ public function getRewardRuleInfo($where) { $where['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDRewardRule->get($where); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } return ResultWrapper::success(self::formatRewardRule($dbResult)); } /** * 提成规则格式化 * @param $params * @return array */ public function formatRewardRule($params) { if(isset($params['id'])){ $data = [$params]; }else{ $data = $params; } $ruleIds = array_column($data, 'id'); $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId); $modelResult = $objMStaff->getAllStaffData(['rewardRuleId' => $ruleIds, 'deleteStatus' => StatusCode::$standard]); $staff = []; if($modelResult->isSuccess()){ $staffResult = $modelResult->getData(); foreach($staffResult as $value){ $staff[$value['rewardRuleId']][] = $value; } } foreach($data as &$value){ !empty($value['goods']) && $value['goods'] = json_decode($value['goods'], true); $value['staff'] = isset($staff[$value['id']]) ? $staff[$value['id']] : []; } unset($value); if(isset($params['id'])){ $return = array_shift($data); }else{ $return = $data; } return $return; } /** * 查询提成规则数据 * @param $where * @return ResultWrapper */ public function getRewardRule($where) { $where['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDRewardRule->get($where); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 查询提成规则数据 * @param $where * @return ResultWrapper */ public function selectRewardRule($where) { $where['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDRewardRule->select($where); if ($dbResult === false) { return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } }