StoreProduct.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use app\models\store\SystemStockBill;
  5. use app\admin\model\store\{
  6. StoreDescription,
  7. StoreProductAttrValue,
  8. StoreProductAttr,
  9. StoreProductAttrResult,
  10. StoreProductCate,
  11. StoreProductRelation,
  12. StoreCategory as CategoryModel,
  13. StoreProduct as ProductModel
  14. };
  15. use app\admin\model\ump\StoreBargain;
  16. use app\admin\model\ump\StoreCombination;
  17. use app\admin\model\ump\StoreSeckill;
  18. use crmeb\services\{
  19. JsonService, UtilService as Util, JsonService as Json, FormBuilder as Form
  20. };
  21. use crmeb\traits\CurdControllerTrait;
  22. use think\facade\Route as Url;
  23. use app\admin\model\system\{
  24. SystemAttachment, ShippingTemplates
  25. };
  26. /**
  27. * 产品管理
  28. * Class StoreProduct
  29. * @package app\admin\controller\store
  30. */
  31. class StoreProduct extends AuthController
  32. {
  33. use CurdControllerTrait;
  34. protected $bindModel = ProductModel::class;
  35. /**
  36. * 显示资源列表
  37. *
  38. * @return \think\Response
  39. */
  40. public function index()
  41. {
  42. $type = $this->request->param('type', 1);
  43. //获取分类
  44. $this->assign('cate', CategoryModel::getTierList(null, 1));
  45. //出售中产品
  46. $onsale = ProductModel::where('is_del', 0)->where('is_consumer',0)->where('is_show', 1)->count();
  47. //待上架产品
  48. $forsale = ProductModel::where('is_del', 0)->where('is_consumer',0)->where('is_show', 0)->count();
  49. //仓库中产品
  50. $warehouse = ProductModel::where('is_del', 0)->where('is_consumer',0)->count();
  51. //已经售馨产品
  52. $outofstock = ProductModel::getModelObject(['type' => 4])->where('is_consumer',0)->count();
  53. //警戒库存
  54. $policeforce = ProductModel::getModelObject(['type' => 5])->where('is_consumer',0)->count();
  55. //回收站
  56. $recycle = ProductModel::where('is_del', 1)->where('is_consumer',0)->count();
  57. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  58. return $this->fetch();
  59. }
  60. public function consumer()
  61. {
  62. $type = $this->request->param('type', 1);
  63. //获取分类
  64. $this->assign('cate', CategoryModel::getTierList(null, 1,1));
  65. //出售中产品
  66. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->where('is_consumer',1)->count();
  67. //待上架产品
  68. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->where('is_consumer',1)->count();
  69. //仓库中产品
  70. $warehouse = ProductModel::where('is_del', 0)->where('is_consumer',1)->count();
  71. //已经售馨产品
  72. $outofstock = ProductModel::getModelObject(['type' => 4])->where('is_consumer',1)->count();
  73. //警戒库存
  74. $policeforce = ProductModel::getModelObject(['type' => 5])->where('is_consumer',1)->count();
  75. //回收站
  76. $recycle = ProductModel::where('is_del', 1)->where('is_consumer',1)->count();
  77. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  78. return $this->fetch();
  79. }
  80. /**
  81. * 异步查找产品
  82. *
  83. * @return json
  84. */
  85. public function product_ist()
  86. {
  87. $where = Util::getMore([
  88. ['page', 1],
  89. ['limit', 20],
  90. ['store_name', ''],
  91. ['cate_id', ''],
  92. ['excel', 0],
  93. ['order', ''],
  94. [['type', 'd'], $this->request->param('type/d')],
  95. ['is_consumer',0]
  96. ]);
  97. return Json::successlayui(ProductModel::ProductList($where));
  98. }
  99. /**
  100. * 设置单个产品上架|下架
  101. *
  102. * @return json
  103. */
  104. public function set_show($is_show = '', $id = '')
  105. {
  106. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  107. $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  108. if ($res) {
  109. return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
  110. } else {
  111. return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
  112. }
  113. }
  114. /**
  115. * 快速编辑
  116. *
  117. * @return json
  118. */
  119. public function set_product($field = '', $id = '', $value = '')
  120. {
  121. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  122. if (ProductModel::where(['id' => $id])->update([$field => $value]))
  123. return Json::successful('保存成功');
  124. else
  125. return Json::fail('保存失败');
  126. }
  127. /**
  128. * 设置批量产品上架
  129. *
  130. * @return json
  131. */
  132. public function product_show()
  133. {
  134. $post = Util::postMore([
  135. ['ids', []]
  136. ]);
  137. if (empty($post['ids'])) {
  138. return Json::fail('请选择需要上架的产品');
  139. } else {
  140. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  141. if ($res)
  142. return Json::successful('上架成功');
  143. else
  144. return Json::fail('上架失败');
  145. }
  146. }
  147. /**
  148. * 显示创建资源表单页.
  149. *
  150. * @return \think\Response
  151. */
  152. public function create($id = 0,$consumer=0)
  153. {
  154. $this->assign('id', (int)$id);
  155. $this->assign('consumer', (int)$consumer);
  156. return $this->fetch();
  157. }
  158. /**
  159. * 获取规则属性模板
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function get_rule()
  165. {
  166. return Json::successful(\app\models\store\StoreProductRule::field(['rule_name', 'rule_value'])->select()->each(function ($item) {
  167. $item['rule_value'] = json_decode($item['rule_value'], true);
  168. })->toArray());
  169. }
  170. /**
  171. * 获取产品详细信息
  172. * @param int $id
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. */
  177. public function get_product_info($id = 0,$type=0)
  178. {
  179. $list = CategoryModel::getTierList(null, 1,$type);
  180. $menus = [];
  181. foreach ($list as $menu) {
  182. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0 ? 0 : 1];//,'disabled'=>$menu['pid']== 0];
  183. }
  184. $data['tempList'] = ShippingTemplates::order('sort', 'desc')->field(['id', 'name'])->select()->toArray();
  185. $data['cateList'] = $menus;
  186. $data['productInfo'] = [];
  187. if ($id) {
  188. $productInfo = ProductModel::get($id);
  189. if (!$productInfo) {
  190. return Json::fail('修改的产品不存在');
  191. }
  192. $productInfo['cate_id'] = explode(',', $productInfo['cate_id']);
  193. $productInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
  194. $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
  195. if ($productInfo['spec_type'] == 1) {
  196. $result = StoreProductAttrResult::getResult($id, 0);
  197. foreach ($result['value'] as $k => $v) {
  198. $num = 1;
  199. foreach ($v['detail'] as $dv) {
  200. $result['value'][$k]['value' . $num] = $dv;
  201. $num++;
  202. }
  203. }
  204. $productInfo['items'] = $result['attr'];
  205. $productInfo['attrs'] = $result['value'];
  206. $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0, 'brokerage_two' => 0];
  207. } else {
  208. $result = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->find();
  209. if ($result) {
  210. $single = $result->toArray();
  211. } else {
  212. $single = [];
  213. }
  214. $productInfo['items'] = [];
  215. $productInfo['attrs'] = [];
  216. $productInfo['attr'] = [
  217. 'pic' => $single['image'] ?? '',
  218. 'price' => $single['price'] ?? 0,
  219. 'cost' => $single['cost'] ?? 0,
  220. 'ot_price' => $single['ot_price'] ?? 0,
  221. 'stock' => $single['stock'] ?? 0,
  222. 'bar_code' => $single['bar_code'] ?? '',
  223. 'weight' => $single['weight'] ?? 0,
  224. 'volume' => $single['volume'] ?? 0,
  225. 'brokerage' => $single['brokerage'] ?? 0,
  226. 'brokerage_two' => $single['brokerage_two'] ?? 0,
  227. ];
  228. }
  229. if ($productInfo['activity']) {
  230. $activity = explode(',', $productInfo['activity']);
  231. foreach ($activity as $k => $v) {
  232. if ($v == 1) {
  233. $activity[$k] = '秒杀';
  234. } elseif ($v == 2) {
  235. $activity[$k] = '砍价';
  236. } elseif ($v == 3) {
  237. $activity[$k] = '拼团';
  238. }
  239. }
  240. $productInfo['activity'] = $activity;
  241. } else {
  242. $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
  243. }
  244. $data['productInfo'] = $productInfo;
  245. }
  246. return JsonService::successful($data);
  247. }
  248. /**
  249. * 保存新建的资源
  250. *
  251. *
  252. */
  253. public function save($id)
  254. {
  255. $data = Util::postMore([
  256. ['cate_id', []],
  257. 'store_name',
  258. 'store_info',
  259. 'keyword',
  260. ['unit_name', '件'],
  261. ['image', []],
  262. ['slider_image', []],
  263. ['postage', 0],
  264. ['is_sub', 0],
  265. ['sort', 0],
  266. ['sales', 0],
  267. ['ficti', 100],
  268. ['give_integral', 0],
  269. ['is_show', 0],
  270. ['temp_id', 0],
  271. ['is_hot', 0],
  272. ['is_benefit', 0],
  273. ['is_best', 0],
  274. ['is_new', 0],
  275. ['mer_use', 0],
  276. ['is_postage', 0],
  277. ['is_good', 0],
  278. ['description', ''],
  279. ['spec_type', 0],
  280. ['video_link', ''],
  281. ['items', []],
  282. ['attrs', []],
  283. ['activity', []],
  284. ['is_consumer',0],
  285. ['bar_code',''],
  286. ]);
  287. foreach ($data['activity'] as $k => $v) {
  288. if ($v == '秒杀') {
  289. $data['activity'][$k] = 1;
  290. } elseif ($v == '砍价') {
  291. $data['activity'][$k] = 2;
  292. } else {
  293. $data['activity'][$k] = 3;
  294. }
  295. }
  296. $data['activity'] = implode(',', $data['activity']);
  297. $detail = $data['attrs'];
  298. $data['price'] = min(array_column($detail, 'price'));
  299. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  300. $data['cost'] = min(array_column($detail, 'cost'));
  301. $attr = $data['items'];
  302. unset($data['items'], $data['video'], $data['attrs']);
  303. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  304. $cate_id = $data['cate_id'];
  305. $data['cate_id'] = implode(',', $data['cate_id']);
  306. if (!$data['store_name']) return Json::fail('请输入产品名称');
  307. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  308. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  309. $data['image'] = $data['image'][0];
  310. $data['slider_image'] = json_encode($data['slider_image']);
  311. $data['stock'] = array_sum(array_column($detail, 'stock'));
  312. ProductModel::beginTrans();
  313. foreach ($detail as &$item) {
  314. if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
  315. return Json::fail('一二级返佣相加不能大于商品售价');
  316. }
  317. }
  318. if ($id) {
  319. unset($data['sales']);
  320. ProductModel::edit($data, $id);
  321. $description = $data['description'];
  322. unset($data['description']);
  323. StoreDescription::saveDescription($description, $id);
  324. StoreProductCate::where('product_id', $id)->delete();
  325. $cateData = [];
  326. foreach ($cate_id as $cid) {
  327. $cateData[] = ['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()];
  328. }
  329. StoreProductCate::insertAll($cateData);
  330. if ($data['spec_type'] == 0) {
  331. $attr = [
  332. [
  333. 'value' => '规格',
  334. 'detailValue' => '',
  335. 'attrHidden' => '',
  336. 'detail' => ['默认']
  337. ]
  338. ];
  339. $detail[0]['value1'] = '规格';
  340. $detail[0]['detail'] = ['规格' => '默认'];
  341. }
  342. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  343. if ($attr_res) {
  344. ProductModel::commitTrans();
  345. return Json::success('修改成功!');
  346. } else {
  347. ProductModel::rollbackTrans();
  348. return Json::fail(StoreProductAttr::getErrorInfo());
  349. }
  350. } else {
  351. $data['add_time'] = time();
  352. $data['code_path'] = '';
  353. $res = ProductModel::create($data);
  354. $description = $data['description'];
  355. StoreDescription::saveDescription($description, $res['id']);
  356. $cateData = [];
  357. foreach ($cate_id as $cid) {
  358. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  359. }
  360. StoreProductCate::insertAll($cateData);
  361. if ($data['spec_type'] == 0) {
  362. $attr = [
  363. [
  364. 'value' => '规格',
  365. 'detailValue' => '',
  366. 'attrHidden' => '',
  367. 'detail' => ['默认']
  368. ]
  369. ];
  370. $detail[0]['value1'] = '规格';
  371. $detail[0]['detail'] = ['规格' => '默认'];
  372. }
  373. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
  374. if ($attr_res) {
  375. ProductModel::commitTrans();
  376. return Json::success('添加产品成功!');
  377. } else {
  378. ProductModel::rollbackTrans();
  379. return Json::fail(StoreProductAttr::getErrorInfo());
  380. }
  381. }
  382. }
  383. public function attr_save()
  384. {
  385. $where = Util::getMore([
  386. ['bar_code', 20],
  387. ['in_stock',0],
  388. ]);
  389. if($where['in_stock']<1) return app('json')->fail('补货数量不能少于1');
  390. $info = StoreProductAttrValue::alias('a')->join("StoreProduct b","a.product_id=b.id","left")->where('a.bar_code',$where['bar_code'])->field('a.price,a.unique,a.stock as in_stock,a.product_id,b.store_name,b.image,a.bar_code')->find();
  391. if(!$info) return app('json')->fail('商品条形码不存在');
  392. $info = $info->toarray();
  393. $info['in_stock'] = $where['in_stock'];
  394. $info['admin_id'] = $this->adminId;
  395. if(SystemStockBill::order_create($info))
  396. {
  397. return Json::successful('入库成功');
  398. }
  399. else
  400. {
  401. return Json::fail('入库失败');
  402. }
  403. }
  404. public function bill()
  405. {
  406. return $this->fetch();
  407. }
  408. public function bill_lst()
  409. {
  410. $where = Util::getMore([
  411. ['key',''],
  412. ['page',0],
  413. ['limit',0],
  414. ['status',-2],
  415. ]);
  416. return Json::successlayui(SystemStockBill::lst($where));
  417. }
  418. public function audit($id)
  419. {
  420. if (!$id) return $this->failed('数据不存在');
  421. $Bill = SystemStockBill::where('id',$id)->find();
  422. if (!$Bill) return Json::fail('数据不存在!');
  423. $f = array();
  424. $f[] = Form::radio('status', '状态', $Bill->getData('status'))->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  425. $f[] = Form::textarea('refuse_msg','备注');
  426. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('audit_save', array('id' => $id)), 2);
  427. $this->assign(compact('form'));
  428. return $this->fetch('public/form-builder');
  429. }
  430. public function batch_audit($id)
  431. {
  432. if (!$id) return $this->failed('数据不存在');
  433. $f = array();
  434. $f[] = Form::radio('status', '状态')->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  435. $f[] = Form::textarea('refuse_msg','备注');
  436. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('batch_audit_save', array('id' => $id)), 2);
  437. $this->assign(compact('form'));
  438. return $this->fetch('public/form-builder');
  439. }
  440. public function audit_save($id)
  441. {
  442. $where = Util::postMore([
  443. ['refuse_msg',''],
  444. ['status',1],
  445. ]);
  446. if(!SystemStockBill::be(['id'=>$id,'status'=>0])) return $this->failed('数据不存在或已经审核');
  447. $where['audit_time'] = time();
  448. $where['audit_admin'] = $this->adminId;
  449. SystemStockBill::edit($where,$id);
  450. if($where['status']==1) {
  451. $info = SystemStockBill::find($id)->toArray();
  452. $rs = StoreProductAttrValue::in_Stock($info);
  453. }
  454. return Json::successful('审核成功');
  455. }
  456. /**
  457. * 批量审核
  458. */
  459. public function batch_audit_save()
  460. {
  461. $where = Util::postMore([
  462. ['refuse_msg',''],
  463. ['status',1],
  464. ]);
  465. $ids = explode(',',input('id'));
  466. if($where['status']==1)
  467. {
  468. foreach ($ids as $v)
  469. {
  470. $info = SystemStockBill::where('id',$v)->where('status',0)->find();
  471. if($info)
  472. {
  473. $rs = StoreProductAttrValue::in_Stock($info);
  474. }
  475. }
  476. }
  477. $where['audit_time'] = time();
  478. $where['audit_admin'] = $this->adminId;
  479. SystemStockBill::where('id','in',join(",",$ids))->update($where);
  480. return Json::successful('批量审核成功');
  481. }
  482. public function edit_content($id)
  483. {
  484. if (!$id) return $this->failed('数据不存在');
  485. $product = ProductModel::get($id);
  486. if (!$product) return Json::fail('数据不存在!');
  487. $this->assign([
  488. 'content' => $product->description,
  489. 'field' => 'description',
  490. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  491. ]);
  492. return $this->fetch('public/edit_content');
  493. }
  494. /**
  495. * 显示编辑资源表单页.
  496. *
  497. * @param int $id
  498. * @return \think\Response
  499. */
  500. public function edit($id)
  501. {
  502. if (!$id) return $this->failed('数据不存在');
  503. $product = ProductModel::get($id);
  504. if (!$product) return Json::fail('数据不存在!');
  505. $field = [
  506. Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
  507. $list = CategoryModel::getTierList(null, 1);
  508. $menus = [];
  509. foreach ($list as $menu) {
  510. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
  511. }
  512. return $menus;
  513. })->filterable(1)->multiple(1),
  514. Form::input('store_name', '产品名称', $product->getData('store_name')),
  515. Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
  516. Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
  517. Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
  518. Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
  519. Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
  520. 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'),
  521. Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
  522. Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
  523. Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
  524. Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
  525. Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
  526. Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
  527. Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
  528. Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
  529. Form::number('sort', '排序', $product->getData('sort'))->col(8),
  530. Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
  531. Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  532. Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  533. Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  534. Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  535. Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  536. Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  537. ];
  538. $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
  539. $this->assign(compact('form'));
  540. return $this->fetch('public/form-builder');
  541. }
  542. /**
  543. * 保存更新的资源
  544. *
  545. * @param $id
  546. */
  547. public function update($id)
  548. {
  549. $data = Util::postMore([
  550. ['cate_id', []],
  551. 'store_name',
  552. 'store_info',
  553. 'keyword',
  554. 'bar_code',
  555. ['unit_name', '件'],
  556. ['image', []],
  557. ['slider_image', []],
  558. ['postage', 0],
  559. ['ot_price', 0],
  560. ['price', 0],
  561. ['sort', 0],
  562. ['stock', 0],
  563. ['temp_id', 0],
  564. ['ficti', 100],
  565. ['give_integral', 0],
  566. ['is_show', 0],
  567. ['cost', 0],
  568. ['is_hot', 0],
  569. ['is_benefit', 0],
  570. ['is_best', 0],
  571. ['is_new', 0],
  572. ['mer_use', 0],
  573. ['is_postage', 0],
  574. ['is_good', 0],
  575. ]);
  576. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  577. $cate_id = $data['cate_id'];
  578. $data['cate_id'] = implode(',', $data['cate_id']);
  579. if (!$data['store_name']) return Json::fail('请输入产品名称');
  580. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  581. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  582. // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
  583. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
  584. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
  585. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
  586. $data['image'] = $data['image'][0];
  587. $data['slider_image'] = json_encode($data['slider_image']);
  588. ProductModel::edit($data, $id);
  589. StoreProductCate::where('product_id', $id)->delete();
  590. foreach ($cate_id as $cid) {
  591. StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
  592. }
  593. return Json::successful('修改成功!');
  594. }
  595. public function attr($id)
  596. {
  597. if (!$id) return $this->failed('数据不存在!');
  598. // $result = StoreProductAttrResult::getResult($id);
  599. $result = StoreProductAttrValue::getStoreProductAttrResult($id);
  600. $image = ProductModel::where('id', $id)->value('image');
  601. $this->assign(compact('id', 'result', 'image'));
  602. return $this->fetch();
  603. }
  604. /**
  605. * 生成属性
  606. * @param int $id
  607. */
  608. public function is_format_attr($id = 0, $type = 0)
  609. {
  610. $data = Util::postMore([
  611. ['attrs', []],
  612. ['items', []]
  613. ]);
  614. $attr = $data['attrs'];
  615. $value = attr_format($attr)[1];
  616. $valueNew = [];
  617. $count = 0;
  618. foreach ($value as $key => $item) {
  619. $detail = $item['detail'];
  620. sort($item['detail'], SORT_STRING);
  621. $suk = implode(',', $item['detail']);
  622. $types = 1;
  623. if ($id) {
  624. $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', 'suk');
  625. if (!count($sukValue)) {
  626. if ($type == 0) $types = 0; //编辑商品时,将没有规格的数据不生成默认值
  627. $sukValue[$suk]['pic'] = '';
  628. $sukValue[$suk]['price'] = 0;
  629. $sukValue[$suk]['cost'] = 0;
  630. $sukValue[$suk]['ot_price'] = 0;
  631. $sukValue[$suk]['stock'] = 0;
  632. $sukValue[$suk]['bar_code'] = '';
  633. $sukValue[$suk]['weight'] = 0;
  634. $sukValue[$suk]['volume'] = 0;
  635. $sukValue[$suk]['brokerage'] = 0;
  636. $sukValue[$suk]['brokerage_two'] = 0;
  637. }
  638. } else {
  639. $sukValue[$suk]['pic'] = '';
  640. $sukValue[$suk]['price'] = 0;
  641. $sukValue[$suk]['cost'] = 0;
  642. $sukValue[$suk]['ot_price'] = 0;
  643. $sukValue[$suk]['stock'] = 0;
  644. $sukValue[$suk]['bar_code'] = '';
  645. $sukValue[$suk]['weight'] = 0;
  646. $sukValue[$suk]['volume'] = 0;
  647. $sukValue[$suk]['brokerage'] = 0;
  648. $sukValue[$suk]['brokerage_two'] = 0;
  649. }
  650. if ($types) { //编辑商品时,将没有规格的数据不生成默认值
  651. foreach (array_keys($detail) as $k => $title) {
  652. $header[$k]['title'] = $title;
  653. $header[$k]['align'] = 'center';
  654. $header[$k]['minWidth'] = 130;
  655. }
  656. foreach (array_values($detail) as $k => $v) {
  657. $valueNew[$count]['value' . ($k + 1)] = $v;
  658. $header[$k]['key'] = 'value' . ($k + 1);
  659. }
  660. $valueNew[$count]['detail'] = $detail;
  661. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  662. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  663. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  664. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  665. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  666. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  667. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  668. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  669. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  670. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  671. $count++;
  672. }
  673. }
  674. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  675. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  676. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  677. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  678. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  679. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  680. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  681. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  682. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  683. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  684. return Json::successful($info);
  685. }
  686. public function set_attr($id)
  687. {
  688. if (!$id) return $this->failed('产品不存在!');
  689. list($attr, $detail) = Util::postMore([
  690. ['items', []],
  691. ['attrs', []]
  692. ], null, true);
  693. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  694. if ($res)
  695. return $this->successful('编辑属性成功!');
  696. else
  697. return $this->failed(StoreProductAttr::getErrorInfo());
  698. }
  699. public function clear_attr($id)
  700. {
  701. if (!$id) return $this->failed('产品不存在!');
  702. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  703. return $this->successful('清空产品属性成功!');
  704. else
  705. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  706. }
  707. /**
  708. * 删除指定资源
  709. *
  710. * @param int $id
  711. * @return \think\Response
  712. */
  713. public function delete($id)
  714. {
  715. if (!$id) return $this->failed('数据不存在');
  716. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  717. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  718. $data['is_del'] = 0;
  719. if (!ProductModel::edit($data, $id))
  720. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  721. else
  722. return Json::successful('成功恢复产品!');
  723. } else {
  724. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  725. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  726. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  727. if ($res1 || $res2 || $res3) {
  728. return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
  729. } else {
  730. $data['is_del'] = 1;
  731. if (!ProductModel::edit($data, $id))
  732. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  733. else
  734. return Json::successful('成功移到回收站!');
  735. }
  736. }
  737. }
  738. /**
  739. * 点赞
  740. * @param $id
  741. * @return mixed|\think\response\Json|void
  742. */
  743. public function collect($id)
  744. {
  745. if (!$id) return $this->failed('数据不存在');
  746. $product = ProductModel::get($id);
  747. if (!$product) return Json::fail('数据不存在!');
  748. $this->assign(StoreProductRelation::getCollect($id));
  749. return $this->fetch();
  750. }
  751. /**
  752. * 收藏
  753. * @param $id
  754. * @return mixed|\think\response\Json|void
  755. */
  756. public function like($id)
  757. {
  758. if (!$id) return $this->failed('数据不存在');
  759. $product = ProductModel::get($id);
  760. if (!$product) return Json::fail('数据不存在!');
  761. $this->assign(StoreProductRelation::getLike($id));
  762. return $this->fetch();
  763. }
  764. /**
  765. * 修改产品价格
  766. */
  767. public function edit_product_price()
  768. {
  769. $data = Util::postMore([
  770. ['id', 0],
  771. ['price', 0],
  772. ]);
  773. if (!$data['id']) return Json::fail('参数错误');
  774. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  775. if ($res) return Json::successful('修改成功');
  776. else return Json::fail('修改失败');
  777. }
  778. /**
  779. * 修改产品库存
  780. *
  781. */
  782. public function edit_product_stock()
  783. {
  784. $data = Util::postMore([
  785. ['id', 0],
  786. ['stock', 0],
  787. ]);
  788. if (!$data['id']) return Json::fail('参数错误');
  789. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  790. if ($res) return Json::successful('修改成功');
  791. else return Json::fail('修改失败');
  792. }
  793. /**
  794. * 检测商品是否开活动
  795. * @param $id
  796. * @throws \think\db\exception\DataNotFoundException
  797. * @throws \think\db\exception\DbException
  798. * @throws \think\db\exception\ModelNotFoundException
  799. */
  800. public function check_activity($id)
  801. {
  802. if ($id != 0) {
  803. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  804. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  805. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  806. if ($res1 || $res2 || $res3) {
  807. return Json::successful('该商品有活动开启,无法删除属性');
  808. } else {
  809. return Json::fail('删除成功');
  810. }
  811. } else {
  812. return Json::fail('没有参数ID');
  813. }
  814. }
  815. public function product_br()
  816. {
  817. $list = StoreProductAttrValue::where('bar_code','<','99999')->where('volume','>','0')->column('product_id,bar_code,price,volume');
  818. $str = "";
  819. foreach ($list as $v)
  820. {
  821. $title = ProductModel::where('id',$v['product_id'])->value('store_name');
  822. $str.=sprintf("PLU\t%d\t%s\t\t%s\t%s\t1\t1\t999\t\t\r\n",$v['volume'],$v['bar_code'],$title,bcadd($v['price'],0,2));
  823. }
  824. $str = $this->charsetToGBK($str);
  825. header("Content-type: text/plain; charset=gbk");
  826. header("Accept-Ranges: bytes");
  827. header("Content-Disposition: attachment; filename=NewFile.txt");
  828. header("Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  829. header("Pragma: no-cache" );
  830. header("Expires: 0" );
  831. exit($str);
  832. }
  833. /**
  834. * 将非GBK字符集的编码转为GBK
  835. *
  836. * @param mixed $mixed 源数据
  837. *
  838. * @return mixed GBK格式数据
  839. */
  840. function charsetToGBK($mixed)
  841. {
  842. if (is_array($mixed)) {
  843. foreach ($mixed as $k => $v) {
  844. if (is_array($v)) {
  845. $mixed[$k] = charsetToGBK($v);
  846. } else {
  847. $encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  848. if ($encode == 'UTF-8') {
  849. $mixed[$k] = iconv('UTF-8', 'GBK', $v);
  850. }
  851. }
  852. }
  853. } else {
  854. $encode = mb_detect_encoding($mixed, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  855. //var_dump($encode);
  856. if ($encode == 'UTF-8') {
  857. $mixed = iconv('UTF-8', 'GBK', $mixed);
  858. }
  859. }
  860. return $mixed;
  861. }
  862. }