index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <div class="page-container row-container">
  3. <section class="list-part-container" style="flex: 2">
  4. <!-- 搜索区 -->
  5. <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
  6. <el-form-item label="年份:" prop="year">
  7. <el-date-picker v-model="queryParams.year" type="year" value-format="YYYY" :editable="false"
  8. :clearable="false" placeholder="请选择时间" @change="handleYearChange" style="width: 130px" />
  9. </el-form-item>
  10. <el-form-item label="工艺图纸电子化基数:" v-hasPermi="['business:conversion:query']" prop="number">
  11. <span v-if="!disabled">{{ number }}</span>
  12. <el-input v-if="disabled" v-model.trim="number" style="width: 220px; margin-left: 0px" />
  13. </el-form-item>
  14. <el-form-item>
  15. <el-button v-if="!disabled" type="primary" v-hasPermi="['business:conversion:add']"
  16. @click="handleEdit">编辑</el-button>
  17. <el-button v-if="disabled" type="success" @click="handleSave">保存</el-button>
  18. <el-button v-if="disabled" type="info" @click="handleCancel">取消</el-button>
  19. </el-form-item>
  20. </el-form>
  21. <!-- 列表区 -->
  22. <div ref="echartDom" style="width: 100%; height: 100%;"></div>
  23. <!-- 表单 -->
  24. </section>
  25. </div>
  26. </template>
  27. <script setup name="Conversion">
  28. import { listConversion, addConversion } from '@/api/business/conversion'
  29. import { ref, onMounted, reactive, computed } from 'vue'
  30. import * as echarts from 'echarts'
  31. import { getProcuctDrawing } from '@/api/business/drawing'
  32. import { markRaw } from "vue"
  33. const { proxy } = getCurrentInstance();
  34. const disabled = ref(false)
  35. const echartInstance = ref(null)
  36. const itemNum = ref(0)
  37. const echartDom = ref(null)
  38. const procuctDrawing = ref(0)
  39. const number = ref(0)
  40. const rawData = ref([])
  41. const months = ref(['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'])
  42. const currentMonthIndex = computed(() => new Date().getMonth())
  43. /** 字典 */
  44. const { conversion_mes } = proxy.useDict("conversion_mes");
  45. const data = reactive({
  46. queryParams: {
  47. year: new Date().getFullYear().toString()
  48. }
  49. })
  50. const { queryParams } = toRefs(data)
  51. function processChartData(rawData, selectedYear) {
  52. const monthlyData = new Array(12).fill(null);
  53. rawData.forEach(record => {
  54. const recordDate = new Date(record.date);
  55. const recordYear = recordDate.getFullYear();
  56. const monthIndex = recordDate.getMonth();
  57. if(selectedYear < 2024 ){
  58. itemNum.value =0
  59. }else if(selectedYear === 2024 && monthIndex == 2){
  60. itemNum.value = 78
  61. }else if(selectedYear === 2024 && monthIndex == 3){
  62. itemNum.value = 82
  63. }else if(selectedYear === 2024 && monthIndex == 4){
  64. itemNum.value = 85
  65. }else if(selectedYear === 2024 && monthIndex == 5){
  66. itemNum.value = 88
  67. }else if(selectedYear <= 2024 && monthIndex <= 1){
  68. itemNum.value =0
  69. }
  70. else{
  71. itemNum.value = procuctDrawing.value;
  72. }
  73. if (recordYear === selectedYear) {
  74. monthlyData[monthIndex] = (itemNum.value / parseInt(record.number) * 100).toFixed(2);
  75. }
  76. });
  77. // 设置默认值逻辑
  78. for (let i = 0; i < 12; i++) {
  79. if (monthlyData[i] === null) {
  80. // 如果选择的年份是2024年,且月份是3月或之后,或者年份晚于2024年,则使用默认值100
  81. monthlyData[i] = (selectedYear >= 2024 && (i >= 2 || selectedYear > 2024)) ? itemNum.value : 0;
  82. }
  83. // 转换为百分比形式
  84. monthlyData[i] = parseInt(monthlyData[i]);
  85. }
  86. console.log(monthlyData)
  87. return monthlyData;
  88. }
  89. function handleEdit() {
  90. disabled.value = !disabled.value;
  91. }
  92. function handleCancel() {
  93. disabled.value = !disabled.value;
  94. getList();
  95. }
  96. function handleSave() {
  97. console.log(number.value);
  98. addConversion({ number: number.value }).then(res => {
  99. if (res.code == 200) {
  100. proxy.$modal.msgSuccess("操作成功");
  101. disabled.value = !disabled.value;
  102. getList();
  103. }
  104. });
  105. }
  106. function sortDataByDateAsc(rawData) {
  107. return rawData.slice().sort((a, b) => new Date(a.date) - new Date(b.date));
  108. }
  109. function updateChart(monthlyData, displayMonths, selectedYear, currentYear){
  110. echartInstance.value.setOption({
  111. title: { text: '工艺图纸电子化率' },
  112. tooltip: {
  113. trigger: 'axis',
  114. axisPointer: {
  115. type: 'shadow'
  116. },
  117. formatter: function (params) {
  118. const param = params[0];
  119. const monthIndex = param.dataIndex;
  120. const month = displayMonths[monthIndex];
  121. let requiredDocumentsThisMonth = 0;
  122. var uploadedProducts = 0;
  123. const sortedData = sortDataByDateAsc(rawData.value);
  124. if(selectedYear<2024){
  125. requiredDocumentsThisMonth = 0
  126. }else if (sortedData[(monthIndex-2)]) {
  127. requiredDocumentsThisMonth = sortedData[(monthIndex-2)].number;
  128. }else if ((selectedYear > 2023 || (selectedYear === 2024 && monthIndex >= 2)) && !sortedData[monthIndex]) {
  129. requiredDocumentsThisMonth = 100; // 2024年3月及以后默认值为100
  130. }
  131. if(selectedYear < 2024 ){
  132. uploadedProducts =0
  133. }else if(selectedYear === 2024 && monthIndex == 2){
  134. uploadedProducts = 78
  135. }else if(selectedYear === 2024 && monthIndex == 3){
  136. uploadedProducts = 82
  137. }else if(selectedYear === 2024 && monthIndex == 4){
  138. uploadedProducts = 85
  139. }else if(selectedYear === 2024 && monthIndex == 5){
  140. uploadedProducts = 81
  141. }else if(selectedYear <= 2024 && monthIndex <= 1){
  142. uploadedProducts =0
  143. }
  144. else{
  145. uploadedProducts = procuctDrawing.value;
  146. }
  147. if(requiredDocumentsThisMonth>0){
  148. var rate = (uploadedProducts/requiredDocumentsThisMonth*100).toFixed(2)
  149. }else{
  150. var rate = 0
  151. }
  152. const mes1 = conversion_mes.value[0].label;
  153. const mes2 = conversion_mes.value[1].label ;
  154. return `
  155. ${month}:<br/>
  156. ${mes1}${requiredDocumentsThisMonth},<br/>
  157. ${mes2}${uploadedProducts},<br/>
  158. 电子化率:${rate}%
  159. `;
  160. }
  161. },
  162. xAxis: {
  163. type: 'category',
  164. data: displayMonths
  165. },
  166. yAxis: {
  167. type: 'value'
  168. },
  169. series: [{
  170. name: '电子化率',
  171. type: 'line',
  172. data: monthlyData
  173. }]
  174. });
  175. }
  176. function initChart() {
  177. echartInstance.value = markRaw(echarts.init(echartDom.value));
  178. updateChart([], months.value); // 使用空数据初始化图表
  179. }
  180. function handleQuery() {
  181. getList();
  182. }
  183. function handleYearChange(year) {
  184. const currentYear = new Date().getFullYear();
  185. if (parseInt(year) > currentYear) {
  186. queryParams.value.year = currentYear.toString();
  187. } else {
  188. queryParams.value.year = year;
  189. }
  190. getList();
  191. }
  192. function resetQuery() {
  193. queryParams.value.year = new Date().getFullYear().toString();
  194. getList();
  195. }
  196. function sortDataByDateDesc(rawData) {
  197. return rawData.slice().sort((a, b) => new Date(b.date) - new Date(a.date));
  198. }
  199. function getList() {
  200. // 解构 queryParams 和 currentMonthIndex 为局部变量
  201. const { queryParams } = toRefs(data);
  202. const currentMonthIndex = computed(() => new Date().getMonth());
  203. // 获取当前选择的年份和当前年份
  204. const selectedYear = parseInt(queryParams.value.year);
  205. const currentYear = new Date().getFullYear();
  206. listConversion(selectedYear).then(res => {
  207. rawData.value = sortDataByDateDesc(res.data);
  208. console.log(rawData.value)
  209. number.value = rawData.value[0].number;
  210. getProcuctDrawing().then(res => {
  211. procuctDrawing.value = res.data;
  212. // 处理图表数据
  213. const monthlyData = processChartData(rawData.value, selectedYear);
  214. const displayMonths = selectedYear === currentYear ?
  215. months.value.slice(0, currentMonthIndex.value + 1) :
  216. months.value;
  217. // 将selectedYear和currentYear传递给updateChart
  218. updateChart(monthlyData, displayMonths, selectedYear, currentYear);
  219. });
  220. });
  221. }
  222. onMounted(() => {
  223. initChart();
  224. getList();
  225. });
  226. </script>