ChemCostComparison.js 9.9 KB

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