祖安之光
2025-10-21 f3c73a86f00061125e1946125c2f7499064a5708
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<template>
  <div class="app-container">
    <div style="display: flex;justify-content: space-between">
      <el-form :inline="true" style="display: flex;align-items: center;flex-wrap: wrap;" >
        <el-form-item>
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="openDialog('add',{})"
              v-hasPermi="['inventoryRecord:add']"
          >新增</el-button>
        </el-form-item>
        <el-form-item v-if="isAdmin" label="企业:" >
          <el-select v-model="data.queryParams.companyId" placeholder="请选择" clearable>
            <el-option
                v-for="item in companyList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item >
          <el-button v-if="isAdmin" type="primary" @click="getList">查询</el-button>
          <el-button v-if="isAdmin" type="primary" plain @click="reset">重置</el-button>
<!--          <el-button type="primary">导出</el-button>-->
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="dataList" :border="true">
      <el-table-column type="index" label="序号" width="80"/>
      <el-table-column prop="recordName" align="center" label="材料名称"/>
      <el-table-column prop="inventory" align="center" label="当前库存"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openDialog('view',scope.row)">查看</el-button>
          <el-button link type="primary" @click="openDialog('edit',scope.row)" v-hasPermi="['inventoryRecord:edit']">编辑</el-button>
          <el-button link type="primary" @click="downloadFile(scope.row)">导出</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['inventoryRecord:del']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
 
    <edit-dialog ref="dialogRef" @getList=getList></edit-dialog>
  </div>
</template>
 
<script setup>
import {getCurrentInstance, onMounted, onUnmounted, reactive, ref, toRefs} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {delCompany, getCompany} from "@/api/onlineEducation/company";
import Cookies from "js-cookie";
import editDialog from './components/editDialog.vue'
import useUserStore from "@/store/modules/user";
import { utils, writeFile } from 'xlsx-js-style';
import {
  delWarehousingRecord,
  getWarehousingRecordDetail,
  getWarehousingRecordDetailList
} from "@/api/outsourcingCooperate/outsourcingCooperate";
 
 
const userStore = useUserStore()
const { proxy } = getCurrentInstance();
const loading = ref(false);
const dialogRef = ref();
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyId: null
  },
  total: 0,
  dataList: [],
  companyList: [],
  isAdmin: false,
});
 
const { queryParams, total, dataList,companyList, isAdmin } = toRefs(data);
const userInfo = ref()
onMounted(async ()=>{
  if(userStore.roles.includes('admin')){
    data.isAdmin = true
    await getCompanyList()
  }else{
    data.isAdmin = false
    data.queryParams.companyId = userStore.companyId
  }
  await getList()
})
 
onUnmounted(()=>{
 
})
 
const getList = async () => {
  loading.value = true
  const res = await getWarehousingRecordDetailList(data.queryParams)
  if(res.code == 200){
    data.dataList = res.data.list || []
    data.total = res.data.total
  }else{
    ElMessage.warning(res.message)
  }
  loading.value = false
}
 
const getCompanyList = async ()=>{
  const queryParams = {
    pageNum: 1,
    pageSize: 999
  }
  const res = await getCompany(queryParams)
  if (res.code == 200) {
    data.companyList = res.data.list?res.data.list:[]
    // data.queryParams.companyId = data.companyList[0].id
  } else {
    ElMessage.warning(res.message)
  }
}
 
const downloadFile = async (val) => {
  const res = await getWarehousingRecordDetail({ id: val.id })
  if (res.code == 200) {
    if (res.data) {
      const jsonData = res.data
 
      // 列定义
      const columns = [
        { label: '序号', key: 'index' },
        { label: '登记时间', key: 'boardingTime' },
        { label: '出入库', key: 'recordType' },
        { label: '规格', key: 'specification' },
        { label: '单位', key: 'materialUnit' },
        { label: '数量', key: 'num' },
        { label: '经手人', key: 'createBy' },
        { label: '备注', key: 'remark' }
      ];
 
      // 样式定义
      const headerStyle = {
        font: { bold: true, sz: 12, color: { rgb: "000000" } },
        fill: { fgColor: { rgb: "CCCCCC" } },
        alignment: { horizontal: "center", vertical: "center" },
        border: {
          top: { style: "thin", color: { rgb: "000000" } },
          bottom: { style: "thin", color: { rgb: "000000" } },
          left: { style: "thin", color: { rgb: "000000" } },
          right: { style: "thin", color: { rgb: "000000" } }
        }
      };
 
      const dataStyle = {
        font: { sz: 11 },
        alignment: { vertical: "center" },
        border: {
          top: { style: "thin", color: { rgb: "000000" } },
          bottom: { style: "thin", color: { rgb: "000000" } },
          left: { style: "thin", color: { rgb: "000000" } },
          right: { style: "thin", color: { rgb: "000000" } }
        }
      };
 
      const titleStyle = {
        font: { bold: true, sz: 16, color: { rgb: "2C3E50" } },
        alignment: { horizontal: "center", vertical: "center" }
      };
 
      const summaryStyle = {
        font: { bold: true, sz: 12, color: { rgb: "000000" } },
        alignment: { horizontal: "left", vertical: "center" },
        border: {
          top: { style: "thin", color: { rgb: "000000" } },
          bottom: { style: "thin", color: { rgb: "000000" } },
          left: { style: "thin", color: { rgb: "000000" } },
          right: { style: "thin", color: { rgb: "000000" } }
        }
      };
 
      // 处理数据,添加序号和转换recordType
      const processData = () => {
        return jsonData.warehousingRecordDetails.map((item, index) => {
          // 转换recordType:1->入库,2->出库
          const recordTypeText = item.recordType === 1 ? '入库' : item.recordType === 2 ? '出库' : '';
 
          return {
            index: index + 1,
            boardingTime: item.boardingTime,
            recordType: recordTypeText,
            specification: item.specification,
            materialUnit: item.materialUnit,
            num: item.num,
            createBy: item.createBy,
            remark: item.remark
          };
        });
      };
 
      // 创建工作簿
      const wb = utils.book_new();
      const wsData = [];
 
      // 添加标题行(合并所有列)
      const titleRow = [{
        v: jsonData.recordName,
        t: 's',
        s: titleStyle
      }];
      // 填充空单元格以匹配列数
      for (let i = 1; i < columns.length; i++) {
        titleRow.push({ v: '', t: 's', s: titleStyle });
      }
      wsData.push(titleRow);
 
      // 添加表头行
      const headerRow = columns.map(col => ({
        v: col.label,
        t: 's',
        s: headerStyle
      }));
      wsData.push(headerRow);
 
      // 添加数据行
      const processedData = processData();
      processedData.forEach(item => {
        const row = columns.map(col => {
          const value = item[col.key];
          return {
            v: value,
            t: typeof value === 'number' ? 'n' : 's',
            s: dataStyle
          };
        });
        wsData.push(row);
      });
 
      // 添加汇总行(当前库存)
      const summaryRow = [
        { v: `当前库存:${jsonData.inventory}`, t: 's', s: summaryStyle }
      ];
      wsData.push(summaryRow);
 
      // 创建工作表
      const ws = utils.aoa_to_sheet(wsData);
 
      // 设置合并单元格(只保留标题行合并)
      const merges = [
        // 标题行合并
        { s: { r: 0, c: 0 }, e: { r: 0, c: columns.length - 1 } },
        //库存行合并
        { s: { r: wsData.length - 1, c: 0 }, e: { r: wsData.length - 1, c: columns.length - 1 } }
      ];
      ws['!merges'] = merges;
 
      // 设置列宽
      const colWidths = [
        { wch: 8 },   // 序号
        { wch: 20 },  // 登记时间
        { wch: 10 },  // 出入库
        { wch: 15 },  // 规格
        { wch: 10 },  // 单位
        { wch: 12 },  // 数量
        { wch: 15 },  // 经手人
        { wch: 20 }   // 备注
      ];
      ws['!cols'] = colWidths;
 
      // 添加工作表到工作簿
      utils.book_append_sheet(wb, ws, jsonData.recordName);
 
      // 生成Excel文件并下载
      writeFile(wb, `${jsonData.recordName}.xlsx`);
    } else {
      ElMessage.warning('暂无数据')
    }
  } else {
    ElMessage.warning(res.message)
  }
}
 
const openDialog = (type, value) => {
  dialogRef.value.openDialog(type, value, data.queryParams.companyId, data.isAdmin, data.companyList);
}
 
/** 重置新增的表单以及其他数据  */
const reset= async()=> {
  data.queryParams = {
    pageNum: 1,
    pageSize: 10,
    companyId: null
  }
  await getCompanyList()
  await getList()
}
const handleDelete = (val) => {
  ElMessageBox.confirm(
      '确定删除此条数据?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then( async() => {
        const res = await delWarehousingRecord({id: val.id})
        if(res.code == 200){
          ElMessage.success('数据删除成功')
          await getList()
        }else{
          ElMessage.warning(res.message)
        }
      })
}
 
</script>