| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\admin;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\GoodsPic;
- use app\models\Level;
- use app\models\PostageRules;
- use app\models\VerifyCard;
- use yii\base\Model;
- use yii\helpers\Json;
- class GoodsForm extends Model
- {
- public $store_id = 1;
- public $status;
- public $keyword;
- public $cat_id;
- public $type;
- public $id;
- public $name;
- public $key_word;
- public $unit;
- public $price;
- public $cost_price;
- public $original_price;
- public $service;
- public $freight;
- public $pieces;
- public $forehead;
- public $content;
- public $goods_num;
- public $goods_no;
- public $integral;
- public $use_attr;
- public $cover_pic;
- public $attr;
- public $confine_count;
- public $order_min_count;
- public $goods_pic_list;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['price', 'cost_price', 'original_price', 'name'], 'required'],
- [['status','store_id', 'id',
- 'freight', 'pieces', 'goods_num', 'use_attr', 'confine_count', 'order_min_count'], 'integer'],
- [['keyword', 'key_word'], 'string', 'max' => 60],
- [['name','service', 'goods_no', 'cover_pic'], 'string', 'max' => 255],
- [['content','attr'], 'string'],
- [['unit'], 'string', 'max' => 10],
- [['status'], 'default', 'value' => 1],
- [['cat_id', 'integral', 'goods_pic_list'], 'default', 'value' => Json::encode([])],
- [['use_attr', 'forehead', 'freight', 'confine_count'], 'default', 'value' => 0],
- [['price', 'cost_price', 'original_price', 'forehead'], 'number', 'max' => 9999999999]
- ];
- }
- public function attributeLabels()
- {
- return [
- 'name' => '商品名称',
- 'keyword' => '关键字',
- 'cat_id' => '分类',
- 'unit' => '单位',
- 'price' => '售价',
- 'cost_price' => '成本价',
- 'original_price' => '原价',
- 'service' => '服务',
- 'forehead' => '满额包邮',
- 'content' => '图文详情',
- 'use_attr' => '是否使用规格',
- 'goods_pic_list' => '商品图片'
- ];
- }
- public function save()
- {
- if(!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $t = \Yii::$app->db->beginTransaction();
- $goods = $this->id ? (Goods::findOne($this->id) ?: new Goods()) : new Goods();
- $goods->store_id = $this->store_id;
- $goods->name = $this->name;
- $goods->key_word = $this->key_word ?: $goods->key_word;
- $goods->unit = $this->unit;
- $goods->price = $this->price;
- $goods->original_price = $this->original_price;
- $goods->service = $this->service;
- $goods->freight = $this->freight;
- $goods->pieces = $this->pieces ? $this->pieces : 0;
- $goods->forehead = $this->forehead;
- $goods->detail = $this->content;
- $goods->goods_num = $this->goods_num;
- $goods->integral = $this->integral;
- $goods->use_attr = $this->use_attr;
- $goods->cover_pic = $this->cover_pic;
- $goods->confine_count = $this->confine_count ? $this->confine_count : 0;
- isset($this->order_min_count) && $goods->order_min_count = $this->order_min_count;
- $goods->cost_price = $this->cost_price ? $this->cost_price : 0;
-
- if ($goods->save()) {
- // 设置规格
- $this->setAttr($goods);
- $cat_arr = Json::decode($this->cat_id);
- if ($cat_arr && count($cat_arr)) {
-
- if ($goods->id > 0) {
- GoodsCat::deleteAll(['goods_id' => $goods->id]);
- }
- $goodsCatForm = new GoodsCat();
- foreach ($cat_arr as $cat_id) {
- $goodsCat = clone $goodsCatForm;
- $goodsCat->goods_id = $goods->id;
- $goodsCat->cat_id = $cat_id;
- $goodsCat->store_id = $goods->store_id;
- if (!$goodsCat->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- }
- }
- if ($this->goods_pic_list) {
- if ($goods->id > 0) {
- GoodsPic::deleteAll(['goods_id' => $goods->id]);
- }
- $goodsPicForm = new GoodsPic();
- foreach (Json::decode($this->goods_pic_list) as $pic) {
- $goodsPic = clone $goodsPicForm;
- $goodsPic->goods_id = $goods->id;
- $goodsPic->pic_url = $pic['pic_url'];
- if (!$goodsPic->save()) {
- $t->rollBack();
- return [
- 'code' => 0,
- 'msg' => '保存失败'
- ];
- }
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- }else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '添加失败'
- ];
- }
- }
- public function getEdit($id)
- {
- if ($id) {
- $goods = Goods::findOne($id);
- if (empty($goods)) {
- return [
- 'code' => 1,
- 'msg' => '商品获取失败'
- ];
- }
- $getAttrGroupList = $goods->getAttrGroupList();
- $goods = $goods->toArray();
- $cat_id = GoodsCat::find()
- ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
- ->select(['cat_id'])->asArray()->all();
- $cat_id = array_column($cat_id, 'cat_id');
- $goods['cat_id'] = $cat_id;
- $goods['integral'] = json_decode($goods['integral'],true);
- $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
- ->where(['goods_id' => $id])->asArray()->all();
- foreach ($goods as &$val) {
- $val = is_int($val) ? (string)$val : $val;
- }
- } else {
- $goods = null;
- }
- $goods['content'] = $goods['detail'];
- // 获取运费规则
- $postage = PostageRules::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'mch_id' => 0
- ])->select(['id', 'name', 'is_enable'])->asArray()->all();
- // 获取所有的会员等级
- $level = Level::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'status' => 1
- ])->select(['id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
- // 卡券
- $verify_list = VerifyCard::find()
- ->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0
- ])->orderBy(['sort' => SORT_ASC])
- ->select([
- 'id', 'name'
- ])
- ->all();
- // 获取商品已添加核销卡
- if (empty($goods['verify_card_id'])) {
- $goods_verify_card_list = [];
- } else {
- $verify_cards = explode(',', $goods['verify_card_id']);
- foreach ($verify_cards as $k => $v) {
- $goods_verify_card_list[] = VerifyCard::find()
- ->select(['id','name'])
- ->where(['id' => $v,'is_delete' => 0])
- ->asArray()->one();
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'info' => $goods,
- 'AttrGroupList' =>$getAttrGroupList ?: [],
- 'postage' => $postage,
- 'level' => $level,
- 'verify_list' => $verify_list,
- 'goods_verify_card_list' => $goods_verify_card_list
- ]
- ];
- }
- /**
- * @param Goods $goods
- */
- private function setAttr($goods)
- {
- if (!$this->use_attr) {
- list($default_attr, $default_attr_group) = $this->getDefaultAttr();
- if (empty($this->attr)) {
- $this->attr = [
- [
- 'attr_list' => [
- [
- 'attr_group_name' => $default_attr_group->attr_group_name,
- 'attr_id' => $default_attr->id,
- 'attr_name' => $default_attr->attr_name,
- ],
- ],
- 'num' => intval($this->goods_num) ? intval($this->goods_num) : 0,
- 'price' => $this->price,
- 'supplier_price' => 0,
- 'no' => $this->goods_no,
- ],
- ];
- } else {
- $this->attr = is_array($this->attr) ? $this->attr : (empty($this->attr) ? [] : Json::decode($this->attr));
- $attr_list = [
- [
- 'attr_group_name' => $default_attr_group->attr_group_name,
- 'attr_id' => $default_attr->id,
- 'attr_name' => $default_attr->attr_name,
- ],
- ];
- $this->attr[0]['attr_list'] = $attr_list;
- $this->attr[0]['num'] = intval($this->goods_num) ? intval($this->goods_num) : 0;
- $this->attr[0]['price'] = intval($this->price) ? intval($this->price) : 0;
- $this->attr[0]['no'] = $this->goods_no;
- }
- } else {
- $this->attr = Json::decode($this->attr);
- }
-
- if (empty($this->attr) || !is_array($this->attr)) {
- return;
- }
- $new_attr = [];
- foreach ($this->attr as $i => $item) {
- $new_attr_item = [
- 'attr_list' => [],
- 'num' => intval($item['num']),
- 'price' => doubleval($item['price']),
- 'supplier_price' => doubleval($item['supplier_price']),
- 'no' => $item['no'] ? $item['no'] : '',
- 'pic' => $item['pic'] ? $item['pic'] : '',
- 'share_commission_first' => $item['share_commission_first'] ? $item['share_commission_first'] : '',
- 'share_commission_second' => $item['share_commission_second'] ? $item['share_commission_second'] : '',
- 'share_commission_third' => $item['share_commission_third'] ? $item['share_commission_third'] : '',
- ];
- foreach ($item as $k => $v) {
- if (strpos($k,'member') !== false) {
- $new_attr_item[$k] = $v;
- }
- }
- foreach ($item['attr_list'] as $a) {
- $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]);
- if (!$attr_group_model) {
- $attr_group_model = new AttrGroup();
- $attr_group_model->attr_group_name = $a['attr_group_name'];
- $attr_group_model->store_id = $this->store_id;
- $attr_group_model->is_delete = 0;
- $attr_group_model->save();
- }
- $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]);
- if (!$attr_model) {
- $attr_model = new Attr();
- $attr_model->attr_name = $a['attr_name'];
- $attr_model->attr_group_id = $attr_group_model->id;
- $attr_model->is_delete = 0;
- $attr_model->save();
- }
- $new_attr_item['attr_list'][] = [
- 'attr_id' => $attr_model->id,
- 'attr_name' => $attr_model->attr_name,
- ];
- }
- $new_attr[] = $new_attr_item;
- }
- $goods->attr = Json::encode($new_attr);
- $goods->save();
- }
- /**
- * @return array
- */
- private function getDefaultAttr()
- {
- $default_attr_name = '默认';
- $default_attr_group_name = '规格';
- $attr = Attr::findOne([
- 'attr_name' => $default_attr_name,
- 'is_delete' => 0,
- 'is_default' => 1,
- ]);
- $attr_group = null;
- if (!$attr) {
- $attr_group = AttrGroup::findOne([
- 'attr_group_name' => $default_attr_group_name,
- 'is_delete' => 0,
- ]);
- if (!$attr_group) {
- $attr_group = new AttrGroup();
- $attr_group->store_id = $this->store_id;
- $attr_group->attr_group_name = $default_attr_group_name;
- $attr_group->is_delete = 0;
- $attr_group->save(false);
- }
- $attr = new Attr();
- $attr->attr_group_id = $attr_group->id;
- $attr->attr_name = $default_attr_name;
- $attr->is_delete = 0;
- $attr->is_default = 1;
- $attr->save(false);
- } else {
- $attr_group = AttrGroup::findOne($attr->attr_group_id);
- }
- return [$attr, $attr_group];
- }
- public function setAttrNum()
- {
- $data = post_params();
- $goods = Goods::findOne($data['id']);
- if ($goods) {
- $goods->attr = Json::encode($data['attr']);
- $res = $goods->save() ? ['code' => 0, 'msg' => '保存成功'] :
- ['code' => 1, 'msg' => '保存失败'];
- } else {
- $res = ['code' => 1, 'msg' => '参数错误'];
- }
- return $res;
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-18
- * @desc: 根据id获取商品列表
- * @param array $id
- * @return void
- */
- public static function getGoodsListById ($id = [])
- {
- if (empty($id) && !is_array($id)) {
- return [];
- }
- return Goods::find()->where(['in', 'id', $id])->select('id, name, cover_pic')->asArray()->all();
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-03-02
- * @desc: 根据商品id获取规格属性列表
- * @param [type] $id
- * @return void
- */
- public static function getAttrByGoodsId ($id)
- {
- $goods = Goods::findOne(['id' => $id, 'store_id' => get_store_id(), 'mch_id' => get_mch_id()]);
- if (!$goods->attr) {
- return [];
- }
-
- $attr_data = json_decode($goods->attr, true);
- foreach ($attr_data as $i => $attr_data_item) {
- if (!isset($attr_data[$i]['no'])) {
- $attr_data[$i]['no'] = '';
- }
- if (!isset($attr_data[$i]['pic'])) {
- $attr_data[$i]['pic'] = '';
- }
- foreach ($attr_data[$i]['attr_list'] as $j => $attr_list) {
- $attr_group = $goods->getAttrGroupByAttId($attr_data[$i]['attr_list'][$j]['attr_id']);
- $t = $attr_data[$i]['attr_list'][$j]['attr_name'];
- unset($attr_data[$i]['attr_list'][$j]['attr_name']);
- $attr_data[$i]['attr_list'][$j]['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null;
- $attr_data[$i]['attr_list'][$j]['attr_name'] = $t;
- }
- }
- return $attr_data;
- }
- }
|