343 lines
13 KiB
PHP
343 lines
13 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: zhuxiaowei
|
||
* Date: 2019-03-19
|
||
* Time: 09:28
|
||
*/
|
||
namespace app\command;
|
||
use app\common\KeywordJson;
|
||
use app\common\service\PayService;
|
||
use app\facade\HyData;
|
||
use app\facade\Pinyin;
|
||
use app\website\service\QueryDataService;
|
||
use app\ydyd_service\model\UsersModel;
|
||
use BaiduOcr\AipOcr;
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\Db;
|
||
use think\Exception;
|
||
use think\facade\Cache;
|
||
|
||
class HyDataCommand extends Command{
|
||
|
||
protected function configure(){
|
||
$this->setName('HyDataCommand')->setDescription("华源商品入库");//这里的setName和php文件名一致,setDescription随意
|
||
}
|
||
|
||
/*
|
||
* 测试
|
||
*/
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
//华源商品抓取
|
||
try{
|
||
for ($int = 0; $int < 100; $int++)
|
||
{
|
||
$name = 'huayuangoods_'.$int;
|
||
$for = Cache::get($name);
|
||
if($for){
|
||
echo '开始抓取'.$int.'_商品名称:'.$for['KEYS']." \n";
|
||
$jsonT = '{"search_words":"'.$for['KEYS'].'","now_page":"1","price_order":"","retail_flag":"","term_validity_type":"0"}';
|
||
$data = json_decode($jsonT,true);
|
||
$hy_data=HyData::search($data,false);//参数二由$buyer_status变更为$need_price
|
||
$appId = '17614601';
|
||
$apiKey = 'INwmAAe72KANYGdwGOFrWPOL';
|
||
$secretKey = 'kWvnrMrd2VOR3q79REW8FtVrvNBsbUj1';
|
||
$api = new AipOcr($appId, $apiKey, $secretKey);
|
||
foreach ($hy_data['data'] as $inum=>$idetail){
|
||
if (isset($idetail['djimage']) && $idetail['djimage'] && isset($idetail['DJ']) && (($idetail['DJ']=='')|| $idetail['DJ']=='2000000') ){
|
||
//价格图片转文字
|
||
$img_url = 'https://www.hyey.cn'.$idetail['djimage'];
|
||
$img = file_get_contents($img_url);
|
||
$local_path = 'web'.DIRECTORY_SEPARATOR.'img'.DIRECTORY_SEPARATOR.'123456.png';
|
||
file_put_contents($local_path,$img);
|
||
$output = '';
|
||
exec('tesseract '.$local_path.' stdout', $output,$return_var);
|
||
if (!$return_var){
|
||
$tmp_dj = trim(implode('', $output));
|
||
if ($tmp_dj){
|
||
$hy_data['data'][$inum]['DJ']=$tmp_dj;
|
||
}else{
|
||
$res = $api->basicAccurate($img);
|
||
if (isset($res['words_result'])&&isset($res['words_result'][0])&&isset($res['words_result'][0]['words'])&&is_numeric($res['words_result'][0]['words'])&&$res['words_result'][0]['words']){
|
||
$hy_data['data'][$inum]['DJ'] = $res['words_result'][0]['words'];
|
||
}else{
|
||
unset($hy_data['data'][$inum]);
|
||
}
|
||
unset($img_url);
|
||
unset($img);
|
||
unset($res);
|
||
}
|
||
}else{
|
||
$res = $api->basicAccurate($img);
|
||
if (isset($res['words_result'])&&isset($res['words_result'][0])&&isset($res['words_result'][0]['words'])&&is_numeric($res['words_result'][0]['words'])&&$res['words_result'][0]['words']){
|
||
$hy_data['data'][$inum]['DJ'] = $res['words_result'][0]['words'];
|
||
}else{
|
||
unset($hy_data['data'][$inum]);
|
||
}
|
||
unset($img_url);
|
||
unset($img);
|
||
unset($res);
|
||
}
|
||
}
|
||
}
|
||
$hy_data['data'] = array_values($hy_data['data']);//重建索引
|
||
$goods_list=[];
|
||
Db::startTrans();
|
||
foreach ($hy_data['data'] as $v) {
|
||
$tmp_data=[];
|
||
$tmp_data['keyword'] = $for['KEYS'];
|
||
$tmp_data['name']=$v['YPMC']??'';
|
||
$tmp_data['is_online_sale']=$v['ZRPZ']??0;
|
||
$tmp_data['spec_desc']=$v['GG']??'';
|
||
$tmp_data['new_manufacturer_name']=$v['CDMC']??'';
|
||
$tmp_data['supplier_id']=10006;
|
||
$tmp_data['entirety_num']=$v['BZ']??'';
|
||
$tmp_data['retail_num']=$v['ZBZ']??'';
|
||
$tmp_data['goods_unit']=$v['DW']??'';
|
||
$tmp_data['drug_form']=$v['JX']??'';
|
||
$tmp_data['prescribe_flag']=0;
|
||
$tmp_data['batch_number']=$v['PH']??'';
|
||
$tmp_data['manufacture_date']=$v['YXQ']??'';
|
||
$tmp_data['batch_type']=1;
|
||
$tmp_data['country_code']=$v['PZWH']??'';
|
||
$tmp_data['stock_num']=$v['SL']??'';
|
||
if(isset($v['SL']) && $v['SL'] == '充足'){
|
||
$tmp_data['stock_num']=9999;
|
||
}
|
||
$tmp_data['show_price']=$v['LSJ']??'';
|
||
$tmp_data['cost_price']=$v['DJ']??'';
|
||
if(isset($v['DJ']) && (float)$v['DJ'] > 0 && $v['DJ'] != '会员可查'){
|
||
$a = $v['DJ']*1.05;
|
||
$tmp_data['retail_price']=$a;
|
||
}else{
|
||
if($v['LSJ'] && (float)$v['LSJ'] > 0){
|
||
$a = $v['LSJ']*1.05;
|
||
$tmp_data['retail_price']=$a;
|
||
}else{
|
||
$tmp_data['retail_price']=0;
|
||
}
|
||
}
|
||
if ($v['ISRETAIL']==1) {
|
||
$tmp_data['retail_flag']=0;
|
||
} else {
|
||
$tmp_data['retail_flag']=1;
|
||
|
||
}
|
||
$tmp_data['entirety_price']=$tmp_data['retail_price'];
|
||
$tmp_data['pc_shop_thumb_url']='www.hyey.cn'.$v['PICNAME']??'';
|
||
$tmp_data['sale_rotalty']=0.005;
|
||
$tmp_data['extend_goods_id']=$v['ID']??'';
|
||
$goods_list[]=$tmp_data;
|
||
// $this->addNewProduct($tmp_data);
|
||
}
|
||
if(count($hy_data['data']) > 0 ){
|
||
Cache::rm('huayuangoods_'.$int);
|
||
}
|
||
Db::commit();
|
||
}
|
||
}
|
||
}catch (Exception $exception){
|
||
Db::rollback();
|
||
echo $exception->getMessage()." \n";
|
||
return;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 新版商品录入接口 20190903
|
||
*
|
||
* 新增商品
|
||
*
|
||
* 涉及数据库表:
|
||
* goods、manufacturer、goods_cate_relation、
|
||
* suppliers、goods_supplier_relation、
|
||
* goods_batch、goods_batch_area_relation
|
||
* admin_log
|
||
*
|
||
* 事务操作,写入执行顺序:
|
||
* 1、 manufacturer 获取manufacturer_id
|
||
* 2、 suppliers 获取supplier_id,检测是否已存在(此处选择supplier_id,供应商账号存在未创建的情况supplier_user_id可能不存在)
|
||
* 3、 goods、goods_cate_relation
|
||
* 4、 goods_supplier_relation
|
||
* 5、 goods_batch
|
||
* 6、 goods_batch_area_relation
|
||
* 7、 admin_log
|
||
*
|
||
* 备注:
|
||
* goods/goods_status等表 状态字段status改为默认存储1(无需审核---确认操作由上下架状态字段完成)
|
||
*
|
||
* 扩展:
|
||
* 使用模型进行事务操作(可以使用模型的字段数据自动处理特性),多模型进行事务操作
|
||
*
|
||
* */
|
||
public function addNewProduct($params){
|
||
//公用数据
|
||
$admin_user_id = 0;
|
||
$ip = \request()->ip();
|
||
$date_time = date('Y-m-d H:i:s');
|
||
$date = date('Y-m-d');
|
||
|
||
/**
|
||
* 数据准备放到上文(数据验证、关联数据查询获取等)
|
||
* 具体写入操作(以及写入后的相关值获取)在事务中完成
|
||
* 异常类:自定义、tp框架、or直接使用php Exception类
|
||
* */
|
||
try{
|
||
$goods_id = 0;
|
||
//manufacturer
|
||
$manu_data = [];
|
||
$manufacturer_id = 0;
|
||
if ($params['new_manufacturer_name']){
|
||
$exist = Db::name('manufacturer')->where('name',$params['new_manufacturer_name'])->find();
|
||
if (!$exist){
|
||
$manu_data['name'] = $params['new_manufacturer_name']??'';
|
||
$py = Pinyin::getPyArray($params['new_manufacturer_name'])??'';
|
||
$manu_data['simple_pinyin'] = $py['simple_pinyin']??'';
|
||
$manu_data['resume'] = $params['new_manufacturer_name']??'';
|
||
$manu_data['description'] = $params['new_manufacturer_name']??'';
|
||
$manu_data['admin_user_id'] = $admin_user_id;
|
||
$manu_data['create_time'] = $date_time;
|
||
$manu_data['update_time'] = $date_time;
|
||
$manu_data['add_ip'] = $ip;
|
||
$manu_data['update_ip'] = $ip;
|
||
$manufacturer_id = Db::name('manufacturer')->insertGetId($manu_data);
|
||
if (!$manufacturer_id){
|
||
throw new \Exception('生产厂商数据写入失败!');
|
||
}
|
||
}else{
|
||
$manufacturer_id = $exist['id'];
|
||
}
|
||
}else{
|
||
throw new \Exception('缺少参数!');
|
||
}
|
||
|
||
//goods
|
||
$is_goods_existed = false;
|
||
//新增药品时判断是否已存在:名称+规格+剂型+单位+国药准字(+厂商)
|
||
$tmp = [];
|
||
$tmp['name'] = $params['name'];
|
||
$tmp['spec_desc'] = $params['spec_desc'];
|
||
$tmp['drug_form'] = $params['drug_form'];
|
||
$tmp['unit'] = $params['goods_unit'];
|
||
$tmp['country_code'] = $params['country_code'];
|
||
//$tmp['manufacturer'] = $params['manufacturer_name'];
|
||
//商品状态正常
|
||
$tmp['status'] = 1;
|
||
$goods_exist = Db::name('goods')->where($tmp)->find();
|
||
if ($goods_exist){
|
||
$is_goods_existed = true;
|
||
$goods_id = $goods_exist['goods_id'];
|
||
}else{
|
||
$is_goods_existed = false;
|
||
}
|
||
if (!$is_goods_existed){
|
||
$goods_data = [];
|
||
$goods_data['name'] = $params['name'];
|
||
$goods_data['keyword'] = $params['keyword']??'';
|
||
$goods_py = Pinyin::getPyArray($params['name']);
|
||
$goods_data['simple_pinyin'] = $goods_py['simple_pinyin'];
|
||
$goods_data['pinyin'] = $goods_py['pinyin'];
|
||
$goods_data['manufacturer_id'] = $manufacturer_id;
|
||
$goods_data['bar_code'] = 'test123';
|
||
$goods_data['app_gallery'] = [];
|
||
$goods_data['sketch'] = $params['sketch']??'';
|
||
$goods_data['spec_desc'] = $params['spec_desc'];
|
||
$goods_data['unit'] = $params['goods_unit'];
|
||
$goods_data['prescribe_flag'] = $params['prescribe_flag']??0;
|
||
$goods_data['status'] = 1;//商品无需审核,默认审核通过,上下架状态字段控制商品输出状态
|
||
$goods_data['show_price'] = $params['show_price'];
|
||
$goods_data['pc_thumb'] = $params['pc_shop_thumb_url'];
|
||
$goods_data['pc_gallery'] = json_encode([$params['pc_shop_thumb_url']]);
|
||
$goods_data['country_code'] = $params['country_code'];
|
||
$goods_data['drug_form'] = $params['drug_form'];
|
||
$goods_data['create_time'] = $date_time;
|
||
$goods_data['add_ip'] = $ip;
|
||
$goods_data['update_time'] = $date_time;
|
||
$goods_data['update_ip'] = $ip;
|
||
$goods_data['admin_user_id'] = $admin_user_id;
|
||
$goods_data['is_online_sale'] = $params['is_online_sale'];
|
||
|
||
$goods_id = Db::name('goods')->insertGetId($goods_data);
|
||
if (!$goods_id){
|
||
throw new \Exception('药品写入数据库失败!',-187);
|
||
}
|
||
}
|
||
//suppliers
|
||
$supplier_id = $params['supplier_id'];
|
||
$is_jh = false;//供应商是否为金壶,后文goods_batch操作需要
|
||
$gsr_data = [];
|
||
$gsr_data['goods_id'] = $goods_id;
|
||
$gsr_data['supplier_id'] = $supplier_id;//ydyd_goods_supplier_relation表新增supplier_id字段
|
||
$res_gsr_id = Db::name('goods_supplier_relation')->insertGetId($gsr_data);
|
||
if (!$res_gsr_id){
|
||
throw new \Exception('gsr insert error',-128);
|
||
}
|
||
$gb_data = [];
|
||
$gba_data_pre = [];//价格预设数据
|
||
$gb_data['goods_id'] = $goods_id;
|
||
$gb_data['relation_id'] = $res_gsr_id;
|
||
$gb_data['supplier_id'] = $supplier_id;//goods_batch新增supplier_id字段
|
||
$gb_data['supplier_sn'] = $params['extend_goods_id'];
|
||
$gb_data['supplier_user_id'] = 4;
|
||
$gb_data['batch_number'] = $params['batch_number'];
|
||
$gb_data['batch_type'] = $params['batch_type'];
|
||
$gb_data['batch_status'] = 1;
|
||
$gb_data['retail_num'] = $params['retail_num'];
|
||
$gb_data['entirety_num']= $params['entirety_num'];
|
||
$gb_data['cost_price'] = $params['cost_price'];
|
||
//最低销售价默认为0.00此处未涉及最低销售价,暂不处理
|
||
$gb_data['term_validity'] = $params['manufacture_date'].'-01';//前端提交manufacture_date字段实际对应有效期term_validity
|
||
$gb_data['source_type'] = $is_jh?0:1;//金壶:0;其他供应商:1
|
||
$gb_data['create_time'] = $date_time;
|
||
$gb_data['add_ip'] = $ip;
|
||
$gb_data['update_time'] = $date_time;
|
||
$gb_data['update_ip'] = $ip;
|
||
$gb_data['admin_user_id'] = $admin_user_id;
|
||
$gb_data['retail_flag'] = $params['retail_flag'];
|
||
$gb_data['cs_proportion'] = $params['sale_rotalty'];
|
||
$gba_data_pre['price'] = $params['retail_price'];
|
||
$gb_data['stock_num'] = $params['stock_num'];
|
||
$gb_data['total_num'] = $params['stock_num'];
|
||
$batch_id = Db::name('goods_batch')->insertGetId($gb_data);
|
||
if (!$batch_id){
|
||
throw new \Exception('gb insert operate error',-129);
|
||
}
|
||
$gba_data_basic = [];
|
||
$gba_data_basic['batch_type'] = $params['batch_type'];
|
||
$gba_data_basic['goods_id'] = $goods_id;
|
||
$gba_data_basic['platform_royalty'] = 0.005;
|
||
$gba_data_basic['is_onshelf'] = 1;//默认为下架状态(待执行上架操作)
|
||
$gba_data_basic['price_type'] = 0;//表单未提交该字段,取默认值0-正常价格
|
||
if ($batch_id){
|
||
//零/整单条价格记录
|
||
$gba_data_basic['batch_id'] = $batch_id;
|
||
$gba_data_basic['price'] = $gba_data_pre['price'];
|
||
$gba_data_basic['original_price'] = $gba_data_pre['price'];//原价,首次写入,默认当前价格
|
||
$gba_data_single = $gba_data_basic;
|
||
$res_gba_single = Db::name('goods_batch_area_relation')->insertGetId($gba_data_single);
|
||
if (!$res_gba_single){
|
||
throw new \Exception('gba insert single error',-189);
|
||
}
|
||
}
|
||
$admin_log_data = [];
|
||
$admin_log_data['admin_user_id'] = $admin_user_id;
|
||
$admin_log_data['type'] = 'insert';
|
||
$admin_log_data['content'] = $admin_user_id.'管理员新增药品,id:'.$goods_id;
|
||
$admin_log_data['add_time'] = $date_time;
|
||
$admin_log_data['add_ip'] = $ip;
|
||
$res_admin_log = Db::name('admin_log')->insert($admin_log_data);
|
||
if (!$res_admin_log){
|
||
throw new \Exception('log insert error',-276);
|
||
}
|
||
echo $params['name'].'导入成功'." \n";
|
||
return true;
|
||
} catch (\Exception $exception) {
|
||
echo $exception->getMessage()." \n";
|
||
return false;
|
||
}
|
||
|
||
}
|
||
} |