| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace addons\QiJuhe\common\models;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\goods\Goods;
- use Yii;
- /**
- * This is the model class for table "{{%addons_qijuhe_storage}}".
- *
- * @property int $id
- * @property int|null $mall_id
- * @property int|null $supply_id
- * @property int|null $goods_id
- * @property int|null $middle_goods_id
- * @property int|null $is_delete
- * @property int|null $created_at
- * @property int|null $updated_at
- * @property int|null $is_import 是否添加到了系统商品库 1导入中 2导入成功
- * @property int|null $mall_goods_id 对应的系统商品id
- * @property int|null $import_at 导入时间
- * @property string|null $import_msg 导入错误信息
- */
- class StorageModel extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_qijuhe_storage}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'supply_id', 'middle_goods_id', 'goods_id', 'is_delete', 'created_at', 'updated_at', 'is_import', 'mall_goods_id', 'batch_no', 'import_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'supply_id' => 'Supply ID',
- 'goods_id' => 'Goods ID',
- 'middle_goods_id' => 'middle goods id',
- 'is_delete' => 'Is Delete',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'is_import' => 'Is Import',
- 'mall_goods_id' => 'Mall Goods ID',
- 'import_at' => 'Import At',
- 'import_msg' => 'Import Msg',
- 'batch_no' => '批次好',
- ];
- }
- public function goods()
- {
- return $this->hasOne(GoodsModel::class, ['id' => 'goods_id']);
- }
- /**
- * 批量插入员工
- * @desc
- *
- * @param $user_list
- *
- * @return int
- * @throws \yii\db\Exception
- */
- public static function batchAdd($data)
- {
- $field = array_keys($data[0]);
- $value = [];
- foreach ($data as $item) {
- $value[] = array_values($item);
- }
- $result = \Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $value)->execute();
- return $result;
- }
- public static function getGoodsIds($ids)
- {
- return self::find()->select('id,middle_goods_id')->where(['id' => $ids])->asArray()->all();
- }
- public static function findGoodsIds($mall_id, $supply_id, $goods_ids)
- {
- return self::find()->select('middle_goods_id')->where(['mall_id' => $mall_id, 'supply_id' => $supply_id, 'middle_goods_id' => $goods_ids])->asArray()->column();
- }
- public static function byGoodsIdFindStorageId($goods_ids)
- {
- return self::find()->select('id')->where([
- 'middle_goods_id' => $goods_ids,
- ])->asArray()->column();
- }
- /**
- * @PostMapping("updateImportStatus")
- * @Middlewares({
- * @Middleware()
- * })
- * @param array $ids
- * @param int $status 0 未导入 1导入中 2导入成功
- * @param string $msg 导入错误信息
- */
- public static function updateImportStatus(array $ids, $status = 1, $msg = '')
- {
- return self::updateAll(['is_import' => $status, 'import_msg' => $msg], ['id' => $ids]);
- }
- public static function remove(array $ids, bool $del_goods = true)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- if ($del_goods) {
- $goods_ids = self::find()->where(['id' => $ids])->select('mall_goods_id')->column();
- if (!empty($goods_ids)) {
- $goods_ids = array_filter($goods_ids, function ($val) {
- return !empty($val);
- });
- if (!empty($goods_ids)) Goods::updateAll(['status' => StatusEnum::DELETE], ['id' => $goods_ids]);
- }
- }
- self::deleteAll(['id' => $ids]);
- // 移除商城商品
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- }
- /**
- * 获取该供应链下选品数量
- *
- * @param integer $mall_id
- * @param integer $supply_id
- * @param boolean $is_import
- * @return int
- */
- public static function getStorageNumBySupplyId(int $mall_id, int $supply_id, bool $is_import = true)
- {
- return static::find()->where([
- 'mall_id' => $mall_id,
- 'supply_id' => $supply_id,
- 'is_import' => 2,
- ])->count();
- }
- /**
- * 获取中台商品ID
- *
- * @param integer $mall_id
- * @param integer $supply_id
- * @param integer $limit
- * @param integer $page
- * @return array
- */
- public static function getMiddleGoodsIdsBySupplyId(int $mall_id, int $supply_id)
- {
- return static::find()
- ->alias('storage')
- ->leftJoin([
- 'goods' => GoodsModel::tableName()
- ], 'goods.id = storage.goods_id')
- ->where([
- 'storage.mall_id' => $mall_id,
- 'storage.supply_id' => $supply_id,
- 'storage.is_import' => 2,
- 'goods.is_display' => 1,
- ])
- ->select('storage.middle_goods_id')
- ->column();
- }
- /**
- * 通过中台商品ids获取主商城商品ids
- *
- * @param array $ids
- * @return array
- */
- public static function getMallGoodIdsByMiddleGoodsIds(array $ids)
- {
- return static::find()
- ->where(['middle_goods_id' => $ids])
- ->select('mall_goods_id')
- ->column();
- }
- /**
- * 标记删除
- *
- * @param integer $mall_id
- * @param array $goods_ids
- * @return boolean
- */
- public static function changeStatusByMidGoodsIds(int $mall_id, array $goods_ids, int $status = 3)
- {
- return self::updateAll(['is_import' => $status], ['middle_goods_id' => $goods_ids, 'mall_id' => $mall_id]);
- }
- /**
- * 获取主商城商品id
- *
- * @param integer $mall_id
- * @param integer|array $goods_ids
- * @return array
- */
- public static function getMallGoodsIds(int $mall_id, $goods_ids)
- {
- return static::find()
- ->where(['mall_id' => $mall_id, 'middle_goods_id' => $goods_ids])
- ->select('mall_goods_id')
- ->column();
- }
- }
|