<?php
|
|
namespace app\admin\controller;
|
|
use app\supplier\model\DistributionArea as DistributionAreaModel;
|
use think\Exception;
|
use think\facade\Config;
|
use think\facade\Db;
|
use think\facade\View;
|
use think\facade\Request;
|
use app\supplier\model\GoodsAnnex;
|
use app\supplier\model\GoodsSpec;
|
use app\supplier\model\GoodsAttribute;
|
use app\supplier\model\GoodsLabel;
|
use app\supplier\model\GoodsRetailPriceLimit;
|
|
//use app\supplier\model\Goods;
|
|
//商品零售价限制
|
|
class Goods extends Common {
|
|
/**
|
* 商品管理
|
* @return string
|
*/
|
public function index() {
|
return View::fetch();
|
}
|
|
/**
|
* 获取商品列表
|
* @return mixed
|
*/
|
public function getGoodsList() {
|
try {
|
if (!Request::isPost()) {
|
throw new \Exception(lang('request_method_incorrect'));
|
}
|
$platform_goods_code = input('platform_goods_code');
|
$supplier_goods_code = input('supplier_goods_code');
|
|
$condition['g.istemp'] = 1;//编辑过的商品
|
if ($platform_goods_code) {
|
$condition['g.platform_goods_code'] = $platform_goods_code;
|
}
|
if ($supplier_goods_code) {
|
$condition['g.supplier_goods_code'] = $supplier_goods_code;
|
}
|
$p = input('p', 1);
|
$_GET['p'] = $p;
|
$other['page_size'] = input('page_size', 20);
|
$order = 'g.id asc';
|
$count = \app\supplier\model\Goods::alias('g')->where($condition)->count();
|
$list = \app\supplier\model\Goods::alias('g')
|
->join('goods_desc gd', 'g.id=gd.goods_id')
|
->field("g.id,g.sales_form,g.supplier_goods_code,g.platform_goods_code,g.goods_purpose,g.add_time,g.status,g.publish_status,g.whether_dis,gd.goods_title_cn,g.first_cate_code,g.second_cate_code,g.three_cate_code,(select url from web_goods_picture where goods_id=g.id order by id asc limit 1) as picture,g.review_comments,g.is_show")->where($condition)->order($order)->page($p, $other['page_size'])->select()->toArray();
|
$other['count'] = $count;
|
if (!$list) {
|
throw new \Exception(lang('no_data_found'));
|
}
|
$categorylist = get_category();
|
$categorylist = datalist($categorylist, 'cate_code', '', FALSE);
|
foreach ($list as &$item) {
|
$item['first_cate_name'] = $categorylist[$item['first_cate_code']]['cate_name'];
|
$item['second_cate_name'] = $categorylist[$item['second_cate_code']]['cate_name'];
|
$item['three_cate_name'] = $categorylist[$item['three_cate_code']]['cate_name'];
|
}
|
$result['list'] = $list;
|
$result['other'] = $other;
|
return $this->successResponse($result);
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
function updateExt() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$postField = 'id,is_show';
|
$data = Request::only(explode(',', $postField), 'post');
|
if (!$data['id']) {
|
throw new Exception(lang('parameter_error'));
|
}
|
Db::name('goods')->update($data);
|
$result['message'] = '操作成功';
|
return $this->successResponse($result);
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
/**
|
* 审核商品
|
* @return array|int[]
|
*/
|
public function reviewGoods() {
|
// 开始数据库事务
|
Db::startTrans();
|
try {
|
if (!Request::isPost()) {
|
throw new \Exception(lang('request_method_incorrect'));
|
}
|
$id = input('post.id');
|
if (!is_numeric($id)) {
|
throw new \Exception(lang('invalid_id'));
|
}
|
|
$status = input('post.status');
|
//更新数据
|
$info = Db::name('goods')->field('id,status')->where('id', $id)->find();
|
if (!$info) {
|
return $this->errorResponse(lang('request_method_incorrect'));
|
}
|
if ($status == 1) {
|
Db::name('goods')->where('id', $id)->update(['status' => 1, 'review_comments' => '']);
|
$message = '操作成功,审核通过';
|
} else {
|
$review_comments = trim(input('review_comments'));
|
if (!$review_comments) {
|
throw new \Exception('请填写审核不通过原因');
|
}
|
Db::name('goods')->where('id', $id)->update(['status' => 3, 'review_comments' => $review_comments]);
|
$message = '操作成功,审核不通过';
|
}
|
// 提交事务
|
Db::commit();
|
return $this->successResponse($message);
|
|
} catch (\Exception $exc) {
|
// 回滚事务
|
Db::rollback();
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 创建商品
|
* @return string
|
*/
|
public function create() {
|
////////
|
View::assign('menuitem', 'goods-index');
|
$id = input('id');
|
if (!is_numeric($id)) {
|
$this->redirect(url('/admin/goods/index')->build());
|
}
|
$other['id'] = $id;
|
View::assign('other', $other);
|
return View::fetch();
|
}
|
|
public function getGoodsInfo() {
|
|
try {
|
if (!Request::isPost()) {
|
throw new \Exception(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
if (!is_numeric($id)) {
|
throw new \Exception(lang('invalid_id'));
|
}
|
|
$where = [
|
'id' => $id,
|
];
|
$info = Db::name('goods')->field('id,first_cate_code,second_cate_code,three_cate_code,brand_code')->where($where)->find();
|
if ($info['three_cate_code']) {
|
$info['cate_code'] = [$info['first_cate_code'], $info['second_cate_code'], $info['three_cate_code']];
|
}
|
$info['goods_purpose'] = json_decode($info['goods_purpose'], TRUE);
|
//////获取描述标题数据/////////
|
$where_other = [
|
'goods_id' => $info['id'],
|
];
|
$goods_desc = Db::name('goods_desc')->field('goods_title_en,goods_title_cn')->where($where_other)->find();
|
//获取描述内容数据
|
$goods_desc_content = Db::name('goods_desc_content')->field('goods_desc_en,goods_desc_cn')->where($where_other)->find();
|
|
////////////////
|
$result['info'] = $info;
|
$result['goods_desc'] = $goods_desc;
|
$result['goods_desc_content'] = $goods_desc_content;
|
return $this->successResponse($result);
|
} catch (\Exception $e) {
|
return $this->errorResponse($e->getMessage());
|
}
|
}
|
|
/**
|
* 获取商品分销信息
|
* @return array|int[]
|
*/
|
public function getGoodsDisInfo() {
|
|
try {
|
if (!Request::isPost()) {
|
throw new \Exception(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
|
$condition['g.istemp'] = 1;//编辑过的商品
|
if ($id) {
|
$condition['g.id'] = $id;
|
}
|
|
$info = \app\supplier\model\Goods::alias('g')
|
->join('goods_desc gd', 'g.id=gd.goods_id')
|
->join('goods_desc_content gdc', 'g.id=gdc.goods_id')
|
->field("g.id,g.warehouse_mode,g.sales_form,g.supplier_goods_code,g.platform_goods_code,g.goods_purpose,g.add_time,g.status,g.publish_status,g.whether_dis,g.is_limit_dis_platform,gd.goods_title_cn,gd.goods_title_en,gd.goods_keyword_en,gd.goods_keyword_cn,g.first_cate_code,g.second_cate_code,g.three_cate_code,g.docking_way,g.price_method,g.freight_attr_code,g.is_tort,g.is_stop_buy,g.recommend_man,gdc.goods_desc_en,gdc.goods_desc_cn")->where($condition)->find();
|
if ($info) {
|
// $categorylist = get_category();
|
// $categorylist = datalist($categorylist, 'cate_code', '', FALSE);
|
// $info['first_cate_name'] = $categorylist[$info['first_cate_code']]['cate_name'];
|
// $info['second_cate_name'] = $categorylist[$info['second_cate_code']]['cate_name'];
|
// $info['three_cate_name'] = $categorylist[$info['three_cate_code']]['cate_name'];
|
$info['attr_name'] = Db::name('goods_freight_attr')->where('attr_code', $info['freight_attr_code'])->value('attr_name');
|
$info['goods_keyword_en'] = $info['goods_keyword_en'] ? json_decode($info['goods_keyword_en'], TRUE) : [];
|
$info['goods_keyword_cn'] = $info['goods_keyword_cn'] ? json_decode($info['goods_keyword_cn'], TRUE) : [];
|
|
$info['goodsSpecInfo'] = Db::name('goods_spec')->field('*')->where('goods_id', $info['id'])->find();
|
$goods_attribute_info = Db::name('goods_attribute')->field('*')
|
->withAttr('material_main', function ($value, $data) {
|
return Db::name('goods_material')->where('id', $value)->value('material_name');
|
})
|
->withAttr('material_one', function ($value, $data) {
|
return Db::name('goods_material')->where('id', $value)->value('material_name');
|
})
|
->withAttr('material_two', function ($value, $data) {
|
return Db::name('goods_material')->where('id', $value)->value('material_name');
|
})
|
->withAttr('material_three', function ($value, $data) {
|
if ($value) {
|
$value = json_decode($value, TRUE);
|
return Db::name('goods_material')->whereIn('id', $value)->column('material_name');
|
} else {
|
return [];
|
}
|
})
|
->where('goods_id', $info['id'])->find();
|
$info['goodsAttributeInfo'] = $goods_attribute_info;
|
$info['goodsAttributExtendList'] = Db::name('goods_attribute_extend')->field('*')->where('goods_id', $info['id'])->select()->toArray();
|
|
$info['goodsRetailPriceLimitList'] = GoodsRetailPriceLimit::field('*')->where('goods_id', $info['id'])
|
->withAttr('sale_country_code', function ($value, $data) {
|
return Db::name('stock_country')->where('id', $value)->value("CONCAT(country_code,' ',country)");
|
})
|
->withAttr('sales_platform', function ($value, $data) {
|
return Db::name('sales_platform')->where('id', $value)->value("platform_name");
|
})
|
->order('id asc')->select()->toArray();///
|
$info['goodsLabelList'] = GoodsLabel::field('label_type,goods_label')->where('goods_id', $info['id'])->order('id asc')->select()->toArray();///
|
if ($info['is_limit_dis_platform'] == 1) {
|
$info['goodsLimitDisPlatformList'] = Db::name('goods_limit_dis_platform')
|
->alias('a')
|
->join('dis_platform b', 'a.dis_platform = b.id')
|
->field('b.platform_name')->where('goods_id', $info['id'])->order('a.id asc')->select()->toArray();///
|
} else {
|
$info['goodsLimitDisPlatformList'] = [];
|
}
|
$result['info'] = $info;
|
///分销区域//
|
$disAreaList = Db::name('goods_distribution_area')
|
->alias('gda')
|
->join('distribution_area da', 'da.id=gda.distribution_area_id')
|
->field("gda.one_shipment,gda.distribution_area_id")->where('gda.goods_id', $info['id'])->where('gda.whether_dis', 1)->withAttr('one_shipment', function ($value, $data) {
|
return $value == 1 ? '一件代发' : '';
|
})->withAttr('distribution_area_id', function ($value, $data) {
|
return $this->getDisArea($value);
|
})->select()->toArray();
|
$result['disAreaList'] = $disAreaList;
|
//分销库存
|
$disStockList = Db::name('goods_distribution_area_stock')
|
->alias('gdas')
|
->join('third_party_stock tps', 'tps.id=gdas.third_party_stock_id')
|
->field("gdas.distribution_area_id,gdas.supplier_stock_amount,gdas.platform_stock_amount,gdas.goods_distribution_area_id,tps.third_party_stock_name,tps.docking_code,tps.stock_type")->where('gdas.goods_id', $info['id'])->where('gdas.whether_dis', 1)
|
->withAttr('distribution_area_id', function ($value, $data) {
|
return $this->getDisArea($value);
|
})->select()->toArray();
|
$result['disStockList'] = $disStockList;
|
//分销价格///
|
$disPriceList = Db::name('goods_distribution_area_price')
|
->alias('gdap')
|
->join('goods_distribution_area gda', 'gda.id=gdap.goods_distribution_area_id')
|
->join('logistics_product lp', 'lp.id=gdap.logistics_product_id')
|
->field("gdap.distribution_area_id,gdap.goods_distribution_area_id,lp.product_name_cn,gdap.supply_price,gdap.dis_price,gdap.package_goods_amount,gda.take_effect_time")->where('gdap.goods_id', $info['id'])->where('gda.whether_dis', 1)
|
->withAttr('distribution_area_id', function ($value, $data) {
|
return $this->getDisArea($value);
|
})
|
->withAttr('take_effect_time', function ($value, $data) {
|
return date('Y-m-d H:i:s', $value);
|
})->select()->toArray();
|
$result['disPriceList'] = $disPriceList;
|
//退货地址列表//
|
$disReturnGoodsList = Db::name('goods_distribution_area')
|
->alias('gda')
|
->join('third_party_stock tps', 'tps.id=gda.third_party_stock_id')
|
->join('third_party_stock_address tpsa', 'tpsa.id=gda.third_party_stock_address_id')
|
->field("gda.distribution_area_id,tps.third_party_stock_name,tpsa.stock_address,tpsa.stock_city,tpsa.stock_province,tpsa.stock_post_code,tpsa.stock_country_id,tpsa.receive_man,tpsa.tel")->where('gda.goods_id', $info['id'])->where('gda.whether_dis', 1)
|
->withAttr('distribution_area_id', function ($value, $data) {
|
return $this->getDisArea($value);
|
})
|
->withAttr('stock_country_id', function ($value, $data) {
|
return Db::name('stock_country')->where('id', $value)->value('country');
|
})
|
->select()->toArray();
|
$result['disReturnGoodsList'] = $disReturnGoodsList;
|
$categorylist = get_category();
|
$categorylist = datalist($categorylist, 'cate_code', '', FALSE);
|
$catelist = Db::name('goods_category')
|
->alias('GC')
|
->join('docking_platform DP', 'DP.cate_code=GC.cate_code')
|
->field('GC.*,DP.docking_platform_name')->where(['GC.goods_id' => $info['id'], 'GC.status' => 1])->select()->toArray();
|
foreach ($catelist as &$value) {
|
$value['first_cate_name'] = $categorylist[$value['first_cate_code']]['cate_name'];
|
$value['second_cate_name'] = $categorylist[$value['second_cate_code']]['cate_name'];
|
$value['three_cate_name'] = $categorylist[$value['three_cate_code']]['cate_name'];
|
}
|
$result['catelist']=$catelist;
|
return $this->successResponse($result);
|
} else {
|
return $this->errorResponse(lang('no_data_found'));
|
}
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 获取商品图片列表
|
* @return array|int[]
|
*/
|
public function getGoodsPictureList() {
|
try {
|
if (!Request::isPost()) {
|
throw new \Exception(lang('request_method_incorrect'));
|
}
|
$goods_id = input('goods_id');
|
$picture_type = input('picture_type');
|
|
if (is_array($picture_type)) {
|
$where[] = ['a.goods_id', '=', $goods_id];
|
if (is_array($picture_type)) {
|
$where[] = ['b.type', 'in', $picture_type];
|
}
|
$list = Db::name('goods_picture')
|
->alias('a')
|
->join('goods_picture_type b', 'a.id=b.picture_id')
|
->field('DISTINCT a.id,a.url')->where($where)->order('id asc')->select()->toArray();
|
} else {
|
$where[] = ['goods_id', '=', $goods_id];
|
$list = Db::name('goods_picture')->field('id,url')->where($where)->order('id asc')->select()->toArray();
|
}
|
if (!$list) {
|
throw new \Exception(lang('no_data_found'));
|
}
|
$result['list'] = $list;
|
return $this->successResponse($result);
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 根据类型获取商品图片数量
|
* @return void
|
*/
|
public function getGoodsPictureCount() {
|
try {
|
if (!Request::isPost()) {
|
throw new \Exception(lang('request_method_incorrect'));
|
}
|
$goods_id = input('goods_id');
|
$goodsPictureCount['type0'] = Db::name('goods_picture')->where(['goods_id' => $goods_id])->count();
|
$goodsPictureCount['type1'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 1])->count();
|
$goodsPictureCount['type2'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 2])->count();
|
$goodsPictureCount['type3'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 3])->count();
|
$goodsPictureCount['type4'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 4])->count();
|
$goodsPictureCount['type5'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 5])->count();
|
$goodsPictureCount['type6'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 6])->count();
|
$goodsPictureCount['type7'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 7])->count();
|
$result['goodsPictureCount'] = $goodsPictureCount;
|
return $this->successResponse($result);
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 获取分销区域
|
*/
|
private function getDisArea($id) {
|
return Db::name('distribution_area')
|
->alias('da')
|
->join('deliver_region dr', 'dr.deliver_region_id=da.deliver_region_id')
|
->field("da.dis_currency,dr.region_name,da.dis_model")
|
->where('da.id', $id)->find();
|
}
|
|
/**
|
* 获取分类列表
|
* @return array
|
*/
|
public function getCategoryList() {
|
try {
|
if (!Request::isPost()) {
|
throw new \Exception(lang('request_method_incorrect'));
|
}
|
$result['list'] = $this->getchildrenids(0);
|
return $this->successResponse($result);
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 循环获取分类数据
|
* @param $id
|
* @return array|void
|
* @throws \think\db\exception\DataNotFoundException
|
* @throws \think\db\exception\DbException
|
* @throws \think\db\exception\ModelNotFoundException
|
*/
|
protected function getchildrenids($id) {
|
$where['father_code'] = $id;
|
$result = Db::name('category')->field('id,cate_code as value,cate_name as label')->where($where)->cacheAlways(TRUE, 0, 'category')->order('id asc')->select()->toArray();
|
if ($result) {
|
foreach ($result as &$val) {
|
$val['children'] = $this->getchildrenids($val['value']); ////
|
}
|
return $result;
|
}
|
}
|
|
}
|