AdoptGoodsForm.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\adopt;
  8. use app\constants\OptionSetting;
  9. use app\plugins\adopt\models\AdoptGoodsDevice;
  10. use app\models\Attr;
  11. use app\models\AttrGroup;
  12. use app\models\Form;
  13. use app\models\Goods;
  14. use app\models\GoodsBook;
  15. use app\models\GoodsCat;
  16. use app\models\GoodsPic;
  17. use app\models\Level;
  18. use app\models\Mch;
  19. use app\models\Option;
  20. use app\models\PostageRules;
  21. use app\models\VerifyCard;
  22. use app\modules\mch\models\LevelListForm;
  23. use app\plugins\adopt\models\AdoptCat;
  24. use app\plugins\adopt\models\AdoptGoods;
  25. use app\utils\Delivery\Delivery;
  26. use yii\base\BaseObject;
  27. use yii\base\Model;
  28. use yii\helpers\Json;
  29. use app\jobs\SyncMdGoodsJob;
  30. use app\models\GoodsChainLevel;
  31. use app\models\ShareHolderLevel;
  32. use app\utils\Tools;
  33. class AdoptGoodsForm extends Model
  34. {
  35. public $store_id;
  36. public $status;
  37. public $keyword;
  38. public $cat_id;
  39. public $type;
  40. public $id;
  41. public $name;
  42. public $key_word;
  43. public $unit;
  44. public $price;
  45. public $cost_price;
  46. public $original_price;
  47. public $service;
  48. public $weight;
  49. public $freight;
  50. public $pieces;
  51. public $forehead;
  52. public $quick_purchase;
  53. public $content;
  54. public $goods_num;
  55. public $goods_no;
  56. public $integral;
  57. public $use_attr;
  58. public $is_negotiable;
  59. public $cover_pic;
  60. public $individual_share;
  61. public $share_type;
  62. public $share_commission_first;
  63. public $share_commission_second;
  64. public $share_commission_third;
  65. public $video_url;
  66. public $attr;
  67. public $attr_member_price_List;
  68. public $attr_setting_type;
  69. public $virtual_sales;
  70. public $confine_count;
  71. public $is_level;
  72. public $goods_pic_list;
  73. public $goods_verify_card_list;
  74. public $delivery_type;
  75. public $product_type;
  76. public $date_book;
  77. public $service_book;
  78. public $form;
  79. public $rate;
  80. public $rate_type;
  81. public $desc;
  82. public $yield;
  83. public $varieties;
  84. public $growth_cycle;
  85. public $adopt_income;
  86. public $self_pick_date;
  87. public $self_pick_times;
  88. // 监控设备ID
  89. public $device_id;
  90. public $share_holder_profit_switch;
  91. public $share_holder_high_profit;
  92. public $is_open;
  93. public $chain_level_value;
  94. public $chain_type;
  95. /**
  96. * 初始化
  97. */
  98. public function init()
  99. {
  100. $this->store_id = get_store_id();
  101. }
  102. /**
  103. * @return array
  104. */
  105. public function rules()
  106. {
  107. return [
  108. [[
  109. 'status', 'quick_purchase', 'store_id', 'id',
  110. 'freight', 'pieces', 'goods_num', 'use_attr', 'individual_share', 'attr_setting_type',
  111. 'virtual_sales', 'confine_count', 'is_level', 'product_type', 'growth_cycle', 'share_holder_profit_switch', 'chain_type', 'rate_type'
  112. ], 'integer'],
  113. [['keyword', 'key_word'], 'string', 'max' => 60],
  114. [['name', 'service', 'video_url', 'goods_no', 'cover_pic', 'desc', 'yield', 'varieties',], 'string', 'max' => 255],
  115. [['content',], 'string'],
  116. [['attr', 'attr_member_price_List', 'goods_verify_card_list', 'service_book', 'date_book', 'form', 'adopt_income', 'self_pick_date', 'self_pick_times', 'chain_level_value',], 'safe',],
  117. [['unit'], 'string', 'max' => 10],
  118. [['status', 'quick_purchase', 'chain_type'], 'default', 'value' => 1],
  119. [['cat_id', 'integral', 'goods_pic_list', 'delivery_type', 'device_id'], 'default', 'value' => []],
  120. [[
  121. 'use_attr', 'is_negotiable', 'individual_share',
  122. 'share_type', 'forehead', 'freight', 'attr_setting_type',
  123. 'virtual_sales', 'confine_count', 'is_level', 'share_holder_profit_switch', 'is_open',
  124. ], 'default', 'value' => 0],
  125. [['unit'], 'default', 'value' => '件'],
  126. [['service'], 'default', 'value' => ''],
  127. [['price', 'cost_price', 'original_price', 'forehead', 'share_commission_first', 'share_commission_second', 'share_commission_third', 'weight', 'rate', 'share_holder_high_profit',], 'number', 'max' => 9999999999]
  128. ];
  129. }
  130. public function attributeLabels()
  131. {
  132. return [
  133. 'name' => '商品名称',
  134. 'keyword' => '关键字',
  135. 'cat_id' => '分类',
  136. 'device_id' => '设备ID',
  137. 'unit' => '单位',
  138. 'price' => '售价',
  139. 'cost_price' => '成本价',
  140. 'original_price' => '原价',
  141. 'service' => '服务',
  142. 'forehead' => '满额包邮',
  143. 'quick_purchase' => '是否添加快速购买',
  144. 'content' => '图文详情',
  145. 'use_attr' => '是否使用规格',
  146. 'is_negotiable' => '是否开启面议',
  147. 'goods_pic_list' => '商品图片'
  148. ];
  149. }
  150. /**
  151. * @param array $arr | status 状态 name 名称 cat_id 分类id mch_id 入驻商 is_verify 审核 select 查询数据
  152. * @return array
  153. */
  154. public static function getList ($arr = [])
  155. {
  156. $query = Goods::find()->alias('g')
  157. ->leftJoin(['ag' => AdoptGoods::tableName()], 'g.id = ag.goods_id')
  158. ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.md_food_id' => 0])
  159. ->andWhere(['not like', 'g.name', '当面付']);
  160. if (isset($arr['status']) && $arr['status'] > -1) {
  161. $query->andWhere([
  162. 'g.status' => $arr['status']
  163. ]);
  164. }
  165. $query->andWhere([
  166. 'g.product_type' => 5
  167. ]);
  168. if (isset($arr['name']) && !empty($arr['name'])) {
  169. $query->andWhere([
  170. 'like',
  171. 'g.name',
  172. $arr['name']
  173. ]);
  174. }
  175. if (isset($arr['cat_id']) && $arr['cat_id'] > 0) {
  176. $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
  177. 'gc.cat_id' => $arr['cat_id']
  178. ])->groupBy('gc.goods_id');
  179. }
  180. if (!empty($arr['select'])) {
  181. $select = $arr['select'];
  182. } else {
  183. $select = [
  184. 'g.id', 'g.name', 'g.status', 'g.service',
  185. 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
  186. 'g.price', 'g.goods_num', 'g.sort', 'g.cover_pic', 'g.original_price',
  187. 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type',
  188. 'g.is_recommend', 'ag.desc', 'ag.yield', 'ag.varieties', 'ag.adopt_income', 'ag.growth_cycle'
  189. ];
  190. }
  191. $query->with(['mch'])->select($select)
  192. ->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]);
  193. $pagination = pagination_make($query);
  194. $pagination['data'] = $pagination['list'];
  195. foreach($pagination['data'] as $k => $v) {
  196. $goods_cat = GoodsCat::find()->alias('gc')
  197. ->leftJoin(['c' => AdoptCat::tableName()], 'gc.cat_id=c.id')
  198. ->where([ 'gc.goods_id' => $v['id'] ])
  199. ->select(['c.name'])
  200. ->asArray()
  201. ->all();
  202. $pagination['data'][$k]['cat'] = $goods_cat;
  203. }
  204. unset($pagination['list']);
  205. $book_goods_ids = [];
  206. return [
  207. 'code' => 0,
  208. 'msg' => 'success',
  209. 'book_goods_ids' => $book_goods_ids,
  210. 'data' => $pagination
  211. ];
  212. }
  213. public function saveAdopt()
  214. {
  215. if(!$this->validate()) {
  216. return [
  217. 'code' => 1,
  218. 'msg' => $this->getErrorSummary(false)[0],
  219. ];
  220. }
  221. $full_cut = [
  222. 'pieces' => 0,
  223. 'forehead' => 0
  224. ];
  225. if($this->pieces){
  226. $full_cut['pieces'] = $this->pieces;
  227. }
  228. if($this->forehead){
  229. $full_cut['forehead'] = $this->forehead;
  230. }
  231. $t = \Yii::$app->db->beginTransaction();
  232. $goods = $this->id ? Goods::findOne($this->id) ?: new Goods() : new Goods();
  233. $goods->store_id = $this->store_id;
  234. $goods->name = $this->name;
  235. $goods->key_word = $this->key_word;
  236. $goods->unit = $this->unit;
  237. $goods->price = $this->price;
  238. $goods->cost_price = $this->cost_price;
  239. $goods->original_price = $this->original_price;
  240. $goods->service = $this->service;
  241. $goods->weight = $this->weight;
  242. $goods->freight = $this->freight;
  243. $goods->pieces = $this->pieces;
  244. $goods->full_cut = Json::encode($full_cut);
  245. $goods->forehead = $this->forehead;
  246. $goods->quick_purchase = $this->quick_purchase;
  247. $goods->video_url = $this->video_url;
  248. $goods->detail = $this->content;
  249. $goods->goods_num = $this->goods_num;
  250. $goods->goods_no = $this->goods_no;
  251. $goods->integral = json_encode( $this->integral);
  252. $goods->use_attr = $this->use_attr;
  253. $goods->is_negotiable = $this->is_negotiable;
  254. $goods->cover_pic = $this->cover_pic;
  255. $goods->individual_share = $this->individual_share;
  256. $goods->share_type = $this->share_type;
  257. $goods->share_commission_first = $this->share_commission_first ?? 0;
  258. $goods->share_commission_second = $this->share_commission_second ?? 0;
  259. $goods->share_commission_third = $this->share_commission_third ?? 0;
  260. $goods->attr_setting_type = $this->attr_setting_type ? 1 : 0;
  261. $goods->virtual_sales = $this->virtual_sales;
  262. $goods->confine_count = $this->confine_count;
  263. $goods->rate = $this->rate ?? 0;
  264. $goods->rate_type = $this->rate_type ?? 0;
  265. $goods->is_level = $this->is_level;
  266. $goods->delivery_type = $this->delivery_type ? json_encode($this->delivery_type) : '["express"]';
  267. $goods->product_type = $this->product_type;
  268. $goods->form_name = $this->form['form_name'];
  269. $goods->is_form = $this->form['is_form'];
  270. $goods->share_holder_profit_switch = $this->share_holder_profit_switch ?: 0;
  271. $goods->share_holder_high_profit = $this->share_holder_high_profit ?: '';
  272. if ($this->goods_verify_card_list) {
  273. $verify_card_id = '';
  274. foreach($this->goods_verify_card_list as $val) {
  275. $verify_card_id .= $verify_card_id ? ','.$val['id'] : $val['id'];
  276. }
  277. $goods->verify_card_id = $verify_card_id;
  278. }
  279. if ($goods->save()) {
  280. $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $goods['id']]);
  281. if (!$goodsChainLevel) {
  282. $goodsChainLevel = new GoodsChainLevel();
  283. $goodsChainLevel->goods_id = $goods['id'];
  284. }
  285. $goodsChainLevel->type = $this->chain_type;
  286. $goodsChainLevel->is_open = $this->is_open;
  287. $goodsChainLevel->value = json_encode($this->chain_level_value);
  288. $goodsChainLevel->save();
  289. // 自定义表单
  290. Form::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'goods_id' => $goods['id']]);
  291. if ($this->form['is_form'] == 1) {
  292. if (!$this->form['form_list']) {
  293. $t->rollBack();
  294. return [
  295. 'code'=>1,
  296. 'msg'=>'请填写表单设置'
  297. ];
  298. }
  299. }
  300. if ($this->form['form_list']) {
  301. $this->form['form_list'] = array_values($this->form['form_list']);
  302. foreach ($this->form['form_list'] as $index => $value) {
  303. if (!$value['name']) {
  304. $t->rollBack();
  305. return [
  306. 'code' => 1,
  307. 'msg' => '请输入字段名称'
  308. ];
  309. }
  310. if (in_array($value['type'], ['radio', 'checkbox'])) {
  311. if (!$value['default']) {
  312. $t->rollBack();
  313. return [
  314. 'code' => 1,
  315. 'msg' => '请输入单选或多选的默认值'
  316. ];
  317. }
  318. }
  319. if ($value['id']) {
  320. $form = Form::findOne(['store_id' => $this->store_id, 'id' => $value['id']]);
  321. } else {
  322. $form = new Form();
  323. $form->goods_id = $goods['id'];
  324. }
  325. $form->is_delete = 0;
  326. $form->created_at = time();
  327. $form->type = $value['type'];
  328. $form->name = $value['name'];
  329. $form->default = $value['default'];
  330. $form->required = $value['required'] ? $value['required'] : 0;
  331. $form->tip = $value['tip'];
  332. $form->sort = $index;
  333. $form->store_id = $this->store_id;
  334. if (!$form->save()) {
  335. $t->rollBack();
  336. return [
  337. 'code' => 1,
  338. 'msg' => $form->errors[0]
  339. ];
  340. }
  341. }
  342. }
  343. // 认养
  344. if ($this->product_type == Goods::GOODS_TYPE_ADOPT)
  345. {
  346. if ($goods->id > 0) {
  347. $adopt_goods = AdoptGoods::findOne(['store_id' => $this->store_id, 'goods_id' => $goods->id]);
  348. if ($adopt_goods){
  349. $adopt_goods->updated_at = time();
  350. } else{
  351. $adopt_goods = new AdoptGoods();
  352. $adopt_goods->goods_id = $goods->id;
  353. $adopt_goods->created_at = time();
  354. }
  355. }
  356. $adopt_goods->desc = $this->desc;
  357. $adopt_goods->yield = $this->yield;
  358. $adopt_goods->varieties = $this->varieties;
  359. $adopt_goods->growth_cycle = $this->growth_cycle;
  360. $adopt_goods->adopt_income =$this->adopt_income ? json_encode($this->adopt_income) : json_encode([]);
  361. $adopt_goods->self_pick_date =$this->self_pick_date ? json_encode($this->self_pick_date) : json_encode([]);
  362. $adopt_goods->self_pick_times =$this->self_pick_times ? json_encode($this->self_pick_times) : json_encode([]);
  363. $adopt_goods->is_delete = 0;
  364. $adopt_goods->store_id = $this->store_id;
  365. if (!$adopt_goods->save()) {
  366. $t->rollBack();
  367. return [
  368. 'code' => 1,
  369. 'msg' => $adopt_goods->errors[0]
  370. ];
  371. }
  372. }
  373. // 设置规格
  374. try {
  375. $this->setAttr($goods);
  376. } catch(\Exception $e){}
  377. if ($this->cat_id && count($this->cat_id)) {
  378. if ($goods->id > 0) {
  379. GoodsCat::deleteAll(['goods_id' => $goods->id]);
  380. }
  381. $goodsCatForm = new GoodsCat();
  382. foreach ($this->cat_id as $cat_id) {
  383. $goodsCat = clone $goodsCatForm;
  384. $goodsCat->goods_id = $goods->id;
  385. $goodsCat->cat_id = $cat_id;
  386. $goodsCat->store_id = $goods->store_id;
  387. if (!$goodsCat->save()) {
  388. $t->rollBack();
  389. return [
  390. 'code' => 1,
  391. 'msg' => '保存失败'
  392. ];
  393. }
  394. }
  395. }
  396. if ($goods->id > 0) {
  397. AdoptGoodsDevice::deleteAll(['goods_id' => $goods->id]);
  398. }
  399. if ($this->device_id && count($this->device_id)) {
  400. $goodsDeviceForm = new AdoptGoodsDevice();
  401. foreach ($this->device_id as $device_id) {
  402. $goodsDev = clone $goodsDeviceForm;
  403. $goodsDev->goods_id = $goods->id;
  404. $goodsDev->device_id = $device_id;
  405. $goodsDev->store_id = $goods->store_id;
  406. if (!$goodsDev->save()) {
  407. $t->rollBack();
  408. return [
  409. 'code' => 1,
  410. 'msg' => '保存失败'
  411. ];
  412. }
  413. }
  414. }
  415. if ($this->goods_pic_list) {
  416. if ($goods->id > 0) {
  417. GoodsPic::deleteAll(['goods_id' => $goods->id]);
  418. }
  419. $goodsPicForm = new GoodsPic();
  420. foreach ($this->goods_pic_list as $pic) {
  421. $goodsPic = clone $goodsPicForm;
  422. $goodsPic->goods_id = $goods->id;
  423. $goodsPic->pic_url = $pic['pic_url'];
  424. if (!$goodsPic->save()) {
  425. $t->rollBack();
  426. return [
  427. 'code' => 1,
  428. 'msg' => '保存失败'
  429. ];
  430. }
  431. }
  432. }
  433. $t->commit();
  434. // // 同步门店商品
  435. // queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]));
  436. return [
  437. 'code' => 0,
  438. 'msg' => '保存成功'
  439. ];
  440. } else {
  441. $t->rollBack();
  442. return [
  443. 'code' => 1,
  444. 'msg' => '添加失败'
  445. ];
  446. }
  447. }
  448. public function getAdoptEdit($id)
  449. {
  450. if ($id) {
  451. $goods = Goods::findOne($id);
  452. if (empty($goods)) {
  453. return [
  454. 'code' => 1,
  455. 'msg' => '商品获取失败'
  456. ];
  457. }
  458. $getAttrGroupList = $goods->getAttrGroupList();
  459. $goods = $goods->toArray();
  460. $cat_id = GoodsCat::find()
  461. ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
  462. ->select(['cat_id'])->asArray()->all();
  463. $cat_id = array_column($cat_id, 'cat_id');
  464. $goods['cat_id'] = $cat_id;
  465. $goods['device_id'] = AdoptGoodsDevice::find()->where(['goods_id'=>$goods['id'],'is_delete'=>0])->select('device_id')->column();
  466. $goods['integral'] = json_decode($goods['integral'],true);
  467. $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
  468. ->where(['goods_id' => $id])->asArray()->all();
  469. foreach ($goods as &$val) {
  470. $val = is_int($val) ? (string)$val : $val;
  471. }
  472. $goods['content'] = $goods['detail'];
  473. $adopt_goods = AdoptGoods::find()
  474. ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
  475. ->asArray()->one();
  476. if ($adopt_goods){
  477. $goods['desc'] = $adopt_goods['desc'];
  478. $goods['yield'] = $adopt_goods['yield'];
  479. $goods['varieties'] = $adopt_goods['varieties'];
  480. $goods['growth_cycle'] = $adopt_goods['growth_cycle'];
  481. $goods['adopt_income'] = $adopt_goods['adopt_income'] ? json_decode($adopt_goods['adopt_income']) : [];
  482. $goods['adopt_goods_id'] = $adopt_goods['id'] ? $adopt_goods['id'] : [];
  483. $goods['self_pick_date'] = $adopt_goods['self_pick_date'] ? json_decode($adopt_goods['self_pick_date']) : [];
  484. $goods['self_pick_times'] = $adopt_goods['self_pick_times'] ? json_decode($adopt_goods['self_pick_times']) : [];
  485. }
  486. } else {
  487. $goods = null;
  488. $goods['is_open'] = '0';
  489. $goods['chain_level_value'] = [];
  490. }
  491. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
  492. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
  493. $send_type = $send_type ? Json::decode($send_type['value']) : [];
  494. $send_type_arr = [];
  495. foreach ((array)$send_type as $key => $send) {
  496. if ($send['value'] == 1) {
  497. $send_type_arr[$key] = $send['text'];
  498. }
  499. }
  500. $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr;
  501. $goods['goods_delivery_type'] = !empty($goods['delivery_type']) ? Json::decode($goods['delivery_type']) : array_keys($arr);
  502. $goods['delivery_type'] = $arr;
  503. // 获取运费规则
  504. $postage = PostageRules::find()->where([
  505. 'store_id' => get_store_id(),
  506. 'is_delete' => 0,
  507. 'mch_id' => 0
  508. ])->select(['id', 'name', 'is_enable'])->asArray()->all();
  509. // 获取所有的会员等级
  510. $level = Level::find()->where([
  511. 'store_id' => get_store_id(),
  512. 'is_delete' => 0,
  513. 'status' => 1
  514. ])->select(['level as id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
  515. // 卡券
  516. $verify_list = VerifyCard::find()
  517. ->where([
  518. 'store_id' => get_store_id(),
  519. 'is_delete' => 0
  520. ])->orderBy(['sort' => SORT_ASC])
  521. ->select([
  522. '*'
  523. ])->asArray()
  524. ->all();
  525. $verify_list = Tools::getVerifyList($verify_list);
  526. $goods_verify_card_list = [];
  527. // 获取商品已添加核销卡
  528. if (!empty($goods) && !empty($goods['verify_card_id'])) {
  529. $verify_cards = explode(',', $goods['verify_card_id']);
  530. foreach ($verify_cards as $k => $v) {
  531. $res = VerifyCard::find()
  532. ->select(['id','name'])
  533. ->where(['id' => $v,'is_delete' => 0])
  534. ->asArray()->one();
  535. if ($res) {
  536. $goods_verify_card_list[] = $res;
  537. }
  538. }
  539. }
  540. // 服务商品预约
  541. $goods['service_book'] = [
  542. 'date' => 0,
  543. 'data' => [],
  544. 'flag' => 1
  545. ];
  546. $goods['date_book'] = [
  547. 'data' => [],
  548. 'date' => 30
  549. ];
  550. $goods['form'] = [
  551. 'form_list' => [],
  552. 'is_form' => 0,
  553. 'form_name' => ''
  554. ];
  555. $goods['rate_type'] = (string)(int)$goods['rate_type'];
  556. $goods['share_holder_profit_switch'] = (int)$goods['share_holder_profit_switch'];
  557. $goods['share_holder_high_profit'] = (float)$goods['share_holder_high_profit'];
  558. // 获取等级分润配置
  559. $gooodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $id]);
  560. $goods['is_open'] = '0';
  561. $goods['chain_type'] = '1';
  562. $goods['chain_level_value'] = [];
  563. if ($gooodsChainLevel) {
  564. $goods['is_open'] = (string)$gooodsChainLevel->is_open;
  565. $goods['chain_type'] = (string)$gooodsChainLevel->type;
  566. if ($chainLevelValue = json_decode($gooodsChainLevel->value)) {
  567. $goods['chain_level_value'] = $chainLevelValue;
  568. }
  569. }
  570. if (isset($goods['id'])) {
  571. $goods_book_config = GoodsBook::findOne(['goods_id' => $goods['id']]);
  572. if ($goods_book_config) {
  573. $service_book = $goods_book_config->service_book;
  574. $date_book = $goods_book_config->date_book;
  575. if ($service_book) {
  576. $goods['service_book'] = Json::decode($service_book);
  577. }
  578. if ($date_book) {
  579. $goods['date_book']['data'] = Json::decode($date_book);
  580. $goods['date_book']['date'] = count($goods['date_book']['data']);
  581. }
  582. }
  583. $goods['form']['form_list'] = Form::find()->where(['is_delete' => 0, 'store_id' => $this->store_id, 'goods_id' => $goods['id']])->orderBy(['sort' => SORT_ASC])->asArray()->all();
  584. $goods['form']['is_form'] = intval($goods['is_form']);
  585. $goods['form']['form_name'] = $goods['form_name'];
  586. }
  587. // 获取等级
  588. $shareHolderLevel = ShareHolderLevel::find()->where([
  589. 'store_id' => get_store_id(),
  590. 'is_delete' => 0,
  591. 'status' => 1
  592. ])->select(['id', 'name'])->orderBy(['id' => SORT_ASC])->asArray()->all();
  593. return [
  594. 'code' => 0,
  595. 'msg' => 'success',
  596. 'data' => [
  597. 'shareHolderLevel' => $shareHolderLevel,
  598. 'info' => $goods,
  599. 'AttrGroupList' => isset($getAttrGroupList) ? $getAttrGroupList : [],
  600. 'postage' => $postage,
  601. 'level' => $level,
  602. 'verify_list' => $verify_list,
  603. 'goods_verify_card_list' => $goods_verify_card_list
  604. ]
  605. ];
  606. }
  607. /**
  608. * @param Goods $goods
  609. */
  610. private function setAttr($goods)
  611. {
  612. if ($goods->product_type == Goods::GOODS_TYPE_DATE || $goods->product_type == Goods::GOODS_TYPE_TIME) {
  613. $defaultAttr = '[{"attr_list":[{"attr_id":1,"attr_name":"默认"}],"num":0,"price":0,"no":"","pic":"","share_commission_first":"","share_commission_second":"","share_commission_third":""}]';
  614. $goods->attr = $defaultAttr;
  615. $goods->save();
  616. return;
  617. }
  618. if (!$this->use_attr) {
  619. list($default_attr, $default_attr_group) = $this->getDefaultAttr();
  620. if (empty($this->attr)) {
  621. $this->attr = [
  622. [
  623. 'attr_list' => [
  624. [
  625. 'attr_group_name' => $default_attr_group->attr_group_name,
  626. 'attr_id' => $default_attr->id,
  627. 'attr_name' => $default_attr->attr_name,
  628. ],
  629. ],
  630. 'num' => intval($this->goods_num) ? intval($this->goods_num) : 0,
  631. 'price' => $this->price,
  632. 'no' => $this->goods_no,
  633. ],
  634. ];
  635. } else {
  636. $attr_list = [
  637. [
  638. 'attr_group_name' => $default_attr_group->attr_group_name,
  639. 'attr_id' => $default_attr->id,
  640. 'attr_name' => $default_attr->attr_name,
  641. ],
  642. ];
  643. $this->attr[0]['attr_list'] = $attr_list;
  644. $this->attr[0]['num'] = intval($this->goods_num) ? intval($this->goods_num) : 0;
  645. $this->attr[0]['price'] = intval($this->price) ? intval($this->price) : 0;
  646. $this->attr[0]['no'] = $this->goods_no;
  647. }
  648. }
  649. if (empty($this->attr) || !is_array($this->attr)) {
  650. return;
  651. }
  652. $new_attr = [];
  653. foreach ($this->attr as $i => $item) {
  654. $new_attr_item = [
  655. 'attr_list' => [],
  656. 'num' => intval($item['num']),
  657. 'price' => doubleval($item['price']),
  658. 'no' => $item['no'] ? $item['no'] : '',
  659. 'pic' => $item['pic'] ? $item['pic'] : '',
  660. 'share_commission_first' => $item['share_commission_first'] ? $item['share_commission_first'] : '',
  661. 'share_commission_second' => $item['share_commission_second'] ? $item['share_commission_second'] : '',
  662. 'share_commission_third' => $item['share_commission_third'] ? $item['share_commission_third'] : '',
  663. ];
  664. foreach ($item as $k => $v) {
  665. if (strpos($k,'member') !== false) {
  666. $new_attr_item[$k] = $v;
  667. }
  668. }
  669. foreach ($item['attr_list'] as $a) {
  670. $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]);
  671. if (!$attr_group_model) {
  672. $attr_group_model = new AttrGroup();
  673. $attr_group_model->attr_group_name = $a['attr_group_name'];
  674. $attr_group_model->store_id = $this->store_id;
  675. $attr_group_model->is_delete = 0;
  676. $attr_group_model->save();
  677. }
  678. $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
  679. if (!$attr_model) {
  680. $attr_model = new Attr();
  681. $attr_model->attr_name = $a['attr_name'];
  682. $attr_model->attr_group_id = $attr_group_model->id;
  683. $attr_model->is_delete = 0;
  684. $attr_model->save();
  685. }
  686. $new_attr_item['attr_list'][] = [
  687. 'attr_id' => $attr_model->id,
  688. 'attr_name' => $attr_model->attr_name,
  689. ];
  690. }
  691. $new_attr[] = $new_attr_item;
  692. }
  693. $goods->attr = Json::encode($new_attr);
  694. $goods->save();
  695. }
  696. /**
  697. * @return array
  698. */
  699. private function getDefaultAttr()
  700. {
  701. $default_attr_name = '默认';
  702. $default_attr_group_name = '规格';
  703. $attr = Attr::findOne([
  704. 'attr_name' => $default_attr_name,
  705. 'is_delete' => 0,
  706. 'is_default' => 1,
  707. ]);
  708. $attr_group = null;
  709. if (!$attr) {
  710. $attr_group = AttrGroup::findOne([
  711. 'attr_group_name' => $default_attr_group_name,
  712. 'is_delete' => 0,
  713. ]);
  714. if (!$attr_group) {
  715. $attr_group = new AttrGroup();
  716. $attr_group->store_id = $this->store_id;
  717. $attr_group->attr_group_name = $default_attr_group_name;
  718. $attr_group->is_delete = 0;
  719. $attr_group->save(false);
  720. }
  721. $attr = new Attr();
  722. $attr->attr_group_id = $attr_group->id;
  723. $attr->attr_name = $default_attr_name;
  724. $attr->is_delete = 0;
  725. $attr->is_default = 1;
  726. $attr->save(false);
  727. } else {
  728. $attr_group = AttrGroup::findOne($attr->attr_group_id);
  729. }
  730. return [$attr, $attr_group];
  731. }
  732. public function setAttrNum()
  733. {
  734. $data = post_params();
  735. $goods = Goods::findOne($data['id']);
  736. $arrt = Json::encode($data['attr']);
  737. if ($goods) {
  738. $goods->attr = $arrt;
  739. $goods_num = 0;
  740. foreach($data['attr'] as $item) {
  741. $goods_num += $item['num'];
  742. }
  743. $goods->goods_num = $goods_num;
  744. // if ($goods->mch_id > 0) {
  745. // $mch = Mch::findOne($goods->mch_id);
  746. // if ($mch->is_goods_ex == 1) {
  747. // $goods->status = 0;
  748. // $goods->is_verify = -1;
  749. // }
  750. // }
  751. $res = ['code' => 1, 'msg' => '保存失败'];
  752. if ($goods->save()) {
  753. // 同步门店商品
  754. queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]));
  755. $res = ['code' => 0, 'msg' => '保存成功'];
  756. }
  757. } else {
  758. $res = ['code' => 1, 'msg' => '参数错误'];
  759. }
  760. return $res;
  761. }
  762. public function updateAttrNum()
  763. {
  764. $data = post_params();
  765. $goods = Goods::findOne($data['id']);
  766. $arrt = Json::encode($data['attr']);
  767. if ($goods) {
  768. $goods->attr = $arrt;
  769. $goods_num = 0;
  770. foreach($data['attr'] as $item) {
  771. $goods_num += $item['num'];
  772. }
  773. $goods->goods_num = $goods_num;
  774. // if ($goods->mch_id > 0) {
  775. // $mch = Mch::findOne($goods->mch_id);
  776. // if ($mch->is_goods_ex == 1) {
  777. // $goods->status = 0;
  778. // $goods->is_verify = -1;
  779. // }
  780. // }
  781. $res = ['code' => 1, 'msg' => '保存失败'];
  782. if ($goods->save()) {
  783. // 同步门店商品
  784. queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]));
  785. $res = ['code' => 0, 'msg' => '保存成功'];
  786. }
  787. } else {
  788. $res = ['code' => 1, 'msg' => '参数错误'];
  789. }
  790. return $res;
  791. }
  792. // 修改商品属性
  793. public function setType()
  794. {
  795. $type = get_params('type');
  796. $goods_id = get_params('id');
  797. $value = get_params('value', 0);
  798. if (!$type || !$goods_id) {
  799. return [
  800. 'code' => 1,
  801. 'msg' => '参数错误'
  802. ];
  803. }
  804. $goods = Goods::findOne([
  805. 'id' => $goods_id,
  806. 'mch_id' => get_mch_id(),
  807. 'store_id' => get_store_id()
  808. ]);
  809. if (empty($goods)) {
  810. return [
  811. 'code' => 1,
  812. 'msg' => '查询商品为空'
  813. ];
  814. }
  815. $goods->$type = $value;
  816. if ($type == 'goods_num' && $goods->use_attr == 0) {
  817. $attr = Json::decode($goods->attr);
  818. $attr[0]['num'] = $value;
  819. $goods->attr = Json::encode($attr);
  820. }
  821. if ($type == 'price' && $goods->use_attr == 0) {
  822. $attr = Json::decode($goods->attr);
  823. $attr[0]['price'] = doubleval($value);
  824. $goods->attr = Json::encode($attr);
  825. }
  826. // if ($goods->mch_id > 0) {
  827. // $mch = Mch::findOne($goods->mch_id);
  828. // if ($mch->is_goods_ex == 1) {
  829. // $goods->status = 0;
  830. // $goods->is_verify = -1;
  831. // }
  832. // }
  833. if ($goods->save()) {
  834. // 同步门店商品
  835. queue_push(new SyncMdGoodsJob(['goods_ids' => [$goods->id]]));
  836. return [
  837. 'code' => 0,
  838. 'msg' => '保存成功'
  839. ];
  840. } else {
  841. return [
  842. 'code' => 1,
  843. 'msg' => '保存失败'
  844. ];
  845. }
  846. }
  847. // 修改商品属性
  848. public function updateAttribute()
  849. {
  850. $type = post_params('type');
  851. $goods_id = post_params('id');
  852. $value = post_params('value', 0);
  853. if (!$type || !$goods_id) {
  854. return [
  855. 'code' => 1,
  856. 'msg' => '参数错误'
  857. ];
  858. }
  859. $goods = Goods::findOne([
  860. 'id' => $goods_id,
  861. 'store_id' => get_store_id()
  862. ]);
  863. if (empty($goods)) {
  864. return [
  865. 'code' => 1,
  866. 'msg' => '查询商品为空'
  867. ];
  868. }
  869. $goods->$type = $value;
  870. if ($type == 'goods_num' && $goods->use_attr == 0) {
  871. $attr = Json::decode($goods->attr);
  872. $attr[0]['num'] = $value;
  873. $goods->attr = Json::encode($attr);
  874. }
  875. if ($type == 'price' && $goods->use_attr == 0) {
  876. $attr = Json::decode($goods->attr);
  877. $attr[0]['price'] = doubleval($value);
  878. $goods->attr = Json::encode($attr);
  879. }
  880. // if ($goods->mch_id > 0) {
  881. // $mch = Mch::findOne($goods->mch_id);
  882. // if ($mch->is_goods_ex == 1) {
  883. // $goods->status = 0;
  884. // $goods->is_verify = -1;
  885. // }
  886. // }
  887. if ($goods->save()) {
  888. return [
  889. 'code' => 0,
  890. 'msg' => '保存成功'
  891. ];
  892. } else {
  893. return [
  894. 'code' => 1,
  895. 'msg' => '保存失败'
  896. ];
  897. }
  898. }
  899. /**
  900. * Undocumented function
  901. *
  902. * @Author LGL 24963@qq.com
  903. * @DateTime 2021-01-18
  904. * @desc: 根据id获取商品列表
  905. * @param array $id
  906. * @return void
  907. */
  908. public static function getGoodsListById ($id = [])
  909. {
  910. if (empty($id) && !is_array($id)) {
  911. return [];
  912. }
  913. return Goods::find()->where(['in', 'id', $id])->select('id, name, cover_pic')->asArray()->all();
  914. }
  915. /**
  916. * Undocumented function
  917. *
  918. * @Author LGL 24963@qq.com
  919. * @DateTime 2021-03-02
  920. * @desc: 根据商品id获取规格属性列表
  921. * @param [type] $id
  922. * @return void
  923. */
  924. public static function getAttrByGoodsId ($id)
  925. {
  926. $goods = Goods::findOne(['id' => $id, 'store_id' => get_store_id(), 'mch_id' => get_mch_id()]);
  927. if (!$goods->attr) {
  928. return [];
  929. }
  930. $attr_data = json_decode($goods->attr, true);
  931. foreach ($attr_data as $i => $attr_data_item) {
  932. if (!isset($attr_data[$i]['no'])) {
  933. $attr_data[$i]['no'] = '';
  934. }
  935. if (!isset($attr_data[$i]['pic'])) {
  936. $attr_data[$i]['pic'] = '';
  937. }
  938. foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
  939. $attr_group = $goods->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
  940. $t = $attr_data[$i]['attr_list'][$j]['attr_name'];
  941. unset($attr_data[$i]['attr_list'][$j]['attr_name']);
  942. $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
  943. $attr_data[$i]['attr_list'][$j]['attr_name'] = $t;
  944. }
  945. }
  946. return $attr_data;
  947. }
  948. private function compareDate($arr): bool
  949. {
  950. array_multisort(array_column($arr, 'start_time'), SORT_ASC, $arr);
  951. foreach ($arr as $k => $v){
  952. if ($v['start_time'] >= $v['end_time'])
  953. return false;
  954. if ($k > 0 && $arr[$k]['start_time'] < $arr[$k-1]['end_time'])
  955. return false;
  956. }
  957. return true;
  958. }
  959. }