(笔记)当界面需求较少时直接在DTO类中写逻辑,省去service层

 需求:开发月度报表、总报表、折线图

像这种,就简单加三个查询,我个人认为可以直接省去service层代码,只使用controller层和mapper层即可。虽说三层架构的初衷也是为了解耦,但是这种只有查询的代码完全可以省去中间两个文件。

/**
 * @Description 采购组织、负责人查询实体类
 * @Author jink
 * @Date 2024/5/9 16:31
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OrgDTO {

    //采购部门
    private String departname;

    //接受到前端的时间数组
    private String[] time;

    //起始时间
    private String begin;

    //结束时间
    private String end;

    //负责人
    private String user;

    //1:月份查询 0:其他
    private Short flag;

以上是基础的字段,现在需求,根据报表的结构分为三种不同的情况:

1:月度表,默认查询近一年的数据,如果有查询条件则按照查询条件,以月份维度进行查询

2:总表,按照月为单位查询,查起始月份1号至终点月份31号

3:折线图,默认查询近十二个月,以日期维度进行查询

由于我是全栈,所以前后端都是我做(╯︿╰),为了省力气,我直接用的同一个时间范围查询器,虽然在前端我已经将回传的格式转换为yyyy-MM格式,但是由于后端的查询逻辑需要的格式都不相同,例如因为月度表由于需要以yyyy.MM格式在前端展示,因此在分组时我就已经将时间format成了yyyy.MM的格式了,因此查询需要yyyy.mm的格式,但是其他两个又不需要这样的操作,因此做了不同的判断。话不多说,直接贴代码,此文章仅作为一种省力气的不规范模式进行编写,不建议参考,但是这又何尝不是工厂模式的体现。

/**
 * @Description 采购组织、负责人查询实体类
 * @Author jink
 * @Date 2024/5/9 16:31
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OrgDTO {

    //采购部门
    private String departname;

    //接受到前端的时间数组
    private String[] time;

    //起始时间
    private String begin;

    //结束时间
    private String end;

    //负责人
    private String user;

    //1:月份查询 0:其他
    private Short flag;

    //统一时间查询的格式为:yyyy.MM
    private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM");

    public String getBegin() {
        try {
            switch (this.flag) {
                case 0: {
                    if (this.time.length > 0 && this.time[0] != null) {
                        return this.time[0].split("-")[0] + "." + this.time[0].split("-")[1];
                    } else {
                        return LocalDate.now().minusMonths(12).toString().split("-")[0] + "." + LocalDate.now().minusMonths(12).toString().split("-")[1];
                    }
                }
                case 1: {
                    if (this.time.length > 0 && this.time[0] != null) {
                        return time[0];
                    } else {
                        return LocalDate.now().minusMonths(12).toString().split("-")[0] + "." + LocalDate.now().minusMonths(12).toString().split("-")[1] + ".01";
                    }
                }
                case 2: {
                    if (this.time.length > 0 && this.time[0] != null) {
                        return time[0];
                    }
                }
            }
            return begin;
        } catch (Exception e) {
            return begin;
        }
    }

    public String getEnd() {
        try {
            switch (this.flag) {
                case 0: {
                    if (this.time.length > 0 && this.time[1] != null) {
                        return this.time[1].split("-")[0] + "." + this.time[1].split("-")[1];
                    } else {
                        return LocalDate.now().toString().split("-")[0] + "." + LocalDate.now().toString().split("-")[1];
                    }
                }
                case 1: {
                    if (this.time.length > 0 && this.time[1] != null) {
                        return time[1];
                    } else {
                        return LocalDate.now().toString().split("-")[0] + "." + LocalDate.now().toString().split("-")[1] + ".31";
                    }
                }
                case 2: {
                    if (this.time.length > 0 && this.time[1] != null) {
                        return time[1];
                    }
                }
            }
            return end;
        } catch (Exception e) {
            return end;
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值