holidays.jsx 21.5 KB
Newer Older
1 2
/* eslint-disable global-require */
import React, { useState, useEffect, useRef } from 'react';
邓超's avatar
邓超 committed
3
import axios from 'axios';
4 5 6 7 8 9 10 11 12 13 14 15
import {
  Calendar,
  Button,
  Select,
  Space,
  message,
  Popconfirm,
  Modal,
  Form,
  Input,
  Radio,
  Upload,
16
  AutoComplete,
17 18 19 20 21 22 23 24
} from 'antd';
import {
  ImportOutlined,
  DeleteOutlined,
  ExclamationCircleOutlined,
  ExportOutlined,
  PlusOutlined,
  SyncOutlined,
邓超's avatar
邓超 committed
25 26
  LeftOutlined,
  RightOutlined,
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
} from '@ant-design/icons';
import {
  AddFlowHoliday,
  GetWorkDayManageInfo,
  DeleteFlowHoliday,
  DownLoadFlowHoliday,
  ImportFlowHoliday,
} from '@/services/holidays/holidays';
import { calendar } from 'js-calendar-converter';
import moment from 'moment';
import 'moment/dist/locale/zh-cn';
import locale from 'antd/es/date-picker/locale/zh_CN';
import classNames from 'classnames';
import WorkTiemConfig from './components/WorkTiemConfig';
import AddModal from './components/AddModal';
import Synchronize from './components/Synchronize';
43
import HolidayConfig from './components/HolidayConfig';
44
import styles from './holidays.less';
45

46
const { confirm } = Modal;
47
const holidaysList = new Set(['元旦', '春节', '清明节', '劳动节', '端午节', '中秋节', '国庆节']);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
moment.updateLocale('zh-cn', {
  weekdaysMin: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
});
const Holidays = () => {
  const [holidayStatistics, setHolidayStatistics] = useState({
    SumDays: {
      type: 'SumDays',
      name: '总天数',
      value: 0,
      icon: require('../../../../assets/images/holidays/SumDays.png'),
      color: '#3D78FF',
    },
    WeekendDays: {
      type: 'WeekendDays',
      name: '周末天数',
      value: 0,
      icon: require('../../../../assets/images/holidays/WeekendDays.png'),
      color: '#FF5933',
    },
    Holidays: {
      type: 'Holidays',
      name: '法定假日',
      value: 0,
      icon: require('../../../../assets/images/holidays/Holiday.png'),
      color: '#FFB02E',
    },
    TakeDays: {
      type: 'TakeDays',
      name: '调休天数',
      value: 0,
      icon: require('../../../../assets/images/holidays/TakeDays.png'),
      color: '#8687FF',
    },
  });
  const [size, setSize] = useState({
    width: document.documentElement.clientWidth,
    height: document.documentElement.clientHeight,
  });
  const [visible, setVisible] = useState({
    holiday: false,
    workTime: false,
    synchronize: false,
90
    calendar: false,
91
  });
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  const workTime = useRef([
    {
      icon: require('../../../../assets/images/holidays/icon1.png'),
      name: '上午',
      from: '',
      to: '',
      type: 1,
    },
    {
      icon: require('../../../../assets/images/holidays/icon2.png'),
      name: '下午',
      from: '',
      to: '',
      type: 2,
    },
    {
      icon: require('../../../../assets/images/holidays/icon3.png'),
      name: '晚上',
      from: '',
      to: '',
      type: 3,
    },
  ]); // 工作时间
  const workTimeMsg = useRef({});
  const calendarHoliday = useRef(new Map()); // 日历上得假期
  const queryData = useRef({ year: new Date().getFullYear(), month: new Date().getMonth() + 1 });
  const tableData = useRef([]);
120
  const editDateMsg = useRef({ isRest: '', date: '', msg: '', holidayOptions: '' });
邓超's avatar
邓超 committed
121
  const [currentDate, setCurrentDate] = useState(moment().format('YYYY-MM-DD'));
122
  const [form] = Form.useForm();
123
  const [formRest] = Form.useForm();
124 125 126 127 128
  useEffect(() => {
    document.querySelector('.ant-picker-content').setAttribute('border', 1);
    let year = new Date().getFullYear();
    let month = new Date().getMonth() + 1;
    getData(year, month);
邓超's avatar
邓超 committed
129
    resizeListener();
130 131 132
    window.addEventListener('resize', resizeListener);
    return () => {
      window.removeEventListener('resize', resizeListener);
133 134 135
      document.querySelectorAll('.ant-popconfirm').forEach(ele => {
        ele.style.zoom = 'normal';
      });
136 137 138
      document.querySelectorAll('.ant-select-dropdown').forEach(ele => {
        ele.style.zoom = 'normal';
      });
139 140 141
    };
  }, []);
  const resizeListener = () => {
邓超's avatar
邓超 committed
142
    console.log(document.documentElement.clientWidth);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    setSize({
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
    });
  };
  const getData = (year, month) => {
    GetWorkDayManageInfo({ year, month }).then(res => {
      if (res.code === 0) {
        calendarHoliday.current = new Map([]);
        res.data.CalendarHoliday.forEach(item => {
          calendarHoliday.current.set(item.DayDate, item);
        });

        tableData.current = res.data.HolidayList;
        workTime.current.forEach(item => {
          switch (item.name) {
            case '上午':
160 161
              item.from = res.data.WorkTime ? res.data.WorkTime.WORKTIME_MOR_FROM : '00:00';
              item.to = res.data.WorkTime ? res.data.WorkTime.WORKTIME_MOR_TO : '00:00';
162 163
              break;
            case '下午':
164 165
              item.from = res.data.WorkTime ? res.data.WorkTime.WORKTIME_AFT_FROM : '00:00';
              item.to = res.data.WorkTime ? res.data.WorkTime.WORKTIME_AFT_TO : '00:00';
166 167
              break;
            case '晚上':
168 169
              item.from = res.data.WorkTime ? res.data.WorkTime.WORKTIME_EVE_FROM : '00:00';
              item.to = res.data.WorkTime ? res.data.WorkTime.WORKTIME_EVE_TO : '00:00';
170 171 172 173 174 175 176 177
              break;

            default:
              break;
          }
        });
        console.log(workTime.current, 'workTime.current');
        const obj = JSON.parse(JSON.stringify(holidayStatistics));
178 179 180 181
        obj.Holidays.value = res.data.HolidayStatistics?.Holidays;
        obj.SumDays.value = res.data.HolidayStatistics?.SumDays;
        obj.TakeDays.value = res.data.HolidayStatistics?.TakeDays;
        obj.WeekendDays.value = res.data.HolidayStatistics?.WeekendDays;
182 183 184 185 186 187 188
        setHolidayStatistics(obj);
        // setFlag(flag + 1);
      }
    });
  };
  // 删除节假日
  const delRow = record => {
189 190
    console.log(record, 'record');
    let dayDate = record.DayDate;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    DeleteFlowHoliday({ dayDate }).then(res => {
      if (res.code === 0) {
        message.success('删除成功');
        getData(queryData.current.year, queryData.current.month);
      } else {
        message.error(res.msg);
      }
    });
  };

  // 日历日期变化监听
  const onPanelChange = (value, mode) => {
    console.log(value, mode, '监听');
    // eslint-disable-next-line no-underscore-dangle
    let time = value._d;
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    queryData.current.year = year;
    queryData.current.month = month;
    getData(year, month);
  };
213

214 215 216
  // 点击到的日期
  const onSelect = (value, obj) => {
    let date = value.format('YYYY-MM-DD');
217 218 219 220 221 222 223 224 225 226 227
    let options = tableData.current.map(item => ({
      value: item.DayName,
      label: item.DayName,
    }));
    editDateMsg.current = {
      isRest: calendarHoliday.current.has(date),
      date,
      msg: obj,
      holidayOptions: options,
    };
    setVisible({ ...visible, calendar: true });
228 229 230 231 232 233 234 235 236 237 238 239 240 241
  };
  // 日历渲染替换
  const dateCellRender = value => {
    // eslint-disable-next-line no-underscore-dangle
    let time = value._d;
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    let week = time.getDay();
    month = month < 10 ? `0${month}` : month;
    day = day < 10 ? `0${day}` : day;
    let obj = calendar.solar2lunar(year, month, day);
    let date = `${year}-${month}-${day}`;
    let holidayMsg = calendarHoliday.current?.get(date);
242
    let today = moment(new Date()).format('YYYY-MM-DD');
243 244 245 246 247 248 249
    return (
      <div
        key={date}
        className={classNames(styles.calendarCell, {
          [styles.otherMonthDay]: month != queryData.current.month,
          [styles.rest]: holidayMsg?.DayType === 1,
          [styles.holidays]: holidayMsg?.DayType === 3,
邓超's avatar
邓超 committed
250
          [styles.weekend]: week === 0 || week === 6,
251
          [styles.choose]: date === currentDate,
252
          [styles.today]: date === today,
253 254
        })}
        onDoubleClick={() => onSelect(value, obj)}
邓超's avatar
邓超 committed
255 256 257
        onClick={e => {
          setCurrentDate(date);
        }}
258 259 260
      >
        <div className={styles.tiemBox}>
          <p> {time.getDate()}</p>
邓超's avatar
邓超 committed
261 262 263 264
          <p> {obj.lunarFestival || obj.festival || obj.Term || obj.IDayCn}</p>
        </div>
        <div
          className={styles.tips}
265 266 267 268
          style={{
            display: week === 0 || week === 6 ? 'block' : 'none',
            background: calendarHoliday.current.has(date) ? '#FAAD14' : '#3D78FF',
          }}
邓超's avatar
邓超 committed
269
        >
270
          {calendarHoliday.current.has(date) ? '休' : '班'}
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
        </div>
        <div
          className={styles.icon}
          style={{ display: calendarHoliday.current.has(date) ? 'block' : 'none' }}
        />
      </div>
    );
  };
  // 日历头部渲染
  const headerRender = ({ value, onChange }) => {
    const start = 0;
    const end = 12;
    const monthOptions = [];
    const current = value.clone();
    const localeData = value.localeData();
    const months = [];
邓超's avatar
邓超 committed
287

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    for (let i = 0; i < 12; i++) {
      current.month(i);
      months.push(localeData.monthsShort(current));
    }
    for (let i = start; i < end; i++) {
      monthOptions.push(
        <Select.Option key={i} value={i} className="month-item">
          {months[i]}
        </Select.Option>,
      );
    }

    const year = value.year();
    const month = value.month();
    const options = [];
    for (let i = year - 10; i < year + 10; i += 1) {
      options.push(
        <Select.Option key={i} value={i} className="year-item">
          {i}
        </Select.Option>,
      );
    }
邓超's avatar
邓超 committed
310

311 312 313
    return (
      <div className={styles.calendarHeader}>
        <div className={styles.left}>
邓超's avatar
邓超 committed
314 315 316 317 318 319 320
          <LeftOutlined
            onClick={() => {
              let newYear = year - 1;
              const now = value.clone().year(newYear);
              onChange(now);
            }}
          />
321
          <Select
322
            style={{ margin: '0 5px', width: '80px' }}
323 324 325
            dropdownMatchSelectWidth={false}
            className="my-year-select"
            value={year}
邓超's avatar
邓超 committed
326 327 328 329 330 331 332 333
            onDropdownVisibleChange={() => {
              setTimeout(() => {
                console.log(document.querySelectorAll('.ant-select-dropdown'));
                document.querySelectorAll('.ant-select-dropdown').forEach(ele => {
                  ele.style.zoom = size.width / 1920;
                });
              }, 0);
            }}
334
            onChange={newYear => {
邓超's avatar
邓超 committed
335
              console.log(newYear);
336 337 338 339 340 341
              const now = value.clone().year(newYear);
              onChange(now);
            }}
          >
            {options}
          </Select>
邓超's avatar
邓超 committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
          <RightOutlined
            onClick={() => {
              let newYear = year + 1;
              const now = value.clone().year(newYear);
              onChange(now);
            }}
          />
          <LeftOutlined
            onClick={() => {
              let val = value.clone();
              let newMonth = month - 1;
              let now;
              if (newMonth < 1) {
                let newYear = year - 1;
                now = val.set({ year: newYear, month: 11 });
              } else {
                now = value.clone().month(newMonth);
              }
              console.log(now.format('YYYY-MM-DD'));
              onChange(now);
            }}
          />
364
          <Select
365
            style={{ margin: '0 5px', width: '80px' }}
366
            dropdownMatchSelectWidth={false}
邓超's avatar
邓超 committed
367 368 369 370 371 372 373 374
            onDropdownVisibleChange={() => {
              setTimeout(() => {
                console.log(document.querySelectorAll('.ant-select-dropdown'));
                document.querySelectorAll('.ant-select-dropdown').forEach(ele => {
                  ele.style.zoom = size.width / 1920;
                });
              }, 0);
            }}
375 376
            value={month}
            onChange={newMonth => {
邓超's avatar
邓超 committed
377
              console.log(newMonth);
378 379 380 381 382 383
              const now = value.clone().month(newMonth);
              onChange(now);
            }}
          >
            {monthOptions}
          </Select>
邓超's avatar
邓超 committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
          <RightOutlined
            onClick={() => {
              let val = value.clone();
              let newMonth = month + 1;
              let now;
              if (newMonth > 12) {
                newMonth = 1;

                now = val.year(year + 1).month(newMonth);
              } else {
                now = value.clone().month(newMonth);
              }

              onChange(now);
            }}
          />
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        </div>
        <div className={styles.right}>
          <Upload showUploadList={false} accept=".json" beforeUpload={beforeUpload}>
            <Button style={{ marginLeft: '10px' }} type="primary" ghost>
              <div style={{ display: 'flex', alignItems: 'center' }}>
                <ImportOutlined style={{ marginRight: '5px' }} />
                <div> 导入</div>
              </div>
            </Button>
          </Upload>
          <Button onClick={hadelExport} style={{ marginLeft: '10px' }} type="primary" ghost>
            <div style={{ display: 'flex', alignItems: 'center' }}>
              <ExportOutlined style={{ marginRight: '5px' }} />
              <div> 导出</div>
            </div>
          </Button>
          <Button
            onClick={() => setVisible({ ...visible, holiday: true })}
            style={{ marginLeft: '10px' }}
            type="primary"
          >
            <div style={{ display: 'flex', alignItems: 'center' }}>
              <PlusOutlined style={{ marginRight: '5px' }} />
              <div> 新增</div>
            </div>
          </Button>
          <Button
            onClick={() => setVisible({ ...visible, synchronize: true })}
            style={{ marginLeft: '10px', background: '#FAAD14', border: 'none', color: '#fff' }}
          >
            <div style={{ display: 'flex', alignItems: 'center' }}>
              <SyncOutlined style={{ marginRight: '5px' }} />
              <div>一键同步</div>
            </div>
          </Button>
        </div>
      </div>
    );
  };
邓超's avatar
邓超 committed
439

440 441 442 443 444 445 446 447 448
  // 导出
  const hadelExport = () => {
    window.location.href = DownLoadFlowHoliday({
      year: queryData.current.year,
    });
  };
  // 提交上传文件
  const beforeUpload = val => {
    console.log(val);
邓超's avatar
邓超 committed
449 450 451 452
    // if (!form.getFieldValue('fileName')) {
    //   message.info('请上传文件');
    //   return;
    // }
453 454 455 456 457 458 459
    const formData = new FormData();
    formData.append('_files', val);

    ImportFlowHoliday(formData)
      .then(res => {
        console.log(res);
        if (res.code === 0) {
邓超's avatar
邓超 committed
460
          getData(queryData.current.year, queryData.current.month);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
          message.success('上传成功');
        } else {
          message.error(res.msg);
        }
      })
      .catch(() => {
        message.error('网络异常,请稍后再试');
      });
  };

  // 保存工作时间
  const saveWorkTime = () => {
    setVisible({ ...visible, workTime: false });
    getData(queryData.current.year, queryData.current.month);
  };
  // 添加节假日
  const addHoliday = () => {
    setVisible({ ...visible, holiday: false });
    getData(queryData.current.year, queryData.current.month);
  };
  // 同步节假日
  const startSynchronize = () => {
    setVisible({ ...visible, synchronize: false });
    getData(queryData.current.year, queryData.current.month);
  };
  // 编辑工作时间
  const editWorkTime = value => {
    workTimeMsg.current = value;
    setVisible({ ...visible, workTime: true });
  };
邓超's avatar
邓超 committed
491 492 493 494 495 496
  const disabledDate = value => {
    return (
      value < moment(`${queryData.current.year}-${queryData.current.month}`).startOf('month') ||
      value > moment(`${queryData.current.year}-${queryData.current.month}`).endOf('month')
    );
  };
497 498 499 500 501
  // 日历保存回调
  const calendarSubmit = () => {
    setVisible({ ...visible, calendar: false });
    getData(queryData.current.year, queryData.current.month);
  };
502 503

  return (
邓超's avatar
邓超 committed
504 505 506 507 508
    <div
      className={styles.calendarPage}
      // style={{ transform: `scale(${size.width / 1920}, ${size.height / 960})` }}
      style={{ zoom: size.width / 1920 }}
    >
509 510 511 512
      {/* 左侧日历 */}
      <div className={styles.leftContent}>
        <div className={styles.calendarBox}>
          <Calendar
邓超's avatar
邓超 committed
513 514
            disabledDate={disabledDate}
            prevFarthestDate={false}
515 516 517 518
            locale={locale}
            dateFullCellRender={dateCellRender}
            onPanelChange={onPanelChange}
            headerRender={headerRender}
邓超's avatar
邓超 committed
519
            onSelect={() => {}}
520 521 522 523 524 525 526 527 528 529 530
          />
        </div>
        {/* 工作时间管理 */}
        <div className={styles.workTiemContainer}>
          <div className={styles.header}>
            <div className={styles.headerLeft}>
              <div className={styles.lineBox} />
              <div className={styles.text}>工作时间管理</div>
            </div>
          </div>
          <div className={styles.content}>
531
            {workTime.current?.map(item => (
532
              <div
533
                title="点击修改工作时间"
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
                className={classNames(styles.workTimeBox, {
                  [styles.mor]: item.name === '上午',
                  [styles.aft]: item.name === '下午',
                  [styles.eve]: item.name === '晚上',
                })}
                key={item.name}
                onClick={() => editWorkTime(item)}
              >
                <div className={styles.left}>
                  <div className={styles.icon}>
                    <img src={item.icon} alt="" />
                  </div>
                </div>
                <div className={styles.right}>
                  <div className={styles.title}>{item.name}</div>
                  <div className={styles.time}>
                    {item.from}-{item.to}
                  </div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
      <div className={styles.rightContent}>
        <div className={styles.header}>
          <div className={styles.lineBox} />
          <div className={styles.title}>节假日列表</div>
        </div>
        <div className={styles.statisticsBox}>
          {Object.values(holidayStatistics).map(item => (
            <div className={styles.statistics} type={item.type} key={item.name}>
              <div className={styles.left}>
                <div className={styles.icon}>
                  <img src={item.icon} alt="" />
                </div>
              </div>
              <div className={styles.right}>
                <div className={styles.desBox}>
                  <div className={styles.label}>{item.name}</div>
                  <div className={styles.value}>
                    <i>{item.value}</i>
                  </div>
                </div>
                <div className={styles.progress}>
                  <div
                    className={styles.rate}
                    style={{
                      background: item.color,
                      width: `${(item.value / holidayStatistics.SumDays.value) * 100}%`,
                    }}
                  />
                </div>
              </div>
            </div>
          ))}
        </div>
        <div className={styles.holidaysList}>
          {tableData.current.map((item, index) => (
            <div className={styles.itemBox} key={index}>
              <div className={styles.header}>
邓超's avatar
邓超 committed
595 596
                <div className={styles.title}>
                  {item.DayName}({item.DayDate?.split(',').length}天)
597 598 599 600 601 602 603 604 605
                  <img
                    src={require('/src/assets/images/holidays/Holiday.png')}
                    alt=""
                    style={{
                      width: '25px',
                      marginLeft: '10px',
                      display: holidaysList.has(item.DayName) ? 'inline-block' : 'none',
                    }}
                  />
邓超's avatar
邓超 committed
606 607 608 609 610 611 612 613 614 615 616 617
                </div>
                <div
                  className={styles.del}
                  onClick={() => {
                    setTimeout(() => {
                      console.log(document.querySelectorAll('.ant-popconfirm'));
                      document.querySelectorAll('.ant-popconfirm').forEach(ele => {
                        ele.style.zoom = size.width / 1920;
                      });
                    }, 0);
                  }}
                >
618 619 620 621 622 623
                  <Popconfirm
                    title="是否删除该节假日?"
                    onConfirm={() => delRow(item)}
                    onCancel={() => message.error('取消删除')}
                    okText="是"
                    cancelText="否"
邓超's avatar
邓超 committed
624
                    style={{ zoom: size.width / 1920 }}
625 626 627 628 629 630
                  >
                    <DeleteOutlined style={{ fontSize: '16px', color: '#e86060' }} />
                  </Popconfirm>
                </div>
              </div>
              <div className={styles.content}>
邓超's avatar
邓超 committed
631
                {item.DayDate?.split(',').map((ele, i) => <span key={i}>{ele}</span>)}
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
              </div>
            </div>
          ))}
        </div>
      </div>
      <WorkTiemConfig
        visible={visible.workTime}
        onSubumit={saveWorkTime}
        handleCancel={() => setVisible({ ...visible, workTime: false })}
        msg={workTimeMsg.current}
        allTime={workTime.current}
      />
      <AddModal
        visible={visible.holiday}
        onSubumit={addHoliday}
        handleCancel={() => setVisible({ ...visible, holiday: false })}
      />
      <Synchronize
        visible={visible.synchronize}
        onSubumit={startSynchronize}
        handleCancel={() => setVisible({ ...visible, synchronize: false })}
      />
654 655 656 657 658 659
      <HolidayConfig
        visible={visible.calendar}
        onSubumit={calendarSubmit}
        msg={editDateMsg.current}
        handleCancel={() => setVisible({ ...visible, calendar: false })}
      />
660 661 662 663 664
    </div>
  );
};

export default Holidays;