981 lines
39 KiB
PHP
981 lines
39 KiB
PHP
<?php
|
||
|
||
namespace app\common\service;
|
||
|
||
use app\common\model\SkuModel;
|
||
use app\facade\AppConstant;
|
||
use app\facade\HyData;
|
||
use app\facade\Pinyin;
|
||
use think\Exception;
|
||
use think\facade\Cache;
|
||
use think\facade\Config;
|
||
use think\facade\Request;
|
||
use traits\controller\Jump;
|
||
use think\Db;
|
||
|
||
class GoodsService
|
||
{
|
||
use Jump;
|
||
const SEARCH_LIMIT=10;
|
||
const NO_HY_PRICE = '已售罄';
|
||
const APP_DEFAULT_IMG_URL = 'ydyd-develop-file.oss-cn-hangzhou.aliyuncs.com/pc_shop_pic_url_1588925532218.jpg';
|
||
|
||
|
||
/**
|
||
* 商品管理列表20190907
|
||
* 数据检索字段:名称、供应商、厂商、商品上下架状态、批次号、批次类型
|
||
*/
|
||
public function getList($search_data = [])
|
||
{
|
||
$_where = [];
|
||
$_where[] = ['gba.is_delete','=',0];
|
||
//默认排序
|
||
$_order = ['gba.is_onshelf'=>'asc','g.goods_id'=>'desc'];
|
||
|
||
//提成比例排序
|
||
if (isset($search_data['iSortCol_0'])&&isset($search_data['sSortDir_0'])&&($search_data['iSortCol_0']==10)&&in_array($search_data['sSortDir_0'],['asc','desc'])){
|
||
$_order = ['gba.platform_royalty'=>$search_data['sSortDir_0'],'gba.is_onshelf'=>'asc','g.goods_id'=>'desc'];
|
||
}
|
||
//商品名称:拼音+中文
|
||
if (isset($search_data['goods_name'])&&$search_data['goods_name']){
|
||
//判断$search_data['goods_name']是否含有中文;注意:1.药品名称为英文也要搜索药品名称(此时不含中文) 2.药品名称中文+字母混合,搜索字母部分也要搜索药品名称
|
||
//临时封装函数is_str_include_chinese,待完善
|
||
if (is_str_include_chinese($search_data['goods_name'])){
|
||
$_where[] = ['g.name','like','%'.$search_data['goods_name'].'%'];
|
||
}else{
|
||
$_where[] = ['g.name|g.simple_pinyin|g.pinyin','like','%'.$search_data['goods_name'].'%'];
|
||
}
|
||
}
|
||
|
||
if (isset($search_data['batch_number'])&&$search_data['batch_number']){
|
||
$_where[] = ['gb.batch_number','=',$search_data['batch_number']];
|
||
}
|
||
if (isset($search_data['batch_type'])&&in_array($search_data['batch_type'],['1','2','all'])){
|
||
if (in_array($search_data['batch_type'],['1','2'])){
|
||
$_where[] = ['gb.batch_type','=',$search_data['batch_type']];
|
||
}
|
||
}
|
||
if (isset($search_data['supplier'])&&$search_data['supplier']){
|
||
$_where[] = ['s.supplier_name','like','%'.$search_data['supplier'].'%'];
|
||
}
|
||
|
||
if (isset($search_data['manufacturer'])&&$search_data['manufacturer']){
|
||
$_where[] = ['m.name','like','%'.$search_data['manufacturer'].'%'];
|
||
}
|
||
if (isset($search_data['is_onshelf'])&&in_array($search_data['is_onshelf'],['0','1','2','all'])){
|
||
if (in_array($search_data['is_onshelf'],['0','1','2'])){
|
||
$_where[] = ['gba.is_onshelf','=',$search_data['is_onshelf']];
|
||
}
|
||
}
|
||
if(isset($search_data['inIds']) && $search_data['inIds']){
|
||
$_where[] = ['gba.id','in',$search_data['inIds']];
|
||
}
|
||
|
||
if(isset($search_data['notIds']) && $search_data['notIds']){
|
||
$_where[] = ['gba.id','not in',$search_data['notIds']];
|
||
}
|
||
|
||
if(isset($search_data['isOnlySelf']) && $search_data['isOnlySelf']){
|
||
$_where[] = ['gb.supplier_id','=',10000];
|
||
}
|
||
|
||
$_field = '';
|
||
//goods
|
||
$_field.='g.goods_id,g.name,g.spec_desc,g.unit,g.drug_form,g.sort_order,app_thumb,pc_thumb,';
|
||
//supplier
|
||
$_field.='s.supplier_name,';
|
||
//manufacturer
|
||
$_field.='m.name as manufacturer_name,';
|
||
//goods_batch
|
||
$_field.='gb.batch_number,gb.retail_flag,gb.batch_type,gb.cost_price,gb.stock_num,';
|
||
//gba
|
||
$_field.='gba.id as gba_id,gba.is_onshelf,gba.area_id,gba.price,gba.platform_royalty,gba.discount_price,gba.discount_num,gba.sort';
|
||
//药品列表数据
|
||
$res_list = Db::name('goods_batch_area_relation')->alias('gba')->field($_field)
|
||
->leftJoin('goods_batch gb','gba.batch_id=gb.batch_id')
|
||
->leftJoin('goods g','g.goods_id=gb.goods_id')
|
||
->leftJoin('goods_supplier_relation gsr','gb.relation_id=gsr.id')
|
||
->leftJoin('suppliers s','s.supplier_id=gsr.supplier_id')
|
||
->leftJoin('manufacturer m','m.id=g.manufacturer_id')
|
||
->where($_where)
|
||
->order($_order)
|
||
->paginate($search_data['limit'], false, ['page' => $search_data['page']]);
|
||
|
||
return $res_list;
|
||
}
|
||
|
||
|
||
/**
|
||
* 商品列表-默认
|
||
*/
|
||
public function getGoodsList($params){
|
||
$per_page = $params['per_page']??20;
|
||
$current_page = $params['current_page']??1;
|
||
|
||
$fields = 'g.goods_id,g.name as goods_name,g.show_price,g.is_online_sale,g.country_code,g.app_thumb,g.pc_thumb,g.drug_form,g.keyword,g.sketch,m.name as manufacturer_name,g.spec_desc,';
|
||
$fields .= 'spu.batch_type,spu.retail_flag,spu.retail_num,spu.entirety_num,spu.batch_number,spu.stock_num,spu.term_validity,spu.supplier_sn,';
|
||
$fields .= 'sku.id as sku_id,sku.area_id,sku.price,sku.price_type,sku.discount_price,sku.discount_num,sku.discount_start_time,sku.discount_end_time';
|
||
|
||
$where = [];
|
||
$where[] = ['g.status','=',1];
|
||
$where[] = ['spu.batch_status','=',1];
|
||
$where[] = ['spu.stock_num','>',0];
|
||
$where[] = ['sku.is_onshelf','=',1];
|
||
$where[] = ['sku.is_delete','=',0];
|
||
|
||
$order_by = [];
|
||
$order_by[] = ['sku.sort','asc'];
|
||
$order_by[] = ['sku.id','desc'];
|
||
$order_by[] = ['spu.batch_id','desc'];
|
||
$order_by[] = ['g.goods_id','desc'];
|
||
|
||
$goods_list = Db::name('goods_batch_area_relation')->alias('sku')
|
||
->field($fields)
|
||
->join('goods_batch spu','spu.batch_id=sku.batch_id')
|
||
->join('goods g','g.goods_id=spu.goods_id')
|
||
->join('manufacturer m','m.id=g.manufacturer_id')
|
||
->where($where)->order($order_by)
|
||
->paginate($per_page,false,['page'=>$current_page])
|
||
->toArray();
|
||
|
||
$res = [];
|
||
$res['code'] = 200;
|
||
$res['msg'] = 'success';
|
||
$res['data'] = $goods_list;
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 商品详情
|
||
* @done 用户收藏
|
||
* @done 已购状态(首营逻辑已变更-无需处理(默认认证即完成首营资料))
|
||
* @done 控销采购权
|
||
* @todo 新人价:新人对应新人活动价,老客户对应常规销售价格
|
||
* @todo 活动商品库存管理---订单处理---goods_batch add 活动库存字段 or 操作价格表库存字段discount_number
|
||
* @todo 活动-关联sku设置
|
||
*
|
||
*/
|
||
public function getGoodsDetail($init_data,$sku_id){
|
||
//$sku_id = input('param.sku_id');
|
||
$login = $init_data?true:false;
|
||
|
||
$fields = '"0" as is_collect,g.goods_id,g.name as goods_name,g.prescribe_flag,g.pc_content,g.app_content,g.show_price,g.is_online_sale,g.country_code,g.app_thumb,g.pc_thumb,g.drug_form,g.keyword,g.sketch,m.name as manufacturer_name,g.spec_desc,';
|
||
//限购字段
|
||
$fields .= 'g.max_purchase,g.max_purchase_period,g.unit,';
|
||
$fields .= 'spu.batch_id,spu.batch_type,spu.retail_flag,spu.retail_num,spu.entirety_num,spu.batch_number,spu.stock_num,spu.term_validity,spu.supplier_id,spu.supplier_sn,';
|
||
$fields .= 'sku.id as price_id,sku.original_price,sku.area_id,sku.price,sku.price_type,sku.new_price,sku.discount_price,sku.discount_num,sku.discount_start_time,sku.discount_end_time';
|
||
|
||
$where = [];
|
||
$where[] = ['g.status','=',1];
|
||
$where[] = ['g.is_online_sale','=',0];
|
||
$where[] = ['spu.batch_status','=',1];
|
||
$where[] = ['spu.stock_num','>',0];
|
||
$where[] = ['sku.is_onshelf','=',1];
|
||
$where[] = ['sku.is_delete','=',0];
|
||
|
||
$detail = Db::name('goods_batch_area_relation')->alias('sku')
|
||
->field($fields)
|
||
->join('goods_batch spu','spu.batch_id=sku.batch_id')
|
||
->join('goods g','spu.goods_id=g.goods_id')->join('manufacturer m','m.id=g.manufacturer_id')
|
||
->where('sku.id',$sku_id)->find();
|
||
|
||
if (!$detail){
|
||
$res = [];
|
||
$res['code'] = -107;
|
||
$res['message'] = '商品不存在或者已下架';
|
||
|
||
return $res;
|
||
}
|
||
//库存预处理
|
||
$sale_pack_num = '';
|
||
if ($detail){
|
||
$detail['is_buy'] = 1;//是否可以加入购物车
|
||
$sale_pack_num = $detail['retail_flag']==0?$detail['retail_num']:$detail['entirety_num'];
|
||
if (empty($detail['pc_thumb'])){
|
||
$detail['pc_thumb'] = $detail['app_thumb']?:self::APP_DEFAULT_IMG_URL;
|
||
}
|
||
if($detail['pc_thumb'] == 'www.hyey.cn'){
|
||
$detail['pc_thumb'] = self::APP_DEFAULT_IMG_URL;
|
||
}
|
||
//非网处理
|
||
if (isset($detail['is_online_sale'])&&$detail['is_online_sale']==1){
|
||
$detail['is_buy'] = 0;
|
||
}
|
||
|
||
//是否已加入购物车
|
||
$detail['is_in_cart'] = 0;
|
||
$detail['num_in_cart'] = 0;
|
||
if ($login){
|
||
$cart_where = [];
|
||
$cart_where[] = ['user_id','=',$init_data['user_id']];
|
||
$cart_where[] = ['price_id','=',$sku_id];
|
||
$cart_where[] = ['price_type','=',0];//冗余-兼容代理价格
|
||
$num_in_cart = Db::name('user_cart')->where($cart_where)->value('goods_number');
|
||
if ($num_in_cart){
|
||
$detail['is_in_cart'] = 1;
|
||
$detail['num_in_cart'] = $num_in_cart;
|
||
}
|
||
}
|
||
|
||
|
||
//处理hy商品价格更新
|
||
//@todo hy被更改为自营待处理(列表已过滤,此处待完善)
|
||
if ($detail['supplier_id']==10006){
|
||
if ($login){
|
||
$extend_price_rate = $init_data['extend_price_rate']??1.05;
|
||
$hy_price = $this->getHyPrice($detail['supplier_sn'],$extend_price_rate);
|
||
if ($hy_price){
|
||
$detail['price'] = $hy_price;
|
||
}else{
|
||
$detail['price'] = self::NO_HY_PRICE;
|
||
$detail['is_buy'] = 0;
|
||
}
|
||
}else{
|
||
$detail['price'] = '登录后查看价格';
|
||
$detail['is_buy'] = 0;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
//价格处理:活动价(特殊情况:新人活动价)/正常销售价
|
||
/**
|
||
* 新人活动:
|
||
* 1.用户新人身份判定(users表判定字段)
|
||
* 2.新人活动判定,activity_name='新人优惠' 查询对应activity_id 绑定的商品price_id
|
||
* 3.当前商品sku_id是否包含于2的price_id
|
||
*/
|
||
//存在活动价格设置为0(数据库记录 0.0000/0.00)的情况
|
||
if ($detail && $detail['supplier_id']!=10006 && isset($detail['discount_price'])){
|
||
|
||
//$sale_pack_num = $detail['retail_flag']==0?$detail['retail_num']:$detail['entirety_num'];
|
||
|
||
//discount_start_time/discount_end_time int(10)
|
||
//@todo 活动价格与正常销售价格都存在,且当前活动状态正常(时间有效且库存满足),只保留活动价,不展示正常销售价格(else: 显示 活动价格+正常价格 ,当前处理为不共存)
|
||
$date = time();
|
||
// if ($detail['discount_start_time']<$date && $detail['discount_end_time']>$date && $detail['discount_num']>=$sale_pack_num){
|
||
if (((!$detail['discount_start_time'] && !$detail['discount_end_time']) || ($detail['discount_start_time']<$date && $detail['discount_end_time']>$date)) && ($detail['discount_num']>=$sale_pack_num || $detail['discount_num']==0)){
|
||
if ($detail['discount_price']*10000){
|
||
$detail['price'] = $detail['discount_price'];
|
||
}
|
||
//$detail['stock_num'] = $detail['discount_num'];
|
||
//unset($detail['price']);//保留new_price,后文处理
|
||
unset($detail['discount_price']);
|
||
}else{
|
||
//活动状态无效,保留常规销售价格
|
||
unset($detail['discount_start_time']);
|
||
unset($detail['discount_end_time']);
|
||
unset($detail['discount_price']);
|
||
unset($detail['new_price']);//新人价格、活动价格 默认周期一样
|
||
}
|
||
}
|
||
|
||
|
||
//收藏状态+新人价格处理(新人活动价or普通活动价or常规销售价)
|
||
if ($login && $detail){
|
||
$detail['is_collect'] = 0;
|
||
$where = [];
|
||
$where[] = ['user_id','=',$init_data['user_id']];
|
||
$where[] = ['goods_id','=',$detail['goods_id']];
|
||
$user_collect = Db::name('user_collect')->where($where)->find();
|
||
if ($user_collect){
|
||
$detail['is_collect'] = 1;
|
||
}
|
||
|
||
//新人身份,活动有效性上文已判定
|
||
$is_new_user = $init_data['is_new_user'];
|
||
$date = time();
|
||
if ($is_new_user && isset($detail['new_price']) && ((!$detail['discount_start_time'] && !$detail['discount_end_time']) || ($detail['discount_start_time']<$date && $detail['discount_end_time']>$date)) && ($detail['discount_num']>=$sale_pack_num || $detail['discount_num']==0)){
|
||
//上文已处理,活动价与常规销售价不共存
|
||
/* if (isset($detail['price'])){
|
||
unset($detail['price']);
|
||
}*/
|
||
if (isset($detail['discount_price'])){
|
||
unset($detail['discount_price']);
|
||
}
|
||
if ($detail['new_price']*10000){
|
||
$detail['price'] = $detail['new_price'];
|
||
}
|
||
unset($detail['new_price']);
|
||
//$detail['stock_num'] = $detail['discount_num'];
|
||
//$detail['discount_num'] && $detail['stock_num'] = $detail['discount_num'];
|
||
}
|
||
|
||
if (!$is_new_user){
|
||
if (isset($detail['new_price'])){
|
||
unset($detail['new_price']);
|
||
}
|
||
}
|
||
}
|
||
|
||
//控销采购--只针对优势商品
|
||
if ($detail && isset($detail['batch_type']) && $detail['batch_type']==2 && $login && $detail['supplier_id']!=10006){
|
||
$where = [];
|
||
$where[] = ['goods_id','=',$detail['goods_id']];
|
||
$where[] = ['user_id','=',$init_data['user_id']];
|
||
$where[] = ['status','=',1];
|
||
|
||
$goods_limit_check = Db::name('goods_limit_relation')->where($where)->find();
|
||
if ($goods_limit_check){
|
||
switch ($goods_limit_check['status']){
|
||
case 0:
|
||
$detail['purchase_right']= 0;
|
||
$detail['purchase_right_remark'] = '采购权申请中,等待审核通过后购买!';
|
||
break;
|
||
case 1:
|
||
$detail['purchase_right']= 1;
|
||
$detail['purchase_right_remark'] = '采购权已通过!';
|
||
break;
|
||
case 2:
|
||
$detail['purchase_right']= 2;
|
||
$detail['purchase_right_remark'] = '采购权申请未通过!';
|
||
$detail['purchase_right_verify_msg'] = $goods_limit_check['verify_remark'];
|
||
break;
|
||
}
|
||
}else{
|
||
$detail['purchase_right'] = -1;
|
||
$detail['purchase_right_remark'] = '当前商品为控销商品,尚未未申请采购权!';
|
||
}
|
||
|
||
}
|
||
|
||
//限购处理
|
||
if ($detail){
|
||
$detail['is_limit'] = 0;
|
||
//限购逻辑:优势+限购周期+限购量
|
||
if ($detail['supplier_id']!=10006 &&isset($detail['max_purchase'])&&isset($detail['max_purchase_period'])&&$detail['max_purchase']&&$detail['max_purchase_period']){
|
||
$detail['is_limit'] = 1;
|
||
$detail['limit_info'] = $detail['max_purchase_period'].'天内限购'.$detail['max_purchase'].$detail['unit'];
|
||
}
|
||
}
|
||
|
||
//登录状态-价格展示
|
||
if(!$login){
|
||
isset($detail['price'])&&($detail['price']= '登录后查价');
|
||
isset($detail['new_price'])&&($detail['new_price']= '登录后查价');
|
||
isset($detail['discount_price'])&&($detail['discount_price']= '登录后查看当前价格');
|
||
isset($detail['original_price'])&&($detail['original_price']= '登录后查看价格');
|
||
|
||
}
|
||
|
||
//价格字段统一处理成price(discount_price、new_price、price)
|
||
//优先级 new_price》discount_price》price
|
||
if ($detail){
|
||
if (isset($detail['new_price']) && $detail['new_price']*10000 && isset($detail['discount_price'])&&$detail['discount_price']*10000){
|
||
$detail['price'] = $detail['new_price']??$detail['discount_price']??$detail['price'];
|
||
}
|
||
|
||
}
|
||
|
||
|
||
//存在or空或0值
|
||
|
||
$res = [];
|
||
|
||
if ($detail){
|
||
$res['code'] = 200;
|
||
$res['message'] = 'success';
|
||
$res['data'] = $detail;
|
||
}else{
|
||
$res['code'] = -100;
|
||
$res['message'] = '商品不存在或者已下架';
|
||
}
|
||
|
||
return $res;
|
||
|
||
}
|
||
|
||
/**
|
||
* 商品数据-此处实现列表+搜索;
|
||
* 活动+专区拆分独立
|
||
*
|
||
* 默认搜索支持:药品名称(关键词)、分类(cate_id)、
|
||
* 药品分类(cate_id)+活动楼层:活动(activity_id)--列表页实现?
|
||
*/
|
||
public function getGoodsSearch($init_data,$param){
|
||
|
||
$login = $init_data?true:false;
|
||
$fields = '';
|
||
$order = [];
|
||
//自营排序优先级>流通
|
||
$order['spu.supplier_id'] = 'asc';
|
||
$where = [];
|
||
//基础条件
|
||
$where[] = ['g.status','=',1];
|
||
$where[] = ['g.is_online_sale','=',0];
|
||
$where[] = ['spu.batch_status','=',1];
|
||
$where[] = ['spu.stock_num','>',0];
|
||
$where[] = ['sku.is_onshelf','=',1];
|
||
$where[] = ['sku.is_delete','=',0];
|
||
//有效期
|
||
$where[] = ['spu.term_validity','>',date('Y-m-d')];
|
||
|
||
//@todo 导致筛选条件变更的因素关联筛选条件处理
|
||
//分类
|
||
if (isset($param['cate_id']) && $param['cate_id']){
|
||
//goods_id
|
||
$goods_id_list = Db::name('goods_cate_relation')->where('cate_id',$param['cate_id'])->column('goods_id');
|
||
if ($goods_id_list){
|
||
$where[] = ['g.goods_id','in',$goods_id_list];
|
||
}else{
|
||
$where[] = ['g.goods_id','=',0];
|
||
}
|
||
}
|
||
|
||
//搜索词---暂时只处理药品名称搜索,不考虑 药品+规格+厂商等
|
||
$search_words = $param['search_words']??'';
|
||
if ($search_words){
|
||
$where[] = ['g.name|g.simple_pinyin|g.pinyin','like','%'.htmlspecialchars($search_words).'%'];
|
||
}
|
||
|
||
//活动:关键词存在则不查询活动
|
||
$is_activity = false;
|
||
if (!$search_words && isset($param['activity_id']) && $param['activity_id']){
|
||
$activity_sku_id_list = Db::name('activity_goods_relation')->where('activity_id',$param['activity_id'])->column('price_id');
|
||
//$where[] = ['sku.id','in',$activity_sku_id_list];
|
||
if ($activity_sku_id_list){
|
||
$where[] = ['sku.id','in',$activity_sku_id_list];
|
||
$is_activity = true;
|
||
}else{
|
||
$where[] = ['sku.id','=',0];
|
||
}
|
||
}
|
||
|
||
|
||
|
||
//零整
|
||
$retail_flag = $param['retail_flag']??'';//取值空/0/1/
|
||
if ($retail_flag!==''){
|
||
$where[] = ['spu.retail_flag','=',$retail_flag];
|
||
}
|
||
|
||
//剂型
|
||
$drug_form = $param['drug_form']??'';//后期剂型--改为int+维护剂型缓存数据
|
||
if ($drug_form!==''){
|
||
$where[] = ['g.drug_form','=',$drug_form];
|
||
}
|
||
|
||
//有效期 0:半年以上,1:一年以上,2:所有,3:近效期
|
||
$term_validity = $param['term_validity']??2;
|
||
if ($term_validity!==2){
|
||
switch ($term_validity){
|
||
case 0:
|
||
$where[] = ['spu.term_validity','>',date('Y-m-d',strtotime('+6 month'))];
|
||
break;
|
||
case 1:
|
||
$where[] = ['spu.term_validity','>',date('Y-m-d',strtotime('+1 year'))];
|
||
break;
|
||
case 3:
|
||
$where[] = ['spu.term_validity','<',date('Y-m-d',strtotime('+90 day'))];
|
||
}
|
||
}
|
||
|
||
//价格区间--销售价;活动价or正常销售价处于搜索价格区间
|
||
$price_start = $param['price_start']??'';
|
||
$price_end = $param['price_end']??'';
|
||
if ($price_start!=='' && $price_end!==''){
|
||
$where[] = ['sku.price|sku.discount_price','between',[$price_start,$price_end]];
|
||
}
|
||
|
||
//排序处理
|
||
if ($is_activity){
|
||
$order['sku.sort'] = 'desc';
|
||
}
|
||
$sort_field = $param['sort_field']??'';
|
||
$sort_order = $param['sort_order']??0;
|
||
$sort_fields_limit = ['price','default','sale','term_validity'];
|
||
if ($sort_field && in_array_case($sort_field,$sort_fields_limit)){
|
||
$sort_order_seq = ['asc','desc'];
|
||
$sort_order = $sort_order_seq[(int)$sort_order]??'asc';
|
||
switch ($sort_field){
|
||
case 'price':
|
||
//$order[] = ['sku.price|sku.discount_price',$sort_order];
|
||
$order['sku.price']=$sort_order;
|
||
break;
|
||
case 'sale':
|
||
$order['spu.sale_num'] = $sort_order;
|
||
break;
|
||
case 'term_validity':
|
||
$order['spu.term_validity'] = $sort_order;
|
||
break;
|
||
}
|
||
}
|
||
|
||
//默认排序
|
||
$order['sku.sort'] = 'asc';
|
||
$order['sku.id'] = 'desc';
|
||
$order['spu.batch_id'] = 'desc';
|
||
$order['g.goods_id'] = 'desc';
|
||
//初始化信息,登录状态、认证状态
|
||
if ($login==false){
|
||
$fields .='"价格登录可见" as price,"价格登录可见" as original_price,"活动价格登录可见" as discount_price,';//下文处理是否为活动商品
|
||
}else{
|
||
$fields .='sku.price,sku.original_price as original_price, sku.discount_price,';//活动价、正常销售价?
|
||
}
|
||
|
||
$fields .= 'g.goods_id,g.name as goods_name,g.max_purchase_period,g.max_purchase,g.show_price,g.is_online_sale,g.country_code,g.app_thumb,g.pc_thumb,g.drug_form,g.keyword,g.sketch,m.name as manufacturer_name,g.spec_desc,';
|
||
//限购字段
|
||
$fields .= 'g.max_purchase,g.max_purchase_period,g.unit,';
|
||
|
||
$fields .= 'spu.batch_type,spu.supplier_id,spu.supplier_sn,spu.batch_id,spu.retail_flag,spu.retail_num,spu.entirety_num,spu.batch_number,spu.stock_num,spu.term_validity,spu.supplier_sn,spu.sale_num,';
|
||
$fields .= 'sku.id as sku_id,sku.area_id,sku.price_type,sku.discount_num,sku.discount_start_time,sku.discount_end_time';
|
||
|
||
//筛选条件处理
|
||
$manufacturer_name = $param['manufacturer']??'';
|
||
|
||
if ($manufacturer_name){
|
||
$where[] = ['m.name|m.simple_pinyin','like','%'.htmlspecialchars($manufacturer_name).'%'];
|
||
}
|
||
//分页
|
||
$per_page = $param['per_page']??30;
|
||
$current_page = $param['current_page']??1;
|
||
|
||
//查询
|
||
/**
|
||
* paginate 返回数据(对象类型)如下
|
||
* [
|
||
* 'current_page'=>$current_page,
|
||
* 'data'=>[],//查询结果$fields数组
|
||
* 'last_page'=>'',
|
||
* 'per_page'=>'',
|
||
* 'total'=>''
|
||
* ]
|
||
*/
|
||
$data = Db::name('goods_batch_area_relation')->alias('sku')->field($fields)
|
||
->join('goods_batch spu','spu.batch_id=sku.batch_id')
|
||
->join('goods g','g.goods_id=spu.goods_id')
|
||
->join('manufacturer m','m.id=g.manufacturer_id')
|
||
->where($where)->order($order)//->buildSql();exit();
|
||
->paginate($per_page,false,['page'=>$current_page])
|
||
->toArray();
|
||
|
||
$extend_price_rate = $init_data['extend_price_rate']??1.05;
|
||
$up_hy = $param['is_update_hy']??true;//is_update_hy:0/1
|
||
$to_del_num = 0;
|
||
|
||
//@todo 是否已加入购物车
|
||
/* $user_cart_data_now = [];
|
||
$uc_price_id_data_now = [];
|
||
$uc_supplier_sn_now = [];
|
||
if ($login){
|
||
$user_cart_data = Db::name('user_cart')->field('id as uc_id,batch_id,price_id,type')->where('user_id',$init_data['user_id'])->select();
|
||
|
||
}*/
|
||
if ($up_hy){
|
||
foreach ($data['data'] as $key=>$value){
|
||
if (empty($value['pc_thumb'])){
|
||
$data['data'][$key]['pc_thumb'] = $value['app_thumb']?:self::APP_DEFAULT_IMG_URL;
|
||
}
|
||
if($value['pc_thumb'] == 'www.hyey.cn'){
|
||
$data['data'][$key]['pc_thumb'] = self::APP_DEFAULT_IMG_URL;
|
||
}
|
||
$data['data'][$key]['is_buy'] = $login?1:0;
|
||
//价格处理:活动价/正常销售价处理;
|
||
if ($value['supplier_id']!=10006 && isset($value['discount_price']) && $value['discount_price'] * 10000){
|
||
$date = strtotime(time());
|
||
$sale_pack_num = $value['retail_flag']==0?$value['retail_num']:$value['entirety_num'];
|
||
//未对分页结果数量产生影响
|
||
if (((!$value['discount_start_time'] && !$value['discount_end_time']) || ($value['discount_start_time']<$date && $value['discount_end_time']>$date)) && ($value['discount_num']>=$sale_pack_num || $value['discount_num']==0)){
|
||
$data['data'][$key]['price'] = $data['data'][$key]['discount_price'];
|
||
// unset($data['data'][$key]['price']);
|
||
}else{
|
||
unset($data['data'][$key]['discount_start_time']);
|
||
unset($data['data'][$key]['discount_end_time']);
|
||
unset($data['data'][$key]['discount_price']);
|
||
unset($data['data'][$key]['discount_num']);
|
||
}
|
||
}
|
||
|
||
//非网处理
|
||
if (isset($data['data'][$key]['is_online_sale'])&& $data['data'][$key]['is_online_sale']==1){
|
||
$data['data'][$key]['is_buy'] = 0;
|
||
}
|
||
|
||
//限购
|
||
$data['data'][$key]['is_limit'] = 0;
|
||
if ($value['max_purchase_period'] && $value['max_purchase'] && $value['supplier_id']!=10006){
|
||
$data['data'][$key]['is_limit'] = 1;
|
||
$data['data'][$key]['limit_info'] = $value['max_purchase_period'].'天内限购'.$value['max_purchase'].$value['unit'];
|
||
}
|
||
|
||
if ($value['supplier_id']==10006){
|
||
if ($value['supplier_sn']){
|
||
if ($login){
|
||
$hy_price = $this->getHyPrice($value['supplier_sn'],$extend_price_rate);
|
||
if ($hy_price){
|
||
$data['data'][$key]['price'] = $hy_price;
|
||
}else{
|
||
//$data['data'][$key]['price'] = self::NO_HY_PRICE;
|
||
//$data['data'][$key]['is_buy'] = 0;
|
||
//获取hy价格失败-改为不展示
|
||
$to_del_num++;
|
||
unset($data['data'][$key]);
|
||
}
|
||
}else{
|
||
$data['data'][$key]['price'] = '登录后查看价格';
|
||
$data['data'][$key]['is_buy'] = 0;
|
||
}
|
||
|
||
|
||
}else{
|
||
$to_del_num++;
|
||
unset($data['data'][$key]);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
//分页+数据(索引)整理
|
||
if ($to_del_num){
|
||
$data['total']-=$to_del_num;
|
||
$data['last_page'] = ceil($data['total']/$data['per_page']);
|
||
$data['data'] = array_merge($data['data']);
|
||
}
|
||
|
||
|
||
|
||
$res = [];
|
||
$res['code'] = 200;
|
||
$res['msg'] = 'success';
|
||
$res['data'] = $data;
|
||
return $res;
|
||
|
||
}
|
||
|
||
/**
|
||
* 获取hy商品价格-丁超-20200514
|
||
* 单独采集hy价格入库ydyd_hy_data,保持定期更新,后期查询从中获取
|
||
* @param $supplier_sn string hy商品id
|
||
* @param $price_rate float 折扣等级
|
||
* @return mixed 成功返回价格-失败返回false
|
||
*
|
||
* hy商品字段说明
|
||
* {
|
||
"RN": "15",//列表序号
|
||
"BOSS": "a0540",//
|
||
"YPBH": "8029034",//药品编号
|
||
"ISRETAIL": "1",//零整标志
|
||
"ID": "A74926EE-F7D9-412E-9C38-9A46A4A48D5D",//药品id
|
||
"YPMC": "注射用阿莫西林钠克拉维酸钾",//药品名称
|
||
"YPDM": "ZSYAMXLNKLWSJEYK",//药品编码?
|
||
"GG": "0.6g",//规格
|
||
"CDMC": "苏州二叶制药有限公司",//产地名称(厂家名称)
|
||
"CDDM": "SZEYZYYXGS",//厂家编码
|
||
"PH": "K1190901",//批号
|
||
"SL": "2080",//库存数量
|
||
"BZ": "600",//包装
|
||
"ZBZ": "10",//中包装
|
||
"NZBZ": "10",
|
||
"DW": "瓶",//单位
|
||
"DJ": "会员可查",//销售定价
|
||
"LSJ": "25",//建议零售价=国规价
|
||
"YXQ": "2021-08",//有效期
|
||
"PZWH": "H20045122.",//批准文号=国药准字
|
||
"CK": "aa",//仓库?
|
||
"JX": "粉针剂",//剂型
|
||
"PICNAME": "/uploadpic/600/a0540/8029034/8029034_0.jpg",//图片
|
||
"PICNAME2": "",
|
||
"FID": "",
|
||
"SID": "",
|
||
"ZRPZ": "0",
|
||
"ISG": "0",
|
||
"FGSMC": "自营",//分公司名称?
|
||
"FGSID": "0",//分公司id?
|
||
"PROMOTIONID": "0",//活动id 参考电商promotion概念
|
||
"DISCOUNTRATE": "1"//折扣
|
||
},
|
||
*
|
||
*/
|
||
public function getHyPrice($supplier_sn,$price_rate=1.05){
|
||
$price_cache = Cache::get($supplier_sn);//false、number
|
||
if (!$price_cache){
|
||
$price = Db::name('hy_data')->where('sn',$supplier_sn)->limit(1)->value('price');
|
||
if ($price){
|
||
Cache::set($supplier_sn,$price,86400);
|
||
$price_cache = ceil($price*$price_rate*100)/100;
|
||
}
|
||
}else{
|
||
$price_cache = ceil($price_cache*$price_rate*100)/100;
|
||
}
|
||
return $price_cache;
|
||
|
||
}
|
||
//预留华源商品校验--调用HyData类的checkHyGoods()方法(默认校验价格+库存,支持校验更多字段)
|
||
public function checkHyGoods($supplier_sn,$price_rate=1.05){
|
||
|
||
}
|
||
|
||
/**
|
||
* 商品搜索列表
|
||
*/
|
||
public function searchList($parames)
|
||
{
|
||
if (empty($parames['name'])) {
|
||
throw new Exception('没有你需要的药品');
|
||
}
|
||
|
||
$_where = [];
|
||
//基础条件
|
||
$_where[] = ['g.status', '=', 1];
|
||
$_where[] = ['spu.batch_status', '=', 1];
|
||
$_where[] = ['spu.stock_num', '>', 0];
|
||
$_where[] = ['sku.is_onshelf', '=', 1];
|
||
$_where[] = ['sku.is_delete', '=', 0];
|
||
//有效期
|
||
$_where[] = ['spu.term_validity', '>', date('Y-m-d')];
|
||
|
||
//搜索
|
||
if (isset($parames['name']) && $parames['name']) {
|
||
//药品名称 厂商 规格 搜索
|
||
$search_array = explode(' ', trim($parames['name']));
|
||
|
||
switch (count($search_array)) {
|
||
|
||
case 1:
|
||
$_where[] = ['g.name|m.name|g.simple_pinyin|g.pinyin|m.simple_pinyin', 'like', '%' .$search_array[0]. '%'];
|
||
break;
|
||
case 2:
|
||
$_where[] = ['g.name|m.name|g.simple_pinyin|g.pinyin|m.simple_pinyin', 'like', '%' .$search_array[0]. '%'];
|
||
$_where[] = ['g.name|m.name|g.simple_pinyin|g.pinyin|m.simple_pinyin', 'like', '%' .$search_array[1]. '%'];
|
||
break;
|
||
|
||
}
|
||
|
||
}
|
||
|
||
$_fields = 'g.name,g.goods_id,m.name as manufacturer_name,spu.supplier_id,spu.supplier_sn';
|
||
|
||
$res = Db::name('goods_batch_area_relation')->alias('sku')->distinct(true)->field($_fields)
|
||
->join('goods_batch spu', 'spu.batch_id=sku.batch_id')
|
||
->join('goods g', 'g.goods_id=spu.goods_id')
|
||
->join('manufacturer m', 'm.id=g.manufacturer_id')
|
||
->where($_where)
|
||
->limit(100)->select();
|
||
|
||
$arr_hy_goods_id = [];
|
||
foreach ($res as $k=>$v){
|
||
if ($v['supplier_id']==10006){
|
||
if ($v['supplier_sn']){
|
||
$arr_hy_goods_id[] = $v['supplier_sn'];
|
||
}
|
||
}
|
||
}
|
||
$hy = [];
|
||
if(count($arr_hy_goods_id)>0){
|
||
//hy商品-有价格
|
||
$hy = Db::name('hy_data')->where('sn','in',$arr_hy_goods_id )->column('sn');
|
||
}
|
||
$res2 = [];
|
||
foreach ($res as $val){
|
||
if(count($res2)>=10) break;
|
||
$tmp_data = $val;
|
||
//$tmp_data = [];
|
||
//$tmp_data['name'] = $val['name'];
|
||
//$tmp_data['manufacturer_name'] = $val['manufacturer_name'];
|
||
|
||
if( $val['supplier_id'] != 10006){
|
||
$res2[] = $tmp_data;
|
||
}elseif(count($hy)>0 && $val['supplier_id']== 10006 && in_array($val['supplier_sn'], $hy)){
|
||
$res2[] = $tmp_data;
|
||
}
|
||
}
|
||
//$result = array_slice($tmp_arr,0,10);
|
||
return $res2;
|
||
|
||
|
||
}
|
||
/**
|
||
* 商品收藏
|
||
*
|
||
*/
|
||
public function addCollect_App($user_id,$goods_id,$status){
|
||
//status == 1添加收藏 2删除收藏
|
||
if($status == 1){
|
||
$collect_arr = explode(",", $goods_id);
|
||
$collect_arr = array_unique($collect_arr);
|
||
if(count($collect_arr)>0){
|
||
//bug 备注:find只查询单条
|
||
$__where = [];
|
||
$__where[] = ['user_id','=',$user_id];
|
||
$__where[] = ['goods_id','in',$collect_arr];
|
||
$tmp = Db::name('user_collect')->where($__where)->select();
|
||
|
||
//严格条数一致,未收藏的继续添加收藏
|
||
if($tmp && count($tmp)==count($collect_arr)){
|
||
$result['errmsg'] = '商品已收藏';
|
||
return $result;
|
||
}
|
||
$has_collect = [];
|
||
if ($tmp){
|
||
$has_collect = array_column($tmp,'goods_id');
|
||
}
|
||
|
||
$add_data = [];
|
||
//新增user_collect表数据
|
||
foreach ($collect_arr as $v) {
|
||
if (!in_array($v,$has_collect)){
|
||
$tmp_add_data=[];
|
||
$tmp_add_data['goods_id'] = $v;
|
||
$tmp_add_data['user_id'] = $user_id;
|
||
//$tmp_add_data['collect_time'] = date("Y-m-d H:i:s");
|
||
$add_data[]=$tmp_add_data;
|
||
}
|
||
|
||
}
|
||
|
||
if (count($add_data)>0) {
|
||
Db::startTrans();
|
||
try{
|
||
foreach ($add_data as $val){
|
||
$exist = Db::name('user_collect')->where($val)->find();
|
||
if (!$exist){
|
||
$tmp_x = $val;
|
||
$tmp_x['collect_time'] = date("Y-m-d H:i:s");
|
||
$res_insert = Db::name('user_collect')->insert($tmp_x);
|
||
if ($res_insert){
|
||
Db::name('goods')->where('goods_id',$tmp_x['goods_id'])->setInc('collect_count');
|
||
}
|
||
}
|
||
}
|
||
|
||
//事务结束
|
||
Db::commit();
|
||
}catch (Exception $exception){
|
||
Db::rollback();
|
||
$result['errmsg'] = $exception->getMessage();
|
||
return $result;
|
||
}
|
||
|
||
}else{
|
||
$result['errmsg'] = '商品已收藏';
|
||
return $result;
|
||
}
|
||
|
||
$result['errmsg'] = '收藏成功';
|
||
return $result;
|
||
}else{
|
||
$result['errmsg'] = '参数为空!';
|
||
return $result;
|
||
}
|
||
|
||
}elseif ($status == 2){
|
||
if($goods_id){
|
||
$goods_id = explode(',',$goods_id);
|
||
try{
|
||
Db::name("user_collect")->where("user_id",$user_id)->where("goods_id",'in',$goods_id)->delete();
|
||
$result['errmsg'] = '已取消收藏';
|
||
return $result;
|
||
}catch (Exception $exception){
|
||
$result['errmsg'] = $exception->getMessage();
|
||
return $result;
|
||
}
|
||
}else{
|
||
$result['errmsg'] = '参数不能为空!';
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
$result['errmsg'] = '参数不合法!';
|
||
return $result;
|
||
|
||
}
|
||
/**
|
||
* 商品收藏列表
|
||
*
|
||
*/
|
||
public function Collectlsit ($params){
|
||
|
||
// $data = Request::only(['now_page','page_size']);
|
||
// if(!session('user_id')){
|
||
// $result['errcode'] = -1;
|
||
// $result['errmsg'] = '获取收藏商品列表异常';
|
||
// return $result;
|
||
// }
|
||
// $_where['a.user_id'] = $params['user_id];
|
||
$_where['a.user_id'] = $params['user_id'];
|
||
$_goods_join = "goods b";
|
||
$_goods_join_on = "a.goods_id = b.goods_id";
|
||
|
||
$_factory_join = "manufacturer m";
|
||
$_factory_join_on = "m.id = b.manufacturer_id";
|
||
$_field = "a.user_id,a.goods_id,a.collect_time,b.name,b.pinyin,b.sketch,b.spec_desc,b.status,'' as show_price,b.pc_thumb,b.pc_gallery,b.pc_content,b.app_thumb,b.app_gallery,b.app_content,b.country_code,b.drug_form,m.name as factory_name";
|
||
$_order = "a.collect_time desc";
|
||
|
||
// $total_count = Db::name('user_collect')->alias('a')->join($_goods_join,$_goods_join_on,"RIGHT")->join($_factory_join,$_factory_join_on,"LEFT")->where($_where)->count();
|
||
// $total_page = ceil($total_count/$data['page_size']);
|
||
// $list = Db::name('user_collect')->alias('a')->join($_goods_join,$_goods_join_on,"RIGHT")->join($_factory_join,$_factory_join_on,"LEFT")->page($data['now_page'],$data['page_size'])->where($_where)->field($_field)->order($_order)->select();
|
||
$list = Db::name('user_collect')->alias('a')->join($_goods_join,$_goods_join_on,"RIGHT")->join($_factory_join,$_factory_join_on,"LEFT")->where($_where)->field($_field)->order($_order)->select();
|
||
foreach ($list as $k=>$v){
|
||
if ($v['pc_thumb']=='' || $v['pc_thumb']=='www.hyey.cn'){
|
||
$list[$k]['pc_thumb'] = self::APP_DEFAULT_IMG_URL;
|
||
}
|
||
}
|
||
|
||
|
||
// $result['errcode'] = 0;
|
||
$result['errmsg'] = '获取收藏商品列表成功';
|
||
$result['data']['list'] = $list;
|
||
// $result['data']['total_count'] = $total_count;
|
||
// $result['data']['total_page'] = $total_page;
|
||
return $result;
|
||
|
||
}
|
||
/**
|
||
* 药品排行
|
||
*
|
||
*/
|
||
public function goodsRanking()
|
||
{
|
||
$param['sort_field'] = 'sale';
|
||
$param['sort_order'] = 1;
|
||
$param['is_update_hy'] = 0;
|
||
$param['per_page'] = 30;
|
||
$result = $this->getGoodsSearch(false,$param);
|
||
|
||
$list = $result['data']['data']??array();
|
||
$ret = array();
|
||
$int = 0;
|
||
$nameArray = [];
|
||
foreach ($list as $k=>$item)
|
||
{
|
||
$item['goods_name'] = str_replace("(","(",$item['goods_name']);
|
||
$names = explode("(",$item['goods_name']);
|
||
$item['goods_name'] = $names[0]??'';
|
||
if($item['goods_name']){
|
||
if(!in_array($item['goods_name'],$nameArray)){
|
||
$nameArray[] = $item['goods_name'];
|
||
$i['name'] = $item['goods_name'];
|
||
$i['goods_id'] = $item['goods_id'];
|
||
$ret[] = $i;
|
||
++$int;
|
||
}
|
||
}
|
||
if($int>=20){
|
||
break;
|
||
}
|
||
}
|
||
|
||
return $ret;
|
||
}
|
||
|
||
/**
|
||
* sku详情处理和下单校验(GoodsService/OrderGoodsControlService)
|
||
* 一、价格处理
|
||
* 用户身份+活动价格/新人活动价/常规活动价格
|
||
* 二、采购权
|
||
* 三、有限购需求的商品
|
||
*/
|
||
|
||
public function goodsDetailProcess(){
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
} |