123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- <?php
- namespace JinDouYun\Model\System;
- use JinDouYun\Cache\PageCache;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Dao\Market\DUserCoupon;
- use JinDouYun\Dao\System\DPage;
- use JinDouYun\Dao\System\DPageDefault;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Model\Goods\MGoods;
- use JinDouYun\Model\Market\MActivity;
- use JinDouYun\Model\Market\MCoupon;
- use JinDouYun\Model\Merchant\MMerchant;
- use JinDouYun\Model\Shop\MShop;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- class MPage
- {
- private $objDPage;
- private $onlineEnterpriseId;
- private $onlineUserId;
- private $areaCode;
- private $shopId;
-
- public function __construct($onlineUserId, $enterpriseId, $areaCode = null)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $enterpriseId;
- $this->areaCode = $areaCode;
- $this->objDPage = new DPage('default');
- $this->objDPage->setTable($this->objDPage->get_Table() . '_' . $this->onlineEnterpriseId);
- self::__initDefaultPage();
- self::__initEnablePage();
- }
-
- private function __initDefaultPage()
- {
-
- $dbResult = $this->objDPage->get(['pageType' => StatusCode::$pageType['home'], 'isDefault' => StatusCode::$standard]);
- if (!empty($dbResult)) {
- return;
- }
- $objDPageDefault = new DPageDefault();
- $default = $objDPageDefault->get([
- 'deleteStatus' => StatusCode::$standard,
- 'enableStatus' => StatusCode::$standard,
- 'isDefault' => StatusCode::$standard,
- 'pageType' => StatusCode::$pageType['home']
- ]);
- if (empty($default)) {
- return;
- }
- $this->objDPage->insert([
- 'pageType' => $default['pageType'],
- 'pageName' => $default['pageName'],
- 'pageData' => $default['pageData'],
- 'deleteStatus' => $default['deleteStatus'],
- 'isDefault' => $default['isDefault'],
- 'enableStatus' => StatusCode::$delete,
- ]);
- }
-
- private function __initEnablePage()
- {
-
- $enablePage = $this->objDPage->get([
- 'deleteStatus' => StatusCode::$standard,
- 'enableStatus' => StatusCode::$standard,
- 'pageType' => StatusCode::$pageType['home'],
- 'shopId' => 0,
- ]);
- if (!empty($enablePage)) {
- return;
- }
-
- $this->objDPage->update(['enableStatus' => StatusCode::$standard], ['isDefault' => StatusCode::$standard,'shopId' => 0, 'pageType' => StatusCode::$pageType['home']]);
- }
-
- public function save($params,bool $isStore,$shopId)
- {
- if ($isStore === true){
- $objMMerchant = new MMerchant($this->onlineEnterpriseId,$this->onlineUserId);
- $merchResult = $objMMerchant->getMerchByShopId($shopId);
- if (!$merchResult->isSuccess()){
- return ResultWrapper::fail($merchResult->getData(),$merchResult->getErrorCode());
- }
- $merch = $merchResult->getData();
- $params['shopId'] = $shopId;
- $params['merchantId'] = $merch['merchantId'];
- }
- if (isset($params['id'])) {
- $dbResult = $this->objDPage->get(['id' => $params['id']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- }
- if ($dbResult['isDefault'] == StatusCode::$standard) {
- $params['isDefault'] = StatusCode::$standard;
- }
- $params['enableStatus'] = $dbResult['enableStatus'];
- }
- if (!isset($params['id'])) {
- $params['enableStatus'] = StatusCode::$delete;
- }
- $dbResult = $this->objDPage->replace($params);
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- return ResultWrapper::success($dbResult);
- }
-
- public function getPageInfo($id)
- {
- $dbResult = $this->objDPage->get(['id' => $id], 'id,pageType,pageName,pageData,enableStatus');
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- if (!empty($dbResult['pageData'])) $dbResult['pageData'] = json_decode($dbResult['pageData'], true);
- return ResultWrapper::success($dbResult);
- }
-
- public function updateEnableStatus($params)
- {
- $dbResult = $this->objDPage->get(['id' => $params['id']]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- }
-
- if ($dbResult['pageType'] != StatusCode::$pageType['special']) {
-
- if ($params['enableStatus'] == StatusCode::$standard) {
- $condition = [
- 'enableStatus' => StatusCode::$standard,
- 'deleteStatus' => StatusCode::$standard,
- 'pageType' => $dbResult['pageType']
- ];
- $dbResult = $this->objDPage->update(['enableStatus' => StatusCode::$delete],$condition);
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- }
- }
-
- $dbResult = $this->objDPage->update(['enableStatus' => $params['enableStatus']], $params['id']);
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- return ResultWrapper::success($dbResult);
- }
-
- public function del($id)
- {
- $dbResult = $this->objDPage->get(['id' => $id]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- }
- if ($dbResult['isDefault'] == StatusCode::$standard) {
- return ResultWrapper::fail('系统模版模版不能删除', ErrorCode::$paramError);
- }
- $dbResult = $this->objDPage->update(['deleteStatus' => StatusCode::$delete], $id);
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- return ResultWrapper::success($dbResult);
- }
-
- public function getAll($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDPage->select($selectParams, 'id,pageType,pageName,enableStatus,isDefault', 'createTime DESC', $limit, $offset);
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- $total = $this->objDPage->count($selectParams);
- $return = [
- 'data' => $dbResult,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
-
- public function home($pageType = 1,$shopId = null)
- {
- $dbResult = $this->objDPage->get_field('pageData',
- [
- 'deleteStatus' => StatusCode::$standard,
- 'pageType' => $pageType,
- 'enableStatus' => StatusCode::$standard,
- 'shopId' => 0
- ]
- );
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- $items = empty($dbResult) ? [] : json_decode($dbResult, true);
- foreach ($items as $key => &$item) {
- $setting = $item['modelData'];
- switch ($item['comName']){
- case 'GoodsGroup':
-
- if ($setting['navStyle'] == 2) {
- foreach ($setting['tabList'] as $index => &$val) {
- $item['modelData']['tabList'][$index]['data'] = ($val['goodsFrom'] == 1) ? self::goodsModule($val) : self::checkGoods($val['goods_ids']);
- }
-
- } else {
- $items[$key]['data'] = $setting['goodsFrom'] == 1 ? self::goodsModule($setting) : self::checkGoods($item['modelData']['goods_ids']);
- }
-
- break;
- case 'CouponGroup':
-
- $items[$key]['data'] = self::couponModule($setting);
- break;
- case 'LimitedSeckill':
-
- $items[$key]['data'] = self::secKillModule($setting);
- break;
- case 'Notice':
-
- $items[$key]['data'] = self::noticeModule(['type'=>StatusCode::$standard]);
- break;
- }
- }
- return ResultWrapper::success($items);
- }
-
-
- public function shopHome($merchantId,$pageType)
- {
- $dbResult = $this->objDPage->get(
- [
- 'deleteStatus' => StatusCode::$standard,
- 'pageType' => $pageType,
- 'enableStatus' => StatusCode::$standard,
- 'merchantId' => $merchantId
- ],'pageData,shopId'
- );
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- $shopId = getArrayItem($dbResult,'shopId',0);
- $items = empty($dbResult['pageData']) ? [] : json_decode($dbResult['pageData'],true);
- foreach ($items as $key => &$item) {
- $setting = $item['modelData'];
- switch ($item['comName']){
- case 'GoodsGroup':
-
- if ($setting['navStyle'] == 2) {
- foreach ($setting['tabList'] as $index => &$val) {
- $item['modelData']['tabList'][$index]['data'] = ($val['goodsFrom'] == 1) ? self::goodsModule($val,$shopId) : self::checkGoods($val['goods_ids'],$shopId);
- }
- } else {
- $items[$key]['data'] = $setting['goodsFrom'] == 1 ? self::goodsModule($setting,$shopId) : self::checkGoods($item['modelData']['goods_ids'],$shopId);
- }
- break;
- case 'CouponGroup':
-
- $items[$key]['data'] = self::couponModule($setting);
- break;
- case 'LimitedSeckill':
-
- $items[$key]['data'] = self::secKillModule($setting);
- break;
- case 'Notice':
-
- $items[$key]['data'] = self::noticeModule($setting);
- break;
- }
- }
- return ResultWrapper::success($items);
- }
-
- private function checkGoods($goodsIds,$shopId = 0)
- {
- $objMGoods = new MGoods($this->onlineEnterpriseId, true, $this->onlineUserId);
- $pageParams = pageToOffset(1, 100);
- $selectParams = [
- 'limit' => $pageParams['limit'],
- 'offset' => $pageParams['offset'],
- 'userCenterId' => $this->onlineUserId,
- 'goodsIds' => $goodsIds,
- 'areaCode' => $this->areaCode,
- 'shopId' => $this->shopId
- ];
- if (isset($shopId) && !empty($shopId)){
- $selectParams['shopId'] = $shopId;
- }
-
- $md5Key = md5(json_encode($selectParams));
- $objPageCache = new PageCache();
- $goodsCacheData = $objPageCache->getPage($md5Key);
- if(!empty($goodsCacheData)){
- return self::formatGoods($goodsCacheData);
- }
-
- $result = $objMGoods->search($selectParams);
- if (!$result->isSuccess()) return [];
- $goodsData = $result->getData()['data'];
-
- $objPageCache->addPage($md5Key,$goodsData);
- return self::formatGoods($goodsData);
- }
-
- private function formatGoods($data)
- {
- if (empty($data)){
- return $data;
- }
- foreach ($data as $key => $value){
- if (isset($value['salePrice'])){
- continue;
- }
- $data[$key]['salePrice'] = isset($value['minSalePrice']) ? $value['minSalePrice'] : '0.00';
- }
- return $data;
- }
-
- private function noticeModule($params)
- {
- $objMAnnouncement = new MAnnouncement($this->onlineEnterpriseId);
- $notice = $objMAnnouncement->getAnnouncement($params);
- if (!$notice->isSuccess()) {
- return (object)[];
- }
- return empty($notice->getData()) ? (object) [] : $notice->getData();
- }
-
- private function goodsModule($params,$shopId = 0)
- {
-
- $objMGoods = new MGoods($this->onlineEnterpriseId, true, $this->onlineUserId);
- $pageParams = pageToOffset(1, isset($params['goodsNum']) ? $params['goodsNum'] : 10);
- $selectParams = [
-
- 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : [],
- 'limit' => $pageParams['limit'],
- 'offset' => $pageParams['offset'],
- 'userCenterId' => $this->onlineUserId,
- 'areaCode' => $this->areaCode,
- 'shopId' => $this->shopId
- ];
- if (isset($shopId) && !empty($shopId)){
- $selectParams['shopId'] = $shopId;
- }
-
- $md5Key = md5(json_encode($selectParams));
- $objPageCache = new PageCache();
- $goodsCacheData = $objPageCache->getPage($md5Key);
- if(!empty($goodsCacheData)){
- return self::formatGoods($goodsCacheData);
- }
- $result = $objMGoods->search($selectParams);
- if (!$result->isSuccess()) return [];
- $goodsData = $result->getData();
- $goodsData = isset($goodsData['data']) ? $goodsData['data'] : [];
-
- $objPageCache->addPage($md5Key,$goodsData);
- return self::formatGoods($goodsData);
- }
-
- private function couponModule($params)
- {
- $objMCoupon = new MCoupon($this->onlineUserId, $this->onlineEnterpriseId);
- $paramsData = [
- 'grantType' => StatusCode::$grantType['receive'],
- ];
- $dbResult = $objMCoupon->couponList($paramsData);
- if (!$dbResult->isSuccess()) {
- return [];
- }
- $coupon = $dbResult->getData();
- if (!empty($this->onlineUserId)) {
-
- $objDUserCoupon = new DUserCoupon();
- $sql = 'SELECT couponId,count(`id`) as num FROM qianniao_user_coupon_' . $this->onlineEnterpriseId . ' WHERE userId=' . $this->onlineUserId . ' GROUP BY couponId';
- $countCoupon = $objDUserCoupon->query($sql);
- if ($countCoupon === false) {
- return $coupon;
- }
- $groupNum = [];
- if (!empty($countCoupon)) {
- foreach ($countCoupon as $count) {
- $groupNum[$count['couponId']] = $count['num'];
- }
- }
- foreach ($coupon as $key => $val) {
- $haveNum = isset($groupNum[$val['id']]) ? $groupNum[$val['id']] : 0;
- if ($haveNum >= $val['allowNum']) {
-
- unset($coupon[$key]);
- continue;
- }
- }
- }
- $coupon = array_slice($coupon, 0, $params['couponNum']);
- return $coupon;
- }
-
- private function secKillModule($params)
- {
- if (empty($params['seckill_id'])) {
- return [];
- }
- $objMActivity = new MActivity($this->onlineUserId, $this->onlineEnterpriseId, $this->areaCode);
- $dbResult = $objMActivity->getInfo($params['seckill_id']);
- if (!$dbResult->isSuccess()) {
- return [];
- }
- $skillGoods = $dbResult->getData();
-
- if (empty($skillGoods)){
- return [];
- }
-
- if (isset($skillGoods['auditStatus']) && $skillGoods['auditStatus'] != StatusCode::$auditStatus['auditPass']) {
- return [];
- }
- if ($skillGoods['enableStatus'] == StatusCode::$delete) {
-
- return [];
- }
-
- $customerType = null;
- if (!empty($this->onlineUserId)) {
- $objMCustomer = new MCustomer($this->onlineEnterpriseId, $this->onlineUserId);
- $customer = $objMCustomer->getCustomerInfoByUserCenterId($this->onlineUserId);
- if (!$customer->isSuccess()) {
- $customerType = null;
- } else {
- $customer = $customer->getData();
- $customerType = isset($customer['type']) ? $customer['type'] : null;
- }
- }
-
- if ($skillGoods['customerRange'] == StatusCode::$delete) {
- if (empty($customerType)) {
- return [];
- }
-
- $mapping = explode(',', trim($skillGoods['customerSourceId'], ','));
- if (!in_array($customerType, $mapping)) {
- return [];
- }
- }
- $skillGoods['inActivity'] = StatusCode::$standard;
- $skillGoods['msg'] = '活动进行中';
- if (!isset($skillGoods['startTime']) || !isset($skillGoods['endTime'])) {
- return [];
- }
- if ($skillGoods['startTime'] > time()) {
- $skillGoods['activityGoods'] = [];
- $skillGoods['msg'] = '活动暂未开始';
- $skillGoods['inActivity'] = StatusCode::$delete;
- } elseif ($skillGoods['endTime'] < time()) {
- $skillGoods['activityGoods'] = [];
- $skillGoods['msg'] = '活动已结束';
- $skillGoods['inActivity'] = StatusCode::$delete;
- }
- if (isset($skillGoods['activityGoods'])) {
- foreach ($skillGoods['activityGoods'] as &$goods) {
- $goods['originPrice'] = $goods['salePrice'];
- $goods['salePrice'] = $goods['price'];
- }
- }
- return $skillGoods;
- }
-
- public function getSpecial($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $selectParams['pageType'] = StatusCode::$pageType['special'];
- $selectParams['enableStatus'] = StatusCode::$standard;
- $dbResult = $this->objDPage->select($selectParams, '*', 'updateTime DESC', $limit, $offset);
- $total = $this->objDPage->count($selectParams);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- }
- foreach ($dbResult as $key => &$value) {
- $pageData = json_decode($value['pageData'], true);
- $dbResult[$key]['pageData'] = $pageData;
- }
- $data = [
- 'data' => $dbResult,
- 'total' => $total
- ];
- return ResultWrapper::success($data);
- }
-
- public function getDetailById($id)
- {
- $dbResult = $this->objDPage->get_field('pageData',
- ['id' => $id, 'enableStatus' => StatusCode::$standard, 'deleteStatus' => StatusCode::$standard]
- );
- if ($dbResult === false) return ResultWrapper::fail($this->objDPage->error(), ErrorCode::$dberror);
- $items = empty($dbResult) === true ? [] : json_decode($dbResult, true);
- foreach ($items as $key => &$item) {
- $setting = $item['modelData'];
- switch ($item['comName']){
- case 'GoodsGroup':
-
- if ($setting['navStyle'] == 2) {
- foreach ($setting['tabList'] as $index => &$val) {
- $item['modelData']['tabList'][$index]['data'] = ($val['goodsFrom'] == 1) ? self::goodsModule($val) : self::checkGoods($val['goods_ids']);
- }
- } else {
- $items[$key]['data'] = $setting['goodsFrom'] == 1 ? self::goodsModule($setting) : self::checkGoods($item['modelData']['goods_ids']);
- }
- break;
- case 'CouponGroup':
-
- $items[$key]['data'] = self::couponModule($setting);
- break;
- case 'LimitedSeckill':
-
- $items[$key]['data'] = self::secKillModule($setting);
- break;
- case 'Notice':
-
- $items[$key]['data'] = self::noticeModule($setting);
- break;
- }
-
- }
- return ResultWrapper::success($items);
- }
- }
|