GoodsController.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. <?php
  2. namespace backend\modules\mall\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\GroupBuy\common\enums\GroupBuyEnum;
  6. use addons\GroupBuy\common\models\GroupBuyActive;
  7. use addons\PointUpperLimit\common\service\SettingService;
  8. use addons\ProductGift\common\models\GiftGoods;
  9. use addons\ScoreExpansion\common\models\ScoreExpansion;
  10. use addons\Store\common\models\StoreGoods;
  11. use backend\controllers\MallController;
  12. use backend\modules\mall\services\DescriptionTemplateService;
  13. use common\components\BaseEvent;
  14. use common\components\CsvExport;
  15. use common\enums\AddonsEnum;
  16. use common\enums\ApiStatusEnum;
  17. use common\enums\DownloadEnum;
  18. use common\enums\GoodsTypeEnum;
  19. use common\enums\import\GoodsImportEnum;
  20. use common\enums\RabbitMqEnum;
  21. use common\enums\RedisKeyEnum;
  22. use common\enums\StatusEnum;
  23. use common\enums\WhetherEnum;
  24. use common\events\goods\GoodsCreateEvent;
  25. use common\events\goods\GoodsEditEvent;
  26. use common\globals\GlobalInvoke;
  27. use common\helpers\ArrayHelper;
  28. use common\helpers\csv\CsvExportHelper;
  29. use common\helpers\StringHelper;
  30. use common\helpers\TaskHelper;
  31. use common\models\common\MallSetting;
  32. use common\models\common\Region;
  33. use common\models\goods\FreightRules;
  34. use common\models\goods\Goods;
  35. use common\models\goods\GoodsAssociation;
  36. use common\models\goods\GoodsAttr;
  37. use common\models\goods\GoodsCateRelate;
  38. use common\models\goods\GoodsDescriptionTemplate;
  39. use common\models\goods\GoodsLabel;
  40. use common\models\goods\GoodsMarketing;
  41. use common\models\goods\GoodsService;
  42. use common\models\mall\MallAddons;
  43. use common\models\mall\MallHost;
  44. use common\models\user\UserLevel;
  45. use common\models\common\Addons;
  46. use common\logic\common\AddonsLimit;
  47. use common\models\common\ImportFailLog;
  48. use common\models\common\ImportLog;
  49. use common\models\goods\GoodsExtra;
  50. use common\models\goods\GoodsSearchAddress;
  51. use common\models\goods\template\GoodsDecorationTemplate;
  52. use common\models\goods\template\GoodsTemplateRelation;
  53. use Exception;
  54. use PhpAmqpLib\Message\AMQPMessage;
  55. use Yii;
  56. use yii\helpers\Json;
  57. use function GuzzleHttp\Psr7\str;
  58. /**
  59. *
  60. * 商品控制器
  61. * @Author bing
  62. * @DateTime 2021-08-16 13:12:44
  63. * @copyright: Copyright (c) 2020 广东七件事集团
  64. */
  65. class GoodsController extends MallController
  66. {
  67. /**
  68. * 商品首页
  69. * @Author bing
  70. * @DateTime 2021-08-20 14:49:55
  71. * @copyright: Copyright (c) 2020 广东七件事集团
  72. * @return void
  73. */
  74. public function actionIndex()
  75. {
  76. if (\Yii::$app->request->isAjax) {
  77. if (\Yii::$app->request->isGet) {
  78. //商品列表筛选
  79. $get = Yii::$app->request->get();
  80. $rules = [
  81. [['goods_id', 'is_chanjet_addons', 'limit'], 'integer'],
  82. [['goods_name', 'sort_field'], 'string'],
  83. [['type'], 'in', 'range' => ['all', 'onsale', 'offline', 'sell-out', 'stock-lack']],
  84. [['sort_type'], 'in', 'range' => ['asc', 'desc']],
  85. [['cats'], 'each', 'rule' => ['integer']],
  86. [['start', 'end', 'update_start', 'update_end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
  87. [['goods_type'], 'safe'],
  88. ];
  89. $res = Yii::$app->services->validate->validate($get, $rules);
  90. if ($res === false)
  91. return $this->error(Yii::$app->services->validate->getErrorMessage());
  92. $where = [['mall_id' => $this->mall['id'], 'mch_id' => $this->admin->mch_id, 'g.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  93. //过滤插件商品
  94. $where[]=[
  95. 'or',
  96. ['!=','g.goods_source','Aiyunuo'],
  97. ['is', 'g.goods_source', new \yii\db\Expression('null')]
  98. ];
  99. // $where[] = ['!=', 'g.goods_name', '代客下单插件商品'];
  100. // 筛选
  101. !empty($get['start']) && $where[] = ['>=', 'g.created_at', strtotime($get['start'])];
  102. !empty($get['end']) && $where[] = ['<=', 'g.created_at', strtotime($get['end'])];
  103. !empty($get['update_start']) && $where[] = ['>=', 'g.updated_at', strtotime($get['update_start'])];
  104. !empty($get['update_end']) && $where[] = ['<=', 'g.updated_at', strtotime($get['update_end'])];
  105. !empty($get['goods_name']) && $where[] = ['like', 'g.goods_name', '%' . trim($get['goods_name']) . '%', false];
  106. if(!empty($get['cats'])){
  107. $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['cate_id'=>$get['cats']]]]);
  108. $goods_ids = array_column($goods_cate_relate_list,'goods_id');
  109. $where[] = ['in', 'g.id', $goods_ids];
  110. }
  111. !empty($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']];
  112. if (!empty($get['goods_type'])) {
  113. if (strpos($get['goods_type'], ',') !== false) {
  114. $get['goods_type'] = explode(',', $get['goods_type']);
  115. }
  116. $where[] = ['g.goods_type' => $get['goods_type']];
  117. }
  118. if (!empty($get['type'])) {
  119. switch ($get['type']) {
  120. case 'onsale':
  121. $where[] = ['=', 'g.is_on_sale', WhetherEnum::ENABLED];
  122. break;
  123. case 'offline':
  124. $where[] = ['=', 'g.is_on_sale', WhetherEnum::DISABLED];
  125. break;
  126. case 'sell-out':
  127. $where[] = ['=', 'g.stock', 0];
  128. break;
  129. case 'stock-lack':
  130. $where[] = new \yii\db\Expression('`stock` <= `stock_warning`');
  131. break;
  132. }
  133. }
  134. // with
  135. $with = ['cats', 'attr'];
  136. // 排序
  137. $sort = '';
  138. if (isset($get['sort_field']) && isset($get['sort_type']))
  139. $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type'];
  140. $sort = !empty($sort) ? $sort . ',g.sort ASC,g.id DESC' : 'g.sort ASC,g.id DESC';
  141. // 畅捷通插件相关
  142. if (!empty($get['is_chanjet_addons'])) {
  143. $with[] = 'addonsChanjetRequest';
  144. }
  145. $limit = $get['limit'] ?? 10;
  146. $select = "g.id, g.goods_name, g.price, g.cost_price, g.unit, g.cover_pic, g.is_on_sale, g.is_attr, g.attr_groups, g.attr_groups, g.status, g.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_status, g.check_remark, g.sort, g.created_at, g.stock_warning, g.sales_num";
  147. $params = array('alias' => 'g', 'where' => $where, 'with' => $with, 'limit' => $limit, 'order' => $sort, 'select' => $select);
  148. $page_data = Goods::listPage($params, false, true);
  149. if ($page_data === false)
  150. return $this->error(Goods::getStaticError());
  151. $page_data["export_list"] = Goods::fieldsList();
  152. $goods_marketing_arr = [];
  153. if (!empty($page_data['list'])) {
  154. $goods_id_arr = array_column($page_data['list'], 'id');
  155. $goods_marketing_list = GoodsMarketing::lists(['where' => [['mall_id' => $this->mall_id], ['goods_id' => $goods_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id,marketing_type']);
  156. foreach ($goods_marketing_list as $val) {
  157. $goods_marketing_arr[$val['goods_id']][] = $val['marketing_type'];
  158. }
  159. $mall = $this->mall;
  160. foreach ($page_data['list'] as &$item) {
  161. $addons_name = '';
  162. if (isset($goods_marketing_arr[$item['id']])) {
  163. foreach ($goods_marketing_arr[$item['id']] as $value) {
  164. if (is_string(AddonsEnum::getAddonsNameMap($value)))
  165. $addons_name .= AddonsEnum::getAddonsNameMap($value) . ',';
  166. }
  167. $addons_name = trim($addons_name, ',');
  168. }
  169. $item['addons_name'] = $addons_name;
  170. $item['h5_url'] = MallHost::getHost($this->mall_id,'h5_url'). '/#/plugins/pages/goods/detail?sign=' .$mall['mall_sign'].'&proId='.$item['id'];
  171. }
  172. }
  173. // 判断是否有安装支付宝商家券插件
  174. $page_data['alipay_mini_app_id'] = null;
  175. $page_data['mall_sign'] = $this->mall['mall_sign'];
  176. if (MallAddons::isInstall($this->mall_id,AddonsEnum::ADDONS_NAME_ALIPAY_MCH_COUPON)) {
  177. $page_data['alipay_mini_app_id'] = \Yii::$app->services->setting->mall->get($this->mall_id, 'alipay_mini.app_id');
  178. $page_data['alipay_mini_app_id'] = !empty($page_data['alipay_mini_app_id']) ? $page_data['alipay_mini_app_id'] : null;
  179. }
  180. // 则需要展示sku_id
  181. $page_data['show_sku_dialog'] = StatusEnum::DISABLED;
  182. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_WANG_DIAN_TONG)) {
  183. $page_data['show_sku_dialog'] = StatusEnum::ENABLED;
  184. }
  185. // 是否显示同步商品到门店的按钮
  186. $page_data['synchronize_goods_dialog'] = StatusEnum::DISABLED;
  187. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE)) {
  188. $page_data['synchronize_goods_dialog'] = StatusEnum::ENABLED;
  189. }
  190. // 是否需要设置支付成功后跳转
  191. $page_data['pay_success_jump'] = StatusEnum::DISABLED;
  192. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP)) {
  193. $pay_jump_config = \Yii::$app->services->setting->addons->get($this->mall_id,
  194. AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP, 'basic');
  195. if (!empty($pay_jump_config['is_enable'])) {
  196. $page_data['pay_success_jump'] = intval($pay_jump_config['is_enable_jump']);
  197. }
  198. }
  199. return $this->success('操作成功', $page_data);
  200. } else {
  201. // 商品操作
  202. $form = Yii::$app->request->post();
  203. return $this->doHandle($form);
  204. }
  205. }
  206. $params = Yii::$app->request->get();
  207. $is_compliance = Yii::$app->services->mall->isOpenComplianceMode($this->mall_id);
  208. return $this->render($this->action->id, ['pay_success_jump_component' => MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP), 'search_params' => $params, 'is_compliance' => $is_compliance]);
  209. }
  210. public function actionDel()
  211. {
  212. if (\Yii::$app->request->isAjax) {
  213. // 商品操作
  214. $form = Yii::$app->request->post();
  215. return $this->doHandle($form);
  216. }
  217. }
  218. //批量上传
  219. public function actionDelBatch()
  220. {
  221. if (\Yii::$app->request->isAjax) {
  222. // 商品操作
  223. $form = Yii::$app->request->post();
  224. foreach ($form['id'] as $id){
  225. $res = $this->doHandle(['id'=>$id,'type'=>'destroy']);
  226. }
  227. return $this->success();
  228. }
  229. }
  230. //批量上下架
  231. public function actionDoOnSaleBatch()
  232. {
  233. if (\Yii::$app->request->isAjax) {
  234. // 商品操作
  235. $form = Yii::$app->request->post();
  236. foreach ($form['id'] as $id){
  237. $this->doHandle(['id'=>$id,'type'=>'is_on_sale','is_on_sale'=>$form['is_on_sale']]);
  238. }
  239. return $this->success();
  240. }
  241. }
  242. public function actionDoOnSale()
  243. {
  244. if (\Yii::$app->request->isAjax) {
  245. // 商品操作
  246. $form = Yii::$app->request->post();
  247. return $this->doHandle($form);
  248. }
  249. }
  250. public function actionDoSort()
  251. {
  252. if (\Yii::$app->request->isAjax) {
  253. // 商品操作
  254. $form = Yii::$app->request->post();
  255. return $this->doHandle($form);
  256. }
  257. }
  258. public function actionDoVirtualSales()
  259. {
  260. if (\Yii::$app->request->isAjax) {
  261. // 商品操作
  262. $form = Yii::$app->request->post();
  263. return $this->doHandle($form);
  264. }
  265. }
  266. public function actionDoGoodsName()
  267. {
  268. if (\Yii::$app->request->isAjax) {
  269. // 商品操作
  270. $form = Yii::$app->request->post();
  271. return $this->doHandle($form);
  272. }
  273. }
  274. public function actionDoGoodsStock()
  275. {
  276. if (\Yii::$app->request->isAjax) {
  277. // 商品操作
  278. $form = Yii::$app->request->post();
  279. return $this->doHandle($form);
  280. }
  281. }
  282. public function actionDoGoodsAttrStock()
  283. {
  284. if (\Yii::$app->request->isAjax) {
  285. // 商品操作
  286. $form = Yii::$app->request->post();
  287. if ($form['attr_stock']) {
  288. $attr_stock_array = array_column($form['attr_stock'], 'stock');
  289. $form['stock'] = array_sum($attr_stock_array);
  290. foreach ($form['attr_stock'] as $item) {
  291. if ($item['stock'] < 0) {
  292. return $this->error('商品库存需大于等于0');
  293. }
  294. }
  295. }
  296. return $this->doHandle($form);
  297. }
  298. }
  299. protected function doHandle($form)
  300. {
  301. // 商品操作
  302. $rules = [[['id'], 'required'], [['id'], 'integer']];
  303. $type = $form['type'] ?? '';
  304. switch ($form['type']) {
  305. case 'sort': //排序
  306. $rules[] = [['sort'], 'required'];
  307. break;
  308. case 'destroy': //删除
  309. $form['status'] = -1;
  310. $rules[] = [['status'], 'required'];
  311. break;
  312. case 'goods_name': //修改商品名称
  313. $rules[] = [['goods_name'], 'required'];
  314. break;
  315. case 'is_on_sale': //上下架
  316. $rules[] = [['is_on_sale'], 'required'];
  317. $rules[] = [['is_on_sale'], 'in', 'range' => [WhetherEnum::ENABLED, WhetherEnum::DISABLED]];
  318. break;
  319. case 'virtual_sales': //虚拟销量
  320. $rules[] = [['virtual_sales'], 'required'];
  321. break;
  322. case 'goods_stock': //库存
  323. $rules[] = [['stock'], 'required'];
  324. $rules[] = [['stock'], 'integer', 'message' => '商品库存必须是整数。'];
  325. $rules[] = [
  326. ['stock'],
  327. 'compare',
  328. 'compareValue' => 0,
  329. 'operator' => '>=',
  330. 'type' => 'number',
  331. 'message' => '商品库存需大于等于0'
  332. ];
  333. break;
  334. case 'attr_stock': //多规格库存
  335. $rules[] = [['attr_stock', 'stock'], 'required'];
  336. break;
  337. }
  338. $res = Yii::$app->services->validate->validate($form, $rules);
  339. if ($res === false)
  340. return $this->error(Yii::$app->services->validate->getErrorMessage());
  341. $form['mall_id'] = $this->mall['id'];
  342. $form['mch_id'] = $this->admin->mch_id;
  343. $goods_id = $form['id'];
  344. unset($form['id']);
  345. $res = Goods::goodsHandle($goods_id, $form);
  346. if ($res === false)
  347. return $this->error(Goods::getStaticError());
  348. //修改商品的规格
  349. if ($form['status']) {
  350. $attr_goods = GoodsAttr::find()->where(['goods_id' => $goods_id])->all();
  351. foreach ($attr_goods as $model) {
  352. $model->status = $form['status'];
  353. $model->updated_at = time();
  354. $model->update(false);
  355. }
  356. }
  357. //修改商品的规格库存
  358. if ($form['attr_stock']) {
  359. foreach ($form['attr_stock'] as $attr_stock) {
  360. $attr_goods = GoodsAttr::find()->where(['id' => $attr_stock['id']])->one();
  361. $attr_goods->stock = $attr_stock['stock'];
  362. $attr_goods->updated_at = time();
  363. $attr_goods->update(false);
  364. }
  365. }
  366. if ($type == 'goods_stock') {
  367. $goods = Goods::find()->where(['id' => $goods_id, 'mall_id' => $this->mall['id']])->asArray()->one();
  368. $res = GoodsAttr::setAttr($goods_id, $goods);
  369. if ($res === false) throw new \Exception('设置货品失败:' . GoodsAttr::getStaticError());
  370. }
  371. //
  372. //编辑商品触发
  373. $event = new GoodsEditEvent($this->mall['id']);
  374. $data['goods_id'] = $goods_id;
  375. $data['mall_id'] = $this->mall['id'];
  376. $data['wait_del_attr_id_arr'] = '';
  377. $data['type'] = $type;
  378. if (isset($form['is_on_sale']))
  379. $data['is_on_sale'] = $form['is_on_sale'];
  380. Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  381. return $this->success();
  382. }
  383. /**
  384. *商品导出
  385. * @Author ltm
  386. * @DateTime 2021-12-02 11:16:55
  387. * @copyright: Copyright (c) 2020 广东七件事集团
  388. * @return void
  389. */
  390. public function actionExportExcel()
  391. {
  392. if (\Yii::$app->request->isPost) {
  393. $post = \Yii::$app->request->post();
  394. $post['mall_id'] = $this->mall_id;
  395. $filename = '商品列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  396. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_GOODS, Goods::class, 'exportHandle', $post);
  397. if ($res === false)
  398. return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  399. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  400. }
  401. }
  402. /**
  403. * 商品首页
  404. * @Author ltm
  405. * @DateTime 2021-11-17 11:16:55
  406. * @copyright: Copyright (c) 2020 广东七件事集团
  407. * @return void
  408. */
  409. public function actionSearch()
  410. {
  411. if (Yii::$app->request->isAjax) {
  412. if (Yii::$app->request->isGet) {
  413. //商品列表筛选
  414. $get = Yii::$app->request->get();
  415. $rules = [
  416. [['keyword', 'goods_source'], 'string'],
  417. [['is_partake_goods', 'goods_id','screen_main_goods','req','reqId','price','sudoku_is_attr', 'exclude_goods_ids'], 'safe'],
  418. [['goods_type', 'is_achievement'],'safe']
  419. ];
  420. $res = Yii::$app->services->validate->validate($get, $rules);
  421. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  422. $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
  423. $data = Goods::getPaginationList(15, function($query) use ($get) {
  424. if ($get['is_achievement'] == 1) {
  425. $where = [
  426. 'and',
  427. [
  428. 'mall_id' => $this->mall_id,
  429. 'is_on_sale' => 1,
  430. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  431. ]
  432. ];
  433. } else {
  434. $where = [
  435. 'and',
  436. [
  437. 'mall_id' => $this->mall_id,
  438. 'mch_id' => $this->admin->mch_id,
  439. 'is_on_sale' => 1,
  440. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  441. ]
  442. ];
  443. }
  444. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  445. if(!empty($get['price'])){
  446. $where[]=['price'=>trim($get['price'])];
  447. }
  448. if (isset($get['sudoku_is_attr'])) { // 九宫格插件关联商品。只显示单规格
  449. $where[] = ['is_attr' => 0];
  450. }
  451. if (isset($get['goods_id']) && !empty($get['goods_id'])) {
  452. $where[] = ['!=', 'id', $get['goods_id']];
  453. }
  454. if (isset($get['exclude_goods_ids']) && !empty($get['exclude_goods_ids'])) {
  455. $where[] = ['not in', 'id', $get['exclude_goods_ids']];
  456. }
  457. if (isset($get['goods_type']) && !empty($get['goods_type'])) {
  458. if(is_array($get['goods_type'])){
  459. $where[] = ['in', 'goods_type', $get['goods_type']];
  460. } elseif (is_string($get['goods_type'])) {
  461. $where[] = ['goods_type' => explode(',', $get['goods_type'])];
  462. }else{
  463. $where[] = ['=', 'goods_type', $get['goods_type']];
  464. }
  465. }
  466. if (!empty($get['goods_source'])) {
  467. if ($get['goods_source'] === 'Main') {
  468. $where[] = ['=', 'mch_id', 0];
  469. $where[] = ['=', 'goods_source', ''];
  470. } elseif ($get['goods_source'] === 'Mch') {
  471. $where[] = ['>', 'mch_id', 0];
  472. } else {
  473. $where[] = ['=', 'goods_source', $get['goods_source']];
  474. }
  475. }
  476. if (isset($get['is_partake_goods'])) {
  477. $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_DOUBLE_COPY);
  478. if ($is_install) {
  479. //只返回二二复制(佣金模式)的商品
  480. $partake_goods_list = \addons\DoubleCopy\common\models\DoubleCopyCommissionItemSetting::lists([
  481. 'where' => [
  482. ['mall_id' => $this->mall_id],
  483. ['is_partake' => '1'],
  484. ['commission_type' => 'commission'],
  485. ['status' => [StatusEnum::ENABLED]]],
  486. 'index' => 'item_id']);
  487. if (!empty($partake_goods_list)) {
  488. $partake_goods_ids = array_column($partake_goods_list, 'item_id');
  489. $where[] = ['in', 'id', $partake_goods_ids];
  490. }
  491. //过滤已经设置在二二复制贡献补贴的商品
  492. $not_in_goods_list = \addons\DoubleCopyCommitmentAward\common\models\DoubleCopyCommitmentAwardGoodsSetting::lists([
  493. 'where' => [
  494. ['mall_id' => $this->mall_id],
  495. ['status' => [StatusEnum::ENABLED]]],
  496. 'select' => 'goods_id']);
  497. if (!empty($not_in_goods_list)) {
  498. $not_in_goods_ids = array_column($not_in_goods_list, 'goods_id');
  499. $where[] = ['not in', 'id', $not_in_goods_ids];
  500. }
  501. }
  502. }
  503. $query->where($where);
  504. }, $sort);
  505. return $this->success('操作成功', $data);
  506. }
  507. }
  508. return $this->render($this->action->id);
  509. }
  510. /**
  511. * 商品编辑
  512. * @Author bing
  513. * @DateTime 2021-08-20 14:49:55
  514. * @copyright: Copyright (c) 2020 广东七件事集团
  515. * @return void
  516. */
  517. public function actionEdit()
  518. {
  519. $is_reserve_type = $this->reserveGoodsType();
  520. if (\Yii::$app->request->isAjax) {
  521. if (\Yii::$app->request->isGet) {
  522. // 反显商品信息
  523. $get = Yii::$app->request->get();
  524. $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer']]);
  525. if ($res === false)
  526. return $this->error(Yii::$app->services->validate->getErrorMessage());
  527. $detail = isset($get['id']) ? Goods::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->admin->mch_id]], true, ['cats', 'attr', 'extra', 'reserve', 'reserve.skus', 'reserve.skus.times', 'reserve.skus.times.timeSpec', 'searchAddress']) : [];
  528. if (isset($get['id'])) {
  529. $detail['freight_type'] = !empty($detail['freight_type']) ? explode(',', $detail['freight_type']) : [];
  530. } else {
  531. $detail['freight_type'] = [1];
  532. }
  533. $detail['services'] = !empty($detail['services']) ? explode(',', $detail['services']) : [];
  534. $detail['labels'] = !empty($detail['labels']) ? explode(',', $detail['labels']) : [];
  535. $detail['attr_groups'] = !empty($detail['attr_groups']) ? json_decode($detail['attr_groups'], true) : [];
  536. $detail['bannar_pic'] = !empty($detail['bannar_pic']) ? json_decode($detail['bannar_pic'], true) : [];
  537. $detail['attr_groups_format'] = !empty($detail['attr_groups_format']) ? json_decode($detail['attr_groups_format'], true) : [];
  538. $detail['area_limit'] = !empty($detail['area_limit']) ? json_decode($detail['area_limit'], true) : [];
  539. $detail['cats'] = !empty($detail['cats']) ? $detail['cats'] : [];
  540. if(!empty($detail['searchAddress'])){
  541. $detail['searchAddress']['addressIdsMap'] = GoodsSearchAddress::formatAddressIds($detail['searchAddress']['province_id'],$detail['searchAddress']['city_id'], $detail['searchAddress']['district_id']);
  542. $detail['searchAddress']['town_id'] = $detail['searchAddress']['town_id'] > 0 ? $detail['searchAddress']['town_id'] : '';
  543. }else{
  544. $detail['searchAddress'] = [];
  545. }
  546. // 预约商品字段处理
  547. if (!empty($detail['reserve'])) {
  548. $reserve = &$detail['reserve'];
  549. !empty($reserve['reserve_date']) && $reserve['reserve_date'] = json_decode($reserve['reserve_date'], true);
  550. !empty($reserve['reserve_time_slot']) && $reserve['reserve_time_slot'] = json_decode($reserve['reserve_time_slot'], true);
  551. !empty($reserve['reserve_time_slot_price']) && $reserve['reserve_time_slot_price'] = json_decode($reserve['reserve_time_slot_price'], true);
  552. !empty($reserve['service_type']) && $reserve['service_type'] = json_decode($reserve['service_type'], true);
  553. !empty($reserve['reserve_week']) && $reserve['reserve_week'] = explode(',',$reserve['reserve_week']);
  554. if (!empty($reserve['skus'])) {
  555. foreach($reserve['skus'] as &$sku){
  556. $signId = $sku['sign_id'];
  557. $sku['attr_list'] = $detail['attr_groups_format'][$signId] ?? [];
  558. !empty($sku['reserve_date']) && $sku['reserve_date'] = json_decode($sku['reserve_date'], true);
  559. !empty($sku['reserve_week']) && $sku['reserve_week'] = explode(',',$sku['reserve_week']);
  560. !empty($sku['reserve_time_slot']) && $sku['reserve_time_slot'] = json_decode($sku['reserve_time_slot'], true);
  561. !empty($sku['reserve_time_slot_price']) && $sku['reserve_time_slot_price'] = json_decode($sku['reserve_time_slot_price'], true);
  562. }
  563. }
  564. $reserve['cancel_rule'] = $reserve['cancel_rule'] ? json_decode($reserve['cancel_rule'], true) : [];
  565. }
  566. // 新建商品设置默认属性
  567. if (empty($get['id'])) $detail = Goods::getDefaultSetting($this->mall_id, $detail);
  568. //新增商品重量 默认获取qimall_goods_attr表第一个
  569. // if(isset($detail['is_attr'])){
  570. $detail['attr'] = GoodsAttr::formatFormAttr($detail['attr'] ?? [], $detail['attr_groups_format']);
  571. $detail['goods_weight'] = isset($detail['attr'][0]['weight']) ? $detail['attr'][0]['weight'] : 0;
  572. // }
  573. if (isset($detail['extra'])) {
  574. $extra = &$detail['extra'];
  575. $extra['score_give_setting'] = json_decode($extra['score_give_setting'], true) ?? [];
  576. $extra['balance_give_setting'] = json_decode($extra['balance_give_setting'], true) ?? [];
  577. $extra['score_deduct_setting'] = json_decode($extra['score_deduct_setting'], true) ?? [];
  578. $extra['currency_deduct_setting'] = json_decode($extra['currency_deduct_setting'], true) ?? [];
  579. $extra['member_price_setting'] = json_decode($extra['member_price_setting'], true) ?? [];
  580. $extra['member_buy_limit_setting'] = json_decode($extra['member_buy_limit_setting'], true) ?? [];
  581. $extra['repurchase_setting'] = json_decode($extra['repurchase_setting'], true) ?? [];
  582. $extra['digital_give_setting'] = json_decode($extra['digital_give_setting'], true) ?? [];
  583. $extra['symptom_list'] = json_decode($extra['symptom_list'], true) ?? [];
  584. $extra['description_list'] = json_decode($extra['description_list'], true) ?? [];
  585. }
  586. if (!isset($extra['score_give_setting']['settlement']))
  587. $extra['score_give_setting']['settlement'] = 1;
  588. if(empty($extra['score_deduct_setting'])){
  589. $extra['score_deduct_setting'] = [
  590. 'code'=>'score',
  591. 'name'=> '积分',
  592. 'is_enable'=> 0,
  593. 'deduct_num'=> 0,
  594. 'deduct_type'=> 1,
  595. ];
  596. }
  597. if(empty($extra['balance_give_setting'])){
  598. $extra['balance_give_setting'] = [
  599. 'code'=>'balance',
  600. 'name'=>'余额',
  601. 'give_num'=>0,
  602. 'give_type'=>1,
  603. 'settlement'=>0,
  604. ];
  605. }
  606. if (!empty($extra['member_alone_buy_limit_setting'])) {
  607. $extra['member_alone_buy_limit_setting'] = json_decode($extra['member_alone_buy_limit_setting'], true);
  608. } else {
  609. $extra['member_alone_buy_limit_setting'] = [];
  610. }
  611. unset($detail['attr_groups_format']);
  612. // 获取分类
  613. $cond = [['mall_id' => $this->mall_id, 'mch_id' => $this->admin->mch_id]];
  614. // $cates = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
  615. // $cates = GoodsCate::getTreeCate($cates);
  616. // 获取商品标签
  617. $labels = GoodsLabel::getLabels($cond, [], true, 'id,title,sub_title');
  618. $services = GoodsService::getServices($cond, [], true, 'id,name');
  619. $templates = GoodsDecorationTemplate::getTemplates([['mall_id' => $this->mall_id]], [], true, 'id, name') ?? [];
  620. $goods_template_id = '';
  621. if (isset($detail['id'])) {
  622. $templates_arr = array_column($templates, 'id');
  623. $goods_template = GoodsTemplateRelation::getOne([['mall_id' => $this->mall_id], ['goods_id' => $detail['id']], ['status' => StatusEnum::ENABLED]], true, [], false, 'id, template_id');
  624. $goods_template_id = $goods_template ? (in_array($goods_template['template_id'], $templates_arr) ? $goods_template['template_id'] : $goods_template_id) : $goods_template_id;
  625. }
  626. $detail['goods_template_id'] = $goods_template_id;
  627. // 用户等级
  628. $levels = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
  629. $levels = array_merge([['level' => 0, 'name' => '普通用户']], $levels);
  630. // 运费模板
  631. $freight_rules = FreightRules::getFreightRules($cond, [], true, 'id,name');
  632. // 商品类型
  633. $goods_type = GoodsTypeEnum::getGoodsType();
  634. // 判断是否需要移除预约商品
  635. if ($is_reserve_type == StatusEnum::DELETE) {
  636. unset($goods_type[GoodsTypeEnum::GoodsTypeReserved]);
  637. }
  638. if (!empty($detail['attr'])) {
  639. if ($detail['is_attr'] == 0) {
  640. $detail['goods_no'] = $detail['attr'][0]['goods_no'];
  641. }
  642. }
  643. // 广告模板是否显示
  644. $detail['alipay_mini_app_id'] = null;
  645. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ALIPAY_MCH_COUPON)) {
  646. $detail['alipay_mini_app_id'] = \Yii::$app->services->setting->mall->get($this->mall_id, 'alipay_mini.app_id');
  647. }
  648. // // 商品营销配置+会员权益
  649. // $setting = GoodsExtra::getExtraSetting($this->mall_id, $get['id']);
  650. // $marketing_setting = $setting['marketing'];
  651. // $member_setting = GoodsExtra::getReferencePrice($this->mall_id, $setting['member'], $detail['attr']);
  652. $detail['pay_limit']=json_decode($detail['pay_limit'],true)??[];
  653. $detail['pay_limit']=is_array($detail['pay_limit'])?$detail['pay_limit']:[];
  654. $digital = MallSetting::conf($this->mall_id, 'digital.is_enable');
  655. $detail['digital_name'] = $digital ? MallSetting::conf($this->mall_id, 'digital.name') : '';
  656. $detail['is_show_attr'] = isset(Yii::$app->params['is_show_attr']) ? Yii::$app->params['is_show_attr'] : false;
  657. // 公共调用
  658. $goods_association = GoodsAssociation::getOne([['goods_id'=>$detail['id']],['status'=>StatusEnum::ENABLED]],true);
  659. $extra1_goods_list = [];
  660. $extra2_goods_list = [];
  661. $extra1_goods_id = [];
  662. $extra2_goods_id = [];
  663. if($goods_association){
  664. $extra1_goods_id = json_decode($goods_association['extra1_goods_id'],true);
  665. $extra2_goods_id = json_decode($goods_association['extra2_goods_id'],true);
  666. $extra1_goods_list = Goods::lists(['where'=>[['id'=>$extra1_goods_id]],'select'=>'id as goods_id,price,status,stock as goods_stock,goods_name,cover_pic'],true);
  667. $extra2_goods_list = Goods::lists(['where'=>[['id'=>$extra2_goods_id]],'select'=>'id as goods_id,price,status,stock as goods_stock,goods_name,cover_pic'],true);
  668. }
  669. $detail['extra1_goods_id'] = $extra1_goods_id;
  670. $detail['extra2_goods_id'] = $extra2_goods_id;
  671. if (is_numeric($detail['goods_no'])) {
  672. $detail['goods_no'] = $detail['goods_no'] . ' ';
  673. }
  674. //判断是否安装云盟店插件
  675. $detail['is_install_yunmeng'] = 0;
  676. if(MallAddons::isInstall($this->mall_id,'Yunmeng')){
  677. $yunmeng_setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_YUNMENG, 'basic');
  678. if ($yunmeng_setting['is_enable'] == 1) {
  679. $detail['is_install_yunmeng'] = 1;
  680. }
  681. }
  682. // 商品说明
  683. $descriptionTemplateService = new DescriptionTemplateService(['mall_id' => $this->mall_id]);
  684. $descriptionTemplateList = $descriptionTemplateService->selectList();
  685. if (!empty($detail['extra']['description_template_id'])) {
  686. $templateFields = $descriptionTemplateList[$detail['extra']['description_template_id']]['fields'] ?? [];
  687. $descriptionList = [];
  688. $descriptionMap = array_column($detail['extra']['description_list'], 'value', 'field');
  689. foreach ($templateFields as $field) {
  690. $descriptionList[] = ['field' => $field, 'value' => $descriptionMap[$field] ?? ''];
  691. }
  692. $detail['extra']['description_list'] = $descriptionList;
  693. }
  694. // 是否安装云处方插件
  695. $detail['is_enable_cloud_prescription'] = false;
  696. if(MallAddons::isInstall($this->mall_id,AddonsEnum::ADDONS_NAME_CLOUD_PRESCRIPTION)){
  697. $cloudPrescriptionService = new \addons\CloudPrescription\common\services\SettingService(['mall_id' => $this->mall_id]);
  698. $detail['is_enable_cloud_prescription'] = $cloudPrescriptionService->isEnable();
  699. }
  700. return $this->success('操作成功', compact('detail', 'labels', 'services', 'levels', 'goods_type', 'freight_rules', 'templates','extra1_goods_list','extra2_goods_list', 'descriptionTemplateList'));
  701. } else {
  702. // 编辑商品
  703. $form = json_decode(Yii::$app->request->post('form'), true);
  704. $form['mall_id'] = $this->mall['id'];
  705. $form['mch_id'] = $this->admin->mch_id;
  706. if(!empty($form['id'])){
  707. if(MallAddons::isInstall($this->mall_id,'CloudStock')){
  708. $res = \addons\CloudStock\common\models\CloudStockGoods::findOne(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'goods_id'=>$form['id']]);
  709. if(!empty($form['is_attr'])&&!empty($res)){
  710. return $this->error('该商品是云库存商品,不允许修改成多规格');
  711. }
  712. }
  713. }
  714. if(isset($form['goods_no'])) $form['goods_no'] = (string)$form['goods_no'];
  715. $res = Goods::setData($form);
  716. if ($res === false)
  717. return $this->error(Goods::getStaticError());
  718. //添加商品触发
  719. if (empty($form['id'])) {
  720. $event = new GoodsCreateEvent($this->mall['id']);
  721. $data['goods_id'] = $res['id'];
  722. $data['mall_id'] = $this->mall['id'];
  723. Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  724. }
  725. return $this->success('操作成功');
  726. }
  727. }
  728. // 公共调用
  729. $install_addons = GlobalInvoke::exec(GlobalInvoke::GOODS_DETAIL_EDIT, $this->mall_id, [
  730. 'type' => 'show_component',
  731. 'mall_id' => $this->mall_id,
  732. ]);
  733. $smart_form_addons = [];
  734. $valet_order_addons = [];
  735. $community_addons = [];
  736. foreach ($install_addons as $key => $val) {
  737. $component = $val['component'] ?? '';
  738. if ($component == 'com-smartform-goods') {
  739. $smart_form_addons = $val;
  740. unset($install_addons[$key]);
  741. }
  742. if ($component == 'com-community-goods') {
  743. $community_addons = $val;
  744. unset($install_addons[$key]);
  745. }
  746. if ($component == 'com-valet-order-goods') {
  747. $valet_order_addons = $val;
  748. unset($install_addons[$key]);
  749. }
  750. }
  751. return $this->render($this->action->id, ['components' => $install_addons, 'smart_form_components' => $smart_form_addons,
  752. 'community_components' => $community_addons, 'valet_order_components' => $valet_order_addons, 'is_reserve_type' => $is_reserve_type,'mall_id' => $this->mall_id]);
  753. }
  754. /**
  755. * 预约商品
  756. * @return int -1两个插件都没有安装,0仅限预约商品,1仅限预约服务,2商品/服务
  757. */
  758. protected function reserveGoodsType()
  759. {
  760. // 是否有安装家政服务
  761. $is_install_home_making = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_HOME_MAKING);
  762. // 是否有安装预约服务
  763. $is_install_reserve_service = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_RESERVATION_SERVICE);
  764. $diff = $is_install_home_making <=> $is_install_reserve_service;
  765. if ($diff < 0) { // 如果没有安装预约服务
  766. return 1;
  767. } else if ($diff == 0 && $is_install_home_making == 0) { // 如果两个都没有安装
  768. return -1;
  769. } else if ($diff == 0) { // 两个都有安装
  770. return 2;
  771. } else { // 如果仅安装了家政服务
  772. return 0;
  773. }
  774. }
  775. /**
  776. * 批量导入商品
  777. * @return mixed|string|void
  778. */
  779. public function actionImportGoods()
  780. {
  781. set_time_limit(0);
  782. if (\Yii::$app->request->isAjax) {
  783. $params = \Yii::$app->request->post();
  784. $res = Yii::$app->services->validate->validate($params, [
  785. [['current_num'], 'required'],
  786. [['file'], 'file', 'extensions' => ['csv']],
  787. [['file_path'], 'string'],
  788. [['sync'], 'safe'],
  789. ]);
  790. if ($res === false)
  791. return $this->error(Yii::$app->services->validate->getErrorMessage());
  792. $params['mall_id'] = $this->mall_id;
  793. if (!empty($params['sync'])) {
  794. $res = Goods::importGoodsSync($params);
  795. } else {
  796. $res = Goods::importGoods($params);
  797. }
  798. if ($res === false)
  799. return $this->error(Goods::getStaticError());
  800. if (empty($res['fail_counts']))
  801. return $this->success('操作成功', $res);
  802. if (!empty($res['fail_counts']))
  803. return $this->error('导入失败' . $res['fail_counts'] . '条');
  804. } else {
  805. return $this->render($this->action->id);
  806. }
  807. }
  808. /**
  809. * 导入日志记录
  810. * @return mixed|void
  811. */
  812. public function actionGetGoodsImportLog()
  813. {
  814. $params = \Yii::$app->request->get();
  815. $params['where'] = [
  816. ['mall_id' => $this->mall_id],
  817. ['source' => Goods::class]
  818. ];
  819. $params['order'] = 'id desc';
  820. $data = ImportLog::listPage($params, false, true);
  821. if (!empty($data['list'])) {
  822. foreach ($data['list'] as &$v) {
  823. $v['url'] = '';
  824. if ($v['fail_counts'])
  825. $v['url'] = \Yii::$app->urlManager->createUrl(['mall/goods/export-fail', 'import_log_id' => $v['id']]);
  826. }
  827. }
  828. return $this->success('操作成功', $data);
  829. }
  830. /**
  831. * 会员批量导入模板下载
  832. */
  833. public function actionGoodsBatchSendModel()
  834. {
  835. $csv = new CsvExport();
  836. $file_name = '商品批量导入模板-' . date('YmdHis', time());
  837. $head_list = array_column(GoodsImportEnum::HEAD_LIST, 'field_name');
  838. return $csv->export([], $head_list, $file_name);
  839. }
  840. /**
  841. * 导出失败日志
  842. */
  843. public function actionExportFail()
  844. {
  845. ImportFailLog::exportFailLog(\Yii::$app->request->get());
  846. }
  847. /**
  848. * 商品服务首页
  849. * @Author bing
  850. * @DateTime 2021-08-20 14:49:55
  851. * @copyright: Copyright (c) 2020 广东七件事集团
  852. * @return void
  853. */
  854. public function actionService()
  855. {
  856. if (Yii::$app->request->isAjax) {
  857. if (Yii::$app->request->isGet) {
  858. $keyword = Yii::$app->request->get('keyword', '');
  859. $where = [['mall_id' => $this->mall['id'], 'mch_id' => $this->admin->mch_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  860. $limit = Yii::$app->request->get('limit', $this->pageSize);
  861. $keyword && $where[] = ['like', 'name', $keyword . '%', false];
  862. $order = 'sort asc,id desc';
  863. $params = compact('where', 'order', 'limit');
  864. $page_data = GoodsService::listPage($params, false);
  865. return $this->success('操作成功', $page_data);
  866. } else {
  867. $form = Yii::$app->request->post();
  868. $rules = [[['id'], 'required'], [['id'], 'integer']];
  869. switch ($form['type']) {
  870. case 'sort': //排序
  871. $rules[] = [['sort'], 'required'];
  872. break;
  873. case 'destroy': //删除
  874. $form['status'] = -1;
  875. $rules[] = [['status'], 'required'];
  876. break;
  877. case 'default': //设置默认值
  878. $rules[] = [['is_default'], 'required'];
  879. break;
  880. }
  881. $res = Yii::$app->services->validate->validate($form, $rules);
  882. if ($res === false)
  883. return $this->error(Yii::$app->services->validate->getErrorMessage());
  884. $form['mall_id'] = $this->mall['id'];
  885. $form['mch_id'] = $this->admin->mch_id;
  886. $res = GoodsService::setData($form);
  887. if ($res === false)
  888. return $this->error(GoodsService::getStaticError());
  889. return $this->success('操作成功', $form);
  890. }
  891. }
  892. return $this->render($this->action->id);
  893. }
  894. /**
  895. * 商品服务编辑
  896. * @Author bing
  897. * @DateTime 2021-08-20 14:49:55
  898. * @copyright: Copyright (c) 2020 广东七件事集团
  899. * @return void
  900. */
  901. public function actionServiceEdit()
  902. {
  903. if (Yii::$app->request->isAjax) {
  904. if (Yii::$app->request->isGet) {
  905. $get = Yii::$app->request->get();
  906. $res = Yii::$app->services->validate->validate($get, [[['id'], 'required'], [['id'], 'integer']]);
  907. if ($res === false)
  908. return $this->error(Yii::$app->services->validate->getErrorMessage());
  909. $detail = GoodsService::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->admin->mch_id]], true);
  910. return $this->success('操作成功', compact('detail'));
  911. } else {
  912. $form = Yii::$app->request->post('form');
  913. $res = Yii::$app->services->validate->validate($form, [
  914. [['name'], 'required'],
  915. [['id'], 'integer'],
  916. [['is_default', 'sort', 'remark'], 'safe'],
  917. ]);
  918. if ($res === false)
  919. return $this->error(Yii::$app->services->validate->getErrorMessage());
  920. $form['mall_id'] = $this->mall['id'];
  921. $form['mch_id'] = $this->admin->mch_id;
  922. $res = GoodsService::setData($form);
  923. if ($res === false)
  924. return $this->error(GoodsService::getStaticError());
  925. return $this->success('操作成功');
  926. }
  927. }
  928. return $this->render($this->action->id);
  929. }
  930. /**
  931. * 商品标签首页
  932. * @Author bing
  933. * @DateTime 2021-08-20 14:49:55
  934. * @copyright: Copyright (c) 2020 广东七件事集团
  935. * @return void
  936. */
  937. public function actionLabel()
  938. {
  939. if (Yii::$app->request->isAjax) {
  940. if (Yii::$app->request->isGet) {
  941. $keyword = Yii::$app->request->get('keyword', '');
  942. $where = [['mall_id' => $this->mall['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  943. $keyword && $where[] = ['like', 'title', $keyword . '%', false];
  944. $order = 'sort asc,created_at desc';
  945. $params = compact('where', 'order');
  946. $page_data = GoodsLabel::listPage($params, false);
  947. return $this->success('操作成功', $page_data);
  948. } else {
  949. $form = Yii::$app->request->post();
  950. $rules = [[['id'], 'required'], [['id'], 'integer']];
  951. switch ($form['type']) {
  952. case 'sort':
  953. $rules[] = [['sort'], 'required'];
  954. break;
  955. case 'destroy':
  956. $form['status'] = -1;
  957. $rules[] = [['status'], 'required'];
  958. break;
  959. case 'default':
  960. $rules[] = [['is_default'], 'integer'];
  961. break;
  962. }
  963. $res = Yii::$app->services->validate->validate($form, $rules);
  964. if ($res === false)
  965. return $this->error(Yii::$app->services->validate->getErrorMessage());
  966. $form['mall_id'] = $this->mall['id'];
  967. $res = GoodsLabel::setData($form);
  968. if ($res === false)
  969. return $this->error(GoodsLabel::getStaticError());
  970. return $this->success('操作成功', $form);
  971. }
  972. }
  973. return $this->render($this->action->id);
  974. }
  975. /**
  976. * 商品服务编辑
  977. * @Author bing
  978. * @DateTime 2021-08-20 14:49:55
  979. * @copyright: Copyright (c) 2020 广东七件事集团
  980. * @return void
  981. */
  982. public function actionLabelEdit()
  983. {
  984. if (Yii::$app->request->isAjax) {
  985. if (Yii::$app->request->isGet) {
  986. $get = Yii::$app->request->get();
  987. $res = Yii::$app->services->validate->validate($get, [[['id'], 'required'], [['id'], 'integer']]);
  988. if ($res === false)
  989. return $this->error(Yii::$app->services->validate->getErrorMessage());
  990. $detail = GoodsLabel::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id]], true);
  991. return $this->success('操作成功', compact('detail'));
  992. } else {
  993. $form = Yii::$app->request->post('form');
  994. $res = Yii::$app->services->validate->validate($form, [
  995. [['title'], 'required'],
  996. [['id'], 'integer'],
  997. [['sort', 'sub_title'], 'safe'],
  998. ]);
  999. if ($res === false)
  1000. return $this->error(Yii::$app->services->validate->getErrorMessage());
  1001. $form['mall_id'] = $this->mall['id'];
  1002. $res = GoodsLabel::setData($form);
  1003. if ($res === false)
  1004. return $this->error(GoodsLabel::getStaticError());
  1005. return $this->success('操作成功');
  1006. }
  1007. }
  1008. return $this->render($this->action->id);
  1009. }
  1010. /**
  1011. * 推荐商品列表
  1012. * @return mixed|string|void
  1013. */
  1014. public function actionRecommendGoods()
  1015. {
  1016. if (\Yii::$app->request->isAjax) {
  1017. if (\Yii::$app->request->isGet) {
  1018. //商品列表筛选
  1019. $get = Yii::$app->request->get();
  1020. $where = [['mall_id' => $this->mall['id'], 'mch_id' => $this->admin->mch_id, 'g.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  1021. // 筛选
  1022. !empty($get['keyword']) && $where[] = ['or', ['like', 'g.goods_name', $get['keyword'] . '%', false], ['=', 'g.id', $get['keyword']]];
  1023. $where[] = ['=', 'g.is_on_sale', WhetherEnum::ENABLED];
  1024. //去除重复选中的商品
  1025. if (in_array($get['type'], ["goods", "order_pay", "order_comment"])) {
  1026. $data = MallSetting::getConfByGroup($this->mall_id, ["goods", "order_pay", "order_comment"], true);
  1027. if ($data[$get['type']]['goods_list']['value'])
  1028. $goods_ids = array_column(json_decode($data[$get['type']]['goods_list']['value']), 'id');
  1029. if ($goods_ids)
  1030. $where[] = ['not in', 'g.id', $goods_ids];
  1031. }
  1032. // 排序
  1033. $sort = 'g.id DESC';
  1034. if (isset($get['sort_field']) && isset($get['sort_type']))
  1035. $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type'];
  1036. $select = 'id,goods_name,cover_pic,price';
  1037. $params = array('alias' => 'g', 'where' => $where, 'select' => $select, 'limit' => 10, 'order' => $sort);
  1038. $page_data = Goods::listPage($params, false, true);
  1039. if ($page_data === false)
  1040. return $this->error(Goods::getStaticError());
  1041. return $this->success('操作成功', $page_data);
  1042. }
  1043. }
  1044. return $this->render($this->action->id);
  1045. }
  1046. /**
  1047. * 获取商品模式
  1048. * @Author: lun
  1049. * @DateTime: 2021/10/26 0026 11:47
  1050. * @Copyright: copyright (c) 2021 广东七件事集团
  1051. * @return mixed|string|void
  1052. */
  1053. public function actionGoodsModel()
  1054. {
  1055. // 获取组件配置
  1056. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  1057. $arr = array_keys($addons_setting);
  1058. //排除推三返一
  1059. foreach ($arr as $key) {
  1060. if($key=='Rebate' || $key == 'RecommendReward')
  1061. {
  1062. unset($addons_setting[$key]);
  1063. }
  1064. // 因为微信视频小店去除分销员。所以不需要显示商品独立配置
  1065. if($key == 'WechatVideoShop')
  1066. {
  1067. unset($addons_setting[$key]);
  1068. }
  1069. }
  1070. $first = $arr[0] ?? '';
  1071. $addons_list = Addons::lists(['where' => [['name' => $arr]], 'index' => 'name', 'select' => 'title,name']);
  1072. foreach ($addons_setting as $k => &$item) {
  1073. $item['is_extra'] = 0;
  1074. if (isset($addons_list[$k])) {
  1075. $item['title'] = $addons_list[$k]['title'];
  1076. }
  1077. if(in_array($k,[AddonsEnum::ADDONS_NAME_DISTRIBUTION])){
  1078. $item['is_extra'] = 1;
  1079. }
  1080. }
  1081. if (\Yii::$app->request->isAjax) {
  1082. if (\Yii::$app->request->isGet) {
  1083. // 商品列表筛选
  1084. $params = Yii::$app->request->get();
  1085. $res = Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]);
  1086. if ($res === false)
  1087. return $this->error(Yii::$app->services->validate->getErrorMessage());
  1088. // 获取商品详情
  1089. $detail = Goods::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id, 'mch_id' => $this->admin->mch_id]], true, ['attr']);
  1090. // 获取配置
  1091. $setting_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'item_id', $params['id']], ['=', 'status', StatusEnum::ENABLED]], 'index' => 'commission_type', 'mall_id' => $this->mall_id];
  1092. $setting = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL, $this->mall_id, $setting_params);
  1093. if (!empty($setting['Community'])) {
  1094. foreach ($setting['Community'] as $key => $value) {
  1095. if (!isset($value['calculate_type'])) {
  1096. $setting['Community'][$key]['calculate_type'] = 0;
  1097. }
  1098. }
  1099. }
  1100. // dd($setting);
  1101. // 获取等级
  1102. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name', 'order' => 'level asc'];
  1103. $level_params['mall_id'] = $this->mall_id;
  1104. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  1105. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  1106. // 商品支持的插件分佣币种
  1107. $addons_commission_type = GlobalInvoke::exec(GlobalInvoke::INVOKE_COMMISSION_TYPE, $this->mall_id, ['mall_id' => $this->mall_id]);
  1108. foreach ($addons_commission_type as $type) {
  1109. $commission_type_map = array_merge($commission_type_map, array_column($type, 'label', 'name'));
  1110. }
  1111. $commission_type_map_extra = $commission_type_map;
  1112. //判断有没有安装积分拓客插件
  1113. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION)){
  1114. $ScoreExpansionArr = (new ScoreExpansion())->getGoodsModeCommssionType();
  1115. foreach($ScoreExpansionArr as $type){
  1116. $commission_type_map_extra[$type['name']] = $type['label'];
  1117. }
  1118. }
  1119. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ENERGY_VALUE)){
  1120. foreach(\addons\EnergyValue\common\enums\EnergyValueEnum::getGoodsModeCommssionType() as $type){
  1121. $commission_type_map_extra[$type['name']] = $type['label'];
  1122. }
  1123. }
  1124. // 是否安装并启用额外积分插件
  1125. $is_show_additional_score = StatusEnum::DISABLED;
  1126. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_POINT_UPPER_LIMIT)) {
  1127. $is_show_additional_score = (new SettingService())->isEnable($this->mall_id);
  1128. }
  1129. return $this->success('操作成功', compact('detail', 'level', 'setting',
  1130. 'commission_type_map','commission_type_map_extra', 'is_show_additional_score'));
  1131. } else {
  1132. $params = Yii::$app->request->post('form');
  1133. $res = Yii::$app->services->validate->validate($params, [
  1134. [['goods_id'], 'required'],
  1135. [['goods_id'], 'integer'],
  1136. [array_keys($addons_setting), 'string']
  1137. ]);
  1138. if ($res === false)
  1139. return $this->error(Yii::$app->services->validate->getErrorMessage());
  1140. $trans = Yii::$app->db->beginTransaction();
  1141. try {
  1142. // 初始化保存参数
  1143. $base_params = ['mall_id' => $this->mall_id, 'item_id' => $params['goods_id']];
  1144. foreach ($addons_setting as $name => $addon) {
  1145. if (!empty($params[$name])) {
  1146. $model = new $addon['class'];
  1147. $res = $model::setData($base_params, Json::decode($params[$name], true));
  1148. if ($res == false)
  1149. throw new \Exception($model->getErrorMessage());
  1150. }
  1151. }
  1152. $trans->commit();
  1153. return $this->success("保存成功");
  1154. } catch (\Exception $e) {
  1155. $trans->rollBack();
  1156. return $this->error("保存失败,msg=" . $e->getMessage());
  1157. }
  1158. }
  1159. }
  1160. // dd($addons_setting, $addons_setting["BossBonus"]['title']);
  1161. return $this->render($this->action->id, ['addons_setting' => $addons_setting, 'first' => $first]);
  1162. }
  1163. /**
  1164. * @desc:商品优惠券独立设置查询列表
  1165. * @Author: hua
  1166. * @Date: 2021/12/3
  1167. * @Time: 15:07
  1168. * @Copyright: copyright (c) 2021 广东七件事集团
  1169. * @return mixed|void
  1170. */
  1171. public function actionCouponList()
  1172. {
  1173. $params = Yii::$app->request->post();
  1174. $where[] = ['mall_id' => $this->mall_id];
  1175. $where[] = ['status' => StatusEnum::ENABLED];
  1176. // $where[] = ['is_gift'=>2];
  1177. if (!empty($params['coupon_id']))
  1178. $where[] = ['id' => $params['coupon_id']];
  1179. if (!empty($params['keyword']))
  1180. $where[] = ['like', 'name', $params['keyword'] . '%', false];
  1181. $params['where'] = $where;
  1182. $list = AddonsCoupon::listPage($params);
  1183. $list['appoint_type_map'] = AddonsCouponEnum::getAppointTypeMap();
  1184. if (!empty($list['list'])) {
  1185. foreach ($list['list'] as &$item) {
  1186. $item['coupon_id'] = $item['id'];
  1187. }
  1188. }
  1189. return $this->success('操作成功', $list);
  1190. }
  1191. /**
  1192. * @return mixed|null
  1193. */
  1194. public function actionSku()
  1195. {
  1196. $params = Yii::$app->request->get();
  1197. $where[] = ['status' => StatusEnum::ENABLED];
  1198. $where[] = ['goods_id' => $params['id']];
  1199. $ret = GoodsAttr::lists([
  1200. 'where' => $where,
  1201. 'order' => 'id asc'
  1202. ]);
  1203. if (!empty(GoodsAttr::getStaticError())) return $this->error(GoodsAttr::getStaticError());
  1204. return $this->success('获取成功', $ret);
  1205. }
  1206. /**
  1207. * 同步商品到门店
  1208. */
  1209. public function actionSyncGoodsStore(){
  1210. try{
  1211. $params = Yii::$app->request->post();
  1212. $res = Yii::$app->services->validate->validate($params, [
  1213. [['selected_goods_ids', 'sync_type'], 'required'],
  1214. [['selected_store_ids'], 'safe']
  1215. ]);
  1216. if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  1217. $params['mall_id'] = $this->mall_id;
  1218. $handler_class = \addons\Store\common\models\StoreGoods::class;
  1219. TaskHelper::pushTaskQueue(RabbitMqEnum::EXCHANGE_TASK,RabbitMqEnum::TASK_QUEUE,$params,$handler_class,'syncMallGoods',function(AMQPMessage $message){
  1220. // 生产错误
  1221. Yii::error('发送异步任务失败');
  1222. });
  1223. return $this->success('同步成功');
  1224. }catch(\Exception $e){
  1225. return $this->error($e->getMessage());
  1226. }
  1227. }
  1228. /**
  1229. * @Description: 复制商品
  1230. * @Author: zsan
  1231. * @DateTime 2023-05-31 17:00:50
  1232. * @return \yii\web\Response
  1233. */
  1234. public function actionCopyGoods(){
  1235. try{
  1236. $params = Yii::$app->request->post();
  1237. $res = Yii::$app->services->validate->validate($params, [
  1238. [['id'], 'required'],
  1239. ]);
  1240. if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  1241. $goods = Goods::findOne(['id' => $params['id'], 'mall_id' => $this->mall_id]);
  1242. if(!$goods) throw new \Exception('商品id参数错误');
  1243. $source_goods = $goods['attributes'];
  1244. unset($source_goods['id']);
  1245. if(empty($source_goods['services'])){
  1246. $source_goods['services'] = [];
  1247. }else{
  1248. $source_goods['services'] = explode(',', $source_goods['services']);
  1249. }
  1250. if(empty($source_goods['labels'])){
  1251. $source_goods['labels'] = [];
  1252. }else{
  1253. $source_goods['labels'] = explode(',', $source_goods['labels']);
  1254. }
  1255. if(empty($source_goods['freight_type'])){
  1256. $source_goods['freight_type'] = [];
  1257. }else{
  1258. $source_goods['freight_type'] = explode(',', $source_goods['freight_type']);
  1259. }
  1260. !empty($source_goods['attr_groups']) && $source_goods['attr_groups'] = json_decode($source_goods['attr_groups'], true);
  1261. !empty($source_goods['bannar_pic']) && $source_goods['bannar_pic'] = json_decode($source_goods['bannar_pic'], true);
  1262. !empty($source_goods['area_limit']) && $source_goods['area_limit'] = json_decode($source_goods['area_limit'], true);
  1263. !empty($source_goods['pay_limit']) && $source_goods['pay_limit'] = json_decode($source_goods['pay_limit'], true);
  1264. $extra = $goods->extra ? ArrayHelper::toArray($goods->extra) : [];
  1265. if(isset($extra['goods_id'])) unset($extra['goods_id']);
  1266. isset($extra['member_buy_limit_setting']) && $extra['member_buy_limit_setting'] = json_decode($extra['member_buy_limit_setting'], true);
  1267. $source_goods['extra'] = $extra;
  1268. $cats = $goods->cats ? ArrayHelper::toArray($goods->cats) : [];
  1269. $source_goods['cats'] = array_column($cats, 'id') ?? [];
  1270. if ($goods['is_attr'] == 1) {
  1271. $temp_attrs = ArrayHelper::toArray($goods->attr);
  1272. $attr_groups = $goods['attr_groups'];
  1273. $attr_groups = !empty($attr_groups) ? json_decode($attr_groups, true) : [];
  1274. $attr = [];
  1275. foreach ($temp_attrs as $val) {
  1276. unset($val['id']);
  1277. unset($val['goods_id']);
  1278. $sku_group_name = explode(',', $val['name']);
  1279. $attr_list = [];
  1280. foreach($sku_group_name as $name){
  1281. $name_arr = explode(':', $name);
  1282. foreach($attr_groups as $group){
  1283. if($group['attr_group_name'] == $name_arr[0]){
  1284. foreach($group['attr_list'] as $v){
  1285. if($v['attr_name'] == $name_arr[1]){
  1286. $attr_list[] = [
  1287. 'attr_id' => $v['attr_id'],
  1288. 'attr_name' => $v['attr_name'],
  1289. 'pic_url' => '',
  1290. 'attr_group_id' => $group['attr_group_id'],
  1291. 'attr_group_name' => $group['attr_group_name'],
  1292. ];
  1293. break;
  1294. }
  1295. }
  1296. break;
  1297. }
  1298. }
  1299. }
  1300. $val['attr_list'] = $attr_list;
  1301. $attr[] = $val;
  1302. }
  1303. $source_goods['attr'] = $attr;
  1304. }
  1305. $source_goods['copy_id'] = $params['id'] ?? 0;
  1306. if(!Goods::setData($source_goods)) throw new \Exception(Goods::getStaticError());
  1307. return $this->success('复制成功');
  1308. }catch(\Exception $e){
  1309. return $this->error($e->getMessage());
  1310. }
  1311. }
  1312. public function actionBatchSetScore(){
  1313. try{
  1314. $params = Yii::$app->request->post();
  1315. $res = Yii::$app->services->validate->validate($params, [
  1316. [['selected_goods_ids', 'set_type'], 'required'],
  1317. [['give_type', 'deduct_type', 'settlement', 'type'], 'integer'],
  1318. [['give_num', 'deduct_num',], 'number']
  1319. ]);
  1320. if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  1321. $goods_extra = GoodsExtra::findAll(['goods_id' => $params['selected_goods_ids'],'status' => StatusEnum::ENABLED]);
  1322. if($params['set_type'] == 2){
  1323. $new = ['give_type' => $params['give_type'], 'give_num' => $params['give_num'], 'settlement' => $params['settlement']];
  1324. /** @var GoodsExtra $extra */
  1325. foreach($goods_extra as $extra){
  1326. $score_give_setting = json_decode($extra->score_give_setting, true);
  1327. $score_give_setting = array_merge($score_give_setting, $new);
  1328. $extra->score_give_setting = json_encode($score_give_setting);
  1329. $extra->save();
  1330. }
  1331. }else if($params['set_type'] == 1){
  1332. $new = ['is_enable' => 1,'deduct_type' => $params['deduct_type'],'deduct_num' => $params['deduct_num'], 'type' => $params['type']];
  1333. /** @var GoodsExtra $extra */
  1334. foreach($goods_extra as $extra){
  1335. $score_deduct_setting = $extra->score_deduct_setting ? json_decode($extra->score_deduct_setting, true) : [];
  1336. if(empty($score_deduct_setting)) $score_deduct_setting = ['code'=>'score', 'name'=>'积分'];
  1337. $score_deduct_setting = array_merge($score_deduct_setting, $new);
  1338. $extra->score_deduct_setting = json_encode($score_deduct_setting);
  1339. $extra->save();
  1340. }
  1341. }
  1342. return $this->success('设置成功');
  1343. }catch(\Exception $e){
  1344. return $this->error($e->getMessage());
  1345. }
  1346. }
  1347. /**
  1348. * 获取地区
  1349. * @return mixed|null
  1350. */
  1351. public function actionRegions()
  1352. {
  1353. $params = Yii::$app->request->get();
  1354. $res = Yii::$app->services->validate->validate($params, [
  1355. [['id'], 'integer'],
  1356. [['id'], 'required'],
  1357. ]);
  1358. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1359. $regions = Region::find()
  1360. ->select('id,name,lng,lat')
  1361. ->asArray()
  1362. ->where(['parent_id' => $params['id']])
  1363. ->indexBy('id')
  1364. ->all();
  1365. return $this->success('获取成功', $regions);
  1366. }
  1367. /**
  1368. * @Description: 根据地区id获取第一层子级地区信息列表
  1369. * @Author: zhngsan
  1370. * @DateTime 2023-02-08 11:22:11
  1371. * @return mixed
  1372. */
  1373. public function actionGetRegions()
  1374. {
  1375. if (!\Yii::$app->request->isGet) {
  1376. return $this->error('请求方式错误');
  1377. }
  1378. $region_id = \Yii::$app->request->get('region_id');
  1379. if (empty($region_id)) {
  1380. return $this->error('参数错误');
  1381. }
  1382. $region = Region::findOne($region_id);
  1383. if (empty($region)) {
  1384. return $this->error('地区信息未找到');
  1385. }
  1386. $region_list = Region::find()
  1387. ->select('id,name,lng,lat')
  1388. ->where(['parent_id' => $region->id])
  1389. ->asArray()
  1390. ->all();
  1391. return $this->success('success', $region_list);
  1392. }
  1393. /**
  1394. * 商品说明列表
  1395. *
  1396. * @return void|string
  1397. */
  1398. public function actionDescriptionTemplate()
  1399. {
  1400. if (!$this->request->isAjax) {
  1401. return $this->render('goods-description-template/index');
  1402. }
  1403. $service = new DescriptionTemplateService(['mall_id' => $this->mall_id]);
  1404. $this->success('success', $service->list($this->request->get()));
  1405. }
  1406. /**
  1407. * 删除商品说明
  1408. *
  1409. * @return void
  1410. */
  1411. public function actionDelDescriptionTemplate()
  1412. {
  1413. $id = $this->request->post('id');
  1414. if (!$id) {
  1415. $this->error('ID不能为空');
  1416. return;
  1417. }
  1418. $service = new DescriptionTemplateService(['mall_id' => $this->mall_id]);
  1419. if (!$service->del($id)) {
  1420. $this->error($service->getError());
  1421. return;
  1422. }
  1423. $this->success();
  1424. }
  1425. /**
  1426. * 编辑商品说明
  1427. *
  1428. * @return void|string
  1429. */
  1430. public function actionEditDescriptionTemplate()
  1431. {
  1432. if (!$this->request->isAjax) {
  1433. return $this->render('goods-description-template/edit');
  1434. }
  1435. $service = new DescriptionTemplateService(['mall_id' => $this->mall_id]);
  1436. if ($this->request->isGet) {
  1437. $id = $this->request->get('id');
  1438. if (!$id) {
  1439. $this->success();
  1440. return;
  1441. }
  1442. $this->success('success', $service->detail($id));
  1443. return;
  1444. }
  1445. $post = $this->request->post();
  1446. $rules = [
  1447. [['name', 'frontend_name', 'fields'], 'required'],
  1448. [['id'], 'integer'],
  1449. [['fields'], 'safe'],
  1450. [['name', 'frontend_name'], 'string'],
  1451. ];
  1452. $res = Yii::$app->services->validate->validate($post, $rules);
  1453. if ($res === false) {
  1454. $this->error(Yii::$app->services->validate->getErrorMessage());
  1455. return;
  1456. }
  1457. if (!$service->edit($post)) {
  1458. $this->error($service->getError());
  1459. return;
  1460. }
  1461. $this->success();
  1462. }
  1463. public function actionSetSkusCode()
  1464. {
  1465. $request = Yii::$app->request;
  1466. if ($request->isAjax && $request->isPost) {
  1467. try {
  1468. $post = $request->post();
  1469. $res = Yii::$app->services->validate->validate($post, [
  1470. [['is_show_code'], 'integer'],
  1471. [['skus'], 'safe']
  1472. ]);
  1473. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  1474. foreach ($post['skus'] as $key => $value) {
  1475. $value['is_show_code'] = $post['is_show_code'];
  1476. GoodsAttr::updateAll(['is_show_code' => $post['is_show_code'], 'spec_no' => $value['spec_no']], ['id' => $value['id']]);
  1477. }
  1478. return $this->success('操作成功');
  1479. } catch (\Exception $e) {
  1480. $this->error($e->getMessage());
  1481. }
  1482. }
  1483. }
  1484. }