| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace addons\CloudStock\common\models;
- use addons\CloudStock\common\enums\CloudStockEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\user\User;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\models\common\Download;
- use Yii;
- /**
- * This is the model class for table "{{%addons_cloud_stock_deduction_detail}}".
- *
- * @property int $id 自增id
- * @property int $mall_id 商城id
- * @property int $goods_id 商品id
- * @property int $changes_type 库存变动类型;200会员买货;201给下级配货;202下级向自己补货;203自提货到线下;204系统扣减;
- * @property int $order_id 订单id
- * @property int $order_type order类型,1:会员购买订单,2:补货订单;
- * @property int $not_yet_num 剩余未扣库存数
- * @property int $num 扣库存数
- * @property int $stock_log_id qimall_addons_cloud_stock_goods_stock_log表的id
- * @property int $user_id 扣减代理商用户user_id;0为平台
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class CloudStockDeductionDetail extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_deduction_detail}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'changes_type', 'order_id', 'order_type', 'not_yet_num', 'num', 'stock_log_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['goods_id', 'changes_type', 'stock_log_id', 'user_id'], 'required'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '自增id',
- 'mall_id' => '商城id',
- 'goods_id' => '商品id',
- 'changes_type' => '库存变动类型;200会员买货;201给下级配货;202下级向自己补货;203自提货到线下;204系统扣减;',
- 'order_id' => '订单id',
- 'order_type' => 'order类型,1:会员购买订单,2:补货订单;',
- 'not_yet_num' => '剩余未扣库存数',
- 'num' => '扣库存数',
- 'stock_log_id' => 'qimall_addons_cloud_stock_goods_stock_log表的id',
- 'user_id' => '扣减代理商用户user_id;0为平台',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id'])->select(['id','mobile','nickname','avatar_url']);
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
- }
- public static function setData(array $data)
- {
- if (!empty($data['id'])) {
- $model = self::findOne(['status' => StatusEnum::ENABLED, 'id' => $data['id']]);
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- foreach ($data as $key => $val) {
- $model->$key = $val;
- }
- // dd($model);
- if (!$model->save()) {
- self::$static_error = '保存数据失败:' . $model->getErrorMessage();
- return false;
- }
- return $model;
- }
- /**
- * 导出字段数组
- * @Author: hua
- * @DateTime: 2021/10/11 18:06
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return \string[][]
- */
- public static function fieldsLists($field = null){
- $fields = [
- 'agent_user_id' => '代理商用户id',
- 'agent_user_nickname' => '代理商',
- 'goods_id' => '商品id',
- 'goods_name' => '商品名称',
- 'num' => '扣减库存数',
- 'changes_type' => '扣减库存类型',
- 'order_id' => '订单id',
- 'created_at' => '时间',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- /**
- * 导出
- */
- public static function exportOrder(array $data) {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $fields = isset($params['fields']) ? $params['fields'] : null;
- $exportField = self::fieldsLists($fields);
- $exportField = array_keys($exportField);
- $where[] = ['=', 'mall_id', $params['mall_id']];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- if (!empty($params['keyword'])) {
- $user_list = User::find()->where(['or',
- ['id' => $params['keyword']],
- ['like', 'mobile', $params['keyword']],
- ['like', 'nickname', $params['keyword']]])->select('id')->asArray()->all();
- $user_id_arr = array_column($user_list, 'id');
- $where[] = ['user_id' => $user_id_arr];
- } else {
- if ($params['keyword'] != '') {
- $where[] = ['user_id' => 0];
- }
- }
- if (isset($params['mobile']) && $params['mobile'] !== '') {
- $user_id_arr = User::find()
- ->where([
- 'mall_id' => $params['mall_id'],
- 'status' => StatusEnum::ENABLED
- ])
- ->andWhere(['like', 'mobile', $params['mobile']])
- ->select('id')
- ->column();
- $where[] = ['user_id' => $user_id_arr];
- }
- if (!empty($params['goods_name'])) {
- $goods_list = Goods::find()->where([
- 'like', 'goods_name', $params['goods_name']])->select('id')->asArray()->all();
- $goods_list_arr = array_column($goods_list, 'id');
- $where[] = ['goods_id' => $goods_list_arr];
- }
- if (!empty($params['order_id'])) $where[] = ['order_id' => $params['order_id']];
- if (!empty($params['changes_type'])) $where[] = ['changes_type' => $params['changes_type']];
- if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'] . ' 00:00:00')];
- if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'] . ' 00:00:00')];
- if(!empty($params['order_no'])){
- $buying_order_list = CloudStockBuyingOrder::find()
- ->alias('c')
- ->leftJoin(['order'=>Order::tableName()],'order.id = c.order_id')
- ->where(['order.order_no'=>$params['order_no']])
- ->select('c.id,c.order_id')->all();
- $buying_order_ids = array_column($buying_order_list,'id');
- $order_ids = array_column($buying_order_list,'order_id');
- $fill_order_ids = CloudStockFillOrder::find()->where(['order_no'=>$params['order_no']])->select('id')->column();
- $order_ids = array_merge($buying_order_ids,$order_ids,$fill_order_ids);
- $where[] = ['order_id' => $order_ids];
- }
- $wheres['order'] = 'id desc';
- $wheres['with'] = ['user', 'goods'];
- $wheres['where'] = $where;
- $model = self::find();
- self::paramPack($wheres, $model);
- $list = [];
- $exportheader = [];
- $isSetHeader = true;
- //所有渠道部分修改
- $i = 1;
- $page_size = 1000;
- $change_types = CloudStockEnum::getStockChangeTypeMap();
- while ($log_list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()){
- foreach ($log_list as $l){
- $temp = [];
- //按顺序组装
- if(in_array('id', $fields)) {
- $temp['id'] = $l['id'];
- $isSetHeader && array_push($exportheader, '订单ID');
- }
- if(in_array('agent_user_id', $fields)) {
- $temp['agent_user_id'] = $l['user_id'];
- $isSetHeader && array_push($exportheader, '代理商用户id');
- }
- if(in_array('agent_user_nickname', $fields)) {
- $temp['agent_user_nickname'] = $l['user']['nickname'];
- $isSetHeader && array_push($exportheader, '代理商用户昵称');
- }
- if(in_array('goods_id', $fields)) {
- $temp['goods_id'] = $l['goods_id'];
- $isSetHeader && array_push($exportheader, '商品id');
- }
- if(in_array('goods_name', $fields)) {
- $temp['goods_name'] = $l['goods']['goods_name'];
- $isSetHeader && array_push($exportheader, '商品名称');
- }
- if(in_array('num', $fields)) {
- $temp['num'] = $l['num'];
- $isSetHeader && array_push($exportheader, '扣减库存数');
- }
- if(in_array('changes_type', $fields)) {
- $temp['changes_type'] = $change_types[$l['changes_type']] ?? '';
- $isSetHeader && array_push($exportheader, '扣减库存类型');
- }
- if(in_array('order_id', $fields)) {
- $temp['order_id'] = $l['order_id'];
- $isSetHeader && array_push($exportheader, '订单id');
- }
- if(in_array('created_at', $fields)) {
- $temp['created_at'] = date('Y-m-d H:i:s', $l['created_at']);
- $isSetHeader && array_push($exportheader, '时间');
- }
- $list[] = $temp;
- $isSetHeader = false;
- }
- $i++;
- }
- if (!empty($list)) {
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $exportheader;
- $csv->data = $list;
- $csv->export();
- $csv->updateDown($download, $params['mall_id']);
- }
- }catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- var_dump($e->getMessage().$e->getFile().$e->getLine());
- self::setStaticError($e->getMessage());
- return $e->getMessage();
- }
- }
- }
- public static function fixOrderType()
- {
- $i = 1;
- $limit = 1000;
- while (true) {
- $list = self::find()->where(['order_type' => 1])
- ->offset(($i - 1) * $limit)->limit($limit)->all();
- if (empty($list)) {
- break;
- }
- $i++;
- foreach ($list as $item) {
- $model = CloudStockGoodsStockLog::findOne(['id' => $item['stock_log_id']]);
- if (!empty($model)) {
- $item->order_type = $model->order_type;
- $res = $item->save();
- var_dump($res);
- }
- }
- }
- }
- }
|