123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- /**
- * 分销设置Model
- * Created by PhpStorm.
- * User: haoren
- * Date: 2020/07/22
- * Time: 15:00
- */
- namespace JinDouYun\Model\Commission;
- use Exception;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Model\MBaseModel;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Cache\CommissionCache;
- use JinDouYun\Dao\Commission\DCommissionSetting;
- use JinDouYun\Dao\Commission\DCommissionDesc;
- class MCommissionSetting extends MBaseModel
- {
- private $objDCommissionSetting;
- private $objCommissionCache;
- private $objDCommissionDesc;
- private $userCenterId;
- private $enterpriseId;
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->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);
- }
- }
|