WorkOrder.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <script setup>
  2. import { ref, unref, computed, provide, onUnmounted } from 'vue';
  3. import { useMessage, NDatePicker, NTabs, NTab, NRadioGroup, NRadio, NCheckboxGroup, NCheckbox, NDataTable } from 'naive-ui';
  4. import { BaseButton, RecodeCardItem, TheSubMenu, TheChatView, ChatWelcome, SvgIcon } from '@/components';
  5. import { ChatAsk, ChatAnswer } from '@/components/Chat';
  6. import { orderApi } from "@/api/order";
  7. import { chatApi } from '@/api/chat';
  8. import { formatEchart, isNumberComprehensive } from '@/utils/format';
  9. import { ORDER_OPTION_ENUM } from '@/utils/enum';
  10. import { getOrderAreaOptions } from './config/echartOptions'
  11. import * as echarts from 'echarts';
  12. import dayjs from 'dayjs';
  13. import { useInfinite, useScroll, useChat } from '@/composables';
  14. const { recordList, isFetching, onScrolltolower, onReset } = useInfinite('/front/bigModel/qa/pageList', { module: 1 });
  15. const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom, scrollToTop } = useScroll();
  16. const { chatDataSource, addChat, updateChat, clearChat, updateById } = useChat();
  17. let controller = new AbortController();
  18. let timer = null;
  19. const ANSWER_ID_KEY = '@@id@@';
  20. const chartInstance = {};
  21. const message = useMessage();
  22. const reportDate = ref();
  23. const workOrderParams = ref({
  24. timeBegin: null,
  25. timeEnd: null,
  26. whichWay: 1,
  27. checkGroup: [],
  28. })
  29. const switchActive = ref(false);
  30. const tabActive = ref("daily");
  31. const isLoading = ref(false);
  32. const recordActive = ref(null);
  33. const currenSessionId = ref(null);
  34. const catalogData = ref([]);
  35. const isExistInHistory = computed(() => (recordList.value.findIndex(({ sessionId: sId }) => sId === unref(currenSessionId)) === -1));
  36. const isChart = computed(() => tabActive.value == 'customDaily');
  37. const chatDataSourceItem = computed(() => unref(chatDataSource)[chatDataSource.value.length - 1]);
  38. // 目录版块
  39. provide('updateCatalog', (val) => {
  40. catalogData.value = val;
  41. })
  42. const resetFormData = () => {
  43. workOrderParams.value = {
  44. timeBegin: null,
  45. timeEnd: null,
  46. whichWay: 1,
  47. checkGroup: [],
  48. }
  49. }
  50. const resetState = () => {
  51. tabActive.value = 'daily';
  52. currenSessionId.value = null;
  53. recordActive.value = null;
  54. reportDate.value = null;
  55. resetFormData();
  56. clearChat();
  57. }
  58. // 新建对话
  59. const handleCreateDialog = async () => {
  60. message.destroyAll();
  61. if (unref(isLoading)) {
  62. return message.warning('当前对话生成中');
  63. }
  64. if (!unref(chatDataSource).length) {
  65. return message.info('已切换最新会话');
  66. }
  67. catalogData.value = [];
  68. resetState();
  69. }
  70. // 查询对话详情
  71. const handleChatDetail = async ({ sessionId }) => {
  72. isLoading.value = false;
  73. controller.abort();
  74. if(currenSessionId.value === sessionId) return;
  75. recordActive.value = sessionId;
  76. const { data } = await chatApi.getAnswerHistoryDetail({ sessionId });
  77. let echartData = [];
  78. let whichWay = null;
  79. chatDataSource.value = data.map(item => {
  80. if ( item.remark ) {
  81. const remark = JSON.parse(item.remark);
  82. whichWay = remark.whichWay;
  83. echartData = item.echartWithTableData = formatData(remark);
  84. }
  85. return { ...item, loading: false };
  86. })
  87. echartData.length && createEchart(echartData, whichWay);
  88. currenSessionId.value = sessionId;
  89. resetFormData();
  90. scrollToTop();
  91. }
  92. // 请求
  93. const onRegenerate = async (question, options) => {
  94. controller = new AbortController();
  95. const sessionId = unref(currenSessionId);
  96. const params = {
  97. data: {
  98. sessionId,
  99. showVal: question,
  100. question,
  101. module: 1,
  102. isStrong: Number(unref(switchActive)),
  103. reportDate: reportDate.value,
  104. ...options,
  105. },
  106. signal: controller.signal,
  107. onDownloadProgress: ({ event }) => {
  108. const xhr = event.target;
  109. const { responseText } = xhr;
  110. const [answer] = responseText.split(ANSWER_ID_KEY);
  111. updateChat({
  112. ...chatDataSourceItem.value,
  113. sessionId,
  114. showVal: question,
  115. answer,
  116. loading: true,
  117. delayLoading: false,
  118. innerLoading: false
  119. })
  120. scrollToBottomIfAtBottom();
  121. }
  122. }
  123. try {
  124. const { data } = await chatApi.getChatStream(params);
  125. const [answer, id] = data.split(ANSWER_ID_KEY);
  126. updateChat({
  127. ...chatDataSourceItem.value,
  128. id,
  129. sessionId,
  130. showVal: question,
  131. answer,
  132. loading: false,
  133. delayLoading: false,
  134. innerLoading: false
  135. })
  136. scrollToBottomIfAtBottom();
  137. }
  138. catch (error) {
  139. console.log("取消了请求 - catch", error);
  140. }
  141. finally {
  142. isLoading.value = false;
  143. onReset();
  144. }
  145. }
  146. // 提交问题
  147. const handleSubmit = async (question, params) => {
  148. if (unref(isExistInHistory)) {
  149. const { data: sessionId } = await chatApi.getChatSessionTag();
  150. currenSessionId.value = sessionId;
  151. }
  152. isLoading.value = true;
  153. const option = {
  154. sessionId: unref(currenSessionId),
  155. showVal: question,
  156. answer: '',
  157. loading: true,
  158. delayLoading: true,
  159. echartWithTableData: []
  160. }
  161. if (isChart.value) {
  162. const { data } = await orderApi.postOrderChart(params);
  163. const reuslt = formatData({...data, whichWay: workOrderParams.value.whichWay});
  164. option.echartWithTableData = reuslt;
  165. }
  166. addChat({
  167. ...option,
  168. innerLoading: true,
  169. });
  170. timer = setTimeout(() => {
  171. updateChat({
  172. ...chatDataSourceItem.value,
  173. delayLoading: true,
  174. innerLoading: false,
  175. })
  176. createEchart(option.echartWithTableData, workOrderParams.value.whichWay);
  177. scrollToBottom();
  178. onRegenerate(question, params);
  179. }, 2 * 1000);
  180. }
  181. // 处理推荐问题
  182. const handleCreateOrder = async () => {
  183. const { timeBegin, timeEnd, whichWay, checkGroup } = unref(workOrderParams);
  184. const startDateTime = dayjs(timeBegin);
  185. const endDateTime = dayjs(timeEnd);
  186. let params = {};
  187. let question = `${reportDate.value}智慧工单分析报告`;
  188. if (tabActive.value === 'customDaily') {
  189. const thirtyDaysBeforeEndTime = endDateTime.subtract(31, 'day');
  190. const isStartAfterThirtyDaysBeforeEnd = startDateTime.isAfter(thirtyDaysBeforeEndTime);
  191. if (!timeBegin) return message.warning('请选择开始日期');
  192. if (!timeEnd) return message.warning('请选择结束日期');
  193. if (!isStartAfterThirtyDaysBeforeEnd) return message.warning('只可生成一个月区间的工单报告');
  194. if (!checkGroup.length) return message.warning('请至少选择一项指标项');
  195. params = {
  196. timeBegin,
  197. timeEnd,
  198. whichWay
  199. }
  200. checkGroup.forEach(key => params[key] = true);
  201. question = `${startDateTime.format("MM月DD日")}-${endDateTime.format("MM月DD日")}的在线仪表的日报工单`
  202. } else {
  203. if (!reportDate.value) return message.warning('请选择时间');
  204. }
  205. handleSubmit(question, params)
  206. }
  207. // 创建echart图形
  208. const createEchart = (echartData, whichWay) => {
  209. setTimeout(() => {
  210. echartData.forEach(({ key, xAxisData, yAxisData, }) => {
  211. const dom = document.getElementById(key);
  212. chartInstance[key] = echarts.init(dom, null, { width: 680, height: 300 });
  213. const option = getOrderAreaOptions({ xAxisData, yAxisData, whichWay });
  214. chartInstance[key].setOption(option);
  215. })
  216. })
  217. }
  218. // 格式化 echart 和 table 数据
  219. const formatData = (data) => {
  220. const titleEnum = {
  221. jsGroup: '进水指标',
  222. csGroup: '出水指标',
  223. hyGroup: '化验指标'
  224. }
  225. const { whichWay, jsGroup, csGroup, hyGroup } = data;
  226. return Object.entries({jsGroup, csGroup, hyGroup}).map(([key, value]) => {
  227. if ( value.length ) {
  228. const [ xAxisData, yAxisData ] = formatEchart(value);
  229. const [ item ] = value;
  230. const columns = Object.keys(item).map(k => {
  231. const wihteKeyList = ['jsSlq', 'csSlqc'];
  232. let unit = '';
  233. if(wihteKeyList.includes(k)) {
  234. unit = whichWay === 1? '(m³/h)' : '(m³/d)';
  235. } else if (k !== 'time') {
  236. unit= '(mg/L)';
  237. }
  238. return {
  239. title: ORDER_OPTION_ENUM[k] + unit,
  240. key: k,
  241. width: '150px',
  242. align: 'center'
  243. }
  244. })
  245. const data = value.map(item => {
  246. Object.entries(item).forEach(([k, v]) => {
  247. !v && v!=0 && (item[k] = '-');
  248. if (isNumberComprehensive(v)) {
  249. item[k] = v ? Number(v.toFixed(2)) : 0
  250. }
  251. })
  252. return item;
  253. })
  254. return {
  255. key: key,
  256. title: titleEnum[key],
  257. xAxisData,
  258. yAxisData,
  259. columns,
  260. data
  261. }
  262. }
  263. }).filter(Boolean);
  264. }
  265. // 返回
  266. const handleback = async () => {
  267. catalogData.value = [];
  268. clearInterval(timer);
  269. await chatApi.getStopChatStream(currenSessionId.value);
  270. resetState();
  271. }
  272. // 删除历史对话
  273. const handeChatDelete = async (id) => {
  274. await chatApi.deleteHistory(id);
  275. catalogData.value = [];
  276. onReset();
  277. clearChat();
  278. message.success('删除成功');
  279. }
  280. const handleTabChange = (val) => {
  281. tabActive.value = val;
  282. }
  283. const dateStartDisabled = (timestamp) => {
  284. const { timeEnd } = workOrderParams.value;
  285. if (timeEnd) {
  286. const currentDate = dayjs(timestamp);
  287. const endDateValue = dayjs(timeEnd);
  288. return !currentDate.isBefore(endDateValue);
  289. }
  290. }
  291. const dateEndDisabled = (timestamp) => {
  292. const { timeBegin } = workOrderParams.value;
  293. if (timeBegin) {
  294. const currentDate = dayjs(timestamp);
  295. const endDateValue = dayjs(timeBegin);
  296. return !currentDate.isAfter(endDateValue);
  297. }
  298. }
  299. onUnmounted(() => {
  300. controller.abort();
  301. Object.keys(chartInstance).forEach(key => chartInstance[key].clear());
  302. })
  303. </script>
  304. <template>
  305. <section class="flex items-start h-full">
  306. <TheSubMenu title="智能工单" @scrollToLower="onScrolltolower" :loading="isFetching">
  307. <template #top>
  308. <div class="create-btn px-[11px] pb-[22px]">
  309. <BaseButton @click="handleCreateDialog" icon-name="tool-add-circle">新建工单</BaseButton>
  310. </div>
  311. </template>
  312. <div class="pr-[4px] text-[#5e5e5e]">
  313. <p v-show="!recordList.length" class="pt-[30px] text-[12px] text-[#999] text-center">暂无工单数据</p>
  314. <RecodeCardItem v-for="item, index in recordList" :key="item.sessionId + index" :title="item.showVal"
  315. :time="item.createTime" :data-item="item"
  316. :class="{ 'recode-card-item_active': recordActive === item.sessionId }" @on-click="handleChatDetail"
  317. @on-delete="handeChatDelete" />
  318. </div>
  319. </TheSubMenu>
  320. <TheChatView ref="scrollRef" :is-footer="false" :is-back-btn="!!chatDataSource.length" @on-click-back="handleback" :catalog-data="catalogData">
  321. <template #catalog>
  322. <ChatTree :data="catalogData"></ChatTree>
  323. </template>
  324. <ChatWelcome title="您好,我是LibraAI智慧工单助手" :sub-title="[
  325. '基于大语言模型的智能工单分析助手,可以为您实现数据分析及数据解读',
  326. '选择日期并为您生成日报分析'
  327. ]" v-if="!chatDataSource.length" />
  328. <div class="conversation-item" v-show="chatDataSource.length">
  329. <div v-for="item in chatDataSource" :key="item.sessionId">
  330. <ChatAsk :content="item.showVal" :sessionId="item.sessionId"></ChatAsk>
  331. <ChatAnswer
  332. :id="item.id"
  333. :content="item.answer"
  334. :loading="item.loading"
  335. :delay-loading="item.delayLoading"
  336. :isSatisfied="item.isSatisfied"
  337. :toggleVisibleIcons="false"
  338. loadingText="数据分析中,由于数据查询需要一些时间,请您耐心等候..."
  339. @on-click-icon="params => updateById(params)"
  340. >
  341. <main v-show="!item.innerLoading">
  342. <div
  343. class="area_inner"
  344. v-for="(item, index) in item.echartWithTableData"
  345. :key="index"
  346. >
  347. <div class="echart-warpper" >
  348. <span class="mb-[10px] #1A2029 font-bold">{{ item.title }}</span>
  349. <div :id="item.key" class="w-[680px] h-[300px]"></div>
  350. </div>
  351. <div class="w-[680px]" style="max-width: 680px; overflow-x: auto;">
  352. <NDataTable
  353. bordered
  354. size="small"
  355. :scroll-x="item.columns.length > 4 ? 1200: 670"
  356. :max-height="250"
  357. :single-line="false"
  358. :columns="item.columns"
  359. :data="item.data"
  360. >
  361. <template #empty>
  362. <span class="leading-[32px]">暂无数据</span>
  363. </template>
  364. </NDataTable>
  365. </div>
  366. </div>
  367. </main>
  368. </ChatAnswer>
  369. </div>
  370. </div>
  371. <div class="order-container px-[60px] py-[30px] mt-[36px] rounded-[10px] bg-[#fff]" v-if="!chatDataSource.length">
  372. <div
  373. class="flex items-center justify-start space-x-[24px] pb-[20px] border-b-[1px] border-solid border-[#F1F1F1]">
  374. <n-tabs type="segment" animated style="width: 188px;" size="large" :on-update:value="handleTabChange">
  375. <n-tab name="daily">日报工单</n-tab>
  376. <n-tab name="customDaily">自定义工单</n-tab>
  377. </n-tabs>
  378. <span class="text-[12px] text-[#8F959C]">
  379. {{ tabActive === 'daily' ? '选择日期后为您生成日报工单' : '选择内容后为您生成工单' }}
  380. </span>
  381. </div>
  382. <main class="order pt-[20px] text-[#1A2029]" v-show="tabActive === 'daily'">
  383. <div class="flex items-center justify-start text-[16px] space-x-[16px]">
  384. <span class="font-bold">选择时间</span>
  385. <div class="w-[164px] border-[1px] border-[#EFEFF0] rounded-[8px] overflow-hidden">
  386. <NDatePicker placeholder="选择日期" :readonly="true" v-model:formatted-value="reportDate"
  387. value-format="yyyy-MM-dd">
  388. <template #date-icon>
  389. <SvgIcon name="tool-arrow-bottom"></SvgIcon>
  390. </template>
  391. </NDatePicker>
  392. </div>
  393. </div>
  394. <dl class="pt-[20px] text-[#1A2029] space-y-[16px]">
  395. <dt class="text-[16px] font-bold leading-[22px]">报告内容</dt>
  396. <dd>1、水质数据、生化数据情况</dd>
  397. <dd>2、各项指标数据分析</dd>
  398. <dd>3、对于工艺调整方面、数据趋势方面的措施与建议</dd>
  399. </dl>
  400. </main>
  401. <main class="order pt-[20px] text-[#1A2029]" v-show="tabActive === 'customDaily'">
  402. <div class="flex items-center justify-start mb-[20px] text-[16px] space-x-[16px]">
  403. <span class="font-bold">选择时间</span>
  404. <div class="w-[164px] border-[1px] border-[#EFEFF0] rounded-[8px] overflow-hidden">
  405. <NDatePicker placeholder="开始日期" :readonly="true" v-model:formatted-value="workOrderParams.timeBegin"
  406. value-format="yyyy-MM-dd" :is-date-disabled="dateStartDisabled">
  407. <template #date-icon>
  408. <SvgIcon name="tool-arrow-bottom"></SvgIcon>
  409. </template>
  410. </NDatePicker>
  411. </div>
  412. <span class="text-[16px] text-[#8F959C]">至</span>
  413. <div class="w-[164px] border-[1px] border-[#EFEFF0] rounded-[8px] overflow-hidden">
  414. <NDatePicker placeholder="结束日期" :readonly="true" v-model:formatted-value="workOrderParams.timeEnd"
  415. value-format="yyyy-MM-dd" :is-date-disabled="dateEndDisabled">
  416. <template #date-icon>
  417. <SvgIcon name="tool-arrow-bottom"></SvgIcon>
  418. </template>
  419. </NDatePicker>
  420. </div>
  421. </div>
  422. <div class="flex items-center justify-start mb-[20px] text-[16px] space-x-[16px]">
  423. <span class="font-bold">统计方式</span>
  424. <n-radio-group v-model:value="workOrderParams.whichWay" name="radiogroup2">
  425. <n-radio :value="1">在线仪表</n-radio>
  426. <n-radio :value="0">日报</n-radio>
  427. </n-radio-group>
  428. </div>
  429. <div class="flex items-center justify-start mb-[20px] text-[16px] space-x-[16px]">
  430. <span class="font-bold">进水指标</span>
  431. <n-checkbox-group v-model:value="workOrderParams.checkGroup" name="radiogroup2">
  432. <n-checkbox value="jsSlq">进水水量</n-checkbox>
  433. <n-checkbox value="jsCod">COD</n-checkbox>
  434. <n-checkbox value="jsTn">总氮</n-checkbox>
  435. <n-checkbox value="jsTp">总磷</n-checkbox>
  436. <n-checkbox value="jsNh3">氨氮</n-checkbox>
  437. <n-checkbox value="jsSs">SS</n-checkbox>
  438. </n-checkbox-group>
  439. </div>
  440. <div class="flex items-center justify-start mb-[20px] text-[16px] space-x-[16px]">
  441. <span class="font-bold">出水指标</span>
  442. <n-checkbox-group v-model:value="workOrderParams.checkGroup" name="radiogroup2">
  443. <n-checkbox value="csSlqc">出水水量</n-checkbox>
  444. <n-checkbox value="csCod">COD</n-checkbox>
  445. <n-checkbox value="csTn">总氮</n-checkbox>
  446. <n-checkbox value="csTp">总磷</n-checkbox>
  447. <n-checkbox value="csNh3">氨氮</n-checkbox>
  448. <n-checkbox value="csSs">SS</n-checkbox>
  449. </n-checkbox-group>
  450. </div>
  451. <div class="flex items-start justify-start mb-[20px] text-[16px] space-x-[16px]">
  452. <span class="font-bold">化验指标</span>
  453. <div class="w-[500px]">
  454. <n-checkbox-group v-model:value="workOrderParams.checkGroup" name="radiogroup2">
  455. <n-checkbox value="no3Hlj1Jqr">1#好氧池硝酸盐</n-checkbox>
  456. <n-checkbox value="no3Hlj2Jqr">2#好氧池硝酸盐</n-checkbox>
  457. <n-checkbox value="nh31Jqr">1#缺氧氨氮</n-checkbox>
  458. <n-checkbox value="nh32Jqr">2#缺氧氨氮</n-checkbox>
  459. <n-checkbox value="no3Qyc1Jqr">1#缺氧池硝酸盐</n-checkbox>
  460. <n-checkbox value="no3Qyc2Jqr">2#缺氧池硝酸盐</n-checkbox>
  461. <n-checkbox value="tpRccJqr">二沉池正磷酸盐</n-checkbox>
  462. </n-checkbox-group>
  463. </div>
  464. </div>
  465. <dl class="text-[#1A2029] space-y-[16px]">
  466. <dt class="text-[16px] font-bold leading-[22px]">报告内容</dt>
  467. <dd>1、数据情况</dd>
  468. <dd>2、各项指标数据分析及措施与建议</dd>
  469. </dl>
  470. </main>
  471. <footer class="pt-[24px]">
  472. <button class="btn-primary" @click="handleCreateOrder">立即生成</button>
  473. </footer>
  474. </div>
  475. </TheChatView>
  476. </section>
  477. </template>
  478. <style scoped lang="scss">
  479. .n-input__input-el {
  480. font-size: 12px !important;
  481. }
  482. .area_inner {
  483. display: flex;
  484. align-items: center;
  485. flex-flow: column;
  486. padding-bottom: 20px;
  487. border-radius: 8px;
  488. margin-bottom: 20px;
  489. background: #eff8fc;
  490. }
  491. .list-item {
  492. margin-bottom: 16px;
  493. .title {
  494. font-size: 14px;
  495. font-weight: bold;
  496. line-height: 20px;
  497. }
  498. .item-top {
  499. display: flex;
  500. justify-content: space-between;
  501. margin-bottom: 10px;
  502. .num {
  503. font-size: 12px;
  504. font-weight: 400;
  505. color: #B0B7C0;
  506. }
  507. }
  508. }
  509. .btn-primary {
  510. width: 88px;
  511. height: 32px;
  512. border-radius: 6px;
  513. background-color: #2454FF;
  514. font-size: 14px;
  515. line-height: 32px;
  516. color: #fff;
  517. &:hover {
  518. background: #1D43CC;
  519. }
  520. }
  521. .echart-warpper {
  522. display: flex;
  523. align-items: center;
  524. justify-content: center;
  525. flex-flow: column;
  526. width: 100%;
  527. padding: 20px 0;
  528. border-radius: 8px;
  529. }
  530. </style>
  531. <style lang="scss">
  532. .order {
  533. .n-input__input-el,
  534. .n-input__placeholder {
  535. font-size: 14px;
  536. }
  537. }
  538. .order-container {
  539. .n-tabs-tab__label {
  540. font-size: 14px !important;
  541. font-weight: bold;
  542. color: #5E5E5E;
  543. }
  544. .n-tabs-tab--active {
  545. .n-tabs-tab__label {
  546. color: #1A2029;
  547. }
  548. }
  549. }
  550. </style>