Procházet zdrojové kódy

添加问卷导出 学生字段导出的设置

lrf402788946 před 5 roky
rodič
revize
70117dac02

+ 6 - 0
src/router/index.js

@@ -149,6 +149,12 @@ const system = [
     meta: { title: '问卷', sub: '管理' },
     component: () => import('@/views/questionnaire/index.vue'),
   },
+  {
+    path: '/questionnaire/export/setting',
+    name: 'questionnaire_export_setting',
+    meta: { title: '问卷导出设置' },
+    component: () => import('@/views/questionnaire/export-setting.vue'),
+  },
   {
     path: '/questionnaire/detail',
     name: 'questionnaire_detail',

+ 81 - 0
src/views/questionnaire/export-setting.vue

@@ -0,0 +1,81 @@
+<template>
+  <div id="export-setting">
+    <detail-frame :title="pageTitle">
+      <el-card header="请选择导出问卷时需要的学生信息">
+        <el-checkbox-group v-model="selected">
+          <el-row>
+            <el-col :span="4" v-for="(i, index) in list" :key="index" style="margin:10px 0px">
+              <el-checkbox :label="i.key">{{ i.zh }}</el-checkbox>
+            </el-col>
+          </el-row>
+        </el-checkbox-group>
+        <el-row type="flex" align="middle" justify="center" class="btn_bar">
+          <el-col :span="2">
+            <el-button type="primary" size="mini" @click="toSave">保存</el-button>
+          </el-col>
+        </el-row>
+      </el-card>
+    </detail-frame>
+  </div>
+</template>
+
+<script>
+import _ from 'lodash';
+import detailFrame from '@frame/layout/admin/detail-frame';
+import { mapState, createNamespacedHelpers } from 'vuex';
+const { mapActions: setting } = createNamespacedHelpers('setting');
+const { mapActions: util } = createNamespacedHelpers('util');
+export default {
+  name: 'export-setting',
+  props: {},
+  components: { detailFrame },
+  data: function() {
+    return {
+      selected: [],
+      list: [],
+      setting: {},
+    };
+  },
+  created() {
+    this.search();
+  },
+  methods: {
+    ...setting(['fetch', 'update']),
+    ...util(['findModel']),
+    async search() {
+      let res = await this.findModel('student');
+      if (this.$checkRes(res)) {
+        let keys = Object.keys(res.data).filter(f => f !== 'name'); //名字必须显示,在导出时候自带
+        let arr = keys.map(key => ({ key, zh: _.get(res.data[key], 'zh') }));
+        this.$set(this, `list`, arr);
+      }
+      let setr = await this.fetch();
+      if (this.$checkRes(setr)) {
+        this.$set(this, `setting`, setr.data);
+        let data = _.get(setr.data, 'ques_setting', []);
+        data = data.map(i => i.key);
+        this.$set(this, `selected`, data);
+      }
+    },
+    async toSave() {
+      let duplicate = _.cloneDeep(this.selected);
+      let ds = _.cloneDeep(this.setting);
+      let data = this.list.filter(f => duplicate.includes(f.key));
+      ds.ques_setting = data;
+      let res = await this.update(ds);
+      this.$checkRes(res, `修改成功`, res.errmsg);
+    },
+  },
+  computed: {
+    ...mapState(['user']),
+    pageTitle() {
+      return `${this.$route.meta.title}`;
+    },
+  },
+  metaInfo() {
+    return { title: this.$route.meta.title };
+  },
+};
+</script>
+
+<style lang="less" scoped></style>

+ 56 - 15
src/views/statistics/detail.vue

@@ -48,6 +48,8 @@ import detailFrame from '@frame/layout/admin/detail-frame';
 import { mapState, createNamespacedHelpers } from 'vuex';
 const { mapActions: questionnaire } = createNamespacedHelpers('questionnaire');
 const { mapActions: questionanswer } = createNamespacedHelpers('questionanswer');
+const { mapActions: setting } = createNamespacedHelpers('setting');
+const { mapActions: student } = createNamespacedHelpers('student');
 
 export default {
   name: 'detail',
@@ -69,6 +71,8 @@ export default {
   methods: {
     ...questionnaire(['fetch']),
     ...questionanswer(['query']),
+    ...setting({ getSetting: 'fetch' }),
+    ...student(['findList']),
     async search() {
       let res = await this.fetch(this.querys.questionnaireid);
       if (!this.$checkRes(res)) return;
@@ -151,25 +155,62 @@ export default {
       window.history.go(-1);
     },
     //导出excel数据
-    toExcel() {
+    async toExcel() {
+      let msg = this.$message({ message: '正在导出...', duration: 0 });
       const { export_json_to_excel } = require('@frame/excel/Export2Excel');
       let duplicate = _.cloneDeep(this.cdata);
-      let data = [];
-      for (const dup of duplicate) {
-        const elm = { studentname: dup.studentname };
-        for (const answer of dup.answers) {
-          elm[answer.questionid] = answer.answer;
+      try {
+        let studsi = await this.findList(duplicate.map(i => i.studentid));
+        if (!this.$checkRes(studsi)) {
+          msg.close();
+          this.$message.error('学生信息有误,请检查学生信息');
+          return;
         }
-        data.push(elm);
+        let studs = studsi.data;
+        //获取导出问卷选择导出的字段
+        let setr = await this.getSetting();
+        if (!setr) {
+          this.$message.error('没有找到默认设置信息,请查看是否填写默认设置');
+          return;
+        }
+        let obj = _.get(setr.data, 'ques_setting', []);
+        let arrzh = obj.map(i => i.zh);
+        let arr = obj.map(i => i.key);
+        let nd = duplicate.map(i => {
+          let sr = studs.find(f => f._id == i.studentid);
+          if (sr) {
+            let obj = _.pick(sr, arr);
+            i = { ...i, ...obj };
+          }
+          return i;
+        });
+        let data = [];
+        for (const dup of nd) {
+          const elm = { studentname: dup.studentname };
+          for (const key of arr) {
+            elm[key] = dup[key];
+          }
+          for (const answer of dup.answers) {
+            elm[answer.questionid] = answer.answer;
+          }
+          data.push(elm);
+        }
+        let dquest = _.cloneDeep(this.quest.question);
+        let qarr = dquest.map(i => ({ questionid: i._id, topic: i.topic }));
+        let tHeader = qarr.map(i => i.topic);
+        tHeader = arrzh.concat(tHeader);
+        tHeader.unshift('学生姓名');
+        let filterVal = qarr.map(i => i.questionid);
+        filterVal = arr.concat(filterVal);
+        filterVal.unshift('studentname');
+        data = data.map(v => filterVal.map(j => v[j]));
+        export_json_to_excel(tHeader, data, this.quest.name);
+        msg.close();
+        this.$message.success('导出成功');
+      } catch (error) {
+        msg.close();
+        this.$message.error('导出失败');
       }
-      let dquest = _.cloneDeep(this.quest.question);
-      let qarr = dquest.map(i => ({ questionid: i._id, topic: i.topic }));
-      let tHeader = qarr.map(i => i.topic);
-      tHeader.unshift('学生姓名');
-      let filterVal = qarr.map(i => i.questionid);
-      filterVal.unshift('studentname');
-      data = data.map(v => filterVal.map(j => v[j]));
-      export_json_to_excel(tHeader, data, this.quest.name);
     },
   },
   computed: {