enterpriseId = $enterpriseId; $this->userCenterId = $userCenterId; $this->objCommissionCache = new CommissionCache($enterpriseId, $userCenterId); $this->objDCommissionSetting = new DCommissionSetting(); $this->objDCommissionDesc = new DCommissionDesc(); } /** * 保存设置 * @param $params * @return ResultWrapper */ public function saveSetting($params) { isset($params['info']) && $params['info'] = json_encode($params['info']); isset($params['payType']) && $params['payType'] = json_encode($params['payType']); isset($params['withdrawType']) && $params['withdrawType'] = json_encode($params['withdrawType']); //Logger::logs(E_USER_ERROR,'saveSetting',__CLASS__,__LINE__,$params); if(isset($params['buyGoods'])){ $buyGoods = []; foreach($params['buyGoods'] as $key => $value){ if(!empty($value)){ $buyGoods[$key] = $value; } } $params['buyGoods'] = json_encode($buyGoods, JSON_FORCE_OBJECT); } $params['withdrawInfo'] = json_encode($params['withdrawInfo']); $dbResult = $this->objDCommissionSetting->get(['enterpriseId' => $this->enterpriseId]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror); } $setting = $dbResult; unset($dbResult); $params['updateTime'] = time(); if(!empty($setting)){ //修改 $dbResult = $this->objDCommissionSetting->update($params, ['enterpriseId' => $this->enterpriseId]); }else{ //新增 $params['createTime'] = time(); $params['enterpriseId'] = $this->enterpriseId; $dbResult = $this->objDCommissionSetting->insert($params); } if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror); } //删除cache $this->objCommissionCache->delSetting(); $this->objCommissionCache->delBusinessmanSub(true); //保存cache $selectResult = $this->objDCommissionSetting->get(['enterpriseId' => $this->enterpriseId]); if($selectResult === false){ return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror); } $this->objCommissionCache->saveSetting($selectResult); return ResultWrapper::success($dbResult); } /** * 查询设置 * @param bool $field //true:查询所有 数组:查询多个字段 字符串:查询单个字段 * @param bool $isCustomerId * @return ResultWrapper * @throws Exception */ public function getSetting($field = true, $isCustomerId = false) { $setting = $this->objCommissionCache->getSetting(); if(!$setting){ $setting = $this->objDCommissionSetting->get(['enterpriseId' => $this->enterpriseId]); if($setting === false){ return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror); } if(empty($setting)){ return ResultWrapper::success($setting); } $this->objCommissionCache->saveSetting($setting); } if(!empty($setting['info'])){ $setting['info'] = json_decode($setting['info'], true); }else{ $setting['info'] = []; } if(!empty($setting['payType'])){ $setting['payType'] = json_decode($setting['payType'], true); }else{ $setting['payType'] = []; } if(!empty($setting['withdrawType'])){ $setting['withdrawType'] = json_decode($setting['withdrawType'], true); }else{ $setting['withdrawType'] = []; } if(!empty($setting['withdrawInfo'])){ $setting['withdrawInfo'] = json_decode($setting['withdrawInfo'], true); }else{ $setting['withdrawInfo'] = []; } if(!empty($setting['desc'])){ $setting['desc'] = htmlspecialchars_decode($setting['desc'],ENT_QUOTES); } if($isCustomerId){ //查询customerId $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId); $modelResult = $objMCustomer->getCustomerData(['userCenterId'=>$this->userCenterId]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $customerData = $modelResult->getData(); unset($modelResult); if(empty($customerData)){ return ResultWrapper::fail('客户查询失败', ErrorCode::$dberror); } $customerId = $customerData['id']; // $customerId = 38; }else{ $customerId = null; } $modelResult = self::formatSettingBuyGoods($setting, $customerId); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); unset($modelResult); if($field === true){ return ResultWrapper::success($setting); } if(is_array($field)){ $return = []; foreach($field as $value){ $return[$value] = null; if(isset($field[$value])){ $return[$value] = $field[$value]; } } return ResultWrapper::success($return); } if(isset($setting[$field])){ return ResultWrapper::success($setting[$field]); }else{ return ResultWrapper::success([]); } } /** * 格式化设置购买指定商品字段 * @param $params * @param null $customerId * @return ResultWrapper * @throws Exception */ public function formatSettingBuyGoods($params, $customerId = null) { if(!isset($params['buyGoods']) || empty($params['buyGoods'])) return ResultWrapper::success($params); $params['buyGoods'] = json_decode($params['buyGoods'], true); //查询所有等级 $objMCommissionGrade = new MCommissionGrade($this->enterpriseId, $this->userCenterId); $modelResult = $objMCommissionGrade->getAllGrade(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $gradeArray = $modelResult->getData(); unset($modelResult); if(empty($gradeArray)){ return ResultWrapper::success($params); } $gradeData = []; foreach($gradeArray as $value){ $gradeData[$value['grade']] = $value; } unset($gradeArray); //拿出所有商品id $goodsData = []; $buyGoodsIds = []; foreach($params['buyGoods'] as $grade => $goods){ if(!empty($goods)){ foreach($goods as $v) { if(!empty($v) && isset($v['skuId']) && isset($v['goodsId'])){ $buyGoodsIds[] = [ 'skuId' => $v['skuId'], 'goodsId' => $v['goodsId'] ]; } } } } //查询商品 if(!empty($buyGoodsIds)){ $objMCommissionGoods = new MCommissionGoods($this->enterpriseId, $this->userCenterId); $modelResult = $objMCommissionGoods->formatGoods($buyGoodsIds, $customerId); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $goodsResult = $modelResult->getData(); unset($modelResult); if(!empty($goodsResult)){ foreach($goodsResult as $value){ $goodsData[$value['skuId']] = $value; } } } $grades = []; foreach($gradeData as $value){ $gradeArray = [ 'grade' => $value['grade'], 'gradeId' => $value['id'], 'gradeName' => $value['name'], 'goodsData' => [] ]; if(isset($params['buyGoods'][$value['grade']])){ $buyGoodsData = $params['buyGoods'][$value['grade']]; if(!empty($buyGoodsData)){ foreach($buyGoodsData as $v){ if(!empty($goodsData) && isset($goodsData[$v['skuId']])){ $gradeArray['goodsData'][] = [ 'images' => $goodsData[$v['skuId']]['images'], 'goodsName' => $goodsData[$v['skuId']]['goodsName'], 'unitName' => $goodsData[$v['skuId']]['unitName'], 'specGroup' => $goodsData[$v['skuId']]['specGroup'], 'salePrice' => $goodsData[$v['skuId']]['salePrice'], 'inventory' => $goodsData[$v['skuId']]['inventory'], 'goodsId' => $v['goodsId'], 'skuId' => $v['skuId'], ]; } } } } $grades[$value['grade']] = $gradeArray; } ksort($grades); $params['buyGoods'] = array_values($grades); return ResultWrapper::success($params); } /** * 保存文本设置 * @param $params * @return ResultWrapper */ public function saveTxtSetting($params) { $where = [ 'enterpriseId' => $this->enterpriseId, ]; $dbResult = $this->objDCommissionDesc->get($where); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionDesc->error(), ErrorCode::$dberror); } $setting = $dbResult; unset($dbResult); if(empty($setting)){ //新增 $insert = [ 'enterpriseId' => $this->enterpriseId, 'desc' => json_encode($params['desc']), 'updateTime' => time(), 'createTime' => time() ]; $dbResult = $this->objDCommissionDesc->insert($insert); }else{ //修改 $update = [ 'desc' => json_encode($params['desc']), 'updateTime' => time() ]; $dbResult = $this->objDCommissionDesc->update($update, $where); } if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionDesc->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 查询文字设置 */ public function getTxtSetting() { $where = [ 'enterpriseId' => $this->enterpriseId, ]; $dbResult = $this->objDCommissionDesc->get($where); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionDesc->error(), ErrorCode::$dberror); } $returnData = []; !empty($dbResult) && $returnData = json_decode($dbResult['desc'], true); return ResultWrapper::success($returnData); } /** * 获取分销商品 * @throws Exception */ public function getBuyGoods() { $modelResult = self::getSetting('buyGoods', true); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $buyGoods = $modelResult->getData(); unset($modelResult); $return = []; foreach($buyGoods as $value){ foreach($value['goodsData'] as $v){ $v['grade'] = $value['grade']; $v['gradeName'] = $value['gradeName']; $return[] = $v; } } return ResultWrapper::success($return); } }