123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929 |
- <?php
- /**
- * 调价单管理
- * Created by PhpStorm.
- * User: phperstar
- * Date: 2019/4/16
- * Time: 9:41 AM
- */
- 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
- {
- /**
- * @var int
- */
- private $onlineUserId;
- /**
- * @var int
- */
- private $onlineEnterpriseId;
- /**
- * @var DPriceAdjustment
- */
- private $objDPriceAdjustment;
- private $objDDistrictPriceCity;
- private $objDDistrictPriceProvince;
- private $objPriceCache;
- /**
- * @var DPriceRangeSheet
- */
- private $objDPriceRangeSheet;
- /**
- * MPriceAdjustment constructor.
- * @param $onlineUserId
- * @param $onlineEnterpriseId
- * @throws \Exception
- */
- 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');
- }
- /**
- * 保存调价单
- * @param $sheetData
- * @return ResultWrapper
- */
- 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;
- }
- }
- }
- //self::updateEsData($sheetData);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 更新ES
- * @param $data
- * @return mixed
- */
- public function updateEsData($data)
- {
- if (empty($data)) return $data;
- foreach ($data as $key => $val) {
- $esData = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'id' => $val['id'],//自增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'],//商品id
- 'saleType' => $val['saleType'],//销售类型 1实物销售 2虚库销售
- 'salePriceAreaType' => $val['salePriceAreaType'],//销售价格生效区域类型 1全国统一价 2大区价
- 'salePriceType' => $val['salePriceType'],//销售价格类型 1销售价格 2秒杀活动价
- 'shopId' => $val['shopId'],//店铺id
- 'shopName' => $val['shopName'],//店铺名称
- 'cargoOwnerCode' => '',//货主编码
- 'createUserId' => $val['createUserId'],//创建人id
- 'createUserName' => $val['createUserName'],//创建人姓名
- 'effectiveUserName' => isset($val['effectiveUserName']) ? $val['effectiveUserName'] : '',//生效人姓名
- 'effectiveUserId' => isset($val['effectiveUserId']) ? $val['effectiveUserId'] : '',//生效人id
- 'deleteStatus' => StatusCode::$standard,//删除状态 4删除 5正常
- 'effectiveStatus' => isset($val['effectiveStatus']) ? $val['effectiveStatus'] : StatusCode::$auditStatus['auditPass'],//生效状态 1.待审 2审核通过 3未通过 4审核中
- '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) {
- //echo "es操作成功";
- //return;
- }
- //echo "es操作失败";
- }
- }
- /**
- * 生成文档id
- * @param $id
- * @return string
- */
- private function createEsDocumentId($id)
- {
- return 'EnterpriseId_' . $this->onlineEnterpriseId . '_id_' . $id;
- }
- /**
- * 最终价格保存
- * @param $sheetData
- * @return ResultWrapper
- * @throws \Exception
- */
- 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;
- /*
- case StatusCode::$salePriceAreaType['regionalPrice'];
- return self::saveRegionalPrice($sheetData);
- break;
- case StatusCode::$salePriceAreaType['districtPrice']:
- return self::formatPriceData($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;
- }
- /**
- * 保存物料最终的全国统一价格
- * @param $sheetData
- * @return ResultWrapper
- * @throws \Exception
- */
- 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);
- }
- //unset($dbResult);
- // $objDAreaPrice = new DAreaPrice();
- // // 按照企业分表
- // $tableName = 'qianniao_areaPrice_' . $this->onlineEnterpriseId;
- // $objDAreaPrice->setTable($tableName);
- //
- // $dbResult = $objDAreaPrice->delete(
- // [
- // 'shopId' => $sheetData['shopId'],
- // 'goodsId' => $sheetData['goodsId'],
- // 'salePriceType' => StatusCode::$salePriceType['salePrice']
- // ]
- // );
- // if ($dbResult === false) {
- // $objDNationalUnifiedPrice->rollBack();
- // return ResultWrapper::fail($objDAreaPrice->error(), ErrorCode::$dberror);
- // }
- $objDNationalUnifiedPrice->commit();
- //self::savePriceRange($salePriceData);
- return ResultWrapper::success($dbResult);
- }
- private function formatMerge($dbResult, $salePriceData, $salePriceAreaType)
- {
- if (!is_array($salePriceData['salePrice'])) {
- $salePriceData['salePrice'] = json_decode($salePriceData['salePrice'], true);
- }
- // 如果已经存在价格了,按照skuid合并和替换价格
- 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'];
- }
- /**
- * 报错物料最终的区域价格
- * @param $sheetData
- * @return ResultWrapper
- * @throws \Exception
- */
- 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 {
- //self::savePriceRange($salePriceData);
- 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);
- }
- /**
- * 保存区县级最终价格
- * @param int $cityid 城市id
- * @param array $districtPriceCityData 区县价格数据
- * @return ResultWrapper
- */
- 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);
- }
- }
- /**
- * 保存省级最终价格
- * @param int $provinceId 省id
- * @param array $districtPriceProvincesData 区域价省一级数据
- * @return ResultWrapper
- */
- 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);
- }
- }
- /**
- * 组装区县价格数据
- * @param array $sheetData 调单价数据
- * @return ResultWrapper
- */
- 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;
- }
- /**
- * 切割省市区归组
- * @param array $areaDatas 区域数据
- * @return array
- */
- 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;
- }
- /**
- * 批量生效操作
- * @param array $ids 要生效的当局id集合
- * @param $params
- * @return ResultWrapper
- * @throws \Exception
- */
- 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());
- }
- /**
- * 商品调价单搜索
- * @param $selectParams
- * @return ResultWrapper
- */
- public function search($selectParams)
- {
- $defaultDSL = [
- 'from' => $selectParams['offset'],
- 'size' => $selectParams['limit'],
- 'sort' => [
- 'createTime' => [
- 'order' => 'desc'
- ],
- ],
- ];
- $dsl = [];
- $dsl['query']['bool']['must'][] = [
- 'term' => ['enterpriseId' => $this->onlineEnterpriseId],
- ];
- //店铺id
- 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);
- }
- /**
- * 保存商品价格范围
- * @param array $data
- * @return ResultWrapper
- */
- 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);
- }
-
- /**
- * 商品调价单导出方法
- * @param $priceSheetData
- */
- 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['customerName'] = isset($v['customerName']) ? $v['customerName'] : '';//客户类型
- // $rows['goodsCode'] = isset($v['goodsCode']) ? $v['goodsCode'] : '';//商品编码
- $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;
- }
- }
|