GoodsForm.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\admin;
  8. use app\models\Attr;
  9. use app\models\AttrGroup;
  10. use app\models\Goods;
  11. use app\models\GoodsCat;
  12. use app\models\GoodsPic;
  13. use app\models\Level;
  14. use app\models\PostageRules;
  15. use app\models\VerifyCard;
  16. use yii\base\Model;
  17. use yii\helpers\Json;
  18. class GoodsForm extends Model
  19. {
  20. public $store_id = 1;
  21. public $status;
  22. public $keyword;
  23. public $cat_id;
  24. public $type;
  25. public $id;
  26. public $name;
  27. public $key_word;
  28. public $unit;
  29. public $price;
  30. public $cost_price;
  31. public $original_price;
  32. public $service;
  33. public $freight;
  34. public $pieces;
  35. public $forehead;
  36. public $content;
  37. public $goods_num;
  38. public $goods_no;
  39. public $integral;
  40. public $use_attr;
  41. public $cover_pic;
  42. public $attr;
  43. public $confine_count;
  44. public $order_min_count;
  45. public $goods_pic_list;
  46. /**
  47. * @return array
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['price', 'cost_price', 'original_price', 'name'], 'required'],
  53. [['status','store_id', 'id',
  54. 'freight', 'pieces', 'goods_num', 'use_attr', 'confine_count', 'order_min_count'], 'integer'],
  55. [['keyword', 'key_word'], 'string', 'max' => 60],
  56. [['name','service', 'goods_no', 'cover_pic'], 'string', 'max' => 255],
  57. [['content','attr'], 'string'],
  58. [['unit'], 'string', 'max' => 10],
  59. [['status'], 'default', 'value' => 1],
  60. [['cat_id', 'integral', 'goods_pic_list'], 'default', 'value' => Json::encode([])],
  61. [['use_attr', 'forehead', 'freight', 'confine_count'], 'default', 'value' => 0],
  62. [['price', 'cost_price', 'original_price', 'forehead'], 'number', 'max' => 9999999999]
  63. ];
  64. }
  65. public function attributeLabels()
  66. {
  67. return [
  68. 'name' => '商品名称',
  69. 'keyword' => '关键字',
  70. 'cat_id' => '分类',
  71. 'unit' => '单位',
  72. 'price' => '售价',
  73. 'cost_price' => '成本价',
  74. 'original_price' => '原价',
  75. 'service' => '服务',
  76. 'forehead' => '满额包邮',
  77. 'content' => '图文详情',
  78. 'use_attr' => '是否使用规格',
  79. 'goods_pic_list' => '商品图片'
  80. ];
  81. }
  82. public function save()
  83. {
  84. if(!$this->validate()) {
  85. return [
  86. 'code' => 1,
  87. 'msg' => $this->getErrorSummary(false)[0],
  88. ];
  89. }
  90. $t = \Yii::$app->db->beginTransaction();
  91. $goods = $this->id ? (Goods::findOne($this->id) ?: new Goods()) : new Goods();
  92. $goods->store_id = $this->store_id;
  93. $goods->name = $this->name;
  94. $goods->key_word = $this->key_word ?: $goods->key_word;
  95. $goods->unit = $this->unit;
  96. $goods->price = $this->price;
  97. $goods->original_price = $this->original_price;
  98. $goods->service = $this->service;
  99. $goods->freight = $this->freight;
  100. $goods->pieces = $this->pieces ? $this->pieces : 0;
  101. $goods->forehead = $this->forehead;
  102. $goods->detail = $this->content;
  103. $goods->goods_num = $this->goods_num;
  104. $goods->integral = $this->integral;
  105. $goods->use_attr = $this->use_attr;
  106. $goods->cover_pic = $this->cover_pic;
  107. $goods->confine_count = $this->confine_count ? $this->confine_count : 0;
  108. isset($this->order_min_count) && $goods->order_min_count = $this->order_min_count;
  109. $goods->cost_price = $this->cost_price ? $this->cost_price : 0;
  110. if ($goods->save()) {
  111. // 设置规格
  112. $this->setAttr($goods);
  113. $cat_arr = Json::decode($this->cat_id);
  114. if ($cat_arr && count($cat_arr)) {
  115. if ($goods->id > 0) {
  116. GoodsCat::deleteAll(['goods_id' => $goods->id]);
  117. }
  118. $goodsCatForm = new GoodsCat();
  119. foreach ($cat_arr as $cat_id) {
  120. $goodsCat = clone $goodsCatForm;
  121. $goodsCat->goods_id = $goods->id;
  122. $goodsCat->cat_id = $cat_id;
  123. $goodsCat->store_id = $goods->store_id;
  124. if (!$goodsCat->save()) {
  125. $t->rollBack();
  126. return [
  127. 'code' => 1,
  128. 'msg' => '保存失败'
  129. ];
  130. }
  131. }
  132. }
  133. if ($this->goods_pic_list) {
  134. if ($goods->id > 0) {
  135. GoodsPic::deleteAll(['goods_id' => $goods->id]);
  136. }
  137. $goodsPicForm = new GoodsPic();
  138. foreach (Json::decode($this->goods_pic_list) as $pic) {
  139. $goodsPic = clone $goodsPicForm;
  140. $goodsPic->goods_id = $goods->id;
  141. $goodsPic->pic_url = $pic['pic_url'];
  142. if (!$goodsPic->save()) {
  143. $t->rollBack();
  144. return [
  145. 'code' => 0,
  146. 'msg' => '保存失败'
  147. ];
  148. }
  149. }
  150. }
  151. $t->commit();
  152. return [
  153. 'code' => 0,
  154. 'msg' => '保存成功'
  155. ];
  156. }else {
  157. $t->rollBack();
  158. return [
  159. 'code' => 1,
  160. 'msg' => '添加失败'
  161. ];
  162. }
  163. }
  164. public function getEdit($id)
  165. {
  166. if ($id) {
  167. $goods = Goods::findOne($id);
  168. if (empty($goods)) {
  169. return [
  170. 'code' => 1,
  171. 'msg' => '商品获取失败'
  172. ];
  173. }
  174. $getAttrGroupList = $goods->getAttrGroupList();
  175. $goods = $goods->toArray();
  176. $cat_id = GoodsCat::find()
  177. ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
  178. ->select(['cat_id'])->asArray()->all();
  179. $cat_id = array_column($cat_id, 'cat_id');
  180. $goods['cat_id'] = $cat_id;
  181. $goods['integral'] = json_decode($goods['integral'],true);
  182. $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
  183. ->where(['goods_id' => $id])->asArray()->all();
  184. foreach ($goods as &$val) {
  185. $val = is_int($val) ? (string)$val : $val;
  186. }
  187. } else {
  188. $goods = null;
  189. }
  190. $goods['content'] = $goods['detail'];
  191. // 获取运费规则
  192. $postage = PostageRules::find()->where([
  193. 'store_id' => get_store_id(),
  194. 'is_delete' => 0,
  195. 'mch_id' => 0
  196. ])->select(['id', 'name', 'is_enable'])->asArray()->all();
  197. // 获取所有的会员等级
  198. $level = Level::find()->where([
  199. 'store_id' => get_store_id(),
  200. 'is_delete' => 0,
  201. 'status' => 1
  202. ])->select(['id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
  203. // 卡券
  204. $verify_list = VerifyCard::find()
  205. ->where([
  206. 'store_id' => get_store_id(),
  207. 'is_delete' => 0
  208. ])->orderBy(['sort' => SORT_ASC])
  209. ->select([
  210. 'id', 'name'
  211. ])
  212. ->all();
  213. // 获取商品已添加核销卡
  214. if (empty($goods['verify_card_id'])) {
  215. $goods_verify_card_list = [];
  216. } else {
  217. $verify_cards = explode(',', $goods['verify_card_id']);
  218. foreach ($verify_cards as $k => $v) {
  219. $goods_verify_card_list[] = VerifyCard::find()
  220. ->select(['id','name'])
  221. ->where(['id' => $v,'is_delete' => 0])
  222. ->asArray()->one();
  223. }
  224. }
  225. return [
  226. 'code' => 0,
  227. 'msg' => 'success',
  228. 'data' => [
  229. 'info' => $goods,
  230. 'AttrGroupList' =>$getAttrGroupList ?: [],
  231. 'postage' => $postage,
  232. 'level' => $level,
  233. 'verify_list' => $verify_list,
  234. 'goods_verify_card_list' => $goods_verify_card_list
  235. ]
  236. ];
  237. }
  238. /**
  239. * @param Goods $goods
  240. */
  241. private function setAttr($goods)
  242. {
  243. if (!$this->use_attr) {
  244. list($default_attr, $default_attr_group) = $this->getDefaultAttr();
  245. if (empty($this->attr)) {
  246. $this->attr = [
  247. [
  248. 'attr_list' => [
  249. [
  250. 'attr_group_name' => $default_attr_group->attr_group_name,
  251. 'attr_id' => $default_attr->id,
  252. 'attr_name' => $default_attr->attr_name,
  253. ],
  254. ],
  255. 'num' => intval($this->goods_num) ? intval($this->goods_num) : 0,
  256. 'price' => $this->price,
  257. 'supplier_price' => 0,
  258. 'no' => $this->goods_no,
  259. ],
  260. ];
  261. } else {
  262. $this->attr = is_array($this->attr) ? $this->attr : (empty($this->attr) ? [] : Json::decode($this->attr));
  263. $attr_list = [
  264. [
  265. 'attr_group_name' => $default_attr_group->attr_group_name,
  266. 'attr_id' => $default_attr->id,
  267. 'attr_name' => $default_attr->attr_name,
  268. ],
  269. ];
  270. $this->attr[0]['attr_list'] = $attr_list;
  271. $this->attr[0]['num'] = intval($this->goods_num) ? intval($this->goods_num) : 0;
  272. $this->attr[0]['price'] = intval($this->price) ? intval($this->price) : 0;
  273. $this->attr[0]['no'] = $this->goods_no;
  274. }
  275. } else {
  276. $this->attr = Json::decode($this->attr);
  277. }
  278. if (empty($this->attr) || !is_array($this->attr)) {
  279. return;
  280. }
  281. $new_attr = [];
  282. foreach ($this->attr as $i => $item) {
  283. $new_attr_item = [
  284. 'attr_list' => [],
  285. 'num' => intval($item['num']),
  286. 'price' => doubleval($item['price']),
  287. 'supplier_price' => doubleval($item['supplier_price']),
  288. 'no' => $item['no'] ? $item['no'] : '',
  289. 'pic' => $item['pic'] ? $item['pic'] : '',
  290. 'share_commission_first' => $item['share_commission_first'] ? $item['share_commission_first'] : '',
  291. 'share_commission_second' => $item['share_commission_second'] ? $item['share_commission_second'] : '',
  292. 'share_commission_third' => $item['share_commission_third'] ? $item['share_commission_third'] : '',
  293. ];
  294. foreach ($item as $k => $v) {
  295. if (strpos($k,'member') !== false) {
  296. $new_attr_item[$k] = $v;
  297. }
  298. }
  299. foreach ($item['attr_list'] as $a) {
  300. $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]);
  301. if (!$attr_group_model) {
  302. $attr_group_model = new AttrGroup();
  303. $attr_group_model->attr_group_name = $a['attr_group_name'];
  304. $attr_group_model->store_id = $this->store_id;
  305. $attr_group_model->is_delete = 0;
  306. $attr_group_model->save();
  307. }
  308. $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
  309. if (!$attr_model) {
  310. $attr_model = new Attr();
  311. $attr_model->attr_name = $a['attr_name'];
  312. $attr_model->attr_group_id = $attr_group_model->id;
  313. $attr_model->is_delete = 0;
  314. $attr_model->save();
  315. }
  316. $new_attr_item['attr_list'][] = [
  317. 'attr_id' => $attr_model->id,
  318. 'attr_name' => $attr_model->attr_name,
  319. ];
  320. }
  321. $new_attr[] = $new_attr_item;
  322. }
  323. $goods->attr = Json::encode($new_attr);
  324. $goods->save();
  325. }
  326. /**
  327. * @return array
  328. */
  329. private function getDefaultAttr()
  330. {
  331. $default_attr_name = '默认';
  332. $default_attr_group_name = '规格';
  333. $attr = Attr::findOne([
  334. 'attr_name' => $default_attr_name,
  335. 'is_delete' => 0,
  336. 'is_default' => 1,
  337. ]);
  338. $attr_group = null;
  339. if (!$attr) {
  340. $attr_group = AttrGroup::findOne([
  341. 'attr_group_name' => $default_attr_group_name,
  342. 'is_delete' => 0,
  343. ]);
  344. if (!$attr_group) {
  345. $attr_group = new AttrGroup();
  346. $attr_group->store_id = $this->store_id;
  347. $attr_group->attr_group_name = $default_attr_group_name;
  348. $attr_group->is_delete = 0;
  349. $attr_group->save(false);
  350. }
  351. $attr = new Attr();
  352. $attr->attr_group_id = $attr_group->id;
  353. $attr->attr_name = $default_attr_name;
  354. $attr->is_delete = 0;
  355. $attr->is_default = 1;
  356. $attr->save(false);
  357. } else {
  358. $attr_group = AttrGroup::findOne($attr->attr_group_id);
  359. }
  360. return [$attr, $attr_group];
  361. }
  362. public function setAttrNum()
  363. {
  364. $data = post_params();
  365. $goods = Goods::findOne($data['id']);
  366. if ($goods) {
  367. $goods->attr = Json::encode($data['attr']);
  368. $res = $goods->save() ? ['code' => 0, 'msg' => '保存成功'] :
  369. ['code' => 1, 'msg' => '保存失败'];
  370. } else {
  371. $res = ['code' => 1, 'msg' => '参数错误'];
  372. }
  373. return $res;
  374. }
  375. /**
  376. * Undocumented function
  377. *
  378. * @Author LGL 24963@qq.com
  379. * @DateTime 2021-01-18
  380. * @desc: 根据id获取商品列表
  381. * @param array $id
  382. * @return void
  383. */
  384. public static function getGoodsListById ($id = [])
  385. {
  386. if (empty($id) && !is_array($id)) {
  387. return [];
  388. }
  389. return Goods::find()->where(['in', 'id', $id])->select('id, name, cover_pic')->asArray()->all();
  390. }
  391. /**
  392. * Undocumented function
  393. *
  394. * @Author LGL 24963@qq.com
  395. * @DateTime 2021-03-02
  396. * @desc: 根据商品id获取规格属性列表
  397. * @param [type] $id
  398. * @return void
  399. */
  400. public static function getAttrByGoodsId ($id)
  401. {
  402. $goods = Goods::findOne(['id' => $id, 'store_id' => get_store_id(), 'mch_id' => get_mch_id()]);
  403. if (!$goods->attr) {
  404. return [];
  405. }
  406. $attr_data = json_decode($goods->attr, true);
  407. foreach ($attr_data as $i => $attr_data_item) {
  408. if (!isset($attr_data[$i]['no'])) {
  409. $attr_data[$i]['no'] = '';
  410. }
  411. if (!isset($attr_data[$i]['pic'])) {
  412. $attr_data[$i]['pic'] = '';
  413. }
  414. foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
  415. $attr_group = $goods->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
  416. $t = $attr_data[$i]['attr_list'][$j]['attr_name'];
  417. unset($attr_data[$i]['attr_list'][$j]['attr_name']);
  418. $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
  419. $attr_data[$i]['attr_list'][$j]['attr_name'] = $t;
  420. }
  421. }
  422. return $attr_data;
  423. }
  424. }