ChemCostComparison.js 12 KB

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