2023-04-03 16:02:18 +08:00

1146 lines
45 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/Apache-2.0
* @link https://www.gougucms.com
*/
namespace app\admin\controller\product;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
use app\admin\model\Merchant; // 商户模型
use app\admin\model\EbStoreProduct; // 商品模型
use app\admin\model\StoreCategory; // 商品分类模型
use app\common\controller\FormatList;
use app\admin\model\StoreProduct as StoreProductModel;
use app\admin\validate\StoreProductValidate;
use app\admin\model\GeoCity; // 省市模型
use app\admin\model\GeoArea; // 区域模型
use app\admin\model\GeoStreet; // 街道模型
use app\admin\model\SupplyChain; // 供应链模型
// use app\api\model\Area as AreaModel; // 市场区域模型
// use app\api\model\AreaManager as AreaManagerModel; // 区域负责人模型
/**
*
* 商品控制器
*
*/
class Product extends BaseController
{
protected $adminInfo;
protected $url;
protected $product;
protected $mer_id = 4;//待开发成自动获取商城登陆商户id
public function __construct(EbStoreProduct $product)
{
$this->adminInfo = get_login_admin();
$this->product = $product;
$this->url=[
'/admin/product.product/index',
'/admin/product.product/adds',
'/admin/product.product/edit',
'/admin/product.product/delete',
'/admin/product.product/read',
];
}
protected function auth()
{
$where = [];
if($this->adminInfo['position_id'] != 1){ //不是超级管理员
$www['admin_id'] = $this->adminInfo['id'];
$user_address = Db::table('fa_szxc_information_useraddress')->where($www)->find();
if ($user_address){
if($user_address['auth_range'] == 1){
$where[] = ['village_id','=',$user_address['village_id']];
}elseif ($user_address['auth_range'] == 2){
$where[] = ['street_id','=',$user_address['street_id']];
}elseif ($user_address['auth_range'] == 3){
$where[] = ['area_id','=',$user_address['area_id']];
}else{
$where[] = ['village_id','=',$user_address['village_id']];
}
}else{
$where[] = ['village_id','=',''];
}
}
return $where;
}
/**
*
* 商户商品列表
*
*/
public function index(StoreCategory $category)
{
if (request()->isAjax()) {
// request()->isAjax()
// Ajax 前端获取数据
$params= get_params();
$where = self::auth();
if (isset($params['keywords']) && !empty($params['keywords'])){
// $where[]= ['sotre_name','like','%'.$params['keywords'].'%'];
$where['keyword'] = $where['store_name'] = $params['keywords'];
}
$page = empty($params['page'])?1:$params['page'];
$limit = empty($params['limit'])?10:$params['limit'];
// 平台商品分类id
$where['cate_id'] = empty($params['plate_cate'])?'':$params['plate_cate'];
// 商户商品分类id
// $where['mer_cate_id'] = empty($params['store_cate'])?'':$params['store_cate'];
$where['is_trader'] = isset($params['is_trader'])?$params['is_trader']:1; // 是否自营 1自营 0 不是自营
// product
$where['is_gift_bag'] = empty($params['is_gift'])?'':$params['is_gift'];
$where['is_show'] = empty($params['state'])?'':$params['state'];
$where['temp_id'] = empty($params['shipping_tem'])?'':$params['shipping_tem'];
$where['labels'] = empty($params['tag'])?'':$params['tag'];
$where['type'] = empty($params['type'])?'':$params['type'];//实体商品0 虚拟商品:1
// $where['status'] = 0;//管理、审核、通过
// $where['is_ficti'] = 1;//是否虚拟销量
// $where['product_id'] = 0;
// ['order','sort']
// soft //软删除
// $params['mer_id'] = $this->mer_id;
if (!empty($params['mer_id'])) {
$mer_id = get_params('mer_id');
$where = array_merge($where, $this->switchType($where['type'],$mer_id,0));
}
$mer_id = isset($params['mer_id'])?$params['mer_id']:NULL;
$data = $this->product->getList($mer_id, $where, $page, $limit);
// $list = EbStoreProduct::with(['merchant' => ['merchantType', 'category']])->order('product_id desc')->select();
View::assign('url', $this->url);
View::assign('list', $data['list']);
$result = ['total' => $data['count'], 'data' => $data['list']];
return table_assign(0, '', $result);
}else{
$list = SupplyChain::with(['merchant' => ['merchantType', 'category']])->select();
// 初始空页面展示
$where['mer_id'] = 0;
$where['is_show'] = 0;
$cate_list = $category->getList($where);
$cate_list = FormatList::DropDownMenu($cate_list);
View::assign('cate_list', $cate_list);
View::assign('url', $this->url);
View::assign('list', $list);
return view();
}
}
/**
* 与类型相对应的sql字段
*
* @Author:Liuxiaoquan
* @Date: 2020/5/18
* @param $type 商
* @param int|null $merId 商户id
* @param int|null $productType 产品类型
* @return array $where 条件
*/
public function switchType($type, ?int $merId = 0, $productType = 0)
{
$stock = 0;
// 获得库存
// if ($merId) $stock = merchantConfig($merId, 'mer_store_stock');
switch ($type) {
case 1:
$where = ['is_show' => 1, 'status' => 1,];
break;
case 2:
$where = ['is_show' => 0, 'status' => 1];
break;
case 3:
$where = ['is_show' => 1, 'stock' => 0, 'status' => 1];
break;
case 4:
$where = ['stock' => $stock ? $stock : 0, 'status' => 1];
break;
case 5:
$where = ['soft' => true];
break;
case 6:
$where = ['status' => 0];
break;
case 7:
$where = ['status' => -1];
break;
case 20:
$where = ['status' => 1];
break;
default:
// $where = ['is_show' => 1, 'status' => 1];
break;
}
if ($productType == 0) {
$where['product_type'] = $productType;
if (!$merId) $where['is_gift_bag'] = 0;
}
if ($productType == 1) {
$where['product_type'] = $productType;
}
if ($productType == 10) {
$where['is_gift_bag'] = 1;
}
if (!$merId) $where['star'] = '';
return $where;
}
// /**
// *
// * 新增
// *
// */
// public function add()
// {
// if (request()->isAjax()) {
//
// $params = get_params();
//
// $data['user_id'] = $this->adminInfo['id']; // 操作用户ID
// $data['name'] = $params['title']; // 团队名称
// $data['tel'] = $params['phone']; // 联系电话
// $data['mer_id_list'] = json_encode($params['mer_id']); // 已选商户
//
// $data['street_id'] = $params['street_id']; // 街道ID
// $street = GeoStreet::where('street_id', $data['street_id'])->find(); // 街道数据
// $data['lng'] = $street['lng']; // 经度
// $data['lat'] = $street['lat']; // 纬度
// $area = $street->area; // 区数据
// $data['area_id'] = $area['area_id']; // 区县id
// $city = $area->city; // 获取市级
// $data['address'] = $city['city_name'] . $area['area_name'] . $street['street_name']; // 实际地址
// $data['create_time'] = date('Y-m-d H:i:s');
//
// // 数据入库
// $res = SupplyChain::create($data);
//
// // 关联数据入库
// foreach ($params['mer_id'] as $v) {
//
// $dataLink = [
// 'eb_merchant_id' => $v, // 商户ID
// 'user_id' => $data['user_id'],
// 'create_time' => $data['create_time'],
// ];
//
// $res->linkMerchant()->save($dataLink); // 插入关联数据
// }
//
// if ($res){
// return to_assign(0,'操作成功',['aid'=>$res]);
// }
//
// return to_assign(1, '操作失败,原因:'.$res);
//
// }else{
//
// // 取出正常的商家
// $merchant = Merchant::where('status', 1)->column('mer_id, real_name');
//
// // 区域模型
// $arealist = GeoArea::where('city_code', '510500')->select();
//
// View::assign('editor', get_system_config('other','editor'));
// View::assign('arealist', $arealist);
// View::assign('merchant', $merchant);
// View::assign('url', $this->url);
// return view();
// }
// }
/**
* 添加
*/
public function add()
{
if (request()->isAjax()) {
$param = get_params();
$data=$param;
$data['mer_id'] = 4;
$data['status'] = 1;
$data['mer_status'] =1;
$data['rate'] = 3;
$data['spec_type'] = $data['is_attribute'];//规格 0单 1多
if (!$data['spec_type']) {
if (isset($data['skus']) && count($data['skus']) > 1) throw new ValidateException('单规格商品属性错误');
}
if($data['spec_type'] == 0){
$data['attrValue'][0]['price'] = $data['price'];
$data['attrValue'][0]['cost'] = $data['cost'];
$data['attrValue'][0]['ot_price'] = $data['ot_price'];
$data['attrValue'][0]['stock'] = $data['stock'];
$data['attrValue'][0]['bar_code'] = $data['bar_code'];
$data['attrValue'][0]['weight'] = $data['weight'];
$data['attrValue'][0]['volume'] = $data['volume'];
$new_attr = [];
}
if($data['spec_type'] == 1){
$new_attr = [];
if($data['attr']){
$attr = json_decode($data['attr'],1);
foreach ($attr as $k => $v){
$new_attr[$k]['value'] = $v['title'];
$new_attr[$k]['detail'] = array_column($v['child'],'title');
}
}
$skus = $data['skus'];
foreach ($skus as $k => $v){
foreach ($data['kk'] as $key => $val){
$detail[$val] = $v['value'.$key];
}
$v['detail'] = $detail;
$new_sku[] = $v;
}
$data['attrValue'] = $new_sku;
}
unset($data['skus']);
$data['attr'] = $new_attr;
$data['params'] = [];
$data['mer_labels'] = [];
$data['mer_id'] = $this->mer_id;
$content = [
'content' => $data['content'] ,
'type' => 0
];
$productType=0;
$conType=0;
$product = $this->setProduct($data);
$data['mer_cate_id'] = [$data['mer_cate_id']];
// halt($data);
// event('product.create.before', compact('data','productType','conType'));
return Db::transaction(function () use ($data, $productType,$conType,$content,$product) {
$activity_id = 0;
$product_id = Db::connect('shop')->table('eb_store_product')->strict(false)->field(true)->insertGetId($product);
$settleParams = $this->setAttrValue($data, $product_id, $productType, 0);
$settleParams['cate'] = $this->setMerCate($data['mer_cate_id'], $product_id, $data['mer_id']);
$settleParams['attr'] = $this->setAttr($data['attr'], $product_id);
if ($productType == 0) $this->parameter_create($product_id, $data['params'] ?? [], $data['mer_id']);
$this->save($product_id, $settleParams, $content, $product, $productType);
if (in_array($productType, [0, 1])) {
if ($productType == 1) { //秒杀商品
$dat = $this->setSeckillProduct($data);
$dat['product_id'] = $product_id;
$seckill = Db::connect('shop')->table('eb_store_seckill_active')->strict(false)->field(true)->insertGetId($dat);
$activity_id = $seckill;
}
$product['price'] = $settleParams['data']['price'];
$product['mer_labels'] = $data['mer_labels'];
$this->setparam($product, $product_id, $activity_id, $productType);
}
// $product = $result;
// event('product.create',compact('product'));
// return $product_id;
return to_assign(0, '操作成功', ['aid' => $product_id]);
});
//
}else{
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
return view();
}
}
/**
* @Author:Qinii
* @Date: 2020/5/20
* @param $id
* @param $spec_type
* @param $settleParams
* @param $content
* @return int
*/
public function save($id, $settleParams, $content, $data = [], $productType = 0)
{
Db::connect('shop')->table('eb_store_product_attr')->where('product_id',$id)->delete();
Db::connect('shop')->table('eb_store_product_attr_value')->where('product_id',$id)->delete();
Db::connect('shop')->table('eb_store_product_cate')->where('product_id',$id)->delete();
// halt($settleParams['cate']);
if (isset($settleParams['cate'])) (Db::connect('shop')->table('eb_store_product_cate')->strict(false)->field(true)->insertAll($settleParams['cate']));
if (isset($settleParams['attr'])) (Db::connect('shop')->table('eb_store_product_attr')->strict(false)->field(true)->insertAll($settleParams['attr']));
if (isset($settleParams['attrValue'])) {
$arr = array_chunk($settleParams['attrValue'], 30);
foreach ($arr as $item){
Db::connect('shop')->table('eb_store_product_attr_value')->strict(false)->field(true)->insertAll($item);
}
}
if ($content){
Db::connect('shop')->table('eb_store_product_content')->where('product_id',$id)->delete();
$content['product_id'] = $id;
Db::connect('shop')->table('eb_store_product_content')->strict(false)->field(true)->insert($content);
}
if (isset($settleParams['data'])) {
$data['price'] = $settleParams['data']['price'];
$data['ot_price'] = $settleParams['data']['ot_price'];
$data['cost'] = $settleParams['data']['cost'];
$data['stock'] = $settleParams['data']['stock'];
$data['svip_price'] = $settleParams['data']['svip_price'];
}
$res = Db::connect('shop')->table('eb_store_product')->where('product_id',$id)->update($data);
return $res;
}
public function parameter_create($id, $data,$merId)
{
if (empty($data)) return ;
foreach ($data as $datum) {
if ($datum['name'] && $datum['value']) {
$create[] = [
'product_id' => $id,
'name' => $datum['name'] ,
'value' => $datum['value'],
'sort' => $datum['sort'],
'parameter_id' => $datum['parameter_id'] ?? 0,
'mer_id' => $datum['mer_id'] ?? $merId,
'create_time' => date('Y-m-d H:i:s',time())
];
}
}
if ($create) Db::connect('shop')->table('eb_parameter_value')->strict(false)->field(true)->insertAll($create);
}
public function setparam(array $param, $productId, $activityId, $productType)
{
$data = [
'product_id' => $productId,
'product_type' => $productType ?? 0,
'activity_id' => $activityId,
'store_name' => $param['store_name'],
'keyword' => $param['keyword'] ?? '',
'image' => $param['image'],
'price' => $param['price'],
'status' => 0,
'rank' => $param['rank'] ?? 0,
'temp_id' => $param['temp_id'],
'sort' => $param['sort'] ?? 0,
'mer_labels' => $param['mer_labels'] ?? '',
];
if (isset($param['mer_id'])) $data['mer_id'] = $param['mer_id'];
Db::connect('shop')->table('eb_store_spu')->strict(false)->field(true)->insert($data);
}
public function adds()
{
if (request()->isAjax()) {
$param = get_params();
// 检验完整性
try {
validate(StoreProductValidate::class)->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['admin_id'] = $this->uid;
$this->model->addStoreProduct($param);
}else{
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
return view();
}
}
public function category_arr($id=0){
$where[]=['is_show','=',1];
$where[]=['mer_id','=',0];
if($id!=0){
$where[1]=['mer_id','=',$id];
}
$list=Db::connect('shop')->table('eb_store_category')->where($where)
->field('store_category_id id,pid,cate_name name')->select();
$category_arr=create_tree_list(0,$list,0);
return to_assign(0,'操作成功', $category_arr);
}
/**
* 格式商品商户分类
* @Author:Qinii
* @Date: 2020/9/15
* @param array $data
* @param int $productId
* @param int $merId
* @return array
*/
public function setMerCate(array $data, int $productId, int $merId)
{
$result = [];
foreach ($data as $value) {
$result[] = [
'product_id' => $productId,
'mer_cate_id' => $value,
'mer_id' => $merId,
];
}
return $result;
}
/**
* 格式商品规格
* @Author:Qinii
* @Date: 2020/9/15
* @param array $data
* @param int $productId
* @return array
*/
public function setAttr(array $data, int $productId)
{
$result = [];
try{
foreach ($data as $value) {
$result[] = [
'type' => 0,
'product_id' => $productId,
"attr_name" => $value['value'] ?? $value['attr_name'],
'attr_values' => implode('-!-', $value['detail']),
];
}
} catch (\Exception $exception) {
throw new ValidateException('商品规格格式错误');
}
return $result;
}
/**
* 格式化秒杀商品活动时间
* @Author:Qinii
* @Date: 2020/9/15
* @param array $data
* @return array
*/
public function setSeckillProduct(array $data)
{
$dat = [
'start_day' => $data['start_day'],
'end_day' => $data['end_day'],
'start_time' => $data['start_time'],
'end_time' => $data['end_time'],
'status' => 1,
'once_pay_count' => $data['once_pay_count'],
'all_pay_count' => $data['all_pay_count'],
];
if (isset($data['mer_id'])) $dat['mer_id'] = $data['mer_id'];
return $dat;
}
/**
* 格式商品主体信息
* @Author:Qinii
* @Date: 2020/9/15
* @param array $data
* @return array
*/
public function setProduct(array $data)
{
$give_coupon_ids = '';
if (isset($data['give_coupon_ids']) && !empty($data['give_coupon_ids'])) {
$gcoupon_ids = array_unique($data['give_coupon_ids']);
$give_coupon_ids = implode(',', $gcoupon_ids);
}
if(isset($data['integral_rate'])){
$integral_rate = $data['integral_rate'];
if($data['integral_rate'] < 0) $integral_rate = -1;
if($data['integral_rate'] > 100) $integral_rate = 100;
}
$result = [
'store_name' => $data['store_name'],
'image' => $data['image'],
// 'slider_image' => is_array($data['slider_image']) ? implode(',', $data['slider_image']) : '',
'slider_image' => $data['slider_image'],
'store_info' => $data['store_info'] ?? '',
'keyword' => $data['keyword']??'',
'brand_id' => $data['brand_id'] ?? 0,
'cate_id' => $data['cate_id'] ?? 0,
'unit_name' => $data['unit_name']??'件',
'sort' => $data['sort'] ?? 0,
'is_show' => $data['is_show'] ?? 0,
'is_used' => (isset($data['status']) && $data['status'] == 1) ? 1 : 0,
'is_good' => $data['is_good'] ?? 0,
'video_link' => $data['video_link']??'',
'temp_id' => $data['delivery_free'] ? 0 : ($data['temp_id'] ?? 0),
'extension_type' => $data['extension_type']??0,
'spec_type' => $data['spec_type'] ?? 0,
'status' => $data['status']??0,
'give_coupon_ids' => $give_coupon_ids,
'mer_status' => $data['mer_status'],
'guarantee_template_id' => $data['guarantee_template_id']??0,
'is_gift_bag' => $data['is_gift_bag'] ?? 0,
'integral_rate' => $integral_rate ?? 0,
'delivery_way' => implode(',',$data['delivery_way']),
'delivery_free' => $data['delivery_free'] ?? 0,
'once_min_count' => $data['once_min_count'] ?? 0,
'once_max_count' => $data['once_max_count'] ?? 0,
'pay_limit' => $data['pay_limit'] ?? 0,
'svip_price_type' => $data['svip_price_type'] ?? 0,
];
if (isset($data['mer_id']))
$result['mer_id'] = $data['mer_id'];
if (isset($data['old_product_id']))
$result['old_product_id'] = $data['old_product_id'];
if (isset($data['product_type']))
$result['product_type'] = $data['product_type'];
if (isset($data['type']) && $data['type'])
$result['type'] = $data['type'];
if (isset($data['param_temp_id']))
$result['param_temp_id'] = $data['param_temp_id'];
if (isset($data['extend']))
$result['extend'] = $data['extend'] ? json_encode($data['extend'], JSON_UNESCAPED_UNICODE) :[];
return $result;
}
/**
* 格式商品SKU
* @Author:Qinii
* @Date: 2020/9/15
* @param array $data
* @param int $productId
* @return mixed
*/
public function setAttrValue(array $data, int $productId, int $productType, int $isUpdate = 0)
{
// $extension_status = systemConfig('extension_status');
$extension_status = 0;
if ($isUpdate) {
$where = ['product_id' => $productId];
$product = Db::connect('shop')->table('eb_store_product_attr_value')
->when(isset($where['product_id']) && $where['product_id'] !== '',function($query)use($where){
$query->where('product_id',$where['product_id']);
})
->when(isset($where['sku']) && $where['sku'] !== '',function($query)use($where){
$query->where('sku',$where['sku']);
})
->when(isset($where['unique']) && $where['unique'] !== '',function($query)use($where){
$query->where('unique',$where['unique']);
})->select()->toArray();
$oldSku = $this->detailAttrValue($product, null);
}
$price = $stock = $ot_price = $cost = $svip_price = 0;
try {
foreach ($data['attrValue'] as $value) {
$sku = '';
if (isset($value['detail']) && !empty($value['detail']) && is_array($value['detail'])) {
$sku = implode(',', $value['detail']);
}
$ot_price_ = $value['price'];
if (isset($value['active_price'])) {
$sprice = $value['active_price'] < 0 ? 0 : $value['active_price'];
} elseif (isset($value['presell_price'])) {
$sprice = $value['presell_price'] < 0 ? 0 : $value['presell_price'];
} elseif (isset($value['assist_price'])) {
$sprice = $value['assist_price'] < 0 ? 0 : $value['assist_price'];
} else {
$ot_price_ = $value['ot_price'];
$sprice = ($value['price'] < 0) ? 0 : $value['price'];
}
if (isset($value['svip_price']) && $data['svip_price_type']) {
$svip_price = !$svip_price ? $value['svip_price'] : (($svip_price > $value['svip_price']) ? $value['svip_price'] : $svip_price);
}
$cost = !$cost ? $value['cost'] : (($cost > $value['cost']) ?$cost: $value['cost']);
$price = !$price ? $sprice : (($price > $sprice) ? $sprice : $price);
$ot_price = !$ot_price ? $ot_price_ : (($ot_price > $ot_price_) ? $ot_price : $ot_price_);
$unique = $this->setUnique($productId, $sku, $productType);
$result['attrValue'][] = [
'detail' => json_encode($value['detail'] ?? ''),
"bar_code" => $value["bar_code"] ?? '',
"image" => $value["image"] ?? '',
"cost" => $value['cost'] ? (($value['cost'] < 0) ? 0 : $value['cost']) : 0,
"price" => $value['price'] ? (($value['price'] < 0) ? 0 : $value['price']) : 0,
"volume" => isset($value['volume']) ? ($value['volume'] ? (($value['volume'] < 0) ? 0 : $value['volume']) : 0) :0,
"weight" => isset($value['weight']) ? ($value['weight'] ? (($value['weight'] < 0) ? 0 : $value['weight']) : 0) :0,
"stock" => $value['stock'] ? (($value['stock'] < 0) ? 0 : $value['stock']) : 0,
"ot_price" => $value['ot_price'] ? (($value['ot_price'] < 0) ? 0 : $value['ot_price']) : 0,
"extension_one" => $extension_status ? ($value['extension_one'] ?? 0) : 0,
"extension_two" => $extension_status ? ($value['extension_two'] ?? 0) : 0,
"product_id" => $productId,
"type" => 0,
"sku" => $sku,
"unique" => $unique,
'sales' => $isUpdate ? ($oldSku[$sku]['sales'] ?? 0) : 0,
'svip_price' => $svip_price,
];
$stock = $stock + intval($value['stock']);
}
$result['data'] = [
'price' => $price ,
'stock' => $stock,
'ot_price' => $ot_price,
'cost' => $cost,
'svip_price' => $svip_price,
];
} catch (\Exception $exception) {
throw new ValidateException('规格错误 '.$exception->getMessage());
}
return $result;
}
/**
* @Author:Qinii
* @Date: 2020/5/11
* @param int $id
* @param string $sku
* @param int $type
* @return string
*/
public function setUnique(int $id, $sku, int $type)
{
return $unique = substr(md5($sku . $id), 12, 11) . $type;
// $has = (app()->make(ProductAttrValueRepository::class))->merUniqueExists(null, $unique);
// return $has ? false : $unique;
}
/**
* TODO 单商品sku
* @param $data
* @param $userInfo
* @return array
* @author Qinii
* @day 2020-08-05
*/
public function detailAttrValue($data, $userInfo, $productType = 0, $artiveId = null, $svipInfo = [])
{
$sku = [];
// $make_presll = app()->make(ProductPresellSkuRepository::class);
// $make_assist = app()->make(ProductAssistSkuRepository::class);
// $make_group = app()->make(ProductGroupSkuRepository::class);
foreach ($data as $value) {
$_value = [
'sku' => $value['sku'],
'price' => $value['price'],
'stock' => $value['stock'],
'image' => $value['image'],
'weight' => $value['weight'],
'volume' => $value['volume'],
'sales' => $value['sales'],
'unique' => $value['unique'],
'bar_code' => $value['bar_code'],
];
if($productType == 0 ){
$_value['ot_price'] = $value['ot_price'];
$_value['svip_price'] = $value['svip_price'];
}
// if ($productType == 2) {
// $_sku = $make_presll->getSearch(['product_presell_id' => $artiveId, 'unique' => $value['unique']])->find();
// if (!$_sku) continue;
// $_value['price'] = $_sku['presell_price'];
// $_value['stock'] = $_sku['stock'];
// $_value['down_price'] = $_sku['down_price'];
// }
// //助力
// if ($productType == 3) {
// $_sku = $make_assist->getSearch(['product_assist_id' => $artiveId, 'unique' => $value['unique']])->find();
// if (!$_sku) continue;
// $_value['price'] = $_sku['assist_price'];
// $_value['stock'] = $_sku['stock'];
// }
// //拼团
// if ($productType == 4) {
// $_sku = $make_group->getSearch(['product_group_id' => $artiveId, 'unique' => $value['unique']])->find();
// if (!$_sku) continue;
// $_value['price'] = $_sku['active_price'];
// $_value['stock'] = $_sku['stock'];
// }
//推广员
// if ($this->getUserIsPromoter($userInfo)) {
// $_value['extension_one'] = $value->bc_extension_one;
// $_value['extension_two'] = $value->bc_extension_two;
// }
$sku[$value['sku']] = $_value;
}
return $sku;
}
/**
*
* 编辑
*
*/
public function edit()
{
$id = get_params("id");
if(!$id) return to_assign(1, '非法操作!');
if (request()->isAjax()) {
$param = get_params();
$data=$param;
$data['mer_id'] = 4;
$data['status'] = 1;
$data['mer_status'] =1;
$data['rate'] = 3;
$data['spec_type'] = $data['is_attribute'];//规格 0单 1多
if (!$data['spec_type']) {
if (isset($data['skus']) && count($data['skus']) > 1) throw new ValidateException('单规格商品属性错误');
}
if($data['spec_type'] == 0){
$attr_data[0]['price'] = $data['price'];
$attr_data[0]['cost'] = $data['cost'];
$attr_data[0]['ot_price'] = $data['ot_price'];
$attr_data[0]['stock'] = $data['stock'];
$attr_data[0]['bar_code'] = $data['bar_code'];
$attr_data[0]['weight'] = $data['weight'];
$attr_data[0]['volume'] = $data['volume'];
$new_attr = [];
$data['attrValue'] = $attr_data;
}
// halt($data);
if($data['spec_type'] == 1){
$new_attr = [];
if($data['attr']){
$attr = json_decode($data['attr'],1);
foreach ($attr as $k => $v){
$new_attr[$k]['value'] = $v['title'];
$new_attr[$k]['detail'] = array_column($v['child'],'title');
}
}
$skus = $data['skus'];
foreach ($skus as $k => $v){
foreach ($data['kk'] as $key => $val){
$detail[$val] = $v['value'.$key];
}
$v['detail'] = $detail;
$new_sku[] = $v;
}
$data['attrValue'] = $new_sku;
}
unset($data['skus']);
$data['attr'] = $new_attr;
$data['params'] = [];
$data['mer_labels'] = [];
$data['mer_id'] = $this->mer_id;
$content = [
'content' => $data['content'] ,
'type' => 0
];
$productType=0;
$conType=0;
$product = $this->setProduct($data);
$data['mer_cate_id'] = [$data['mer_cate_id']];
$this->edit_product($id, $data, $this->mer_id, 0);
to_assign(0, '操作成功!');
}else{
$supplyChain = SupplyChain::with(['merchant', 'street', 'area'])->find($id); // 取出当前供应链数据
View::assign('detail', $supplyChain);
// 取出正常的商家
$merchant = Merchant::where('status', 1)->column('mer_id, real_name');
// 区域模型
$arealist = GeoArea::where('city_code', '510500')->select();
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
View::assign('arealist', $arealist);
View::assign('merchant', $merchant);
View::assign('url', $this->url);
$getAdminOneProduct = $this->getAdminOneProduct((int)$id,null);
//dump($getAdminOneProduct);
View::assign('product', $getAdminOneProduct);
return view();
}
}
public function edit_product(int $id, array $data, int $merId, int $productType, $conType = 0)
{
if (!$data['spec_type']) {
$data['attr'] = [];
if (count($data['attrValue']) > 1) throw new ValidateException('单规格商品属性错误');
}
$spuData = $product = $this->setProduct($data);
$settleParams = $this->setAttrValue($data, $id, $productType, 1);
$settleParams['cate'] = $this->setMerCate($data['mer_cate_id'], $id, $merId);
$settleParams['attr'] = $this->setAttr($data['attr'], $id);
$content = [
'content' => $conType ? json_encode($data['content']) : $data['content'] ,
'type' => $conType
];
$spuData['price'] = $settleParams['data']['price'];
$spuData['mer_id'] = $merId;
$spuData['mer_labels'] = $data['mer_labels'];
Db::transaction(function () use ($id, $data, $productType, $settleParams,$content,$product,$spuData,$merId) {
$this->save($id, $settleParams, $content, $product, $productType);
// if ($productType == 1) { //秒杀商品
// $dat = $this->setSeckillProduct($data);
// app()->make(StoreSeckillActiveRepository::class)->updateByProduct($id, $dat);
// }
if ($productType == 0) {
$make = Db::connect('shop')->table('eb_parameter_value');
$make->where('product_id',$id)->delete();
$this->parameter_create($id, $data['params'] ?? [], $data['mer_id']);
}
$this->baseUpdate($spuData, $id, 0, $productType);
});
}
public function baseUpdate(array $param, int $productId, int $activityId, $productType = 0)
{
// if ($productType == 1) {
// $make = app()->make(StoreSeckillActiveRepository::class);
// $activityId = $make->getSearch(['product_id' => $productId])->value('seckill_active_id');
// }
$where = [
'product_id' => $productId,
'activity_id' => $activityId,
'product_type' => $productType,
];
$ret = Db::connect('shop')->table('eb_store_spu')->where($where)->find();
if (!$ret) {
$this->setparam($param, $productId, $activityId, $productType);
} else {
$data = $this->setparams($param, $productId, $activityId, $productType);
$value = $data['mer_labels'];
if (!empty($value)) {
if (!is_array($value)) {
$data['mer_labels'] = ',' . $value . ',';
} else {
$data['mer_labels'] = ',' . implode(',', $value) . ',';
}
}
return Db::connect('shop')->table('eb_store_spu')->where($where)->update($data);
}
}
public function setparams(array $param, $productId, $activityId, $productType)
{
$data = [
'product_id' => $productId,
'product_type' => $productType ?? 0,
'activity_id' => $activityId,
'store_name' => $param['store_name'],
'keyword' => $param['keyword'] ?? '',
'image' => $param['image'],
'price' => $param['price'],
'status' => 0,
'rank' => $param['rank'] ?? 0,
'temp_id' => $param['temp_id'],
'sort' => $param['sort'] ?? 0,
'mer_labels' => $param['mer_labels'] ?? '',
];
if (isset($param['mer_id'])) $data['mer_id'] = $param['mer_id'];
return $data;
}
/**
* TODO 后台管理需要的商品详情
* @param int $id
* @param int|null $activeId
* @return array|\think\Model|null
* @author Qinii
* @day 2020-11-24
*/
public function getAdminOneProduct(int $id, ?int $activeId, $conType = 0)
{
$data = Db::connect('shop')->table('eb_store_product')->where('product_id',$id)->field('product_id,mer_id,store_name,store_info,keyword,brand_id,cate_id,unit_name,sort,sales,price,cost,ot_price,stock,is_good,product_type,spec_type,extension_type,image,slider_image,once_min_count,delivery_way,delivery_free,pay_limit')->find();
$data['delivery_way'] = empty($data['delivery_way']) ? [2] : explode(',',$data['delivery_way']);
if(in_array('1',$data['delivery_way'])){
$data['delivery_way1'] = 1;
}else{
$data['delivery_way1'] = 0;
}
if(in_array('2',$data['delivery_way'])){
$data['delivery_way2'] = 1;
}else{
$data['delivery_way2'] = 0;
}
$data['extend'] = empty($data['extend']) ? [] : json_decode($data['extend']);
if ($data['product_type'] == 0) {
$activeId = 0;
}
$data['slider_image_arr'] = explode(',',$data['slider_image']);
$data['attr'] = Db::connect('shop')->table('eb_store_product_attr')->where('product_id',$id)->select()->toArray();
$data['attrValue'] = Db::connect('shop')->table('eb_store_product_attr_value')->where('product_id',$id)->select()->toArray();
$spu_where = ['activity_id' => $activeId, 'product_type' => $data['product_type'], 'product_id' => $id];
$spu = Db::connect('shop')->table('eb_store_spu')->where($spu_where)->find();
$data['star'] = $spu['star'] ?? '';
$data['mer_labels'] = $spu['mer_labels'] ?? '';
$data['sys_labels'] = $spu['sys_labels'] ?? '';
$data['mer_cate_id'] = Db::connect('shop')->table('eb_store_product_cate')->where('product_id',$id)->value('mer_cate_id');
if($data['attr']){
$child_id = 1;
foreach ($data['attr'] as $k => $v) {
if(strpos($v['attr_values'],'-!-') !== false){
$attr_values = explode('-!-',$v['attr_values']);
foreach ($attr_values as $key =>$val){
$child[]= [
'id' => $child_id,
'title' => $val,
'checked' => true
];
$child_id++;
}
}else{
$child[]= [
'id' => $child_id,
'title' => $v['attr_values'],
'checked' => true
];
}
$data['attr'][$k] = [
'id' => $k+1,
'title' => $v['attr_name'],
'child' => $child
];
unset($child);
$child_id++;
}
$product_attr = $data['attr'];
$data['attr'] = htmlspecialchars_decode(json_encode($data['attr'],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
}
$attrValue = (in_array($data['product_type'], [3, 4])) ? $data['oldAttrValue'] : $data['attrValue'];
unset($data['oldAttrValue'], $data['attrValue']);
$arr = [];
$code = '';
foreach ($attrValue as $key => $item) {
$sku = explode(',', $item['sku']);
$item['old_stock'] = $old_stock ?? $item['stock'];
foreach ($sku as $k => $v) {
$item['value' . $k] = $v;
}
if($data['attr']){
foreach ($product_attr as $kk => $vv){
foreach ($vv['child'] as $key =>$val){
if($val['title'] == $item['value0']){
$code = $val['id'].'-';
}
if($val['title'] == $item['value1']){
$code .= $val['id'];
}
}
}
$arr['skus['.$code.'][image]'] = $item['image'];
$arr['skus['.$code.'][price]']= $item['price'];
$arr['skus['.$code.'][ot_price]'] = $item['ot_price'];
$arr['skus['.$code.'][cost]'] = $item['cost'];
$arr['skus['.$code.'][stock]'] = $item['stock'];
$arr['skus['.$code.'][bar_code]'] = $item['bar_code'];
$arr['skus['.$code.'][weight]'] = $item['weight'];
$arr['skus['.$code.'][volume]'] = $item['volume'];
}else{
$data['attr'] = json_encode($data['attr']);
$arr['image'] = $item['image'];
$arr['price']= $item['price'];
$arr['ot_price'] = $item['ot_price'];
$arr['cost'] = $item['cost'];
$arr['stock'] = $item['stock'];
$arr['bar_code'] = $item['bar_code'];
$arr['weight'] = $item['weight'];
$arr['volume'] = $item['volume'];
}
}
$data['attrValue'] = htmlspecialchars_decode(json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
$data['content'] = Db::connect('shop')->table('eb_store_product_content')->where('product_id',$id)->value('content');
// halt($data);
return $data;
}
/**
*
* 删除
*
*/
public function delete()
{
$id = get_params("id");
if(!$id) return to_assign(1, '非法操作!');
// $supplyChain = SupplyChain::with(['linkMerchant'])->find($id);
// // 删除关联模型
// $res = $supplyChain->together(['linkMerchant'])->delete();
$res = Db::connect('shop')->table('eb_store_product')->where('product_id',$id)->delete();
if ($res){
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
}
public function read(){
$id = get_params("id");
if(!$id) return to_assign(1, '非法操作!');
$supplyChain = SupplyChain::with(['merchant', 'street', 'area'])->find($id); // 取出当前供应链数据
View::assign('detail', $supplyChain);
// 取出正常的商家
$merchant = Merchant::where('status', 1)->column('mer_id, real_name');
// 区域模型
$arealist = GeoArea::where('city_code', '510500')->select();
$store_brand= Db::connect('shop')->table('eb_store_brand')->where(['is_show' => 1])
->select();
View::assign('store_brand', $store_brand);
View::assign('arealist', $arealist);
View::assign('merchant', $merchant);
View::assign('url', $this->url);
$getAdminOneProduct = $this->getAdminOneProduct((int)$id,null);
//dump($getAdminOneProduct);
View::assign('product', $getAdminOneProduct);
return view();
}
}