123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- namespace app\system\controller\v1;
- use app\BaseController;
- use app\model\system\ProductRule;
- use app\model\system\ProductAttr;
- use app\model\system\ProductAttrValue;
- use app\model\system\ProductAttrResult;
- use app\model\system\Product as ProductModel;
- use app\model\system\Category;
- use library\services\UtilService;
- use think\facade\Db;
- class Product extends BaseController
- {
- /**
- * 显示资源列表头部
- *
- * @return \think\Response
- */
- public function type_header()
- {
- //出售中商品
- $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->count();
- //仓库中商品
- $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->count();
- //已经售馨产品
- $outofstock = (new ProductModel)->getWhere(['type' => 4])->count();
- //警戒库存
- $policeforce = (new ProductModel)->getWhere(['type' => 5])->count();
- //回收站
- $recycle = ProductModel::where('is_del', 1)->count();
- $list = [
- ['type' => 1, 'name' => '出售中产品', 'count' => $onsale],
- ['type' => 2, 'name' => '仓库中产品', 'count' => $forsale],
- ['type' => 4, 'name' => '已经售馨产品', 'count' => $outofstock],
- ['type' => 5, 'name' => '警戒库存', 'count' => $policeforce],
- ['type' => 6, 'name' => '产品回收站', 'count' => $recycle],
- ];
- return app('json')->success(compact('list'));
- }
- /**
- * 显示资源列表
- * @return mixed
- */
- public function index()
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 20],
- ['store_name', ''],
- ['cate_id', ''],
- ['factory_id', ''],
- ['label', ''],
- ['excel', 0],
- ['type', 1]
- ]);
- // if($this->merId){
- // $where['mer_id'] = $this->merId;
- // }
- return app('json')->success(ProductModel::ProductList($where));
- }
- /**
- * 获取规则属性模板
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function get_rule()
- {
- return app('json')->success(ProductRule::field(['rule_name', 'rule_value'])->select()->each(function ($item) {
- $item['rule_value'] = json_decode($item['rule_value'], true);
- })->toArray());
- }
- /**
- * 生成属性
- * @param int $id
- */
- public function is_format_attr($id)
- {
- $data = UtilService::getMore([
- ['attrs', []],
- ['items', []]
- ]);
- $attr = $data['attrs'];
- $value = attr_format($attr)[1];
- $valueNew = [];
- $count = 0;
- foreach ($value as $key => $item) {
- $detail = $item['detail'];
- sort($item['detail'], SORT_STRING);
- $suk = implode(',', $item['detail']);
- if ($id) {
- $sukValue = ProductAttrValue::where('product_id', $id)->where('type', 0)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,capacity,image as pic,weight,volume,brokerage', '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]['capacity'] = 0;
- $sukValue[$suk]['bar_code'] = '';
- $sukValue[$suk]['weight'] = 0;
- $sukValue[$suk]['volume'] = 0;
- $sukValue[$suk]['brokerage'] = 0;
- }
- } else {
- $sukValue[$suk]['pic'] = '';
- $sukValue[$suk]['price'] = 0;
- $sukValue[$suk]['cost'] = 0;
- $sukValue[$suk]['ot_price'] = 0;
- $sukValue[$suk]['stock'] = 0;
- $sukValue[$suk]['capacity'] = 0;
- $sukValue[$suk]['bar_code'] = '';
- $sukValue[$suk]['weight'] = 0;
- $sukValue[$suk]['volume'] = 0;
- $sukValue[$suk]['brokerage'] = 0;
- }
- foreach (array_keys($detail) as $k => $title) {
- $header[$k]['title'] = $title;
- $header[$k]['align'] = 'center';
- $header[$k]['minWidth'] = 120;
- }
- 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]['capacity'] = $sukValue[$suk]['capacity'] ? intval($sukValue[$suk]['capacity']) : 0;
- $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
- $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
- $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
- $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
- $count++;
- }
- $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
- $header[] = ['title' => '条形码', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 120];
- $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 95];
- $header[] = ['title' => '出厂价(含所有辅材)', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 95];
- $header[] = ['title' => '代发价(含运费)', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 95];
- $header[] = ['title' => '现货库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 95];
- $header[] = ['title' => '单日产能', 'slot' => 'capacity', 'align' => 'center', 'minWidth' => 95];
- $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 95];
- $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 95];
- $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
- $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
- return app('json')->success(compact('info'));
- }
- /**
- * 获取商品详细信息
- * @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)
- {
- //$mer_id = $this->merId ?: '';
- $list = Category::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['cateList'] = $menus;
- $data['productInfo'] = [];
- if ($id) {
- $productInfo = ProductModel::get($id);
- if (!$productInfo) {
- return app('json')->fail('修改的商品不存在');
- }
- $productInfo['postage'] = floatval($productInfo['postage']);
- $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
- $productInfo['detail_image'] = is_string($productInfo['detail_image']) ? json_decode($productInfo['detail_image'], true) : [];
- if ($productInfo['spec_type'] == 1) {
- $result = ProductAttrResult::getResult($id);
- foreach ($result['value'] as $k => $v) {
- $num = 1;
- foreach ($v['detail'] as $dv) {
- $result['value'][$k]['value' . $num] = $dv;
- $num++;
- }
- }
- $productInfo['items'] = $result['attr'];
- $productInfo['attrs'] = $result['value'];
- $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'capacity' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0];
- } else {
- $result = ProductAttrValue::where('product_id', $id)->where('type', 0)->find();
- $productInfo['items'] = [];
- $productInfo['attrs'] = [];
- $productInfo['attr'] = [
- 'pic' => $result['image'] ?? '',
- 'price' => $result['price'] ? floatval($result['price']) : 0,
- 'cost' => $result['cost'] ? floatval($result['cost']) : 0,
- 'ot_price' => $result['ot_price'] ? floatval($result['ot_price']) : 0,
- 'stock' => $result['stock'] ? floatval($result['stock']) : 0,
- 'capacity' => $result['capacity'] ? floatval($result['capacity']) : 0,
- 'bar_code' => $result['bar_code'] ?? '',
- 'weight' => $result['weight'] ? floatval($result['weight']) : 0,
- 'volume' => $result['volume'] ? floatval($result['volume']) : 0,
- 'brokerage' => $result['brokerage'] ? floatval($result['brokerage']) : 0,
- ];
- }
- $data['productInfo'] = $productInfo;
- }
- return app('json')->success($data);
- }
- /**
- * 保存新建或编辑
- * @param $id
- * @return mixed
- * @throws \Exception
- */
- public function save($id)
- {
- $data = UtilService::getMore([
- 'store_name',
- 'cate_id',
- 'factory_id',
- 'code',
- 'keyword',
- 'store_info',
- 'material',
- 'craft',
- 'packing',
- ['unit_name', '件'],
- ['postage', 0],
- ['image', []],
- ['slider_image', []],
- ['video_link', ''],
- ['detail_image', []],
- ['spec_type', 0],
- ['ficti', 100],
- ['sales', 0],
- ['sort', 0],
- 'label',
- ['is_show', 0],
- ['packet', 0],
- ['issuing', 0],
- ['is_leftover', 0],
- ['is_hot', 0],
- ['is_new', 0],
- ['is_best', 0],
- ['is_sub', 0],
- ['description', ''],
- ['items', []],
- ['attrs', []]
- ]);
- //$data['mer_id'] = $this->merId ?: '';
- $detail = $data['attrs'];
- $data['price'] = min(array_column($detail, 'price'));
- $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 (!$data['cate_id']) return app('json')->fail('请选择商品分类');
- if (!$data['factory_id']) return app('json')->fail('请选择供货商家');
- if (!$data['store_name']) return app('json')->fail('请输入商品名称');
- if (empty($data['image'])) return app('json')->fail('请上传商品图片');
- if (count($data['slider_image']) < 1) return app('json')->fail('请上传商品轮播图');
- $data['slider_image'] = json_encode($data['slider_image']);
- $data['detail_image'] = json_encode($data['detail_image']);
- $data['stock'] = array_sum(array_column($detail, 'stock'));
- ProductModel::beginTrans();
- foreach ($detail as &$item) {
- if ($item['brokerage'] > $item['price']) {
- return app('json')->fail('佣金不能大于商品售价');
- }
- }
- if ($id) {
- unset($data['sales']);
- ProductModel::where('id',$id)->save($data);
- if ($data['spec_type'] == 0) {
- $attr = [
- [
- 'value' => '规格',
- 'detailValue' => '',
- 'attrHidden' => '',
- 'detail' => ['默认']
- ]
- ];
- $detail[0]['value1'] = '规格';
- $detail[0]['detail'] = ['规格' => '默认'];
- }
- $attr_res = ProductAttr::createProductAttr($attr, $detail, $id);
- if ($attr_res) {
- ProductModel::commitTrans();
- return app('json')->success('修改成功!');
- } else {
- ProductModel::rollbackTrans();
- return app('json')->fail(ProductAttr::getErrorInfo());
- }
- } else {
- $data['add_time'] = time();
- $data['code_path'] = '';
- $res = ProductModel::create($data);
- ProductModel::where('id',$res['id'])->update(['code' => sprintf("%05d", $res['id'])]);
- if ($data['spec_type'] == 0) {
- $attr = [
- [
- 'value' => '规格',
- 'detailValue' => '',
- 'attrHidden' => '',
- 'detail' => ['默认']
- ]
- ];
- $detail[0]['value1'] = '规格';
- $detail[0]['detail'] = ['规格' => '默认'];
- }
- $attr_res = ProductAttr::createProductAttr($attr, $detail, $res['id']);
- if ($attr_res) {
- ProductModel::commitTrans();
- return app('json')->success('添加商品成功!');
- } else {
- ProductModel::rollbackTrans();
- return app('json')->fail(ProductAttr::getErrorInfo());
- }
- }
- }
- /**
- * 删除指定资源
- *
- * @param int $id
- * @return \think\Response
- */
- public function delete($id)
- {
- if (!$id) return app('json')->fail('数据不存在');
- if (!ProductModel::be(['id' => $id])) return app('json')->fail('商品数据不存在');
- if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
- $data['is_del'] = 0;
- if (!ProductModel::where('id',$id)->save($data))
- return app('json')->fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
- else
- return app('json')->success('成功恢复商品!');
- } else {
- $data['is_del'] = 1;
- $data['is_show'] = 0;
- if (!ProductModel::where('id',$id)->save($data))
- return app('json')->fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
- else
- return app('json')->success('成功移到回收站!');
- }
- }
- /**
- * 修改状态
- * @param string $is_show
- * @param string $id
- * @return mixed
- */
- public function set_show($is_show = '', $id = '')
- {
- ($is_show == '' || $id == '') && app('json')->fail('缺少参数');
- if (ProductModel::be(['id' => $id, 'is_del' => 1])) return app('json')->fail('商品已删除,不能上架');
- $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
- if ($res) {
- return app('json')->success($is_show == 1 ? '上架成功' : '下架成功');
- } else {
- return app('json')->fail($is_show == 1 ? '上架失败' : '下架失败');
- }
- }
- /**
- * 设置批量商品上架
- * @return mixed
- */
- public function product_show()
- {
- $post = UtilService::getMore([
- ['ids', []]
- ]);
- if (empty($post['ids'])) {
- return app('json')->fail('请选择需要上架的商品');
- } else {
- $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
- if ($res !== false)
- return app('json')->success('上架成功');
- else
- return app('json')->fail('上架失败');
- }
- }
- /**
- * 产品信息
- */
- public function product_info($id)
- {
- $info = ProductModel::where('id', $id)->field('id,store_name,image,price,ot_price,cost,postage')->find()->toArray();
- $score = Db::name("product_score")->where('product_id', $id)->find();
- if($score){
- $info['attribute'] = json_decode($score['attribute'], true);
- $info['stars'] = $score['stars'];
- }
- return app('json')->success($info);
- }
- /**
- * 产品评分
- */
- public function score()
- {
- $data = UtilService::getMore([
- 'product_id',
- ['attribute', []],
- 'stars'
- ]);
- $data["attribute"] = json_encode($data["attribute"]);
- ProductModel::where('id', $data['product_id'])->update(['stars'=>$data["stars"]]);
- $score = Db::name("product_score")->where('product_id', $data['product_id'])->find();
- if($score){
- $res = Db::name("product_score")->where('product_id', $data['product_id'])->update($data);
- }else{
- $res = Db::name("product_score")->insert($data);
- }
- if ($res)
- return app('json')->success('产品评分成功');
- else
- return app('json')->fail('产品评分失败');
- }
- }
|