123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832 |
- <?php
- namespace app\admin\controller\store;
- use app\admin\controller\AuthController;
- use app\models\system\SystemStore;
- use app\admin\model\store\{
- StoreDescription,
- StoreProductAttrValue,
- StoreProductAttr,
- StoreProductAttrResult,
- StoreProductCate,
- StoreProductRelation,
- StoreCategory as CategoryModel,
- StoreProduct as ProductModel
- };
- use app\admin\model\ump\StoreBargain;
- use app\admin\model\ump\StoreCombination;
- use app\admin\model\ump\StoreSeckill;
- use crmeb\services\{
- JsonService, UtilService as Util, JsonService as Json, FormBuilder as Form
- };
- use crmeb\traits\CurdControllerTrait;
- use think\facade\Route as Url;
- use app\admin\model\system\{SystemAttachment, ShippingTemplates, SystemUserLevel};
- /**
- * 产品管理
- * Class StoreProduct
- * @package app\admin\controller\store
- */
- class StoreProduct extends AuthController
- {
- use CurdControllerTrait;
- protected $bindModel = ProductModel::class;
- /**
- * 显示资源列表
- *
- * @return \think\Response
- */
- public function index()
- {
- $type = $this->request->param('type');
- //获取分类
- $this->assign('cate', CategoryModel::getTierList(null, 1));
- //出售中产品
- $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->count();
- //待上架产品
- $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->count();
- //仓库中产品
- $warehouse = ProductModel::where('is_del', 0)->count();
- //已经售馨产品
- $outofstock = ProductModel::getModelObject()->where(ProductModel::setData(4))->where('pav.type', 0)->count('DISTINCT id');
- //警戒库存
- $store_stock = sys_config('store_stock');
- if ($store_stock < 0) $store_stock = 2;
- $policeforce = ProductModel::getModelObject()->where(ProductModel::setData(5))->where('p.stock', '<=', $store_stock)->where('pav.type', 0)->count('DISTINCT id');
- //回收站
- $recycle = ProductModel::where('is_del', 1)->count();
- if ($type == null) $type = 1;
- $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
- return $this->fetch();
- }
- /**
- * 异步查找产品
- *
- * @return json
- */
- public function product_ist()
- {
- $where = Util::getMore([
- ['page', 1],
- ['limit', 20],
- ['store_name', ''],
- ['cate_id', ''],
- ['excel', 0],
- ['order', ''],
- ['type', $this->request->param('type')]
- ]);
- return Json::successlayui(ProductModel::ProductList($where));
- }
- /**
- * 设置单个产品上架|下架
- *
- * @return json
- */
- public function set_show($is_show = '', $id = '')
- {
- ($is_show == '' || $id == '') && Json::fail('缺少参数');
- $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
- if ($res) {
- return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
- } else {
- return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
- }
- }
- /**
- * 快速编辑
- *
- * @return json
- */
- public function set_product($field = '', $id = '', $value = '')
- {
- $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
- if (ProductModel::where(['id' => $id])->update([$field => $value]))
- return Json::successful('保存成功');
- else
- return Json::fail('保存失败');
- }
- /**
- * 设置批量产品上架
- *
- * @return json
- */
- public function product_show()
- {
- $post = Util::postMore([
- ['ids', []]
- ]);
- if (empty($post['ids'])) {
- return Json::fail('请选择需要上架的产品');
- } else {
- $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
- if ($res)
- return Json::successful('上架成功');
- else
- return Json::fail('上架失败');
- }
- }
- /**
- * 显示创建资源表单页.
- *
- * @return \think\Response
- */
- public function create($id = 0)
- {
- $this->assign('id', (int)$id);
- $money_type = [];
- $money_type[] = ['code' => 'now_money', 'name' => '余额'];
- $money_type[] = ['code' => 'integral', 'name' => '积分'];
- $money_type[] = ['code' => 'brokerage_price', 'name' => '佣金'];
- $money_type = array_merge($money_type, sys_data('money_type'));
- $this->assign('money_type', $money_type);
- return $this->fetch();
- }
- /**
- * 获取规则属性模板
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function get_rule()
- {
- return Json::successful(\app\models\store\StoreProductRule::field(['rule_name', 'rule_value'])->select()->each(function ($item) {
- $item['rule_value'] = json_decode($item['rule_value'], true);
- })->toArray());
- }
- /**
- * 获取产品详细信息
- * @param int $id
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function get_product_info($id = 0)
- {
- $list = CategoryModel::getTierList(null, 1);
- $menus = [];
- foreach ($list as $menu) {
- $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0 ? 0 : 1];//,'disabled'=>$menu['pid']== 0];
- }
- $data['tempList'] = ShippingTemplates::order('sort', 'desc')->field(['id', 'name'])->select()->toArray();
- $data['cateList'] = $menus;
- $level_list = SystemUserLevel::where(['is_del' => 0])->order('grade asc')->select();
- $level_price = [];
- foreach ($level_list as $v) {
- $level_price['level_' . $v['id']] = 0;
- }
- $data['level_list'] = $level_list;
- $data['storeList'] = SystemStore::field(['id', 'name'])->select()->toArray();
- $data['productInfo'] = [];
- if ($id) {
- $productInfo = ProductModel::get($id);
- if (!$productInfo) {
- return Json::fail('修改的产品不存在');
- }
- $productInfo['cate_id'] = explode(',', $productInfo['cate_id']);
- $productInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
- $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
- if ($productInfo['spec_type'] == 1) {
- $result = StoreProductAttrResult::getResult($id);
- foreach ($result['value'] as $k => $v) {
- $num = 1;
- foreach ($v['detail'] as $dv) {
- $result['value'][$k]['value' . $num] = $dv;
- $num++;
- if (!isset($result['value'][$k]['level_price'])) {
- $result['value'][$k]['level_price'] = $level_price;
- } else {
- $result['value'][$k]['level_price'] = is_string($result['value'][$k]['level_price']) ? json_decode($result['value'][$k]['level_price'], true) : $result['value'][$k]['level_price'];
- $level_price_real = [];
- foreach ($level_price as $check => $v) {
- $level_price_real[$check] = $result['value'][$k]['level_price'][$check] ?? $v;
- }
- $result['value'][$k]['level_price'] = $level_price_real;
- }
- }
- foreach ($level_list as $vv) {
- $result['value'][$k]['priceList'][] = [
- 'id' => $vv['id'],
- 'name' => $vv['name'],
- 'value' => $result['value'][$k]['level_price']['level_' . $vv['id']],
- 'k' => $k,
- ];
- }
- }
- $productInfo['items'] = $result['attr'] ?? [];
- $productInfo['attrs'] = $result['value'] ?? [];
- $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'level_price' => $level_price, 'ot_price' => 0, 'stock' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0, 'brokerage_two' => 0, 'return_money_type' => '', 'return_money' => 0, 'return_day' => 1];
- } else {
- $result = StoreProductAttrResult::getResult($id);
- $single = isset($result['value'][0]) ? $result['value'][0] : [];
- $productInfo['items'] = [];
- $productInfo['attrs'] = [];
- if (!isset($single['level_price'])) {
- $single['level_price'] = $level_price;
- } else {
- $single['level_price'] = is_string($single['level_price']) ? json_decode($single['level_price'], true) : $single['level_price'];
- $level_price_real = [];
- foreach ($level_price as $check => $v) {
- $level_price_real[$check] = $single['level_price'][$check] ?? $v;
- }
- $single['level_price'] = $level_price_real;
- }
- $productInfo['attr'] = [
- 'pic' => $single['pic'] ?? '',
- 'price' => $single['price'] ?? 0,
- 'cost' => $single['cost'] ?? 0,
- 'ot_price' => $single['ot_price'] ?? 0,
- 'stock' => $single['stock'] ?? 0,
- 'bar_code' => $single['bar_code'] ?? '',
- 'weight' => $single['weight'] ?? 0,
- 'volume' => $single['volume'] ?? 0,
- 'brokerage' => $single['brokerage'] ?? 0,
- 'brokerage_two' => $single['brokerage_two'] ?? 0,
- 'return_money_type' => $single['return_money_type'] ?? '',
- 'return_money' => $single['return_money'] ?? 0,
- 'return_day' => $single['return_day'] ?? 1,
- 'level_price' => $single['level_price'],
- ];
- }
- if ($productInfo['activity']) {
- $activity = explode(',', $productInfo['activity']);
- foreach ($activity as $k => $v) {
- if ($v == 1) {
- $activity[$k] = '秒杀';
- } elseif ($v == 2) {
- $activity[$k] = '砍价';
- } elseif ($v == 3) {
- $activity[$k] = '拼团';
- }
- }
- $productInfo['activity'] = $activity;
- } else {
- $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
- }
- $data['productInfo'] = $productInfo;
- }
- return JsonService::successful($data);
- }
- /**
- * 保存新建的资源
- *
- *
- */
- public function save($id)
- {
- $data = Util::postMore([
- ['cate_id', []],
- 'store_name',
- 'store_info',
- 'keyword',
- ['unit_name', '件'],
- ['image', []],
- ['slider_image', []],
- ['postage', 0],
- ['is_sub', 0],
- ['sort', 0],
- ['sales', 0],
- ['ficti', 100],
- ['give_integral', 0],
- ['is_show', 0],
- ['temp_id', 0],
- ['is_hot', 0],
- ['is_benefit', 0],
- ['is_best', 0],
- ['is_new', 0],
- ['mer_use', 0],
- ['is_postage', 0],
- ['is_good', 0],
- ['description', ''],
- ['return_money_type', ''],
- ['return_money', 0],
- ['return_day', 1],
- ['spec_type', 0],
- ['video_link', ''],
- ['items', []],
- ['attrs', []],
- ['activity', []],
- ['store_id', 0],
- ['store_type', 1],
- ['sp_user', 0],
- ]);
- foreach ($data['activity'] as $k => $v) {
- if ($v == '秒杀') {
- $data['activity'][$k] = 1;
- } elseif ($v == '砍价') {
- $data['activity'][$k] = 2;
- } else {
- $data['activity'][$k] = 3;
- }
- }
- $data['activity'] = implode(',', $data['activity']);
- $detail = $data['attrs'];
- $data['price'] = min(array_column($detail, 'price'));
- if ($data['is_sub']) {
- $data['return_money'] = $detail[0]['return_money'];
- $data['return_money_type'] = $detail[0]['return_money_type'];
- $data['return_day'] = $detail[0]['return_day'];
- }
- $data['ot_price'] = min(array_column($detail, 'ot_price'));
- $data['cost'] = min(array_column($detail, 'cost'));
- $attr = $data['items'];
- unset($data['items'], $data['video'], $data['attrs']);
- if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
- if (!in_array($data['store_type'], [1, 2])) return Json::fail('请选择正确的商品类型');
- $cate_id = $data['cate_id'];
- $data['cate_id'] = implode(',', $data['cate_id']);
- if (!$data['store_name']) return Json::fail('请输入产品名称');
- if (count($data['image']) < 1) return Json::fail('请上传产品图片');
- if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
- $data['image'] = $data['image'][0];
- $data['slider_image'] = json_encode($data['slider_image']);
- $data['stock'] = array_sum(array_column($detail, 'stock'));
- ProductModel::beginTrans();
- foreach ($detail as &$item) {
- if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
- return Json::fail('一二级返佣相加不能大于商品售价');
- }
- }
- if ($id) {
- unset($data['sales']);
- ProductModel::edit($data, $id);
- $description = $data['description'];
- unset($data['description']);
- StoreDescription::saveDescription($description, $id);
- StoreProductCate::where('product_id', $id)->delete();
- $cateData = [];
- foreach ($cate_id as $cid) {
- $cateData[] = ['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()];
- }
- StoreProductCate::insertAll($cateData);
- if ($data['spec_type'] == 0) {
- $attr = [
- [
- 'value' => '规格',
- 'detailValue' => '',
- 'attrHidden' => '',
- 'detail' => ['默认']
- ]
- ];
- $detail[0]['value1'] = '规格';
- $detail[0]['detail'] = ['规格' => '默认'];
- }
- $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id);
- if ($attr_res) {
- ProductModel::commitTrans();
- return Json::success('修改成功!');
- } else {
- ProductModel::rollbackTrans();
- return Json::fail(StoreProductAttr::getErrorInfo());
- }
- } else {
- $data['add_time'] = time();
- $data['code_path'] = '';
- $res = ProductModel::create($data);
- $description = $data['description'];
- StoreDescription::saveDescription($description, $res['id']);
- $cateData = [];
- foreach ($cate_id as $cid) {
- $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
- }
- StoreProductCate::insertAll($cateData);
- if ($data['spec_type'] == 0) {
- $attr = [
- [
- 'value' => '规格',
- 'detailValue' => '',
- 'attrHidden' => '',
- 'detail' => ['默认']
- ]
- ];
- $detail[0]['value1'] = '规格';
- $detail[0]['detail'] = ['规格' => '默认'];
- }
- $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
- if ($attr_res) {
- ProductModel::commitTrans();
- return Json::success('添加产品成功!');
- } else {
- ProductModel::rollbackTrans();
- return Json::fail(StoreProductAttr::getErrorInfo());
- }
- }
- }
- public function edit_content($id)
- {
- if (!$id) return $this->failed('数据不存在');
- $product = ProductModel::get($id);
- if (!$product) return Json::fail('数据不存在!');
- $this->assign([
- 'content' => $product->description,
- 'field' => 'description',
- 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
- ]);
- return $this->fetch('public/edit_content');
- }
- /**
- * 显示编辑资源表单页.
- *
- * @param int $id
- * @return \think\Response
- */
- public function edit($id)
- {
- if (!$id) return $this->failed('数据不存在');
- $product = ProductModel::get($id);
- if (!$product) return Json::fail('数据不存在!');
- $field = [
- Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
- $list = CategoryModel::getTierList(null, 1);
- $menus = [];
- foreach ($list as $menu) {
- $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
- }
- return $menus;
- })->filterable(1)->multiple(1),
- Form::input('store_name', '产品名称', $product->getData('store_name')),
- Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
- Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
- Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
- Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
- Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
- Form::frameImages('slider_image', '产品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')), json_decode($product->getData('slider_image'), 1) ?: [])->maxLength(5)->icon('images')->width('100%')->height('500px'),
- Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
- Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
- Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
- Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
- Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
- Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
- Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
- Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
- Form::number('sort', '排序', $product->getData('sort'))->col(8),
- Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
- Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
- Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
- Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
- Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
- Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
- Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
- ];
- $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- /**
- * 保存更新的资源
- *
- * @param $id
- */
- public function update($id)
- {
- $data = Util::postMore([
- ['cate_id', []],
- 'store_name',
- 'store_info',
- 'keyword',
- 'bar_code',
- ['unit_name', '件'],
- ['image', []],
- ['slider_image', []],
- ['postage', 0],
- ['ot_price', 0],
- ['price', 0],
- ['sort', 0],
- ['stock', 0],
- ['temp_id', 0],
- ['ficti', 100],
- ['give_integral', 0],
- ['is_show', 0],
- ['cost', 0],
- ['is_hot', 0],
- ['is_benefit', 0],
- ['is_best', 0],
- ['is_new', 0],
- ['mer_use', 0],
- ['is_postage', 0],
- ['is_good', 0],
- ]);
- if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
- $cate_id = $data['cate_id'];
- $data['cate_id'] = implode(',', $data['cate_id']);
- if (!$data['store_name']) return Json::fail('请输入产品名称');
- if (count($data['image']) < 1) return Json::fail('请上传产品图片');
- if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
- // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
- if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
- if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
- if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
- $data['image'] = $data['image'][0];
- $data['slider_image'] = json_encode($data['slider_image']);
- ProductModel::edit($data, $id);
- StoreProductCate::where('product_id', $id)->delete();
- foreach ($cate_id as $cid) {
- StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
- }
- return Json::successful('修改成功!');
- }
- public function attr($id)
- {
- if (!$id) return $this->failed('数据不存在!');
- // $result = StoreProductAttrResult::getResult($id);
- $result = StoreProductAttrValue::getStoreProductAttrResult($id);
- $image = ProductModel::where('id', $id)->value('image');
- $this->assign(compact('id', 'result', 'image'));
- return $this->fetch();
- }
- /**
- * 生成属性
- * @param int $id
- */
- public function is_format_attr($id = 0)
- {
- $data = Util::postMore([
- ['attrs', []],
- ['items', []]
- ]);
- $attr = $data['attrs'];
- $value = attr_format($attr)[1];
- $valueNew = [];
- $count = 0;
- $level_list = SystemUserLevel::where(['is_del' => 0])->order('grade asc')->select();
- $level_price = [];
- foreach ($level_list as $v) {
- $level_price['level_' . $v['id']] = 0;
- }
- foreach ($value as $key => $item) {
- $detail = $item['detail'];
- // sort($item['detail'], SORT_STRING);
- $suk = implode(',', $item['detail']);
- if ($id) {
- $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,return_money_type,return_money,return_day,level_price', 'suk');
- if (!count($sukValue)) {
- $sukValue[$suk]['pic'] = '';
- $sukValue[$suk]['price'] = 0;
- $sukValue[$suk]['cost'] = 0;
- $sukValue[$suk]['ot_price'] = 0;
- $sukValue[$suk]['stock'] = 0;
- $sukValue[$suk]['bar_code'] = '';
- $sukValue[$suk]['weight'] = 0;
- $sukValue[$suk]['volume'] = 0;
- $sukValue[$suk]['brokerage'] = 0;
- $sukValue[$suk]['brokerage_two'] = 0;
- $sukValue[$suk]['return_money_type'] = '';
- $sukValue[$suk]['return_money'] = 0;
- $sukValue[$suk]['return_day'] = 1;
- $sukValue[$suk]['level_price'] = $level_price;
- foreach ($level_list as $v) {
- $sukValue[$suk]['priceList'][] = [
- 'id' => $v['id'],
- 'name' => $v['name'],
- 'value' => $sukValue[$suk]['level_price']['level_' . $v['id']],
- 'k' => $key,
- ];
- }
- } else {
- if (!isset($sukValue[$suk]['level_price'])) {
- $sukValue[$suk]['level_price'] = $level_price;
- } else {
- $sukValue[$suk]['level_price'] = is_string($sukValue[$suk]['level_price']) ? json_decode($sukValue[$suk]['level_price'], true) : $sukValue[$suk]['level_price'];
- $level_price_real = [];
- foreach ($level_price as $check => $v) {
- $level_price_real[$check] = $sukValue[$suk]['level_price'][$check] ?? $v;
- }
- $sukValue[$suk]['level_price'] = $level_price_real;
- }
- foreach ($level_list as $v) {
- $sukValue[$suk]['priceList'][] = [
- 'id' => $v['id'],
- 'name' => $v['name'],
- 'value' => $sukValue[$suk]['level_price']['level_' . $v['id']],
- 'k' => $key,
- ];
- }
- }
- } else {
- $sukValue[$suk]['pic'] = '';
- $sukValue[$suk]['price'] = 0;
- $sukValue[$suk]['cost'] = 0;
- $sukValue[$suk]['ot_price'] = 0;
- $sukValue[$suk]['stock'] = 0;
- $sukValue[$suk]['bar_code'] = '';
- $sukValue[$suk]['weight'] = 0;
- $sukValue[$suk]['volume'] = 0;
- $sukValue[$suk]['brokerage'] = 0;
- $sukValue[$suk]['brokerage_two'] = 0;
- $sukValue[$suk]['return_money_type'] = '';
- $sukValue[$suk]['return_money'] = 0;
- $sukValue[$suk]['return_day'] = 1;
- $sukValue[$suk]['level_price'] = $level_price;
- foreach ($level_list as $v) {
- $sukValue[$suk]['priceList'][] = [
- 'id' => $v['id'], 'name' => $v['name'],
- 'value' => $sukValue[$suk]['level_price']['level_' . $v['id']],
- 'k' => $key,
- ];
- }
- }
- foreach (array_keys($detail) as $k => $title) {
- $header[$k]['title'] = $title;
- $header[$k]['align'] = 'center';
- $header[$k]['minWidth'] = 130;
- }
- foreach (array_values($detail) as $k => $v) {
- $valueNew[$count]['value' . ($k + 1)] = $v;
- $header[$k]['key'] = 'value' . ($k + 1);
- }
- $valueNew[$count]['detail'] = $detail;
- $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
- $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
- $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
- $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
- $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
- $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
- $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
- $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
- $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
- $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
- $valueNew[$count]['return_money_type'] = $sukValue[$suk]['return_money_type'] ?? '';
- $valueNew[$count]['return_money'] = $sukValue[$suk]['return_money'] ?? 0;
- $valueNew[$count]['return_day'] = $sukValue[$suk]['return_day'] ?? 1;
- $valueNew[$count]['level_price'] = $sukValue[$suk]['level_price'] ?? [];
- $valueNew[$count]['priceList'] = $sukValue[$suk]['priceList'] ?? [];
- $count++;
- }
- $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
- $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
- $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
- $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
- $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
- $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
- $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
- $header[] = ['title' => '等级价格', 'slot' => 'level_price', 'align' => 'center', 'minWidth' => 200];
- $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
- $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
- return Json::successful($info);
- }
- public function set_attr($id)
- {
- if (!$id) return $this->failed('产品不存在!');
- list($attr, $detail) = Util::postMore([
- ['items', []],
- ['attrs', []]
- ], null, true);
- $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
- if ($res)
- return $this->successful('编辑属性成功!');
- else
- return $this->failed(StoreProductAttr::getErrorInfo());
- }
- public function clear_attr($id)
- {
- if (!$id) return $this->failed('产品不存在!');
- if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
- return $this->successful('清空产品属性成功!');
- else
- return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
- }
- /**
- * 删除指定资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function delete($id)
- {
- if (!$id) return $this->failed('数据不存在');
- if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
- if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
- $data['is_del'] = 0;
- if (!ProductModel::edit($data, $id))
- return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
- else
- return Json::successful('成功恢复产品!');
- } else {
- $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
- $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
- $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
- if ($res1 || $res2 || $res3) {
- return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
- } else {
- $data['is_del'] = 1;
- if (!ProductModel::edit($data, $id))
- return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
- else
- return Json::successful('成功移到回收站!');
- }
- }
- }
- /**
- * 点赞
- * @param $id
- * @return mixed|\think\response\Json|void
- */
- public function collect($id)
- {
- if (!$id) return $this->failed('数据不存在');
- $product = ProductModel::get($id);
- if (!$product) return Json::fail('数据不存在!');
- $this->assign(StoreProductRelation::getCollect($id));
- return $this->fetch();
- }
- /**
- * 收藏
- * @param $id
- * @return mixed|\think\response\Json|void
- */
- public function like($id)
- {
- if (!$id) return $this->failed('数据不存在');
- $product = ProductModel::get($id);
- if (!$product) return Json::fail('数据不存在!');
- $this->assign(StoreProductRelation::getLike($id));
- return $this->fetch();
- }
- /**
- * 修改产品价格
- */
- public function edit_product_price()
- {
- $data = Util::postMore([
- ['id', 0],
- ['price', 0],
- ]);
- if (!$data['id']) return Json::fail('参数错误');
- $res = ProductModel::edit(['price' => $data['price']], $data['id']);
- if ($res) return Json::successful('修改成功');
- else return Json::fail('修改失败');
- }
- /**
- * 修改产品库存
- *
- */
- public function edit_product_stock()
- {
- $data = Util::postMore([
- ['id', 0],
- ['stock', 0],
- ]);
- if (!$data['id']) return Json::fail('参数错误');
- $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
- if ($res) return Json::successful('修改成功');
- else return Json::fail('修改失败');
- }
- /**
- * 检测商品是否开活动
- * @param $id
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function check_activity($id)
- {
- if ($id != 0) {
- $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
- $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
- $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
- if ($res1 || $res2 || $res3) {
- return Json::successful('该商品有活动开启,无法删除属性');
- } else {
- return Json::fail();
- }
- } else {
- return Json::fail();
- }
- }
- }
|