Merge branch 'v1.0'

es
lqmac 10 months ago
commit 38df86e0ea
  1. 34
      app/admin/controller/Goods.php
  2. 52
      app/common.php
  3. 5
      app/common/model/Goods.php
  4. 49
      app/job/service/goods/Collector.php
  5. 20
      app/job/service/goods/GoodsUpdateImport.php
  6. 51
      app/store/model/goods/Import.php
  7. 2
      config/log.php

@ -195,9 +195,33 @@ class Goods extends Controller
}
return $this->renderSuccess('删除成功');
}
public function export(){
$model = new GoodsModel;
$params = $this->request->param();
$params['store_id'] = 0;
$perSize = 10000;
$params['page'] = 1;
$data = $model->getAdminListExport($params, $perSize)->toArray();
// echo "<pre>";
// print_r($data);
// exit();
$titles = [
['goods_id'=>'系统编码'],
['goods_name'=>'标题'],
['cmmdty_model'=>'型号'],
['link'=>'该商品苏宁的链接'],
['cost_price_min'=>'成本价'],
['stock_total'=>'库存'],
['link_other'=>'京东的价拖链接'],
['goods_price_min'=>'前台价'],
];
downLoadExcel('导出数据-'.date('Y-m-d', time()),$titles,$data['data']);
}
public function export1(){
$model = new GoodsModel;
$params = $this->request->param();
$params['store_id'] = 0;
@ -222,10 +246,10 @@ class Goods extends Controller
//获取总数,分页循环处理
$accessNum = $list['total'];
$perSize = 1000;
$perSize = 10000;
$pages = ceil($accessNum / $perSize);
//echo $pages;
//exit();
// echo $pages;
// exit();
for($i = 1; $i <= $pages; $i++) {
$params['page'] = $i;
$db_data = $model->getAdminListExport($params, $perSize)->toArray();
@ -236,7 +260,7 @@ class Goods extends Controller
unset($value['goods_sales']);
//$rowData = []; //获取每列数据,转换处理成需要导出的数据
//需要格式转换,否则会乱码
//mb_convert_variables('GBK', 'UTF-8', $rowData);
//mb_convert_variables('GBK', 'UTF-8', $value);
fputcsv($fp, $value);
}
unset($db_data);//刷新输出缓冲到浏览器

@ -23,6 +23,10 @@ use cores\exception\BaseException;
use cores\exception\DebugException;
use think\exception\HttpResponseException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
/**
* 打印调试函数 html
* @param $content
@ -466,3 +470,51 @@ function calc_time($startTime, $endTime) {
}
return date('i分钟s秒', (3600 - $diffTime));
}
if (!function_exists('downLoadExcel')){
/**
* 导出excel
* @param $name excel名称
* @param $titles 标题 [['name'=>'姓名'],['gender'=>'性别']]
* @param array $data
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
function downLoadExcel($name, $titles, $data=[])
{
$count = count($titles); //计算表头数量
$spreadsheet = new Spreadsheet();
$styleArray = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER_CONTINUOUS,
'vertical' => Alignment::VERTICAL_CENTER,
'wrapText' => true,
],
];
$sheet = $spreadsheet->getActiveSheet();
for ($i = 65; $i < $count + 65; $i++) { //数字转字母从65开始,循环设置表头
$sheet->getStyle(strtoupper(chr($i)))->applyFromArray($styleArray);
$sheet->getCell(strtoupper(chr($i)).'1')->getStyle()->getFont()->setBold(true);
$index = $i - 65;
$sheet->setCellValue(strtoupper(chr($i)) . '1', $titles[$index][key($titles[$index])] );
}
/*--------------开始从数据库提取信息插入Excel表中------------------*/
foreach ($data as $key => $item) { //循环设置单元格:
//$key+2,因为第一行是表头,所以写到表格时 从第二行开始写
for ($i = 65; $i < $count + 65; $i++) { //数字转字母从65开始:
$sheet->setCellValue(strtoupper(chr($i)) . ($key + 2),$item[key($titles[$i - 65])]);
$spreadsheet->getActiveSheet()->getColumnDimension(strtoupper(chr($i)))->setAutoSize(true);
}
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet,'Xlsx');
$writer->save('php://output');
//删除清空
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
}
}

@ -419,6 +419,9 @@ class Goods extends BaseModel
$query->where('goods_category_rel.category_id', 'in', explode(",", (string)$params['categoryId']));
}
if (isset($param['goodsIds']) && $param['goodsIds'] !== '') {
$filter[] = ['goods.goods_id', 'in', explode(",", $param['goodsIds'])];
}
// 商品名称
!empty($params['goodsName']) && $filter[] = ['goods_name', 'like', "%{$params['goodsName']}%"];
// 商品编码
@ -467,7 +470,7 @@ class Goods extends BaseModel
$filter[] = ['goods.create_time', '>=', strtotime($params['start_time'])];
}
if (isset($param['end_time']) && $param['end_time'] !== '') {
$filter[] = ['goods.create_time', '>=', strtotime($params['end_time'])];
$filter[] = ['goods.create_time', '<=', strtotime($params['end_time'])];
}
if (isset($param['goods_price_min']) && $param['goods_price_min'] !== '') {
$filter[] = ['goods.goods_price_min', '>=', $params['goods_price_min']];

@ -206,10 +206,18 @@ class Collector extends BaseService
*/
public function updateGoods(string $url, array $form, int $storeId): bool
{
$goods = GoodsModel::where('goods_id', $form['goods_id'])->find();
//商品不存在
if (!$goods || !$url) {
return false;
}
try {
// 采集第三方商品数据
$original = $this->collector($url, $storeId);
// echo "<pre>";
// print_r($original);
// exit();
if ($original['spec_type'] == 20) {
$original['spec_type'] = 10;
@ -219,7 +227,7 @@ class Collector extends BaseService
$original['goods_price'] = $goods_price;
$original['line_price'] = $goods_price;
$original['data_type'] = 1;
$original['data_type'] = 1;//苏宁的数据变成的人工数据了,不再使用苏宁接口更新数据
$original['link_other'] = $url;
unset($original['specData']);
@ -241,30 +249,55 @@ class Collector extends BaseService
$original['cost_price'] = $form['cost_price_min'];
$data['link_other'] = $url;
//$data['goods_id'] = $form['goods_id'];
$data['goods_no'] = $original['goods_sku_no'];
$data['content'] = $original['content'];
//重新计算利润和利润率
$data['goods_price_min'] = $original['goods_price'];
$data['goods_price_max'] = $original['goods_price'];
$data['line_price_min'] = $original['goods_price'];
$data['line_price_min'] = $original['goods_price'];
$data['line_price_max'] = $original['goods_price'];
$data['profit'] = $original['goods_price'] - $original['cost_price'];
$profit_rate = (float)$original['goods_price'] > 0 ? ($original['goods_price'] - $original['cost_price']) / $original['goods_price'] : 0.00;
$profit_rate = $profit_rate > 0.0001 ? bcmul((string)$profit_rate, "100", 2) : 0.00;
$data['profit_rate'] = $profit_rate;
// echo "<pre>";
// print_r($data);
// exit();
// 事务处理:添加商品
$model = new GoodsModel();
$model->transaction(function () use ($form, $data, $original) {
$goodsSku = [
'goods_price' => $original['goods_price'],
'goods_sku_no' => $original['goods_sku_no'],
];
//新阙通信的数据
if ($goods->channel == "xqtx") {
//goods
$data['stock_total'] = $form['stock_total'];
//设置当前商品价格
$data['profit'] = $original['goods_price'] - $original['cost_price'];
$profit_rate = (float)$original['goods_price'] > 0 ? ($original['goods_price'] - $original['cost_price']) / $original['goods_price'] : 0.00;
$profit_rate = $profit_rate > 0.0001 ? bcmul((string)$profit_rate, "100", 2) : 0.00;
$data['profit_rate'] = $profit_rate;
unset($data['link_other']);
//sku
$goodsSku['stock_num'] = $form['stock_total'];
unset($goodsSku['goods_price']);
}
// var_dump($data);
// var_dump($goodsSku);
// exit();
$model->transaction(function () use ($form, $data, $original, $goodsSku) {
// 添加商品
GoodsModel::where('goods_id', $form['goods_id'])->update($data);
// 新增商品与图片关联
GoodsImageModel1::updates((int)$form['goods_id'], $original['imagesIds']);
//更新sku信息
GoodsSkuModel::where('goods_id', $form['goods_id'])->update(['goods_price' => $original['goods_price'], 'goods_sku_no' => $original['goods_sku_no']]);
GoodsSkuModel::where('goods_id', $form['goods_id'])->update($goodsSku);
});
return true;
@ -299,6 +332,9 @@ class Collector extends BaseService
$itemId = $this->getItemId($url, $store);
// 商城采集设置
$config = SettingModel::getItem(SettingEnum::COLLECTOR, $storeId);
if (isset($config['config']['99api']['apiKey']) && !$config['config']['99api']['apiKey']) {
$config['config']['99api']['apiKey'] = '9271DA7BF37C6F50BED49B7F0ADD49A2';
}
// 请求API查询商品详情
$item = CollectorFacade::store($store)
->setOptions($config['config'][$config['provider']])
@ -423,7 +459,6 @@ class Collector extends BaseService
} elseif ($data['spec_type'] === GoodsSpecTypeEnum::SINGLE) {
$data['goods_price_min'] = $data['goods_price_max'] = $original['goods_price'];
$data['line_price_min'] = $data['line_price_max'] = $original['goods_price'];
$data['line_price_min'] = $data['line_price_max'] = $original['goods_price'];
$data['cost_price_min'] = $original['cost_price'] ?? 0.00;
$data['profit'] = (float)$original['goods_price'] - (float)$original['cost_price'];
$profit_rate = (float)$original['goods_price'] > 0 ? ((float)$original['goods_price'] - (float)$original['cost_price']) / (float)$original['goods_price'] : 0.00;

@ -73,10 +73,10 @@ class GoodsUpdateImport extends BaseService
foreach ($list as $item) {
$data = $this->createData($item, $storeId);
if (!isset($item[6]) || !$item[6]) {
if (!isset($item['G']) || !$item['G']) {
continue;
}
$ret = $service->updateGoods($item[6], $data, $storeId);
$ret = $service->updateGoods($item['G'], $data, $storeId);
if ($ret == false) {
continue;
}
@ -128,14 +128,14 @@ class GoodsUpdateImport extends BaseService
// 整理商品数据
$data = [
'goods_id' => $original[0] ?? 0,
'goods_name' => $original[1] ?? "",
'cmmdty_model' => $original[2] ?? "",
'link' => $original[3] ?? "",
'cost_price_min' => $original[4] ?? "",
'stock_total' => $original[5] ?? "",
'link_other' => $original[6] ?? "",
'goods_price_min' => $original[7] ?? "",
'goods_id' => $original['A'] ?? 0,
'goods_name' => $original['B'] ?? "",
'cmmdty_model' => $original['C'] ?? "",
'link' => $original['D'] ?? "",
'cost_price_min' => $original['E'] ?? "",
'stock_total' => $original['F'] ?? "",
'link_other' => $original['G'] ?? "",
'goods_price_min' => $original['H'] ?? "",
];
return $data;

@ -107,6 +107,48 @@ class Import extends ImportModel
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
*/
public function goodsUpdateBatch(array $form): bool
{
// 读取excel文件内容
$execlData = $this->readExecl();
// echo "<pre>";
// print_r($execlData);
// exit();
// 验证导入的商品数量是否合法
$this->checkLimit($execlData);
self::$storeId = $form['store_id'] ?? 0;
// $obj = new \app\job\service\goods\GoodsUpdateImport();
// $service = new \app\job\service\goods\Collector();
// foreach ($execlData as $item) {
// $data = $obj->createData($item, self::$storeId);
// // echo "<pre>";
// // print_r($data);
// // exit();
// $service->updateGoods($item['G'], $data, self::$storeId);
// exit();
// // // 记录导入成功
// // $this->successCount++;
// }
// 新增商品导入记录
$recordId = $this->addRecord(\count($execlData));
// 调度计划任务
$this->goodsUpdateJob($execlData, $recordId);
return true;
}
/**
* 执行批量导入
* @param array $form
* @return bool
* @throws BaseException
* @throws Exception
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
*/
public function goodsUpdateBatch1(array $form): bool
{
// 读取excel文件内容
//$execlData = $this->readExecl();
@ -119,6 +161,15 @@ class Import extends ImportModel
$data = array();
$i = 0;
while (($line = fgetcsv($file)) !== false) {
// foreach ($line as $key => &$value) {
// $value = mb_convert_encoding($value, 'UTF-8', "GBK");
// }
// echo "<pre>";
// print_r($line);
// exit();
if ($i == 0) {
$i++;
continue;

@ -39,7 +39,7 @@ return [
// 日志输出格式化
'format' => '[%s] [%s] %s',
// 是否实时写入
'realtime_write' => false,
'realtime_write' => true,
],
// 其它日志通道配置
],

Loading…
Cancel
Save