store_id; $status = $this->status; $goods_name = $this->goods_name; $goods_id = $this->goods_id; $nickname = $this->nickname; $tel = $this->tel; $query = FormData::find()->alias('f')->where(['f.store_id' => $store_id, 'f.is_delete' => 0]) ->leftJoin(['g' => Goods::tableName()], 'f.goods_id = g.id') ->leftJoin(['su' => SaasUser::tableName()], 'f.saas_user_id = su.id'); $query->andWhere(['>', 'f.goods_id', 0]); if (!is_null($status) && $status >= 0) { $query->andWhere(['f.status' => $status]); } if (is_null($status)) { $query->andWhere(['f.status' => FormData::STATUS_VALID]); } if ($goods_id) { if(is_array($goods_id)){ $goods_id = explode(',', $goods_id); } $query->andWhere(['f.goods_id' => $goods_id]); } if ($goods_name) { $query->andWhere(['LIKE', 'g.name', $goods_name]); } if ($nickname) { $query->andWhere(['LIKE', 'su.name', $nickname]); } if ($tel) { $query->andWhere(['LIKE', 'su.mobile', $tel]); } if($this->begin){ $query->andWhere(['>=', 'f.created_at', strtotime($this->begin)]); } if($this->end){ $query->andWhere(['<=', 'f.created_at', strtotime($this->end)]); } $query->select('f.*, su.name nickname, su.avatar, su.mobile, g.name goods_name, g.cover_pic goods_cover_pic') ->orderBy('f.id desc'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['form'] = json_decode($item['form'], true); $item['status'] = (int)$item['status']; $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); } if($this->export){ return $this->export($list['list']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $list, 's' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } private function export($list) { $rows = [[ 'ID', '会员信息', '手机号', '商品', '时间', '表单', '表单值', ]]; foreach($list as $item){ if(empty($item['form']) || empty($item['form']['list'])){ continue; } foreach ($item['form']['list'] as $i => $fitem) { $fitemVal = $fitem['default']; if($fitem['type'] == 'uploadImg'){ $fitemVal = '
'; } if(is_array($fitemVal)){ $fitemVal = implode(',', $fitemVal); } if($i == 0){ $r = [ $item['id'], $item['nickname'], $item['mobile'], $item['goods_name'], $item['created_at'], $item['form']['name'], '', ]; $rows[] = $r; } $r = [ '', '', '', '', '', $fitem['name'], $fitemVal, ]; $rows[] = $r; } } $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow() ->addRows($rows)->toBrowser(); } public function del() { $t = \Yii::$app->db->beginTransaction(); try { $id = is_array($this->id) ? $this->id : explode(',', $this->id); FormData::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'id' => $id, 'is_delete' => 0]); $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }