StoreAdminGoodsForm.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\salesman;
  8. use app\constants\OptionSetting;
  9. use app\jobs\storeSync\DiyCommon;
  10. use app\models\Admin;
  11. use app\models\Attr;
  12. use app\models\AttrGroup;
  13. use app\models\AttrLibrary;
  14. use app\models\Cat;
  15. use app\models\CloudGoodsUpdateLog;
  16. use app\models\FoodCat;
  17. use app\models\Goods;
  18. use app\models\GoodsBook;
  19. use app\models\GoodsCat;
  20. use app\models\GoodsFullMinus;
  21. use app\models\GoodsPic;
  22. use app\models\Option;
  23. use app\models\Order;
  24. use app\models\OrderDetail;
  25. use app\models\PostageRules;
  26. use app\models\StoreSyncExtLog;
  27. use app\models\Supplier;
  28. use yii\base\Model;
  29. use yii\helpers\Json;
  30. use app\modules\admin\models\PlatformForm;
  31. class StoreAdminGoodsForm extends Model
  32. {
  33. public $status = 0;//商品列表状态 /分类状态
  34. public $sort = 0;//时间排序
  35. public $model;
  36. public $id;//商品ID
  37. public $goods_name;//商品名称
  38. public $goods_num;//商品数量
  39. public $goods_price;//商品价格
  40. public $attr;//商品规格
  41. public $goods_info;//商品信息
  42. public $cat_name; //分类名称
  43. public $pic_url; //分类缩略图
  44. public $parent_id; //分类父级
  45. public $store_id;
  46. public $cat_id;
  47. public $is_food;
  48. public $min_price;
  49. public $max_price;
  50. public $goods_keyword;
  51. public function rules()
  52. {
  53. return [
  54. [['status', 'sort', 'goods_num', 'goods_price', 'parent_id', 'store_id', 'is_food'], 'integer'],
  55. [['goods_name', 'id', 'pic_url', 'cat_name', 'goods_keyword', 'cat_id'], 'string'],
  56. [['attr', 'goods_info'], 'array'],
  57. [['min_price', 'max_price'], 'number']
  58. ];
  59. }
  60. public function initGoods()
  61. {
  62. $this->model = Goods::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]);
  63. }
  64. //商品列表
  65. public function goodsList(){
  66. try {
  67. $this->initGoods();
  68. $sort = $this->sort;
  69. $status = $this->status;
  70. $store_id = $this->store_id;
  71. $model = $this->model;
  72. $cat_id = $this->cat_id;
  73. $is_food = $this->is_food;
  74. $min_price = $this->min_price;
  75. $max_price = $this->max_price;
  76. $goods_keyword = $this->goods_keyword; //商品id
  77. $up_status = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->andWhere(['status'=>1])->count();
  78. $down_status = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->andWhere(['status'=>0])->count();
  79. $total = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->count();
  80. if ($is_food) {
  81. $model->andWhere(['<>', 'md_food_id', 0]);
  82. } else {
  83. $model->andWhere(['md_food_id' => 0]);
  84. }
  85. //商品状态
  86. if (in_array($status, [0, 1])) {
  87. $model->andWhere(['status' => $status]);
  88. }
  89. //排序方式
  90. switch ($sort) {
  91. case 1:
  92. $model->orderBy('price desc');//价格从高到低
  93. break;
  94. case 2:
  95. $model->orderBy('price asc');//价格从低到高
  96. break;
  97. case 3:
  98. $model->orderBy('goods_num desc');//库存从高到低
  99. break;
  100. case 4:
  101. $model->orderBy('goods_num asc');//库存从低到高
  102. break;
  103. default:
  104. $model->orderBy('created_at desc');//创建时间
  105. break;
  106. }
  107. //商品分类
  108. if ($cat_id) {
  109. $cat_id = explode(',', $cat_id);
  110. if ($is_food) {
  111. $model->andWhere(['cat_id' => $cat_id]);
  112. } else {
  113. $goods_ids = GoodsCat::find()->where(['cat_id' => $cat_id, 'is_delete' => 0])->select('goods_id')->column();
  114. $model->andWhere(['id' => array_merge($goods_ids, [0])]);
  115. }
  116. }
  117. if ($goods_keyword) {
  118. $model->andWhere(['OR', ['LIKE', 'name', $goods_keyword], ['id' => $goods_keyword]]);
  119. }
  120. //价格区间
  121. if ($min_price > 0 && $max_price > 0) {
  122. $model->andWhere(['AND', ['>=', 'price', $min_price], ['<=', 'price', $max_price]]);
  123. }
  124. $model->select('id, name, price, goods_num, virtual_sales, cover_pic, created_at, status, use_attr, attr, md_food_id');
  125. //分页
  126. $pagination = pagination_make($model);
  127. $list = $pagination['list'];
  128. foreach ($list as $i => &$item) {
  129. if(!empty($item['attr'])){
  130. $item['attr'] = is_array($item['attr'])? $item['attr'] : json_decode($item['attr'], true);
  131. }
  132. // $form = new PlatformForm();
  133. // $form->id = $item['cloud_goods_id'];
  134. // $glist = $form->goodsInfo();
  135. // $supplier_name = '';
  136. // if($glist['code'] == 0 && $glist['data']['count'] == 1){
  137. // $supplier_name = $glist['data']['list'][0]['supplier']['name'];
  138. // }
  139. // if (!$supplier_name) {
  140. // continue;
  141. // }
  142. $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]);
  143. $supplier_name = '';
  144. if ($supplier) {
  145. $supplier_name = $supplier->supplier_name;
  146. }
  147. $item['cloud_goods_supplier_name'] = $supplier_name;
  148. }
  149. return [
  150. 'code' => 0,
  151. 'msg' => '获取成功',
  152. 'data' => [
  153. 'sql' => $model->createCommand()->getRawSql(),
  154. 'list' => $list,
  155. 'up_status' => $up_status,
  156. 'down_status' => $down_status,
  157. 'totalCount' => $pagination['totalCount'],
  158. 'pageNo' => $pagination['pageNo'],
  159. 'total' => $total
  160. ]
  161. ];
  162. } catch (\Exception $e) {
  163. return [
  164. 'code' => 1,
  165. 'msg' => $e->getMessage()
  166. ];
  167. }
  168. }
  169. //商品规格库存
  170. public function getGoodsAttr(){
  171. try {
  172. $id = $this->id;
  173. $this->initGoods();
  174. $model = $this->model;
  175. $goods = $model->andWhere(['id' => $id, 'use_attr' => 1])->select('id, attr')->one();
  176. if (empty($goods->attr) || empty($goods)) {
  177. throw new \Exception("获取商品规格库存失败");
  178. }
  179. $attr = !empty($goods->attr) ? json_decode($goods->attr, true) : [];
  180. $getAttrGroupList = $goods->getAttrGroupList();
  181. $getAttrGroupList = json_decode(json_encode($getAttrGroupList), true);
  182. $title = null;
  183. if (!empty($getAttrGroupList)) {
  184. $title = $getAttrGroupList[0]['attr_list'];
  185. }
  186. $arr = [];
  187. if (!empty($title) && !empty($attr)) {
  188. foreach ($title as $index =>$item) {
  189. foreach ($attr as $at) {
  190. if (!empty($at)) {
  191. foreach ($at['attr_list'] as $al) {
  192. if ($item['attr_id'] === $al['attr_id']) {
  193. $arr[$index]['list'][] = $at;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. return [
  201. 'code' => 0,
  202. 'msg' => "获取成功",
  203. 'data' => [
  204. 'attr' => $attr,
  205. 'attr_group' => $arr,
  206. 'title' => $title
  207. ]
  208. ];
  209. } catch (\Exception $e) {
  210. return [
  211. 'code' => 1,
  212. 'msg' => $e->getMessage()
  213. ];
  214. }
  215. }
  216. //修改标题
  217. public function setGoodsName(){
  218. try {
  219. $this->initGoods();
  220. $model = $this->model;
  221. $goods = $model->andWhere(['id' => $this->id])->select('id, name')->one();
  222. if (empty($goods) || empty($goods->name)) {
  223. throw new \Exception("信息不存在");
  224. }
  225. $goods->name = $this->goods_name;
  226. if (!$goods->save()) {
  227. throw new \Exception("保存失败");
  228. }
  229. return [
  230. 'code' => 0,
  231. 'msg' => "修改成功"
  232. ];
  233. } catch (\Exception $e) {
  234. return [
  235. 'code' => 1,
  236. 'msg' => $e->getMessage()
  237. ];
  238. }
  239. }
  240. //删除
  241. public function goodsDel(){
  242. try {
  243. $id = $this->id;
  244. $id = explode(',',$id);
  245. $res = Goods::updateAll(['is_delete' => 1], ['id' => $id]);
  246. if ($res) {
  247. if (count($id) === 1) {
  248. $goods = Goods::findOne($id[0]);
  249. (new DiyCommon)->JobBehaviors($goods->store_id, StoreSyncExtLog::TYPE_PRODUCT, $id);
  250. }
  251. return [
  252. 'code' => 0,
  253. 'msg' => "删除成功"
  254. ];
  255. }
  256. throw new \Exception("删除失败");
  257. } catch (\Exception $e) {
  258. return [
  259. 'code' => 1,
  260. 'msg' => $e->getMessage()
  261. ];
  262. }
  263. }
  264. //上下架
  265. public function setGoodsStatus(){
  266. try {
  267. $id = $this->id;
  268. $id = explode(',',$id);
  269. if (empty($id) ) {
  270. throw new \Exception("没有传入有效数据");
  271. }
  272. $this->initGoods();
  273. $model = $this->model;
  274. if(!empty($this->status)){
  275. //判断库存
  276. $goods = $model->andWhere(['id' => $id])->select('id, status, goods_num, name, cloud_goods_id, is_wholesale')->all();
  277. if (empty($goods)) {
  278. throw new \Exception("获取产品信息失败");
  279. }
  280. foreach ($goods as $item) {
  281. if (empty($item->goods_num) && $item->goods_num <= 0) {
  282. throw new \Exception('商品' . $item->name . "库存不足");
  283. }
  284. //是云仓商品
  285. if ($item->cloud_goods_id > 0 && !intval($item->is_wholesale)) {
  286. //判断是否可以上架 供货商是否更新产品
  287. $CloudGoodsUpdateLog = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $item->cloud_goods_id]);
  288. //存在记录并且不为批发订单
  289. if ($CloudGoodsUpdateLog && !$item->is_wholesale) {
  290. if (!$CloudGoodsUpdateLog->is_audit) {
  291. return [
  292. 'code' => 1,
  293. 'msg' => '供货商商品暂未审核通过'
  294. ];
  295. }
  296. if (!$CloudGoodsUpdateLog->is_update) {
  297. return [
  298. 'code' => 1,
  299. 'msg' => '商品信息需要更新'
  300. ];
  301. }
  302. }
  303. }
  304. }
  305. }
  306. $status = empty($this->status) ? 0 : 1;
  307. //修改上下架状态
  308. $res = Goods::updateAll(['status' => $status], ['id' => $id]);
  309. if ($res) {
  310. if (count($id) === 1) {
  311. $goods = Goods::findOne($id[0]);
  312. (new DiyCommon)->JobBehaviors($goods->store_id, StoreSyncExtLog::TYPE_PRODUCT, $id);
  313. }
  314. return [
  315. 'code' => 0,
  316. 'msg' => "操作成功"
  317. ];
  318. }
  319. throw new \Exception('信息修改失败');
  320. } catch (\Exception $e){
  321. return [
  322. 'code' => 0,
  323. 'msg' => $e->getMessage()
  324. ];
  325. }
  326. }
  327. //修改价格/数量 (规格库存价格)//未测试带规格的
  328. public function setPrice(){
  329. try {
  330. $this->initGoods();
  331. $model = $this->model;
  332. $goods = $model->andWhere(['id' => $this->id])->select('id, status, goods_num ,use_attr, attr, price,
  333. cost_price')->one();
  334. if (empty($goods)) {
  335. throw new \Exception("获取产品信息失败");
  336. }
  337. //判断是否存在以及开启规格
  338. if ($goods->use_attr == 1 && !empty($this->attr)) {
  339. $attr = $this->attr;
  340. $arr = [];
  341. foreach ($attr as $item) {
  342. if ($item['num'] >=0 && $item['price'] >=0) {
  343. $arr[] = $item;
  344. } else {
  345. throw new \Exception("数据错误");
  346. }
  347. }
  348. $goods->attr = json_encode($arr);
  349. } elseif ($goods->use_attr == 0) {
  350. $this->goods_num > 0 && $goods->goods_num = $this->goods_num;
  351. $this->goods_price > 0 && $goods->price = $this->goods_price;
  352. $attr = json_decode($goods->attr, true);
  353. if (!empty($attr)) {
  354. foreach ($attr as &$item) {
  355. $item['price'] = $goods->price;
  356. $item['num'] = $goods->goods_num;
  357. }
  358. } else {
  359. list($default_attr, $default_attr_group) = $this->getDefaultAttr();
  360. $attr = [
  361. [
  362. 'attr_list' => [
  363. [
  364. 'attr_group_name' => $default_attr_group->attr_group_name,
  365. 'attr_id' => $default_attr->id,
  366. 'attr_name' => $default_attr->attr_name,
  367. ],
  368. ],
  369. 'num' => intval($goods->goods_num) ?: 0,
  370. 'price' => $goods->price,
  371. 'cost_price' => $goods->cost_price,
  372. 'no' => '',
  373. 'is_wholesale' => 0,
  374. 'wholesale_num' => 0
  375. ],
  376. ];
  377. }
  378. $goods->attr = json_encode($attr);
  379. //如果库存不足,则下架处理
  380. if ($this->goods_num == 0) {
  381. $goods->status = 0;
  382. }
  383. }
  384. if (!$goods->save()) {
  385. throw new \Exception('数据保存错误' . json_encode($goods->errors));
  386. }
  387. return [
  388. 'code' => 0,
  389. 'msg' => '修改成功'
  390. ];
  391. } catch (\Exception $e) {
  392. return [
  393. 'code' => 1,
  394. 'msg' => $e->getMessage()
  395. ];
  396. }
  397. }
  398. /**
  399. * @return array
  400. */
  401. private function getDefaultAttr()
  402. {
  403. $default_attr_name = '默认';
  404. $default_attr_group_name = '规格';
  405. $attr = Attr::findOne([
  406. 'attr_name' => $default_attr_name,
  407. 'is_delete' => 0,
  408. 'is_default' => 1,
  409. ]);
  410. $attr_group = null;
  411. if (!$attr) {
  412. $attr_group = AttrGroup::findOne([
  413. 'attr_group_name' => $default_attr_group_name,
  414. 'is_delete' => 0,
  415. ]);
  416. if (!$attr_group) {
  417. $attr_group = new AttrGroup();
  418. $attr_group->store_id = $this->store_id;
  419. $attr_group->attr_group_name = $default_attr_group_name;
  420. $attr_group->is_delete = 0;
  421. $attr_group->save(false);
  422. }
  423. $attr = new Attr();
  424. $attr->attr_group_id = $attr_group->id;
  425. $attr->attr_name = $default_attr_name;
  426. $attr->is_delete = 0;
  427. $attr->is_default = 1;
  428. $attr->save(false);
  429. } else {
  430. $attr_group = AttrGroup::findOne($attr->attr_group_id);
  431. }
  432. return [$attr, $attr_group];
  433. }
  434. //修改添加商品
  435. public function getGoodsEdit(){
  436. try {
  437. $id = $this->id;
  438. $store_id = $this->store_id;
  439. $is_food = $this->is_food;
  440. $this->initGoods();
  441. $model = $this->model;
  442. if ($id) {
  443. $goods = $model->where(['id' => $id])->select('id, cat_id, product_type, name, attr, use_attr, freight,
  444. price, goods_num, detail, delivery_type, pieces, forehead, cover_pic, original_price, goods_no,
  445. md_food_id')->one();
  446. if (empty($goods)) {
  447. return [
  448. 'code' => 1,
  449. 'msg' => '商品获取失败'
  450. ];
  451. }
  452. $getAttrGroupList = $goods->getAttrGroupList();
  453. $goods = $goods->toArray();
  454. if ((int)$goods['md_food_id'] === 0) {
  455. $cat_id = GoodsCat::find()
  456. ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
  457. ->select(['cat_id'])->asArray()->all();
  458. $cat_id = array_column($cat_id, 'cat_id');
  459. } else {
  460. $cat_id = [$goods['cat_id']];
  461. }
  462. $goods['cat_id'] = $cat_id;
  463. $goods['integral'] = json_decode($goods['integral'],true);
  464. $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
  465. ->where(['goods_id' => $id, 'is_delete' => 0])->asArray()->all();
  466. foreach ($goods as &$val) {
  467. $val = is_int($val) ? (string)$val : $val;
  468. }
  469. $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : [];
  470. //unset($goods['attr']);
  471. } else {
  472. $goods = null;
  473. }
  474. //商品规格
  475. $AttrLibrary = AttrLibrary::find()->where(['is_delete' => 0, 'store_id' => $store_id])->select('id, name, parent_id')->asArray()->all();
  476. $new_AttrLibrary = $this->getmenu($AttrLibrary);
  477. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, $store_id, 'store')['value'];
  478. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, $store_id, 'pay', $send_type);
  479. $send_type = $send_type ? Json::decode($send_type['value']) : [];
  480. $send_type_arr = [];
  481. foreach ((array)$send_type as $key => $send) {
  482. if ($send['value'] == 1) {
  483. $send_type_arr[$key] = $send['text'];
  484. }
  485. }
  486. //物流方式
  487. $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr;
  488. $goods_delivery_type = array_keys($arr);
  489. foreach ($goods_delivery_type as &$item) {
  490. switch ($item) {
  491. case "express":
  492. $value = '快递';
  493. break;
  494. case "shop":
  495. $value = '到店自提';
  496. break;
  497. case "delivery":
  498. $value = '同城配送';
  499. break;
  500. default:
  501. $value = '';
  502. break;
  503. }
  504. $item = [
  505. 'key' => $item,
  506. 'value' => $value
  507. ];
  508. }
  509. $goods['delivery_type'] = !empty($goods['delivery_type']) ? json_decode($goods['delivery_type'], true) : [];
  510. // unset($goods['goods_delivery_type']);
  511. // 获取运费规则
  512. $postage = PostageRules::find()->where([
  513. 'store_id' => $store_id,
  514. 'is_delete' => 0,
  515. 'mch_id' => 0
  516. ])->select(['id', 'name', 'is_enable'])->asArray()->all();
  517. //分类列表
  518. if ((int)$goods['md_food_id'] !== 0) {
  519. $cat = FoodCat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id, 'md_id' => -1])
  520. ->select('id, name')->asArray()->all();
  521. } else {
  522. $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id])
  523. ->select('id, name, parent_id')->asArray()->all();
  524. }
  525. $cat_arr = $this->getmenu($cat);
  526. return [
  527. 'code' => 0,
  528. 'msg' => 'success',
  529. 'data' => [
  530. 'info' => $goods,
  531. 'postage' => $postage,
  532. 'cat' => $cat_arr,
  533. 'goods_delivery_type' => $goods_delivery_type,
  534. 'new_AttrLibrary' => $new_AttrLibrary,
  535. 'AttrGroupList' => $getAttrGroupList ?? [],
  536. ]
  537. ];
  538. } catch (\Exception $e) {
  539. return [
  540. 'code' => 1,
  541. 'msg' => $e->getMessage()
  542. ];
  543. }
  544. }
  545. //获取运费规则
  546. public function getPostageData()
  547. {
  548. try {
  549. $store_id = $this->store_id;
  550. // 获取运费规则
  551. $postage = PostageRules::find()->where([
  552. 'store_id' => $store_id,
  553. 'is_delete' => 0,
  554. 'mch_id' => 0
  555. ])->select(['id', 'name', 'is_enable'])->asArray()->all();
  556. return [
  557. 'code' => 0,
  558. 'msg' => "获取成功",
  559. 'data' => [
  560. 'postage' => $postage
  561. ]
  562. ];
  563. } catch (\Exception $e) {
  564. return [
  565. 'code' => 1,
  566. 'msg' => $e->getMessage()
  567. ];
  568. }
  569. }
  570. //商品信息保存
  571. public function saveGoods(){
  572. $t = \Yii::$app->db->beginTransaction();
  573. try {
  574. $this->initGoods();
  575. $model = $this->model;
  576. $goods_info = $this->goods_info;
  577. $store_id = $this->store_id;
  578. if (empty($goods_info)) {
  579. throw new \Exception("数据错误");
  580. }
  581. $id = $goods_info['id'];
  582. if (!empty($id)) {
  583. $model = $model->andWhere(['id' => $id])->select('id, cat_id, product_type, attr, use_attr, price, goods_num, freight, detail, delivery_type, pieces, forehead, cover_pic, original_price, goods_no')->one();
  584. if (empty($model)) {
  585. throw new \Exception("获取产品信息失败");
  586. }
  587. } else {
  588. $model = new Goods();
  589. $model->store_id = $store_id;
  590. }
  591. $model->use_attr = $goods_info['use_attr'];
  592. $model->product_type = $goods_info['product_type'];
  593. if ((int)$goods_info['md_food_id'] !== 0) {
  594. $food_cat = FoodCat::findOne($goods_info['cat_id']);
  595. if ($food_cat) {
  596. $model->cat_id = implode(',', $goods_info['cat_id']);
  597. } else {
  598. throw new \Exception("点餐分类错误");
  599. }
  600. }
  601. $model->price = $goods_info['price'];
  602. $model->goods_num = $goods_info['goods_num'];
  603. $model->name = $goods_info['name'];
  604. // if ((int)$goods_info['use_attr'] === 0) {
  605. $model->original_price = $goods_info['original_price'];
  606. $model->goods_no = $goods_info['goods_no'];
  607. // }
  608. $model->detail = $goods_info['detail'];
  609. $model->delivery_type = json_encode($goods_info['delivery_type']);
  610. $model->pieces = $goods_info['pieces'];
  611. $model->forehead = $goods_info['forehead'];
  612. $model->cover_pic = $goods_info['cover_pic'];
  613. $model->freight = $goods_info['freight'] ?: 0;
  614. if ($model->save()) {
  615. //分类处理
  616. // if ($goods_info['cat_id']) {
  617. // if (!empty($id)) {
  618. // GoodsCat::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]);
  619. // }
  620. //
  621. // $goods_cat = new GoodsCat();
  622. // $goods_cat->goods_id = $model->id;
  623. // $goods_cat->store_id = $this->store_id;
  624. // $goods_cat->cat_id = (int)$goods_info['cat_id'];
  625. // $goods_cat->created_at = time();
  626. // $goods_cat->updated_at = time();
  627. // if (!$goods_cat->save()) {
  628. // throw new \Exception(json_encode($goods_cat->errors));
  629. // }
  630. // }
  631. // if ($goods_info['goods_pic_list']) {
  632. // if (!empty($id)) {
  633. // GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]);
  634. // }
  635. // foreach ($goods_info['goods_pic_list'] as $item) {
  636. // if(!isset($item['pic_url']) || empty($item['pic_url'])){
  637. // continue;
  638. // }
  639. // $goods_pic = new GoodsPic();
  640. // $goods_pic->pic_url = $item['pic_url'];
  641. // $goods_pic->goods_id = $model->id;
  642. // if (!$goods_pic->save()) {
  643. // throw new \Exception(json_encode($goods_pic->errors));
  644. // }
  645. // }
  646. //
  647. // }
  648. // //判断如果规格发生改变就删除满减规则
  649. // if (!empty($goods_info['is_change_attr'])) {
  650. // $res = GoodsFullMinus::updateAll(['is_delete' => 1], ['goods_id' => $model->id]);
  651. // \Yii::error($res, "规格发生改变就删除满减规则");
  652. // }
  653. $form = new StoreAdminAttrForm();
  654. $form->store_id = $store_id;
  655. $res = $form->goodsAttrBook($goods_info);
  656. if ($res['code'] != 0) {
  657. throw new \Exception($res['msg']);
  658. }
  659. $t->commit();
  660. return [
  661. 'code' => 0,
  662. 'msg' => "保存成功"
  663. ];
  664. }
  665. throw new \Exception(json_encode($model->errors));
  666. } catch (\Exception $e) {
  667. $t->rollBack();
  668. return [
  669. 'code' => 1,
  670. 'msg' => $e->getMessage().$e->getLine()
  671. ];
  672. }
  673. }
  674. //展示商品详情
  675. public function goodsDetail(){
  676. try {
  677. $id = $this->id;
  678. $this->initGoods();
  679. $model = $this->model;
  680. $store_id = $this->store_id;
  681. if (empty($id)) {
  682. throw new \Exception("获取参数失败");
  683. }
  684. $goods = $model->andWhere(['id' => $id])->select('id, detail, name, price, goods_num, virtual_sales, cat_id,
  685. attr, use_attr, md_food_id')->with('goodsPicList')->asArray()->one();
  686. if (empty($goods)) {
  687. throw new \Exception("获取产品信息失败");
  688. }
  689. if ((int)$goods['md_food_id'] === 0) {
  690. //分类列表
  691. $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id])
  692. ->select('id, name, parent_id')->asArray()->all();
  693. } else {
  694. $cat = FoodCat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id, 'md_id' => -1])
  695. ->select('id, name')->asArray()->all();
  696. }
  697. $cat_arr = $this->getmenu($cat);
  698. //实际销量
  699. $actual_num = Order::find()->where(['and',
  700. ['o.is_delete' => 0, 'o.store_id' => $store_id, 'od.goods_id' => $id]])
  701. ->andWhere(['or', ['<>', 'o.trade_status', '-1'],['<>', 'o.trade_status', '1']])
  702. ->alias('o')->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id = o.id')
  703. ->count();
  704. $goods['virtual_sales'] += $actual_num;
  705. //规格
  706. $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : '';
  707. return [
  708. 'code' => 0,
  709. 'msg' => "获取成功",
  710. 'data' => [
  711. 'goods_info' => $goods,
  712. 'cat' => $cat_arr
  713. ]
  714. ];
  715. } catch (\Exception $e) {
  716. return [
  717. 'code' => 1,
  718. 'msg' => $e->getMessage()
  719. ];
  720. }
  721. }
  722. //分类列表
  723. public function catList(){
  724. $store_id = $this->store_id;
  725. $status = $this->status;
  726. if ($this->is_food) {
  727. $up_status = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])->count();
  728. $down_status = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 0])->count();
  729. $total = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0])->count();
  730. $data = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => $status])
  731. ->select('id, name, pic_url, is_show')->orderBy('sort desc')->asArray()->all();;
  732. } else {
  733. $up_status = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])->count();
  734. $down_status = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 0])->count();
  735. $total = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0])->count();
  736. $data = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => $status])
  737. ->select("id, parent_id, name, pic_url, is_show")->orderBy('sort desc')->asArray()->all();
  738. }
  739. $list = $this->getmenu($data, 0, 1);
  740. return [
  741. 'code' => 0,
  742. 'msg' => "获取成功",
  743. 'data' => [
  744. 'list' => $list,
  745. 'up_status' => $up_status,
  746. 'down_status' => $down_status,
  747. 'total' => $total
  748. ]
  749. ];
  750. }
  751. //分类保存
  752. public function catAdd(){
  753. try {
  754. $id = $this->id;
  755. $cat_name = $this->cat_name;
  756. $pic_url = $this->pic_url;
  757. $parent_id = $this->parent_id;
  758. $status = $this->status;
  759. $sort = $this->sort;
  760. $store_id = $this->store_id;
  761. if ($this->is_food) {
  762. $model = FoodCat::findOne($id)? : new FoodCat();
  763. } else {
  764. $model = Cat::findOne($id)? : new Cat();
  765. $model->parent_id = $parent_id;
  766. }
  767. $model->name = $cat_name;
  768. $model->store_id = $store_id;
  769. $model->pic_url = $pic_url;
  770. $model->sort = $sort;
  771. $model->is_show = $status;
  772. $model->created_at = time();
  773. $model->updated_at = time();
  774. if (!$model->save()) {
  775. throw new \Exception(json_encode($model->errors));
  776. }
  777. return [
  778. 'code' => 0,
  779. 'msg' => "保存成功"
  780. ];
  781. } catch (\Exception $e){
  782. return [
  783. 'code' => 1,
  784. 'msg' => $e->getMessage()
  785. ];
  786. }
  787. }
  788. public function catInfo() {
  789. try {
  790. $id = $this->id;
  791. $store_id = $this->store_id;
  792. $model = Cat::findOne(['id' => $id, 'store_id' => $store_id]);
  793. if (!$model) {
  794. $data = null;
  795. } else {
  796. $data = [
  797. 'name' => $model->name,
  798. 'parent_id' => $model->parent_id,
  799. 'pic_url' => $model->pic_url,
  800. 'sort' => $model->sort,
  801. 'is_show' => $model->is_show,
  802. 'parent_name' => ''
  803. ];
  804. if ($model->parent_id) {
  805. $data['parent_name'] = Cat::findOne(['id' => $model->parent_id, 'store_id' => $store_id])->name ?? '';
  806. }
  807. }
  808. return [
  809. 'code' => 0,
  810. 'msg' => 'success',
  811. 'data' => $data
  812. ];
  813. } catch (\Exception $e) {
  814. return [
  815. 'code' => 1,
  816. 'msg' => $e->getMessage()
  817. ];
  818. }
  819. }
  820. //分类状态更改
  821. public function setCatStatus(){
  822. try {
  823. $id = $this->id;
  824. $status = $this->status;
  825. if (empty($id)) {
  826. throw new \Exception("找不到对应的数据信息");
  827. }
  828. if ($this->is_food) {
  829. $model = FoodCat::findOne($id);
  830. } else {
  831. $model = Cat::findOne($id);
  832. }
  833. if (empty($model)) {
  834. throw new \Exception("找不到对应的数据信息");
  835. }
  836. switch ($status) {
  837. case 0:
  838. if ($this->is_food) {
  839. $res = FoodCat::updateAll(['is_show' => 0], ['id' => $id]);
  840. } else {
  841. $res = Cat::updateAll(['is_show' => 0], ['OR', ['id' => $id], ['parent_id' => $id]]);
  842. }
  843. break;
  844. case 1:
  845. if ($this->is_food) {
  846. $res = FoodCat::updateAll(['is_show' => 0], ['id' => $id]);
  847. } else {
  848. $res = Cat::updateAll(['is_show' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]);
  849. }
  850. break;
  851. case 2:
  852. if ($this->is_food) {
  853. $res = FoodCat::updateAll(['is_show' => 0], ['id' => $id]);
  854. } else {
  855. $res = Cat::updateAll(['is_delete' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]);
  856. }
  857. break;
  858. }
  859. if (!$res) {
  860. throw new \Exception("失败");
  861. }
  862. return [
  863. 'code' => 0,
  864. 'msg' => "成功"
  865. ];
  866. } catch (\Exception $e){
  867. return [
  868. 'code' => 1,
  869. 'msg' => $e->getMessage()
  870. ];
  871. }
  872. }
  873. //递归
  874. public function getmenu($menu, $id = 0, $show_parent = 0, $name = ""){
  875. $arr = [];
  876. foreach ($menu as $k => $v) {
  877. if(($v['parent_id'] ?? 0) == $id){
  878. if ($show_parent == 1) {
  879. $v['parent_name'] = $name;
  880. $v['children'] = $this -> getmenu($menu, $v['id'], $show_parent, $v['name']);
  881. }else {
  882. $v['children'] = $this -> getmenu($menu, $v['id']);
  883. }
  884. $arr[] = $v;
  885. }
  886. }
  887. return $arr;
  888. }
  889. }