EnergyCostComparison.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // 能耗监测
  2. import ChartModule from '@/components/ManagementPage/chartModule';
  3. import PageContent from '@/components/PageContent';
  4. import PageTitle from '@/components/PageTitle';
  5. import { getComparisonData } from '@/services/OperationManagement';
  6. import { UnityAction } from '@/utils/utils';
  7. import { LineChartOutlined } from '@ant-design/icons';
  8. import { history, useParams } from '@umijs/max';
  9. import { message } from 'antd';
  10. import dayjs from 'dayjs';
  11. import { useEffect, useState } from 'react';
  12. import styles from './manage.less';
  13. const typeParams = [
  14. {
  15. // 计划吨水电耗
  16. type: '5',
  17. flag: '0',
  18. },
  19. {
  20. // 实际吨水电耗
  21. type: '5',
  22. flag: '1',
  23. },
  24. {
  25. // 计划用电量
  26. type: '6',
  27. flag: '0',
  28. },
  29. {
  30. // 实际用电量
  31. type: '6',
  32. flag: '1',
  33. },
  34. ];
  35. const CostComparison = () => {
  36. const { projectId } = useParams();
  37. const [open, setOpen] = useState(false);
  38. const [chartData, setChartData] = useState([]);
  39. const [curElecPerCost, setElecPerCost] = useState(0); // 当前月实际吨水电耗
  40. const [curElecUsed, setCurElecUsed] = useState(0); // 当前月实际用电量
  41. const defaultTime = {
  42. s_time: `${dayjs().format('YYYY')}-${dayjs().startOf('year').format('MM')}`,
  43. e_time: `${dayjs().format('YYYY')}-${dayjs().endOf('year').format('MM')}`,
  44. };
  45. const defaultParams = {
  46. project_id: projectId,
  47. start: defaultTime.s_time,
  48. end: defaultTime.e_time,
  49. };
  50. const getChartData = () => {
  51. // 构建请求列表
  52. const queryList = [];
  53. for (let index = 0; index < 4; index++) {
  54. queryList.push(
  55. getComparisonData({ ...defaultParams, ...typeParams[index] }),
  56. );
  57. }
  58. // 获取四组数据
  59. return Promise.all(queryList).catch(() => {
  60. message.error('请求数据失败');
  61. });
  62. };
  63. const getFixed = (maxValue) => {
  64. // 如果小于1,则保留最后两位不为0的数字
  65. // 如果大于1小于10,则保留三位
  66. // 大于10,保留两位
  67. // 大于100,保留一位
  68. // 大于1000,不保留
  69. if (maxValue === 0) {
  70. return 2;
  71. }
  72. let fixed = 0;
  73. if (maxValue < 1) {
  74. const decimal = maxValue.toFixed(100).toString().split('.')[1];
  75. const decimalArr = decimal.split('');
  76. for (let index = 0; index < decimalArr.length; index++) {
  77. if (decimalArr[index] === '0') {
  78. fixed++;
  79. } else {
  80. break;
  81. }
  82. }
  83. fixed += 2;
  84. } else if (maxValue < 10) {
  85. fixed = 3;
  86. } else if (maxValue < 100) {
  87. fixed = 2;
  88. } else if (maxValue < 1000) {
  89. fixed = 1;
  90. }
  91. return fixed;
  92. };
  93. const createChartData = async () => {
  94. const result = await getChartData().catch(() => {
  95. message.error('获取数据失败');
  96. });
  97. if (result && result.length) {
  98. const [planElecPerCost, actualElecPerCost, planElecUsed, actualElecUsed] =
  99. result;
  100. const elecPerCost = { yName: 'kWh/t' };
  101. const elecUsed = { yName: 'kWh' };
  102. elecPerCost.xData = [
  103. ...new Set(
  104. [
  105. ...planElecPerCost.map((item) => item.month),
  106. ...actualElecPerCost.map((item) => item.month),
  107. ].map((item) => item),
  108. ),
  109. ].sort();
  110. let year = `${dayjs(elecPerCost.xData[0]).year()}`;
  111. elecPerCost.xData = [];
  112. for (let index = 0; index < 12; index++) {
  113. elecPerCost.xData.push(`${year}-${dayjs().month(index).format('MM')}`);
  114. }
  115. // 确定保留的小数点
  116. const elecPerCostValue = [...planElecPerCost, ...actualElecPerCost]
  117. .map((item) => item.value)
  118. .reduce((a, b) => Math.max(a, b));
  119. const elecPerCostFixed = getFixed(elecPerCostValue);
  120. elecPerCost.dataList = [
  121. {
  122. type: 0,
  123. yIndex: 1,
  124. name: '计划吨水电耗',
  125. data: elecPerCost.xData.map((month) => {
  126. const pItem = planElecPerCost.find((item) => item.month === month);
  127. if (pItem) {
  128. return pItem.value.toFixed(elecPerCostFixed);
  129. }
  130. return 0;
  131. }),
  132. },
  133. {
  134. type: 0,
  135. yIndex: 1,
  136. name: '实际吨水电耗',
  137. data: elecPerCost.xData.map((month) => {
  138. const aItem = actualElecPerCost.find(
  139. (item) => item.month === month,
  140. );
  141. if (aItem) {
  142. return aItem.value.toFixed(elecPerCostFixed);
  143. }
  144. return 0;
  145. }),
  146. },
  147. ];
  148. elecUsed.xData = [
  149. ...new Set(
  150. [
  151. ...planElecUsed.map((item) => item.month),
  152. ...actualElecUsed.map((item) => item.month),
  153. ].map((item) => item),
  154. ),
  155. ].sort();
  156. year = `${dayjs(elecUsed.xData[0]).year()}`;
  157. elecUsed.xData = [];
  158. for (let index = 0; index < 12; index++) {
  159. elecUsed.xData.push(`${year}-${dayjs().month(index).format('MM')}`);
  160. }
  161. // 确定保留的小数点
  162. const elecUsedMaxValue = [...planElecUsed, ...actualElecUsed]
  163. .map((item) => item.value)
  164. .reduce((a, b) => Math.max(a, b));
  165. const elecUsedFixed = getFixed(elecUsedMaxValue);
  166. elecUsed.dataList = [
  167. {
  168. type: 3,
  169. yIndex: 1,
  170. name: '计划用电量',
  171. data: elecUsed.xData.map((month) => {
  172. const pItem = planElecUsed.find((item) => item.month === month);
  173. if (pItem) {
  174. return pItem.value.toFixed(elecUsedFixed);
  175. }
  176. return 0;
  177. }),
  178. },
  179. {
  180. type: 3,
  181. yIndex: 1,
  182. name: '实际用电量',
  183. data: elecUsed.xData.map((month) => {
  184. const aItem = actualElecUsed.find((item) => item.month === month);
  185. if (aItem) {
  186. return aItem.value.toFixed(elecUsedFixed);
  187. }
  188. return 0;
  189. }),
  190. },
  191. ];
  192. elecUsed.chartType = 'bar';
  193. setChartData([elecPerCost, elecUsed]);
  194. let curElecPerCost = actualElecPerCost?.find((item) =>
  195. dayjs().isSame(item?.month, 'month'),
  196. );
  197. if (curElecPerCost)
  198. setElecPerCost(curElecPerCost?.value.toFixed(elecPerCostFixed));
  199. let curElecUsed = actualElecUsed?.find((item) =>
  200. dayjs().isSame(item?.month, 'month'),
  201. );
  202. if (curElecUsed)
  203. setCurElecUsed(curElecUsed?.value.toFixed(elecUsedFixed));
  204. } else {
  205. setChartData([]);
  206. }
  207. };
  208. useEffect(() => {
  209. createChartData();
  210. }, []);
  211. const goEnergyDetail = () => {
  212. history.push(`/home/energy/detail/${projectId}`);
  213. };
  214. return (
  215. <PageContent closeable={false}>
  216. <PageTitle onReturn={() => UnityAction.sendMsg('menuItem', '首页')}>
  217. 能耗数据
  218. <div
  219. onClick={(e) => {
  220. e.stopPropagation();
  221. setOpen(!open);
  222. }}
  223. style={{ marginLeft: '0.1rem' }}
  224. className={`password-eye ${open ? 'open' : ''}`}
  225. ></div>
  226. </PageTitle>
  227. <div
  228. className="card-box"
  229. style={{ padding: '0.2rem', position: 'relative' }}
  230. >
  231. <LineChartOutlined
  232. style={{
  233. fontSize: '0.4rem',
  234. position: 'absolute',
  235. right: '0.3rem',
  236. color: '#0139f1',
  237. }}
  238. onClick={goEnergyDetail}
  239. />
  240. <div className={styles.curEnergyCost}>
  241. <div
  242. className={styles.item}
  243. style={{
  244. borderRight: '1px solid #eaeaea',
  245. borderBottom: '1px solid #eaeaea',
  246. }}
  247. >
  248. <div className={styles.value}>
  249. {open ? curElecPerCost : '***'}
  250. <span className={styles.unit}>kWh/t</span>
  251. </div>
  252. <div className={styles.name}>近一天吨水电耗</div>
  253. </div>
  254. <div
  255. className={styles.item}
  256. style={{
  257. borderBottom: '1px solid #eaeaea',
  258. }}
  259. >
  260. <div className={styles.value}>
  261. {open ? curElecUsed : '***'}
  262. <span className={styles.unit}>kWh</span>
  263. </div>
  264. <div className={styles.name}>近一天实际用电量</div>
  265. </div>
  266. <div
  267. className={styles.item}
  268. style={{
  269. borderRight: '1px solid #eaeaea',
  270. }}
  271. >
  272. <div className={styles.value}>
  273. {open ? curElecPerCost : '***'}
  274. <span className={styles.unit}>kWh/t</span>
  275. </div>
  276. <div className={styles.name}>当月吨水电耗</div>
  277. </div>
  278. <div className={styles.item}>
  279. <div className={styles.value}>
  280. {open ? curElecUsed : '***'}
  281. <span className={styles.unit}>kWh</span>
  282. </div>
  283. <div className={styles.name}>当月实际用电量</div>
  284. </div>
  285. </div>
  286. {chartData.length !== 0 && (
  287. <div
  288. style={{
  289. height: '9.2rem',
  290. display: 'flex',
  291. flexDirection: 'column',
  292. justifyContent: 'space-between',
  293. padding: '0.6rem 0 0.4rem',
  294. }}
  295. >
  296. <div style={{ height: '3.5rem' }}>
  297. <ChartModule {...chartData[0]} />
  298. </div>
  299. <div style={{ height: '3.5rem' }}>
  300. <ChartModule {...chartData[1]} />
  301. </div>
  302. </div>
  303. )}
  304. </div>
  305. </PageContent>
  306. );
  307. };
  308. export default CostComparison;