SpringBoot:增删改查(查询列表,根据id查询)

SpringBoot:增删改查(查询列表,根据id查询),第1张

SpringBoot:增删改查(查询列表,根据id查询)

Entity: 

import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.unicom.netai.mcmor.common.annotation.Excel;
import com.unicom.netai.mcmor.common.core.domain.baseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;


public class TimePermission extends baseEntity
{
    private static final long serialVersionUID = 1L;

    
    private Long id;

    
    @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
    private Long userId;

    
    @Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
    private String userName;

    
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "开始时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date beginTime;

    
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "结束时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date endTime;

    
    @Excel(name = "是否处于修改状态,1是,0否")
    private Integer status;

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long getId()
    {
        return id;
    }
    public void setUserId(Long userId)
    {
        this.userId = userId;
    }

    public Long getUserId()
    {
        return userId;
    }
    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getUserName()
    {
        return userName;
    }
    public void setBeginTime(Date beginTime)
    {
        this.beginTime = beginTime;
    }

    public Date getBeginTime()
    {
        return beginTime;
    }
    public void setEndTime(Date endTime)
    {
        this.endTime = endTime;
    }

    public Date getEndTime()
    {
        return endTime;
    }
    public void setStatus(Integer status)
    {
        this.status = status;
    }

    public Integer getStatus()
    {
        return status;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("userId", getUserId())
                .append("userName", getUserName())
                .append("beginTime", getBeginTime())
                .append("endTime", getEndTime())
                .append("createTime", getCreateTime())
                .append("updateTime", getUpdateTime())
                .append("status", getStatus())
                .toString();
    }
}

Request: 

public class TimePermissionAddReq {

    private Long id;

    private Long userId;

    
    private String userName;

    
    private String beginTime;

    
    private String endTime;

    private Integer status;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
}

 Respose:

public class TimePermissionResponse {

    
    private Long id;

    
    private Long userId;

    
    private String userName;

    
    private String createTime;

    
    private String updateTime;

    
    private String beginTime;

    
    private String endTime;

    private Integer status;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


    public String getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }
}

 Service:

import com.unicom.netai.mcmor.common.core.domain.AjaxResult;
import com.unicom.netai.mcmor.manager.model.request.TimePermissionAddReq;
import com.unicom.netai.mcmor.manager.model.response.TimePermissionResponse;

import java.util.List;
public interface ITimePermissionService
{
    
    TimePermissionResponse QueryById(Long id);

    
    List QueryList() ;

    
    AjaxResult addOrUpdate(TimePermissionAddReq tmpR);

    
    void deleteTimePermissionById(Long[] ids);
}

ServiceImpl:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.unicom.netai.mcmor.common.core.domain.AjaxResult;
import com.unicom.netai.mcmor.manager.domain.entity.TimePermission;
import com.unicom.netai.mcmor.manager.domain.mapper.TimePermissionMapper;
import com.unicom.netai.mcmor.manager.model.request.TimePermissionAddReq;
import com.unicom.netai.mcmor.manager.model.response.TimePermissionResponse;
import com.unicom.netai.mcmor.manager.service.ITimePermissionService;
import com.unicom.netai.mcmor.manager.util.TimeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class TimePermissionServiceImpl implements ITimePermissionService {

    @Autowired
    private TimePermissionMapper timePermissionMapper;

    
    @Override
    public List QueryList() {

        List responses = new ArrayList<>();

        TimePermission timePermission = new TimePermission();
        List timePermissions = timePermissionMapper.selectTimePermissionList(timePermission);
        for(TimePermission ttimeP : timePermissions){

            TimePermissionResponse response = new TimePermissionResponse();
            response.setId(ttimeP.getId());
            response.setUserId(ttimeP.getUserId());
            response.setUserName(ttimeP.getUserName());
            response.setCreateTime(TimeUtils.getStringDateYYYYMMDDhhmmss(ttimeP.getCreateTime()));
            response.setUpdateTime(TimeUtils.getStringDateYYYYMMDDhhmmss(ttimeP.getUpdateTime()));
            response.setBeginTime(TimeUtils.getStringDateYYYYMMDDhhmmss(ttimeP.getBeginTime()));
            response.setEndTime(TimeUtils.getStringDateYYYYMMDDhhmmss(ttimeP.getEndTime()));
            response.setStatus(ttimeP.getStatus());
            responses.add(response);
        }
        return responses;
    }

    @Override
    public TimePermissionResponse QueryById(Long id) {
        TimePermission timePermission = timePermissionMapper.selectTimePermissionById(id);
        if(timePermission == null){
            return null;
        }

        TimePermissionResponse response = new TimePermissionResponse();
        BeanUtils.copyProperties(timePermission, response);

        TimePermissionResponse responsebyid = new TimePermissionResponse();
        responsebyid.setId(timePermission.getId());
        responsebyid.setUserId(timePermission.getUserId());
        responsebyid.setUserName(timePermission.getUserName());
        responsebyid.setCreateTime(TimeUtils.getStringDateYYYYMMDDhhmmss(timePermission.getCreateTime()));
        responsebyid.setUpdateTime(TimeUtils.getStringDateYYYYMMDDhhmmss(timePermission.getUpdateTime()));
        responsebyid.setBeginTime(TimeUtils.getStringDateYYYYMMDDhhmmss(timePermission.getBeginTime()));
        responsebyid.setEndTime(TimeUtils.getStringDateYYYYMMDDhhmmss(timePermission.getEndTime()));
        response.setStatus(timePermission.getStatus());

        return responsebyid;
    }

    @Override
    public AjaxResult addOrUpdate(TimePermissionAddReq tmp) {

        Long id = tmp.getId();
        if (id == null) {
            return insertData(tmp);
        } else {
            return updateData(tmp);
        }
    }

    private AjaxResult insertData(TimePermissionAddReq tmpR) {

        TimePermission ttmpcheck = new TimePermission();
        ttmpcheck.setId(tmpR.getId());
        ttmpcheck.setUserId(tmpR.getUserId());
        List ttmplist = timePermissionMapper.selectTimePermissionList(ttmpcheck);
        if (CollectionUtils.isNotEmpty(ttmplist)){
            return AjaxResult.error("数据已存在");
        }else{
            TimePermission ttmpdata = new TimePermission();
            ttmpdata.setUserName(tmpR.getUserName());
            ttmpdata.setUserId(tmpR.getUserId());

            Date strBeginDate = TimeUtils.strToDateYYYYMMDDhhmmss(tmpR.getBeginTime());
            Date strEndDate = TimeUtils.strToDateYYYYMMDDhhmmss(tmpR.getEndTime());
            ttmpdata.setBeginTime(strBeginDate);
            ttmpdata.setEndTime(strEndDate);

            Date timenow = new Date();
            ttmpdata.setCreateTime(timenow);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String curT = sdf.format(timenow);
            try {
                long now = sdf.parse(curT).getTime();
                long begin = sdf.parse(tmpR.getBeginTime()).getTime();
                long end = sdf.parse(tmpR.getEndTime()).getTime();
                if (now=begin){
                    ttmpdata.setStatus(1);
                }else{
                    ttmpdata.setStatus(0);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }


            timePermissionMapper.insertTimePermission(ttmpdata);
            return AjaxResult.success();
        }
    }

    private AjaxResult updateData(TimePermissionAddReq tmpR) {

        TimePermission ttmpdata = new TimePermission();
        ttmpdata.setId(tmpR.getId());
        ttmpdata.setUserId(tmpR.getUserId());
        Date strBeginDate = TimeUtils.strToDateYYYYMMDDhhmmss(tmpR.getBeginTime());
        Date strEndDate = TimeUtils.strToDateYYYYMMDDhhmmss(tmpR.getEndTime());
        ttmpdata.setBeginTime(strBeginDate);
        ttmpdata.setEndTime(strEndDate);

        Date timenow = new Date();
        ttmpdata.setUpdateTime(timenow);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curT = sdf.format(timenow);
        try {
            long now = sdf.parse(curT).getTime();
            long begin = sdf.parse(tmpR.getBeginTime()).getTime();
            long end = sdf.parse(tmpR.getEndTime()).getTime();
            if (now=begin){
                ttmpdata.setStatus(1);
            }else{
                ttmpdata.setStatus(0);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


        timePermissionMapper.updateTimePermission(ttmpdata);
        return AjaxResult.success();
    }

    
    @Override
    public void deleteTimePermissionById(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }

        timePermissionMapper.deleteTimePermissionByIds(ids);
    }
}

 TimeUtils:

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {

    static DateFormat format = new SimpleDateFormat("yyyy-MM");

    static DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");

    static DateFormat format2 = new SimpleDateFormat("yyyy年MM月份");

    static DateFormat format3 = new SimpleDateFormat("yyyy年MM月dd日");

    static DateFormat format4 = new SimpleDateFormat("MM月dd日");

    static DateFormat format5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    static DateFormat format6 = new SimpleDateFormat("yyyy");

    static DateFormat format7 = new SimpleDateFormat("MM");

    static DateFormat format8 = new SimpleDateFormat("HH:mm:ss");




    
    public static Date strToDateYYYYMM(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format.parse(strDate, pos);
        return strtodate;
    }


    
    public static Date strToDateYYYY(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format6.parse(strDate, pos);
        return strtodate;
    }
    
    public static Date strToDateYYYYMMDD(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format1.parse(strDate, pos);
        return strtodate;
    }

    
    public static Date strToDateYYYYMMDDhhmmss(String strDate){
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format5.parse(strDate, pos);
        return strtodate;
    }


    public static String getStringDateYYYYMM(Date d) {
        String dateString = format.format(d);
        return dateString;
    }

    public static String getStringDateYYYYMMToYearMonth(String d) {
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format.parse(d, pos);
        return format2.format(strtodate);
    }

    public static String getStringDateYYYYMMDD(Date d) {
        String dateString = format1.format(d);
        return dateString;
    }

    public static String getStringDateYYYYMMDDhhmmss(Date d) {
        String dateString = format5.format(d);
        return dateString;
    }

    public static String getStringDateHHMMSS(Date d) {
        String dateString = format8.format(d);
        return dateString;
    }

    public static Object getStringDateYYYYMMToYearMonthDay(String time) {
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format1.parse(time, pos);
        return format3.format(strtodate);
    }

    public static String getStringDateYYYYMMToMonthDay(String thisWeekTimeTo) {

        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format1.parse(thisWeekTimeTo, pos);
        return format4.format(strtodate);
    }


    public static String getStringDateYYYY(Date d) {
        String dateString = format6.format(d);
        return dateString;
    }

    public static String getStringDateMM(Date d) {
        String dateString = format7.format(d);
        return dateString;
    }
}

Controller:

import com.unicom.netai.mcmor.common.core.controller.baseController;
import com.unicom.netai.mcmor.common.core.domain.AjaxResult;
import com.unicom.netai.mcmor.manager.model.request.TimePermissionAddReq;
import com.unicom.netai.mcmor.manager.model.response.TimePermissionResponse;
import com.unicom.netai.mcmor.manager.service.ITimePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/timepermission")
public class TimePermissionController extends baseController {
    @Autowired
    private ITimePermissionService timePermissionService;

    
    @PostMapping("/queryList")
    public AjaxResult list() {
        try {
            List list = timePermissionService.QueryList();
            return AjaxResult.success(list);
        } catch (Exception e) {
            logger.error("TimePermissionController queryList error : ", e);
        }

        return AjaxResult.error();
    }

    
    @GetMapping(value = "/queryById")
    public AjaxResult getInfo(@RequestParam("id") Long id) {
        try {
            TimePermissionResponse response = timePermissionService.QueryById(id);
            return AjaxResult.success(response);
        } catch (Exception e) {
            logger.error("TimePermissionController queryById error : ", e);
        }

        return AjaxResult.error();
    }

    
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody TimePermissionAddReq tmpR) {
        try {
            return timePermissionService.addOrUpdate(tmpR);
        } catch (Exception e) {
            logger.error("TimePermissionController addOrUpdate error : ", e);
        }

        return AjaxResult.error();
    }

    
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        try {
            timePermissionService.deleteTimePermissionById(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("TimePermissionController remove error : ", e);
        }

        return AjaxResult.error();
    }
}

结束!!! 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4828367.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-10
下一篇 2022-11-10

发表评论

登录后才能评论

评论列表(0条)

保存