123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929 |
- <?php
- namespace JinDouYun\Model\Price;
- use JinDouYun\Cache\PageCache;
- use JinDouYun\Cache\PriceCache;
- use JinDouYun\Dao\Price\DPriceRangeSheet;
- use JinDouYun\Model\Goods\MGoods;
- use JinDouYun\Model\GoodsManage\MSku;
- use JinDouYun\Model\Stock\MInventory;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- use JinDouYun\Model\MBaseModel;
- use JinDouYun\Dao\Price\DAreaPrice;
- use JinDouYun\Dao\Price\DNationalUnifiedPrice;
- use JinDouYun\Dao\Price\DPriceAdjustment;
- use JinDouYun\Dao\Price\DLastEffectiveSalePriceAreaType;
- class MPriceAdjustment extends MBaseModel
- {
-
- private $onlineUserId;
-
- private $onlineEnterpriseId;
-
- private $objDPriceAdjustment;
- private $objDDistrictPriceCity;
- private $objDDistrictPriceProvince;
- private $objPriceCache;
-
- private $objDPriceRangeSheet;
-
- public function __construct($onlineUserId, $onlineEnterpriseId)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- parent::__construct($this->onlineEnterpriseId, $this->onlineUserId);
- $this->objPriceCache = new PriceCache($this->onlineEnterpriseId);
- $this->objDPriceAdjustment = new DPriceAdjustment();
- $this->objDPriceRangeSheet = new DPriceRangeSheet();
- $this->objDPriceAdjustment->setTable($this->objDPriceAdjustment->get_Table() . '_' . $this->onlineEnterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
- $this->objDPriceAdjustment->setSearchIndex('priceAdjustmentSheet_search')->setType('priceAdjustmentSheet');
- }
-
- public function add($sheetData)
- {
-
- $no = createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['priceAdjustment'], $this->onlineUserId);
- foreach ($sheetData as $key => $value) {
- $sheetData[$key]['no'] = $no;
- }
- $dbResult = $this->objDPriceAdjustment->insert($sheetData, true);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDPriceAdjustment->error(), ErrorCode::$dberror);
- }
- if (is_array($dbResult)) {
- foreach ($dbResult as $key => $id) {
- if (isset($sheetData[$key])) {
- $sheetData[$key]['id'] = $id;
- }
- }
- }
-
- return ResultWrapper::success($dbResult);
- }
-
- public function updateEsData($data)
- {
- if (empty($data)) return $data;
- foreach ($data as $key => $val) {
- $esData = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'id' => $val['id'],
- 'no' => $val['no'],
- 'goodsCode' => isset($val['goodsCode']) ? $val['goodsCode'] : createCode(StatusCode::$code['goodsBasic']['prefix'], $val['goodsId'], StatusCode::$code['goodsBasic']['length']),
- 'goodsName' => $val['goodsName'],
- 'goodsId' => $val['goodsId'],
- 'saleType' => $val['saleType'],
- 'salePriceAreaType' => $val['salePriceAreaType'],
- 'salePriceType' => $val['salePriceType'],
- 'shopId' => $val['shopId'],
- 'shopName' => $val['shopName'],
- 'cargoOwnerCode' => '',
- 'createUserId' => $val['createUserId'],
- 'createUserName' => $val['createUserName'],
- 'effectiveUserName' => isset($val['effectiveUserName']) ? $val['effectiveUserName'] : '',
- 'effectiveUserId' => isset($val['effectiveUserId']) ? $val['effectiveUserId'] : '',
- 'deleteStatus' => StatusCode::$standard,
- 'effectiveStatus' => isset($val['effectiveStatus']) ? $val['effectiveStatus'] : StatusCode::$auditStatus['auditPass'],
- 'createTime' => isset($val['createTime']) ? $val['createTime'] : time(),
- 'updateTime' => isset($val['updateTime']) ? $val['updateTime'] : time(),
- 'salePrice' => json_decode($val['salePrice'], true),
- ];
- $_id = self::createEsDocumentId($val['id']);
- $result = $this->objDPriceAdjustment->addUpSearchIndexDocument($esData, $_id);
- if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
-
-
- }
-
- }
- }
-
- private function createEsDocumentId($id)
- {
- return 'EnterpriseId_' . $this->onlineEnterpriseId . '_id_' . $id;
- }
-
- public function addPrice($sheetData)
- {
- switch ($sheetData['salePriceAreaType']) {
- case StatusCode::$salePriceAreaType['nationalUnifiedPrice']:
- return self::saveNationalUnifiedPrice($sheetData);
- break;
- case StatusCode::$salePriceAreaType['areaPrice']:
- return self::saveAreaPrice($sheetData);
- break;
-
- }
- }
-
- public function commonField($sheetData)
- {
- $salePriceData = [
- 'no' => $sheetData['no'],
- 'goodsId' => $sheetData['goodsId'],
- 'shopId' => $sheetData['shopId'],
- 'salePriceType' => $sheetData['salePriceType'],
- 'salePrice' => $sheetData['salePrice'],
- 'updateTime' => $sheetData['createTime'],
- ];
- return $salePriceData;
- }
-
- public function saveNationalUnifiedPrice($sheetData)
- {
- $salePriceData = self::commonField($sheetData);
- $objDNationalUnifiedPrice = new DNationalUnifiedPrice();
- $objDNationalUnifiedPrice->beginTransaction();
-
- $tableName = 'qianniao_nationalUnifiedPrice_' . $this->onlineEnterpriseId;
- $objDNationalUnifiedPrice->setTable($tableName);
-
- $condition = [
- 'shopId' => $salePriceData['shopId'],
- 'salePriceType' => $salePriceData['salePriceType'],
- 'goodsId' => $salePriceData['goodsId'],
- ];
- $dbResult = $objDNationalUnifiedPrice->get($condition);
- if ($dbResult === false) {
- $objDNationalUnifiedPrice->rollBack();
- return ResultWrapper::fail($objDNationalUnifiedPrice->error(), ErrorCode::$dberror);
- }
- $salePriceData['salePrice'] = self::formatMerge($dbResult, $salePriceData, StatusCode::$salePriceAreaType['nationalUnifiedPrice']);
- $dbResult = $objDNationalUnifiedPrice->replace($salePriceData);
- if ($dbResult === false) {
- $objDNationalUnifiedPrice->rollBack();
- return ResultWrapper::fail($objDNationalUnifiedPrice->error(), ErrorCode::$dberror);
- }
-
- $objDNationalUnifiedPrice->commit();
-
- return ResultWrapper::success($dbResult);
- }
- private function formatMerge($dbResult, $salePriceData, $salePriceAreaType)
- {
- if (!is_array($salePriceData['salePrice'])) {
- $salePriceData['salePrice'] = json_decode($salePriceData['salePrice'], true);
- }
-
- if (!empty($dbResult)) {
- $nowSalePrice = json_decode($dbResult['salePrice'], true);
- if (empty($nowSalePrice)) {
- $salePriceData['salePrice'] = json_encode($salePriceData['salePrice']);
- } else {
-
- foreach ($nowSalePrice as $key => $value) {
- if (isset($salePriceData['salePrice'][$key])) {
- if ($salePriceAreaType == StatusCode::$salePriceAreaType['areaPrice']) {
- if (count($salePriceData['salePrice'][$key]) > 1) {
- $nowSalePrice[$key] = array_merge($value, $salePriceData['salePrice'][$key]);
- } else {
- $nowSalePrice[$key] = $salePriceData['salePrice'][$key];
- }
- } else {
- $nowSalePrice[$key] = $salePriceData['salePrice'][$key];
- }
- }else{
- unset($nowSalePrice[$key]);
- }
- unset($salePriceData['salePrice'][$key]);
- }
-
- if (!empty($salePriceData['salePrice'])) {
- $salePriceData['salePrice'] = $salePriceData['salePrice'] + $nowSalePrice;
- } else {
- $salePriceData['salePrice'] = $nowSalePrice;
- }
- $salePriceData['salePrice'] = json_encode($salePriceData['salePrice']);
- }
- }
- if (is_array($salePriceData['salePrice'])) {
- $salePriceData['salePrice'] = json_encode($salePriceData['salePrice']);
- }
- return $salePriceData['salePrice'];
- }
-
- public function saveAreaPrice($sheetData)
- {
- $salePriceData = self::commonField($sheetData);
- $salePriceData['salePrice'] = json_decode($salePriceData['salePrice'], true);
- $objDAreaPrice = new DAreaPrice();
-
- $tableName = 'qianniao_areaPrice_' . $this->onlineEnterpriseId;
- $objDAreaPrice->setTable($tableName);
-
- $condition = [
- 'shopId' => $salePriceData['shopId'],
- 'salePriceType' => $salePriceData['salePriceType'],
- 'goodsId' => $salePriceData['goodsId'],
- ];
- $dbResult = $objDAreaPrice->get($condition);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDAreaPrice->error(), ErrorCode::$dberror);
- }
- $salePriceData['salePrice'] = self::formatMerge($dbResult, $salePriceData, StatusCode::$salePriceAreaType['areaPrice']);
- $dbResult = $objDAreaPrice->replace($salePriceData);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDAreaPrice->error(), ErrorCode::$dberror);
- } else {
-
- return ResultWrapper::success($dbResult);
- }
- }
-
- public function saveRegionalPrice($sheetData)
- {
- $salePriceData = [];
- $sheetData['salePriceArea'] = json_decode($sheetData['salePriceArea'], true);
- foreach ($sheetData['salePriceArea'] as $k => $v) {
- $salePriceData[$k] = self::commonField($sheetData);
- $salePriceData[$k]['regionalDepartmentId'] = $v;
- $salePriceData[$k]['regionalGroupId'] = $sheetData['regionalGroupId'];
- }
- $objDRegionalDepartmentPrice = new DRegionalDepartmentPrice();
-
- $tableName = 'regionalDepartmentPrice_' . $sheetData['shopId'];
- $objDRegionalDepartmentPrice->setTable($tableName);
- $dbResult = $objDRegionalDepartmentPrice->replace(array_values($salePriceData), true);
- if ($dbResult === false) {
- return ResultWrapper::fail($objDRegionalDepartmentPrice->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
-
- public function saveCityPrice($cityid, $districtPriceCityData)
- {
-
- $tableName = 'districtPrice_city_' . $cityid;
- $this->objDDistrictPriceCity->setTable($tableName);
- $dbResult = $this->objDDistrictPriceCity->replace($districtPriceCityData, true);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDDistrictPriceCity->error(), ErrorCode::$dberror);
- } else {
- return ResultWrapper::success($dbResult);
- }
- }
-
- public function saveProvincesPrice($provinceId, $districtPriceProvincesData)
- {
-
- $tableName = 'districtPrice_province_' . $provinceId;
- $this->objDDistrictPriceProvince->setTable($tableName);
- $dbResult = $this->objDDistrictPriceProvince->replace($districtPriceProvincesData, true);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDDistrictPriceProvince->error(), ErrorCode::$dberror);
- } else {
- return ResultWrapper::success($dbResult);
- }
- }
-
- public function formatPriceData($sheetData)
- {
- $sheetData['salePriceArea'] = json_decode($sheetData['salePriceArea'], true);
- $areaData = self::areaGroup($sheetData['salePriceArea']);
- $salePriceData = self::commonField($sheetData);
- if (isset($areaData['citys'])) {
-
- foreach ($areaData['citys'] as $key => $value) {
- foreach ($value['district'] as $k => $v) {
- $salePriceData['districtId'] = $v;
- $districtPriceCityData[] = $salePriceData;
- }
- $result = self::saveCityPrice($key, $districtPriceCityData);
- if (!$result->isSuccess()) {
- return $result;
- }
- }
- }
- if (isset($areaData['provinces'])) {
-
- foreach ($areaData['provinces'] as $key => $value) {
- unset($salePriceData['districtId']);
- $districtPriceProvinceData[] = $salePriceData;
- $result = self::saveProvincesPrice($value, $districtPriceProvinceData);
- if (!$result->isSuccess()) {
- return $result;
- }
- }
- }
- return $result;
- }
-
- public function areaGroup($areaDatas)
- {
- $citys = [];
- $provinces = [];
- foreach ($areaDatas as $key => $value) {
- $areaData = explode('-', $value);
-
- if ($areaData[2] != 0) {
- $citys[$areaData[1]]['district'][] = $areaData[2];
- continue;
- }
-
- if ($areaData[1] != 0) {
- $citys[$areaData[1]]['district'][] = 0;
- continue;
- }
-
- if ($areaData[0] != 0) {
- $provinces[] = $areaData[0];
- }
- }
- return [
- 'citys' => $citys,
- 'provinces' => $provinces,
- ];
- }
-
- public function getAll($params,$export = 0)
- {
- $pageData = pageToOffset($params['page'], $params['pageSize']);
- if ($export) {
- $pageData['limit'] = null;
- $pageData['offset'] = null;
- }
- $condition = null;
- if ($params['keyword']) {
- $condition = "(goodsName like '%" . $params['keyword'] . "%' or goodsCode like '%" . $params['keyword'] . "%' or no like '%" . $params['keyword'] . "%')";
- }
- if ($params['startTime'] && $params['endTime']) {
- if ($condition !== null) {
- $condition .= ' and ';
- }
- $condition .= " createTime >= " . $params['startTime'] . " and createTime <=" . $params['endTime'];
- }
- if ($params['effectiveStatus']) {
- if ($condition !== null) {
- $condition .= ' and ';
- }
- $condition .= " effectiveStatus = " . $params['effectiveStatus'];
- }
-
- if (isset($params['goodsId']) && !empty($params['goodsId'])) {
- if ($condition !== null) {
- $condition .= ' and ';
- }
- $condition .= " goodsId = " . $params['goodsId'];
- }
-
- if (isset($params['shopId']) && !empty($params['shopId'])){
- if ($condition !== null) {
- $condition .= ' and ';
- }
- $condition .= " shopId = ".$params['shopId'];
- }
- $condition = parent::getShopIdQueryParams($condition);
- $total = $this->objDPriceAdjustment->count($condition);
- $dbResult = $this->objDPriceAdjustment->select($condition, '*', 'id desc', $pageData['limit'], $pageData['offset']);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDPriceAdjustment->error(), ErrorCode::$dberror);
- }
- $return = [
- 'data' => self::format($dbResult),
- 'total' => $total,
- ];
-
- if($export){
- self::exportGoodsPriceSheet($return['data']);
- exit;
- }
- return ResultWrapper::success($return);
- }
-
- public function format($data)
- {
- $skuIds = [];
- foreach ($data as $key => &$val) {
- if (!is_array($val['salePrice'])) {
- $val['salePrice'] = json_decode($val['salePrice'], true);
- }
- foreach ($val['salePrice'] as $k => &$v) {
- if ($val['salePriceAreaType'] == StatusCode::$salePriceAreaType['nationalUnifiedPrice']) {
- $v['skuId'] = $k;
- }
- }
- $val['salePrice'] = array_values($val['salePrice']);
- foreach ($val['salePrice'] as $row){
- $skuIds[] = $row['skuId'];
- }
- }
- $objMSku = new MSku($this->onlineUserId,$this->onlineEnterpriseId);
- $skuResult = $objMSku->getSpecNameBySkuId($skuIds);
- $allSkuData = [];
- if ($skuResult->isSuccess()){
- $allSkuData = $skuResult->getData();
- }
- foreach ($data as &$value){
- foreach ($value['salePrice'] as &$row){
- $row['unitName'] = isset($allSkuData[$row['skuId']]) ? $allSkuData[$row['skuId']]['unitName'] : '';
- $row['specGroup'] = isset($allSkuData[$row['skuId']]) ? $allSkuData[$row['skuId']]['specGroup'] : [];
- if (empty($row['specGroup'])){
- $value['specType'] = StatusCode::$specType['single'];
- }else{
- $value['specType'] = StatusCode::$specType['multiple'];
- }
- }
- }
- return $data;
- }
-
- public function effective($ids, $params)
- {
-
- $tableName = 'qianniao_priceAdjustmentSheet_' . $this->onlineEnterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3);
- $this->objDPriceAdjustment->setTable($tableName);
- $priceAdjustment = $this->objDPriceAdjustment->get($ids);
- if ($priceAdjustment === false) {
- return ResultWrapper::fail($this->objDPriceAdjustment->error(), ErrorCode::$dberror);
- }
- if (empty($priceAdjustment)) {
- return ResultWrapper::fail('要生效的调价单不存在', ErrorCode::$contentNotExists);
- }
- $beginStatus = $this->objDPriceAdjustment->beginTransaction();
-
- $dbResult = $this->objDPriceAdjustment->update(
- [
- 'effectiveStatus' => StatusCode::$auditStatus['auditPass'],
- 'effectiveUserName' => isset($params['effectiveUserName']) ? $params['effectiveUserName'] : '',
- 'effectiveUserId' => isset($params['effectiveUserId']) ? $params['effectiveUserId'] : '',
- ], $ids);
- if ($dbResult === false) {
- $this->objDPriceAdjustment->rollBack();
- return ResultWrapper::fail($this->objDPriceAdjustment->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
-
- $objDLastEffectiveSalePriceAreaType = new DLastEffectiveSalePriceAreaType();
- $objDLastEffectiveSalePriceAreaType->setTable('qianniao_lastEffectiveSalePriceAreaType_' . $this->onlineEnterpriseId);
- $data = [
- 'shopId' => $priceAdjustment['shopId'],
- 'goodsCode' => $priceAdjustment['goodsCode'],
- 'goodsId' => $priceAdjustment['goodsId'],
- 'lastEffectiveSalePriceAreaType' => $priceAdjustment['salePriceAreaType'],
- 'no' => $priceAdjustment['no'],
- 'updateTime' => time(),
- ];
- $dbResult = $objDLastEffectiveSalePriceAreaType->replace($data);
- if ($dbResult === false) {
- $this->objDPriceAdjustment->rollBack();
- return ResultWrapper::fail($objDLastEffectiveSalePriceAreaType->error(), ErrorCode::$dberror);
- }
- $result = self::addPrice($priceAdjustment);
- if (!$result->isSuccess()) {
- $this->objDPriceAdjustment->rollBack();
- return ResultWrapper::fail($this->objDPriceAdjustment->error(), ErrorCode::$dberror);
- }
- unset($result);
-
- $objPageCache = new PageCache();
- $objPageCache->delPage();
-
- $this->objPriceCache->delNationSalePrice($priceAdjustment['goodsId']);
-
- $objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId);
- $salePrice = json_decode($priceAdjustment['salePrice'], true);
- foreach($salePrice as $skuId => $v){
- $params = [
- 'costPrice' => $v['salePrice'],
- 'skuId' => $skuId,
- ];
- $modelResult = $objMInventory->updateInventoryCost($params);
- if(!$modelResult->isSuccess()){
- $this->objDPriceAdjustment->rollBack();
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- }
-
- $beginStatus && $this->objDPriceAdjustment->commit();
- return ResultWrapper::success($dbResult);
- }
-
- public function getMinPrice($salePrice)
- {
- $minPrice = $salePrice['salePrice'];
- if ($salePrice['enabledLadder']) {
- foreach ($salePrice['ladderPrice'] as $key => $value) {
- if ($value['price'] < $minPrice) {
- $minPrice = $value['price'];
- }
- }
- }
- return $minPrice;
- }
-
- public function addAndEffective($sheetData, $effectiveParams = [])
- {
- $result = self::add($sheetData);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $sheetId = $result->getData();
- unset($result);
- if (empty($effectiveParams)) {
- $effectiveParams['createTime'] = time();
- }
- $result = self::effective($sheetId, $effectiveParams);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
-
- $objPageCache = new PageCache();
- $objPageCache->delPage();
- return ResultWrapper::success($result->getData());
- }
-
- public function search($selectParams)
- {
- $defaultDSL = [
- 'from' => $selectParams['offset'],
- 'size' => $selectParams['limit'],
- 'sort' => [
- 'createTime' => [
- 'order' => 'desc'
- ],
- ],
- ];
- $dsl = [];
- $dsl['query']['bool']['must'][] = [
- 'term' => ['enterpriseId' => $this->onlineEnterpriseId],
- ];
-
- parent::getAccessShopIds();
- if (parent::$shopIds) {
- $dsl['query']['bool']['filter'][] = [
- 'terms' => ['shopId' => parent::$shopIds]
- ];
- }
-
- if (!empty($selectParams['keyword'])) {
- $dsl['query']['bool']['must'][] = [
- 'multi_match' => [
- 'fields' => ['goodsCode', 'goodsName'],
- 'query' => $selectParams['keyword'],
- 'fuzziness' => 'AUTO',
- ],
- ];
- }
-
- if (!empty($selectParams['effectiveStatus'])) {
- $dsl['query']['bool']['filter'][] =
- ['term' => ['effectiveStatus' => $selectParams['effectiveStatus']]];
- }
-
- if (!empty($selectParams['startTime']) && !empty($selectParams['endTime'])) {
- $dsl['query']['bool']['must'][] = [
- 'range' => [
- 'createTime' => [
- 'gte' => $selectParams['startTime'],
- 'lte' => $selectParams['endTime'],
- ]
- ]
- ];
- } else {
- if (!empty($selectParams['startTime'])) {
- $dsl['query']['bool']['must'][] = [
- 'range' => [
- 'createTime' => [
- 'gte' => $selectParams['startTime'],
- ]
- ]
- ];
- }
- if (!empty($selectParams['endTime'])) {
- $dsl['query']['bool']['must'][] = [
- 'range' => [
- 'createTime' => [
- 'lte' => $selectParams['endTime'],
- ]
- ]
- ];
- }
- }
- $dsl = array_merge($defaultDSL, $dsl);
- $result = $this->objDPriceAdjustment->getSearchQueryDsl($dsl);
- if (isset($result['status']) && $result['status'] == 400) {
- return ResultWrapper::fail('获取数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
- }
- $total = $result['hits']['total'];
- $dbResult = $result['hits']['hits'];
- $list = [];
- foreach ($dbResult as $key => &$value) {
- $data = [];
- $data = $value['_source'];
- $list[] = $data;
- }
- $return = [
- 'data' => self::format($list),
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
-
- public function savePriceRange(array $data)
- {
- if (empty($data)) {
- return ResultWrapper::success([]);
- }
- $salePrice = json_decode($data['salePrice'],true);
- $allSalePrice = array_column($salePrice,'salePrice');
- $sale = [
- 'minSalePrice' => min($allSalePrice),
- 'maxSalePrice' => max($allSalePrice),
- ];
- $add = [
- 'goodsCode' => isset($data['goodsCode']) ? $data['goodsCode'] : '',
- 'goodsName' => isset($data['goodsName']) ? $data['goodsName'] : '',
- 'goodsId' => $data['goodsId'],
- 'shopId' => $data['shopId'],
- 'createTime' => time(),
- 'salePrice' => json_encode($sale),
- ];
- $this->objDPriceRangeSheet->setTable($this->objDPriceRangeSheet->get_Table() . '_' . $this->onlineEnterpriseId);
- $result = $this->objDPriceRangeSheet->replace($add);
- if ($result === false) {
- return ResultWrapper::fail($this->objDPriceRangeSheet->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
-
-
- private static function exportGoodsPriceSheet($priceSheetData)
- {
-
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition:filename=商品调价单明细表记录.csv");
- header('Cache-Control: max-age=0');
- $fp = fopen('php://output', 'a');
-
- $head = ['调价单编码','商品名称','调价日期','调整后价格规格','调整市场价格','阶梯价','销售价','调整人','相关店铺', '状态'];
-
- foreach ($head as $i => $v) {
- $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8');
- }
- fputcsv($fp, $head);
- $limit = 10000;
- $num = 0;
- foreach ($priceSheetData as $v) {
- $marketPrice = [];
- $ladderPrice = '';
- $salePrice = [];
- $unitName = [];
- $specValueName = [];
- if(!empty($v['salePrice'])){
- foreach ($v['salePrice'] as $value){
- $marketPrice[] = $value['marketPrice'];
- $ladderPrice = $value['ladderPrice'];
- $salePrice[] = $value['salePrice'];
- if(!empty($value['specGroup'])){
- foreach ($value['specGroup'] as $vv){
- $specValueName = $vv['specValueName'];
- }
- }
- $unitName[] = $value['unitName'].';'.$specValueName;
- }
- }
-
- $num++;
- if ($num == $limit) {
- ob_flush();
- flush();
- }
- $rows['no'] = isset($v['no']) ? $v['no'] : '';
- $rows['goodsName'] = isset($v['goodsName']) ? $v['goodsName'] : '';
- $rows['createTime'] = isset($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';
- $rows['unitName'] = implode("\n",$unitName);
- $rows['price'] = implode("\n",$marketPrice);
- $rows['ladderPrice'] = !empty($ladderPrice) ? $ladderPrice : "否";
- $rows['salePrice'] = implode("\n",$salePrice);
-
-
- $rows['createUserName'] = isset($v['createUserName']) ? $v['createUserName'] : '';
- $rows['shopName'] = isset($v['shopName']) ? $v['shopName'] : '';
- $rows['effectiveStatus'] = $v['effectiveStatus'] === 2 ? '生效' : '未生效';
- foreach ($rows as $kk => $vv) {
- $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8');
- }
- fputcsv($fp, $rs);
- $rows = [];
- }
- exit;
- }
- }
|