react+ant日历Calendar组件实现动态渲染节假日设置

林运浩
2023-12-01

前端页面代码

import React, { PureComponent,useState  } from 'react';
import { connect } from 'dva';
import { Button, Col, Form, Input, Row,Alert, Calendar,Tag } from 'antd';
import Panel from '../../../components/Panel';
import {HOLIDAY_SAVE, COURTKHTASK_REMOVE} from '../../../actions/Holidayaction';
import Grid from '../../../components/Sword/Grid';
import router from "umi/router";
const FormItem = Form.Item;
import moment from 'moment';
@connect(({ holiday, loading }) => ({
    holiday,
    loading: loading.models.holiday,
}))
@Form.create()
class Holiday extends PureComponent {
state={
    dayvalue:[],
    daystate:'',
}
    componentWillMount() {
        const {dispatch} = this.props;
        const param = {state:1}
        dispatch(HOLIDAY_SAVE({
            payload:param,
            callback:(response)=>{
                this.setState({
                    daystate:1,
                    dayvalue:response.data
                })
                console.log("请求返回节假日:",response)
            }
        }));
    }
    onPanelChange=(value, mode)=> {
        console.log(moment(value).format('YYYY-MM-DD HH:mm:ss')+"&&&"+mode)
    }
    onSelect = (newValue) => {
        const dayvalues = moment(newValue).format('YYYY-MM-D')
        let {daystate} = this.state
        const param = {
            date:dayvalues,
            state:1
        }
        // if(daystate == 1){
        //     alert("您点击速度过快,请稍后!")
        // }
        const {
            form,dispatch
        } = this.props;
        dispatch(HOLIDAY_SAVE({
            payload:param,
            callback:(response)=>{
                this.setState({
                    daystate:1,
                    dayvalue:response.data
                })
                console.log("请求返回节假日:",response)
            }
        }));
        // dayvalue.push(param)
        // console.log("dayvalue",dayvalues)
    };
   getListData=(value)=> {
        let listData;
        let attendanceDate = this.state.dayvalue
        let values = moment(value).format("YYYY-MM-D")
        for (let i = 0; i < attendanceDate.length; i++) {
            // let date=attendanceDate[i].date.split("-");//精确度仅判断到某一日时放开
            let valuesT = attendanceDate[i].date
            // if(values===valuesT){
            //     console.log("日期判断相等",values,valuesT)
            // }
            // console.log(date[2]);
            // if (value.date().toString()===date[2]){//精确度仅判断到某一日
            if(values===valuesT){//判断具体月份日期
                switch (attendanceDate[i].state) {
                    case 1:
                        listData=[
                            {color:'red',content:'节假日'}
                        ];
                        break;
                    default:
                }

            }
        }
        return listData || [];
    }
    dateCellRender=(value)=> {
        const listData = this.getListData(value);
        this.setState({
            daystate:0,
        })
        return (
            <ul className="events">
                {listData.map(item => (
                    <li key={item.content}>
                        <Tag color={item.color}>{item.content}</Tag>
                    </li>
                ))}
            </ul>
        );
    }
    render() {
        const code = 'holiday';

        // const { value, selectedValue } = this.state;

        return (
            <Panel>
                <Calendar className="attendanceCal"
                          onPanelChange={this.onPanelChange}
                          dateCellRender={this.dateCellRender}
                          onSelect={this.onSelect}/>
                {/*<Calendar style={{ backgroundColor: '#BCB7B7' }} onSelect={this.setBackground} onPanelChange={this.onPanelChange}/>*/}
            </Panel>
        );
    }
}
export default Holiday;
后端代码

controller

package com.dhcc.bpm.modules.ckpoc.controller;


import com.alibaba.fastjson.JSONObject;
import com.dhcc.bpm.modules.ckpoc.service.IPbcHolidaysService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("ckpoc/holidays")
@Api(value = "通知任务", tags = "通知任务接口")
public class PbcHolidaysController {
    @Autowired
    private IPbcHolidaysService pbcHolidaysService;

    @PostMapping("/save")
    public R save(@RequestBody JSONObject request){
        log.info("请求报文:{}",request);
        String date = request.getString("date");
        int state = request.getInteger("state");
        return pbcHolidaysService.save(date,state);
    }
}

entity

    package com.dhcc.bpm.modules.ckpoc.entity;
    
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.fasterxml.jackson.databind.annotation.JsonSerialize;
    import com.fasterxml.jackson.databind.ser.std.NullSerializer;
    import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
    import lombok.Data;
    
    @Data
    @TableName("PBC_HOLIDAYS")
    public class PbcHolidays {
    
        private static final long serialVersionUID = 1L;
        /**
         * 主键
         */
        @TableId("id")
        @JsonSerialize( using = ToStringSerializer.class, nullsUsing = NullSerializer.class)
        private Long id;
    
        private String date;
    
        private Integer state;
    }

serviceImpl

package com.dhcc.bpm.modules.ckpoc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.modules.ckpoc.entity.PbcHolidays;
import com.dhcc.bpm.modules.ckpoc.mapper.PbcHolidaysMapper;
import com.dhcc.bpm.modules.ckpoc.service.IPbcHolidaysService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@DS("dm")
public class PbcHolidaysServiceImpl extends ServiceImpl<PbcHolidaysMapper, PbcHolidays> implements IPbcHolidaysService {
    @Override
    public R save(String date, int state) {
        if(StrUtil.isEmpty(date)){
            JSONArray listh = JSON.parseArray(JSON.toJSONString(listPbcHolidays()));
            log.info("返回报文:{}",listh);
            return R.data(listh);
        }
        List<PbcHolidays> list = listPbcHolidays();
        List<String> str = new ArrayList<>();

        for (PbcHolidays p: list) {
            str.add(p.getDate());
        }

        if (0 == list.size()){
            log.info("表内无数据,直接入库");
            PbcHolidays hd = new PbcHolidays();
            hd.setDate(date);
            hd.setState(state);
            baseMapper.insert(hd);
            JSONArray listh = JSON.parseArray(JSON.toJSONString(listPbcHolidays()));
            log.info("返回报文:{}",listh);
            return R.data(listh);
        }

        for (int i = 0;i<str.size();i++) {
            String dates = str.get(i);
            if(date.equals(dates)){
                log.info("已存在,删除");
                QueryWrapper<PbcHolidays> wrapper = new QueryWrapper<>();
                wrapper.eq("DATE",date);
                baseMapper.delete(wrapper);
                JSONArray listh = JSON.parseArray(JSON.toJSONString(listPbcHolidays()));
                log.info("返回报文:{}",listh);
                return R.data(listh);
            }
        }
        log.info("保存");
        PbcHolidays hd1 = new PbcHolidays();
        hd1.setDate(date);
        hd1.setState(state);
        baseMapper.insert(hd1);
        JSONArray listh = JSON.parseArray(JSON.toJSONString(listPbcHolidays()));
        log.info("返回报文:{}",listh);
        return R.data(listh);
    }

    /**
     * 全查PBC_HOLIDAYS 返回list
     * @return
     */
    public List<PbcHolidays> listPbcHolidays(){
        QueryWrapper<PbcHolidays> wrapper = new QueryWrapper<>();
        List<PbcHolidays> list = baseMapper.selectList(wrapper);
        return list;
    }
}

 类似资料: