340 lines
16 KiB
PHP
340 lines
16 KiB
PHP
<?php
|
||
namespace app\common\service;
|
||
|
||
use think\Db;
|
||
use think\Exception;
|
||
|
||
class UserCartService{
|
||
const APP_DEFAULT_IMG_URL = 'ydyd-develop-file.oss-cn-hangzhou.aliyuncs.com/pc_shop_pic_url_1588925532218.jpg';
|
||
public function getAll($user_data){
|
||
$order = [];
|
||
$order['uc.update_time'] = 'desc';
|
||
$order['uc.id'] = 'desc';
|
||
$fields = 'uc.id,g.name,m.name as manufacturer_name,g.pc_thumb,g.spec_desc,g.unit,uc.unit_price as price,uc.user_id,uc.price_id,';
|
||
$fields .= 'sku.batch_id,spu.goods_id,spu.retail_flag,spu.retail_num,spu.entirety_num,spu.batch_type,';
|
||
$fields .= 'spu.term_validity,spu.supplier_user_id,spu.supplier_id,spu.supplier_sn,spu.batch_number,spu.batch_status,';
|
||
$fields .= 'uc.goods_number as num,1 as can_select,';
|
||
$fields .= 'spu.stock_num,g.status,g.is_online_sale,spu.batch_status,sku.is_onshelf,sku.is_delete,';//下文校验字段
|
||
//限购字段
|
||
$fields .= 'g.max_purchase,g.max_purchase_period,g.unit';
|
||
|
||
$data = Db::name('user_cart')->alias('uc')->field($fields)->field($fields)
|
||
->join('goods_batch_area_relation sku','uc.price_id=sku.id')
|
||
->join('goods_batch spu','sku.batch_id=spu.batch_id')
|
||
->join('goods g','g.goods_id=spu.goods_id')
|
||
->join('manufacturer m','m.id=g.manufacturer_id')->order($order)
|
||
->where('user_id',$user_data['user_id'])->select();
|
||
//校验
|
||
$available_goods = [];
|
||
$inavailable_goods = [];
|
||
if ($data && is_array($data)){
|
||
foreach ($data as $key=>$value){
|
||
//限购
|
||
$data[$key]['is_limit'] = 0;
|
||
//$data[$key]['limit_info'] = '';
|
||
//限购逻辑:优势+限购周期+限购量
|
||
if ($value['supplier_id']!=10006 &&isset($value['max_purchase'])&&isset($value['max_purchase_period'])&&$value['max_purchase']&&$value['max_purchase_period']){
|
||
$data[$key]['is_limit'] = 1;
|
||
$data[$key]['limit_info'] = $value['max_purchase_period'].'天内限购'.$value['max_purchase'].$value['unit'];
|
||
}
|
||
|
||
$errmsg = '';
|
||
if ($value['status']!=1){
|
||
$errmsg .= '商品状态异常!';
|
||
}
|
||
if ($value['batch_status']!=1){
|
||
$errmsg .= '药品已下架';
|
||
}
|
||
if ($value['is_online_sale']==1){
|
||
$errmsg .= '非网售药品,请联系客服!';
|
||
}
|
||
if ($value['is_onshelf']==0){
|
||
$errmsg .= '已下架!';
|
||
}
|
||
if ($value['is_delete']==1){
|
||
$errmsg .= 'sku is deleted!';
|
||
}
|
||
//采购权限暂不校验
|
||
|
||
//库存
|
||
if ($value['stock_num']<$value['num'] || $value['stock_num']<=0){
|
||
$errmsg .= '库存不足';
|
||
}
|
||
$tmp_data = $data[$key];
|
||
if (empty($tmp_data['pc_thumb'])){
|
||
$tmp_data['pc_thumb'] = $tmp_data['app_thumb']?:self::APP_DEFAULT_IMG_URL;
|
||
}
|
||
if($tmp_data['pc_thumb'] == 'www.hyey.cn'){
|
||
$tmp_data['pc_thumb'] = self::APP_DEFAULT_IMG_URL;
|
||
}
|
||
$tmp_data['can_select'] = 1;
|
||
$tmp_data['total_price'] = ceil($tmp_data['price']*$tmp_data['num']*100)/100;//金额保留两位小数
|
||
if ($errmsg){
|
||
$tmp_data['can_select'] = 0;
|
||
$tmp_data['warn_info'] = $errmsg;
|
||
$inavailable_goods[] = $tmp_data;
|
||
}else{
|
||
$available_goods[] = $tmp_data;
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
$res = [];
|
||
$res['errcode'] = 200;
|
||
$res['errmsg'] = 'SUCCESS';
|
||
$res['data'] = ['available_goods'=>$available_goods,'inavailable_goods'=>$inavailable_goods];
|
||
|
||
return $res;
|
||
|
||
}
|
||
|
||
public function addGoods($user_data,$param){
|
||
|
||
$price_id = $param['gba_id'];
|
||
//立即购买
|
||
$add_type = false;
|
||
if (isset($param['add_type']) && $param['add_type']=='buy'){
|
||
$add_type = true;
|
||
}
|
||
if (isset($param['goods_number'])){
|
||
$param['goods_number'] = (int)$param['goods_number'];
|
||
if ($param['goods_number']<0){
|
||
throw new Exception('数量不能为负!',-100);
|
||
}
|
||
}
|
||
|
||
$fields = 'g.name,g.goods_id,g.max_purchase_period,g.max_purchase,g.unit,g.pc_thumb,g.pc_gallery,g.sketch,g.drug_form,g.spec_desc,g.pc_content,g.show_price,g.country_code,';
|
||
$fields .= 'spu.stock_num,spu.batch_id,spu.batch_type,spu.retail_num,spu.batch_number,spu.retail_flag,spu.entirety_num,spu.term_validity,spu.supplier_sn,spu.supplier_id,';
|
||
$fields .= 'm.name as manufacturer_name,';
|
||
$fields .= 'sku.price as unit_price,sku.price_type,sku.new_price,sku.discount_price,sku.discount_num,sku.discount_start_time,sku.discount_end_time';
|
||
$add_data = Db::name('goods_batch_area_relation')->alias('sku')->field($fields)
|
||
->join('goods_batch spu','sku.batch_id=spu.batch_id')
|
||
->join('goods g','spu.goods_id=g.goods_id')
|
||
->join('manufacturer m','m.id=g.manufacturer_id')
|
||
->where('sku.id',$price_id)->find();
|
||
//价格处理
|
||
$date_now = time();
|
||
$sale_pack_num = $add_data['retail_flag']==0?$add_data['retail_num']:$add_data['entirety_num'];
|
||
//活动价
|
||
if ( $add_data['supplier_id']!=10006 && isset($add_data['discount_price']) && ((!$add_data['discount_start_time'] && !$add_data['discount_end_time']) || ($add_data['discount_start_time']<$date_now && $add_data['discount_end_time']>$date_now)) && ($add_data['discount_num']>=$sale_pack_num || $add_data['discount_num']==0)){
|
||
if ($add_data['discount_price']*10000){
|
||
$add_data['unit_price'] = $add_data['discount_price'];
|
||
}
|
||
|
||
//$add_data['stock_num'] = $add_data['discount_num'];
|
||
}
|
||
//新人价
|
||
if ($user_data['is_new_user']==1){
|
||
if ( $add_data['supplier_id']!=10006 && isset($add_data['new_price']) && ((!$add_data['discount_start_time'] && !$add_data['discount_end_time']) || ($add_data['discount_start_time']<$date_now && $add_data['discount_end_time']>$date_now)) && ($add_data['discount_num']>=$sale_pack_num || $add_data['discount_num']==0)){
|
||
if ($add_data['new_price']*10000){
|
||
$add_data['unit_price'] = $add_data['new_price'];
|
||
}
|
||
|
||
//$add_data['stock_num'] = $add_data['discount_num'];
|
||
}
|
||
}
|
||
//库存校验 自营+流通
|
||
if ($param['goods_number'] > $add_data['stock_num']){
|
||
throw new Exception('库存不足',-156);
|
||
}
|
||
|
||
if ($add_data){
|
||
$tmp = [];
|
||
$tmp['user_id'] = $user_data['user_id'];
|
||
$tmp['price_type'] = $add_data['price_type'];
|
||
$tmp['price_id'] = $price_id;
|
||
$exist_in_cart = [];//用于下文校验购物车中是否已存在where判断
|
||
$exist_in_cart[] = ['user_id','=',$user_data['user_id']];
|
||
$exist_in_cart[] = ['price_type','=',$add_data['price_type']];
|
||
$exist_in_cart[] = ['price_id','=',$price_id];
|
||
|
||
$tmp['batch_id'] = $add_data['batch_id'];
|
||
$tmp['unit_price'] = $add_data['unit_price'];
|
||
$tmp['goods_number'] = $param['goods_number'];
|
||
$date_time = date('Y-m-d H:i:s');
|
||
$tmp['create_time'] = $date_time;
|
||
$tmp['update_time'] = $date_time;
|
||
$tmp['type'] = '';
|
||
|
||
|
||
//流通
|
||
if ($add_data['supplier_id']==10006){
|
||
$price = Db::name('hy_data')->where('sn',$add_data['supplier_sn'])->limit(1)->value('price');
|
||
if ($price){
|
||
$extend_price_rate = $user_data['extend_price_rate']??1.05;
|
||
$tmp['unit_price'] = ceil($price*$extend_price_rate*100)/100;
|
||
$tmp['batch_id'] = $add_data['supplier_sn'];//兼容pc-此处存储extend_goods_id即supplier_sn
|
||
|
||
$extend_data = [];
|
||
$extend_data['name'] = $add_data['name'];
|
||
$extend_data['unit'] = $add_data['unit'];
|
||
$extend_data['price'] = $add_data['unit_price'];
|
||
$extend_data['sketch'] = $add_data['sketch'];
|
||
$extend_data['gallery'] = $add_data['pc_gallery'];
|
||
$extend_data['can_show'] = 1;
|
||
$extend_data['batch_id_plat'] = $add_data['batch_id']??'';//冗余存储当前batch_id(已入库)
|
||
$extend_data['drug_form'] = $add_data['drug_form'];
|
||
$extend_data['spec_desc'] = $add_data['spec_desc'];
|
||
$extend_data['stock_num'] = $add_data['stock_num'];
|
||
$extend_data['pc_content'] = $add_data['pc_content'];
|
||
$extend_data['retail_flag'] = $add_data['retail_flag'];
|
||
$extend_data['batch_number'] = $add_data['retail_num'];
|
||
$extend_data['country_code'] = $add_data['country_code'];
|
||
$extend_data['retail_num'] = $add_data['retail_num'];
|
||
$extend_data['entirety_num'] = $add_data['entirety_num'];
|
||
$extend_data['term_validity'] = $add_data['term_validity'];
|
||
$extend_data['extend_goods_id'] = $add_data['supplier_sn'];
|
||
$extend_data['manufacturer_name'] = $add_data['manufacturer_name'];
|
||
$extend_data['show_price_int'] = (int)$add_data['show_price'];
|
||
$extend_data['show_price_float'] = explode('.',$add_data['show_price'])[1];
|
||
|
||
$extend_data_json = json_encode($extend_data,JSON_UNESCAPED_UNICODE);
|
||
$tmp['type'] = 'extend';
|
||
$tmp['extend_data'] = $extend_data_json;
|
||
|
||
}else{
|
||
throw new Exception('商品已下架!',-504);
|
||
}
|
||
|
||
}
|
||
$exist = Db::name('user_cart')->field('id as uc_id,goods_number,user_id,price_id')->where($exist_in_cart)->find();
|
||
|
||
//自营商品控销权限
|
||
if (isset($add_data['batch_type']) && $add_data['batch_type']==2 && $add_data['supplier_id']!=10006){
|
||
$cart_num_now = $exist['goods_number']??0;
|
||
$check_array = $this->checkPurchaseAuth($user_data['user_id'],$add_data['goods_id']);
|
||
if ($check_array['errcode']!==200){
|
||
throw new Exception($check_array['errmsg'],$check_array['errcode']);
|
||
}
|
||
|
||
}
|
||
//限购-自营商品
|
||
if ($add_data['supplier_id']!=10006 && $add_data['max_purchase_period'] && $add_data['max_purchase']){
|
||
$cart_num_now = $exist['goods_number']??0;
|
||
$to_add_now = $param['goods_number']??0;
|
||
$check_array = $this->checkMaxPurchase($user_data['user_id'],$add_data,$cart_num_now,$to_add_now);
|
||
if ($check_array['errcode']!==200){
|
||
throw new Exception($check_array['errmsg'],$check_array['errcode']);
|
||
}
|
||
|
||
}
|
||
|
||
if ($exist){
|
||
$uc_id = $exist['uc_id'];
|
||
//非立即购买情况下,进行累加;立即购买情况下直接更新为当前提交数量
|
||
if (!$add_type){
|
||
$tmp['goods_number'] += $exist['goods_number'];
|
||
}
|
||
|
||
$tmp['id'] = $uc_id;
|
||
//首次加入购物车时间不更新
|
||
unset($tmp['create_time']);
|
||
Db::name('user_cart')->update($tmp);
|
||
|
||
}else{
|
||
$uc_id = Db::name('user_cart')->insertGetId($tmp);
|
||
}
|
||
|
||
if ($uc_id){
|
||
$res = [];
|
||
$res['errcode'] = 200;
|
||
$res['errmsg'] = 'SUCCESS';
|
||
$res['uc_id'] = $uc_id;
|
||
|
||
return $res;
|
||
}else{
|
||
throw new Exception('系统异常',-506);
|
||
}
|
||
|
||
}else{
|
||
throw new Exception('商品已下架',-505);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 采购权-控销
|
||
*/
|
||
public function checkPurchaseAuth($user_id,$goods_id){
|
||
$where = [];
|
||
$where[] = ['goods_id','=',$goods_id];
|
||
$where[] = ['user_id','=',$user_id];
|
||
$where[] = ['status','=',1];
|
||
$res = [];
|
||
$goods_limit_check = Db::name('goods_limit_relation')->where($where)->find();
|
||
if ($goods_limit_check){
|
||
switch ($goods_limit_check['status']){
|
||
case 0:
|
||
$res['errcode'] = 500;
|
||
$res['errmsg'] = '采购权申请中,等待审核通过后购买!';
|
||
break;
|
||
case 1:
|
||
$res['errcode'] = 200;
|
||
$res['errmsg'] = '采购权已通过!';
|
||
break;
|
||
case 2:
|
||
$res['errcode'] = 700;
|
||
$res['errmsg'] = '采购权申请未通过!请咨询客服!';
|
||
}
|
||
}else{
|
||
$res['errcode'] = -1;
|
||
$res['errmsg'] = '当前商品为控销商品,尚未未申请采购权!';
|
||
}
|
||
|
||
return $res;
|
||
|
||
}
|
||
|
||
/**
|
||
* 限购处理
|
||
* @param $user_id Int 用户id
|
||
* @param $goods_data array 限购商品数据
|
||
* @param $cart_num_now Int 购物车商品数量
|
||
* @return array
|
||
*/
|
||
public function checkMaxPurchase($user_id,$goods_data,$cart_num_now=0,$to_add_now=0){
|
||
//判断药品是否设了购买数量设置,未限制的max_purchase_period为0,不进入该处理过程
|
||
$res_result = [];
|
||
if ($goods_data['max_purchase_period']){
|
||
$day_limit = $goods_data['max_purchase_period'];
|
||
$limit_time = date("Y-m-d H:i:s",strtotime("- $day_limit days"));//截止至当前时间的该时间周期内
|
||
$_where = [];
|
||
$_where[] = ['oi.user_id','=',$user_id];
|
||
$_where[] = ['oi.status','in',[0,1,6,7]];
|
||
$_where[] = ['oi.create_time','>',$limit_time];
|
||
$_where[] = ['og.goods_id','=',$goods_data['goods_id']];
|
||
//上述时间周期内已购买商品数量总和
|
||
$bought_goods_num = Db::name("order_info")->alias("oi")->join("order_goods og","oi.order_id = og.order_id")->where($_where)->sum("og.goods_number");
|
||
|
||
//可采购数量待处理; 标准 $can_buy_num = $goods_info['max_purchase'] - $bought_goods_num;(该值(负数、0、正数 )+ 包装数量)
|
||
if ($goods_data['max_purchase'] <= $bought_goods_num){
|
||
$res_result['errcode'] = -1;
|
||
$res_result['errmsg'] = "限购商品,最近限购周期内已达最大采购量";//购物车中处理成失效商品?
|
||
}else{
|
||
|
||
$can_put_in_cart_num = $goods_data['max_purchase'] - $bought_goods_num - $cart_num_now-$to_add_now;
|
||
if ($can_put_in_cart_num>=0){
|
||
//不阻断,允许继续加入购物车
|
||
$res_result['errcode'] = 200;
|
||
$res_result['errmsg'] = 'SUCCESS';
|
||
}else{
|
||
//阻断
|
||
$res_result['errcode'] = 500;
|
||
$res_result['errmsg'] = '限购商品,已达到最大可购买量';
|
||
//$res_result['errmsg'] = '限购商品,'.$day_limit.'天内限购'.$goods_data['max_purchase'].$goods_data['unit'].',最近'.$day_limit.'天内已购'.$bought_goods_num.$goods_data['unit'].',当前最多可继续加入购物车'.$can_put_in_cart_num.$goods_data['unit'];
|
||
}
|
||
|
||
}
|
||
|
||
}else{
|
||
$res_result['errcode'] = 200;
|
||
$res_result['errmsg'] = 'SUCCESS';
|
||
}
|
||
|
||
return $res_result;
|
||
}
|
||
|
||
} |