| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * MdGodsForm.php
- * todo 文件描述
- * Created on 2024/4/10 09:22
- * @author: hankaige
- */
- namespace app\modules\client\models\v1;
- use app\models\Goods;
- use app\models\MdGoods;
- use app\modules\client\models\ApiModel;
- class MdGodsForm extends ApiModel
- {
- public $store_id;
- public $md_id;
- public $keyword;
- public $status;//状态字段 -1全部 1售卖中 0已下架 2已售空
- public $ids;// 要修改的商品ID
- public $id;// 单独修改商品价格与库存
- public $goods_price;// 默认规格商品价格
- public $goods_num;// 默认规格商品库存
- public $attr;// 多规格规格数据
- public function rules()
- {
- return [
- [
- [
- 'md_id',
- 'store_id'
- ],
- 'integer'
- ],
- [
- ['md_id'],
- 'required'
- ],
- ];
- }
- public function getGoodsList(): array
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(FALSE)[0]
- ];
- }
- $query = MdGoods::find()->alias('mg')->leftJoin(['g' => Goods::tableName()], 'mg.goods_id=g.id')->where([
- 'mg.md_id' => $this->md_id,
- 'g.is_delete' => 0,
- 'g.store_id' => $this->store_id,
- 'g.status' => Goods::STATUS_NORMAL
- ])->select([
- 'g.name',
- 'g.id',
- 'mg.virtual_sales',
- 'mg.goods_num',
- 'md_price' => 'mg.price',
- 'g.price',
- 'mg.status',
- 'g.cover_pic',
- 'g.use_attr'
- ]);
- if (!empty($this->keyword)) {
- $query->andWhere([
- 'like',
- 'g.name',
- $this->keyword
- ]);
- }
- switch ($this->status) {
- case 1:
- $query->andWhere(['mg.status' => 1]);
- break;
- case 0:
- $query->andWhere(['mg.status' => 0]);
- break;
- case 2:
- $query->andWhere(['mg.goods_num' => 0]);
- break;
- default:
- break;
- }
- $result = pagination_make($query, TRUE, 'mg.created_at DESC');
- foreach($result['list'] as &$item){
- $item['checked'] = 0;
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $result
- ];
- }
- // 批量修改门店商品上下架状态
- public function batchUpdateStatus(): array
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(FALSE)[0]
- ];
- }
- if (empty($this->ids)) {
- return [
- 'code' => 1,
- 'msg' => '请选择要修改的商品'
- ];
- }
- if(!strstr($this->ids,',')){
- $ids = $this->ids;
- }else{
- $ids = explode(',', $this->ids);
- }
- $res = MdGoods::updateAll(['status' => $this->status], ['goods_id' => $ids]);
- if ($res > 0) {
- return [
- 'code' => 0,
- 'msg' => '修改成功',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '没有找到要修改的数据',
- ];
- }
- public function getGoodsAttr()
- {
- try {
- $goods = MdGoods::find()->where([
- 'md_id' => $this->md_id,
- 'goods_id' => $this->id,
- ])->select('id, attr')->one();
- $baseGoods = Goods::findOne($goods->goods_id);
- if (empty($goods->attr) || empty($goods)) {
- throw new \Exception("获取商品规格库存失败");
- }
- $attr = !empty($goods->attr) ? json_decode($goods->attr, TRUE) : [];
- $getAttrGroupList = $goods->getAttrGroupList($baseGoods->use_attr);
- $getAttrGroupList = json_decode(json_encode($getAttrGroupList), TRUE);
- $title = NULL;
- if (!empty($getAttrGroupList)) {
- $title = $getAttrGroupList[0]['attr_list'];
- }
- $arr = [];
- if (!empty($title) && !empty($attr)) {
- foreach ($title as $index => $item) {
- foreach ($attr as $at) {
- if (!empty($at)) {
- foreach ($at['attr_list'] as $al) {
- if ($item['attr_id'] === $al['attr_id']) {
- $arr[$index]['list'][] = $at;
- }
- }
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'attr' => $attr,
- 'attr_group' => $arr,
- 'title' => $title
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function setPrice()
- {
- try {
- $goods = MdGoods::find()->where(['md_id' => $this->md_id])->andWhere(['goods_id' => $this->id])->one();
- $baseGoods = Goods::findOne($this->id);
- if (empty($goods) || empty($baseGoods)) {
- throw new \Exception("获取产品信息失败");
- }
- //判断是否存在以及开启规格
- if ($baseGoods->use_attr == 1 && !empty($this->attr)) {
- // 传过来的规格信息
- $attr = $this->attr;
- $goodsAttr = json_decode($goods['attr'], TRUE);
- $goodsNum = 0;
- foreach ($attr as $item) {
- // 去匹配已经存在的规格信息
- $attrIds = array_column($item['attr_list'], 'attr_id');
- sort($attrIds);
- foreach ($goodsAttr as &$goodsAttrItem) {
- $goodsAttrId = array_column($goodsAttrItem['attr_list'], 'attr_id');
- sort($goodsAttrId);
- if (!array_diff($attrIds, $goodsAttrId)) {
- $goodsAttrItem['price'] = $item['price'];
- $goodsAttrItem['num'] = $item['num'];
- $goodsNum += $goodsAttrItem['num'];
- }
- }
- }
- $goods->attr = json_encode($goodsAttr);
- // 同步最新的库存
- $goods->goods_num = $goodsNum;
- // 同步最新的价格
- $goods->price = min(array_column($goodsAttr, 'price'));
- } elseif ($baseGoods->use_attr == 0) {
- $attr = json_decode($goods->attr, true);
- $attr[0]['price'] = $this->goods_price;
- $attr[0]['num'] = $this->goods_num;
- $goods->attr = json_encode($attr);
- $goods->price = $this->goods_price;
- //如果库存不足,则下架处理
- if ($this->goods_num == 0) {
- $goods->status = 0;
- }
- }
- if (!$goods->save()) {
- throw new \Exception('数据保存错误' . json_encode($goods->errors));
- }
- return [
- 'code' => 0,
- 'msg' => '修改成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|