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; } }