StorageModel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace addons\QiJuhe\common\models;
  3. use common\enums\StatusEnum;
  4. use common\helpers\FormatHelper;
  5. use common\models\goods\Goods;
  6. use Yii;
  7. /**
  8. * This is the model class for table "{{%addons_qijuhe_storage}}".
  9. *
  10. * @property int $id
  11. * @property int|null $mall_id
  12. * @property int|null $supply_id
  13. * @property int|null $goods_id
  14. * @property int|null $middle_goods_id
  15. * @property int|null $is_delete
  16. * @property int|null $created_at
  17. * @property int|null $updated_at
  18. * @property int|null $is_import 是否添加到了系统商品库 1导入中 2导入成功
  19. * @property int|null $mall_goods_id 对应的系统商品id
  20. * @property int|null $import_at 导入时间
  21. * @property string|null $import_msg 导入错误信息
  22. */
  23. class StorageModel extends \yii\db\ActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%addons_qijuhe_storage}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['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'],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'mall_id' => 'Mall ID',
  49. 'supply_id' => 'Supply ID',
  50. 'goods_id' => 'Goods ID',
  51. 'middle_goods_id' => 'middle goods id',
  52. 'is_delete' => 'Is Delete',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. 'is_import' => 'Is Import',
  56. 'mall_goods_id' => 'Mall Goods ID',
  57. 'import_at' => 'Import At',
  58. 'import_msg' => 'Import Msg',
  59. 'batch_no' => '批次好',
  60. ];
  61. }
  62. public function goods()
  63. {
  64. return $this->hasOne(GoodsModel::class, ['id' => 'goods_id']);
  65. }
  66. /**
  67. * 批量插入员工
  68. * @desc
  69. *
  70. * @param $user_list
  71. *
  72. * @return int
  73. * @throws \yii\db\Exception
  74. */
  75. public static function batchAdd($data)
  76. {
  77. $field = array_keys($data[0]);
  78. $value = [];
  79. foreach ($data as $item) {
  80. $value[] = array_values($item);
  81. }
  82. $result = \Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $value)->execute();
  83. return $result;
  84. }
  85. public static function getGoodsIds($ids)
  86. {
  87. return self::find()->select('id,middle_goods_id')->where(['id' => $ids])->asArray()->all();
  88. }
  89. public static function findGoodsIds($mall_id, $supply_id, $goods_ids)
  90. {
  91. return self::find()->select('middle_goods_id')->where(['mall_id' => $mall_id, 'supply_id' => $supply_id, 'middle_goods_id' => $goods_ids])->asArray()->column();
  92. }
  93. public static function byGoodsIdFindStorageId($goods_ids)
  94. {
  95. return self::find()->select('id')->where([
  96. 'middle_goods_id' => $goods_ids,
  97. ])->asArray()->column();
  98. }
  99. /**
  100. * @PostMapping("updateImportStatus")
  101. * @Middlewares({
  102. * @Middleware()
  103. * })
  104. * @param array $ids
  105. * @param int $status 0 未导入 1导入中 2导入成功
  106. * @param string $msg 导入错误信息
  107. */
  108. public static function updateImportStatus(array $ids, $status = 1, $msg = '')
  109. {
  110. return self::updateAll(['is_import' => $status, 'import_msg' => $msg], ['id' => $ids]);
  111. }
  112. public static function remove(array $ids, bool $del_goods = true)
  113. {
  114. $t = Yii::$app->db->beginTransaction();
  115. try {
  116. if ($del_goods) {
  117. $goods_ids = self::find()->where(['id' => $ids])->select('mall_goods_id')->column();
  118. if (!empty($goods_ids)) {
  119. $goods_ids = array_filter($goods_ids, function ($val) {
  120. return !empty($val);
  121. });
  122. if (!empty($goods_ids)) Goods::updateAll(['status' => StatusEnum::DELETE], ['id' => $goods_ids]);
  123. }
  124. }
  125. self::deleteAll(['id' => $ids]);
  126. // 移除商城商品
  127. $t->commit();
  128. return true;
  129. } catch (\Exception $e) {
  130. $t->rollBack();
  131. \Yii::error(FormatHelper::exception($e, __METHOD__));
  132. return false;
  133. }
  134. }
  135. /**
  136. * 获取该供应链下选品数量
  137. *
  138. * @param integer $mall_id
  139. * @param integer $supply_id
  140. * @param boolean $is_import
  141. * @return int
  142. */
  143. public static function getStorageNumBySupplyId(int $mall_id, int $supply_id, bool $is_import = true)
  144. {
  145. return static::find()->where([
  146. 'mall_id' => $mall_id,
  147. 'supply_id' => $supply_id,
  148. 'is_import' => 2,
  149. ])->count();
  150. }
  151. /**
  152. * 获取中台商品ID
  153. *
  154. * @param integer $mall_id
  155. * @param integer $supply_id
  156. * @param integer $limit
  157. * @param integer $page
  158. * @return array
  159. */
  160. public static function getMiddleGoodsIdsBySupplyId(int $mall_id, int $supply_id)
  161. {
  162. return static::find()
  163. ->alias('storage')
  164. ->leftJoin([
  165. 'goods' => GoodsModel::tableName()
  166. ], 'goods.id = storage.goods_id')
  167. ->where([
  168. 'storage.mall_id' => $mall_id,
  169. 'storage.supply_id' => $supply_id,
  170. 'storage.is_import' => 2,
  171. 'goods.is_display' => 1,
  172. ])
  173. ->select('storage.middle_goods_id')
  174. ->column();
  175. }
  176. /**
  177. * 通过中台商品ids获取主商城商品ids
  178. *
  179. * @param array $ids
  180. * @return array
  181. */
  182. public static function getMallGoodIdsByMiddleGoodsIds(array $ids)
  183. {
  184. return static::find()
  185. ->where(['middle_goods_id' => $ids])
  186. ->select('mall_goods_id')
  187. ->column();
  188. }
  189. /**
  190. * 标记删除
  191. *
  192. * @param integer $mall_id
  193. * @param array $goods_ids
  194. * @return boolean
  195. */
  196. public static function changeStatusByMidGoodsIds(int $mall_id, array $goods_ids, int $status = 3)
  197. {
  198. return self::updateAll(['is_import' => $status], ['middle_goods_id' => $goods_ids, 'mall_id' => $mall_id]);
  199. }
  200. /**
  201. * 获取主商城商品id
  202. *
  203. * @param integer $mall_id
  204. * @param integer|array $goods_ids
  205. * @return array
  206. */
  207. public static function getMallGoodsIds(int $mall_id, $goods_ids)
  208. {
  209. return static::find()
  210. ->where(['mall_id' => $mall_id, 'middle_goods_id' => $goods_ids])
  211. ->select('mall_goods_id')
  212. ->column();
  213. }
  214. }