ChemCostComparison.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // 药耗监测
  2. import ChartModule from '@/components/ManagementPage/chartModule';
  3. import PageContent from '@/components/PageContent';
  4. import PageTitle from '@/components/PageTitle';
  5. import {
  6. getChemicalAgents,
  7. getComparisonData,
  8. } from '@/services/OperationManagement';
  9. import { UnityAction } from '@/utils/utils';
  10. import { useParams } from '@umijs/max';
  11. import { Tabs, message } from 'antd';
  12. import dayjs from 'dayjs';
  13. import { useEffect, useState } from 'react';
  14. import styles from './manage.less';
  15. const { TabPane } = Tabs;
  16. const typeParams = [
  17. {
  18. // 计划吨水药耗
  19. type: '3',
  20. flag: '0',
  21. },
  22. {
  23. // 实际吨水药耗
  24. type: '3',
  25. flag: '1',
  26. },
  27. {
  28. // 计划用药量
  29. type: '4',
  30. flag: '0',
  31. },
  32. {
  33. // 实际用药量
  34. type: '4',
  35. flag: '1',
  36. },
  37. ];
  38. const CostComparison = (props) => {
  39. const { projectId } = useParams();
  40. const [open, setOpen] = useState(false);
  41. const [chartData, setChartData] = useState([]);
  42. const [chemList, setChemList] = useState([]);
  43. const [currentChem, setCurrentChem] = useState();
  44. const [topValues, setTopValues] = useState({
  45. chemPer: 0,
  46. chemUser: 0,
  47. });
  48. const curMonth = dayjs().format('YYYY-MM');
  49. const defaultTime = {
  50. s_time: `${dayjs().format('YYYY')}-${dayjs().startOf('year').format('MM')}`,
  51. e_time: `${dayjs().format('YYYY')}-${dayjs().endOf('year').format('MM')}`,
  52. };
  53. const defaultParams = {
  54. project_id: projectId,
  55. start: defaultTime.s_time,
  56. end: defaultTime.e_time,
  57. };
  58. const getChartData = () => {
  59. // 构建请求列表
  60. const queryList = [];
  61. for (let index = 0; index < 4; index++) {
  62. queryList.push(
  63. getComparisonData({ ...defaultParams, ...typeParams[index] }),
  64. );
  65. }
  66. // 获取四组数据
  67. return Promise.all(queryList).catch(() => {
  68. message.error('请求数据失败');
  69. });
  70. };
  71. const getFixed = (maxValue) => {
  72. // 如果小于1,则保留最后两位不为0的数字
  73. // 如果大于1小于10,则保留三位
  74. // 大于10,保留两位
  75. // 大于100,保留一位
  76. // 大于1000,不保留
  77. let fixed = 0;
  78. if (maxValue === 0) return fixed;
  79. if (maxValue < 1) {
  80. //maxValue + 1 防止maxValue过小自动转科学计数法
  81. const decimal = (maxValue + 1).toString().split('.')[1];
  82. const num = decimal.split('').findIndex((num) => num > 0);
  83. fixed = num + 3;
  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 [planChemPerCost, actualChemPerCost, planChem, actualChem] = result;
  99. const chemPerCost = { yName: 'kg/m³' };
  100. const chemUsed = { yName: 'kg' };
  101. chemPerCost.xData = [
  102. ...new Set(
  103. [
  104. ...planChemPerCost.map((item) => item.month),
  105. ...actualChemPerCost.map((item) => item.month),
  106. ].map((item) => item),
  107. ),
  108. ].sort();
  109. let year = `${dayjs(chemPerCost.xData[0]).year()}`;
  110. chemPerCost.xData = [];
  111. for (let index = 0; index < 12; index++) {
  112. chemPerCost.xData.push(`${year}-${dayjs().month(index).format('MM')}`);
  113. }
  114. let topVals = { ...topValues };
  115. // 确定保留的小数点
  116. const chemPerCostMaxValue = [...planChemPerCost, ...actualChemPerCost]
  117. .map((item) => item.value)
  118. .reduce((a, b) => Math.max(a, b));
  119. const chemPerCostFixed = getFixed(chemPerCostMaxValue);
  120. console.log(chemPerCostFixed);
  121. chemPerCost.dataList = [
  122. {
  123. type: 0,
  124. yIndex: 1,
  125. name: '计划吨水药耗',
  126. data: chemPerCost.xData.map((month) => {
  127. const pItem = planChemPerCost.find((item) => item.month === month);
  128. if (pItem) {
  129. return pItem.value?.toFixed(chemPerCostFixed);
  130. }
  131. return 0;
  132. }),
  133. },
  134. {
  135. type: 0,
  136. yIndex: 1,
  137. name: '实际吨水药耗',
  138. data: chemPerCost.xData.map((month) => {
  139. const aItem = actualChemPerCost.find(
  140. (item) => item.month === month,
  141. );
  142. if (aItem) {
  143. if (month == curMonth)
  144. topVals.chemPer = aItem.value.toFixed(chemPerCostFixed);
  145. return aItem.value.toFixed(chemPerCostFixed);
  146. }
  147. return 0;
  148. }),
  149. },
  150. ];
  151. // 合并+去重+排序 两组数据中所有月份
  152. chemUsed.xData = [
  153. ...new Set(
  154. [
  155. ...planChem.map((item) => item.month),
  156. ...actualChem.map((item) => item.month),
  157. ].map((item) => item),
  158. ),
  159. ].sort();
  160. year = `${dayjs(chemUsed.xData[0]).year()}`;
  161. chemUsed.xData = [];
  162. for (let index = 0; index < 12; index++) {
  163. chemUsed.xData.push(`${year}-${dayjs().month(index).format('MM')}`);
  164. }
  165. // 确定保留的小数点
  166. const chemUsedMaxValue = [...planChem, ...actualChem]
  167. .map((item) => item.value)
  168. .reduce((a, b) => Math.max(a, b));
  169. const chemUsedFixed = getFixed(chemUsedMaxValue);
  170. chemUsed.dataList = [
  171. {
  172. type: 3,
  173. yIndex: 1,
  174. name: '计划用药量',
  175. // 根据月份是否在xData内返回数据
  176. data: chemUsed.xData.map((month) => {
  177. const pItem = planChem.find((item) => item.month === month);
  178. if (pItem) {
  179. return pItem.value.toFixed(chemUsedFixed);
  180. }
  181. return 0;
  182. }),
  183. },
  184. {
  185. type: 3,
  186. yIndex: 1,
  187. name: '实际用药量',
  188. data: chemUsed.xData.map((month) => {
  189. const aItem = actualChem.find((item) => item.month === month);
  190. if (aItem) {
  191. if (month == curMonth)
  192. topVals.chemUser = aItem.value.toFixed(chemUsedFixed);
  193. return aItem.value.toFixed(chemUsedFixed);
  194. }
  195. return 0;
  196. }),
  197. },
  198. ];
  199. chemUsed.chartType = 'bar';
  200. setTopValues(topVals);
  201. setChartData([chemPerCost, chemUsed]);
  202. } else {
  203. setChartData([]);
  204. }
  205. };
  206. const getChemList = async () => {
  207. const list = await getChemicalAgents(projectId).catch(() => {
  208. message.error('获取数据失败');
  209. });
  210. setChemList([...list]);
  211. setCurrentChem(list[0]);
  212. typeParams.forEach((item) => {
  213. item.chemical_agents = list[0];
  214. });
  215. };
  216. const handleChemChange = (type) => {
  217. typeParams.forEach((item) => {
  218. item.chemical_agents = type;
  219. });
  220. createChartData();
  221. };
  222. useEffect(() => {
  223. (async () => {
  224. await getChemList();
  225. await createChartData();
  226. })();
  227. }, []);
  228. return (
  229. <PageContent closeable={false}>
  230. <PageTitle onReturn={() => UnityAction.sendMsg('menuItem', '首页')}>
  231. 药耗监测
  232. <div
  233. onClick={(e) => {
  234. e.stopPropagation();
  235. setOpen(!open);
  236. }}
  237. style={{ marginLeft: 10 }}
  238. className={`password-eye ${open ? 'open' : ''}`}
  239. ></div>
  240. </PageTitle>
  241. <div className="card-box" style={{ padding: '0.2rem' }}>
  242. {/* 使用Tabs来展示所有药的标签 */}
  243. <div className="tabs">
  244. {chemList?.map((item) => (
  245. <div
  246. onClick={() => {
  247. setCurrentChem(item);
  248. handleChemChange(item);
  249. }}
  250. className={`tabs-item ${currentChem == item ? 'active' : ''}`}
  251. >
  252. {item}
  253. </div>
  254. ))}
  255. </div>
  256. <div className={styles.curEnergyCost}>
  257. <div className={styles.item}>
  258. <div className={styles.value}>
  259. {open ? topValues.chemPer : '***'}
  260. <span className={styles.unit}>kg/m³</span>
  261. </div>
  262. <div className={styles.name}>当月吨水药耗</div>
  263. </div>
  264. <div className={styles.item}>
  265. <div className={styles.value}>
  266. {open ? topValues.chemUser : '***'}
  267. <span className={styles.unit}>kg</span>
  268. </div>
  269. <div className={styles.name}>当月药量</div>
  270. </div>
  271. </div>
  272. {chartData.length !== 0 && (
  273. <div
  274. style={{
  275. height: '8.8rem',
  276. display: 'flex',
  277. flexDirection: 'column',
  278. justifyContent: 'space-between',
  279. padding: '0.4rem 0',
  280. }}
  281. >
  282. <div style={{ height: '3.5rem' }}>
  283. <ChartModule {...chartData[0]} />
  284. </div>
  285. <div style={{ height: '3.5rem' }}>
  286. <ChartModule {...chartData[1]} />
  287. </div>
  288. </div>
  289. )}
  290. </div>
  291. </PageContent>
  292. );
  293. };
  294. export default CostComparison;