记一次奇葩的数据结构Java实现过程

为了使博客更醒目,先把返回数据贴出来

{
    "code": "1",
    "msg": "获取数据成功",
    "resultData": {
        "Sexs": [
            {
                "Name": "男",
                "Value": "1085"
            },
            {
                "Name": "女",
                "Value": "516"
            }
        ],
        "SexAges": [
            {
                "Sex": "男",
                "Ages": [
                    {
                        "Name": "0~20",
                        "Value": "17"
                    },
                    {
                        "Name": "21~30",
                        "Value": "386"
                    },
                    {
                        "Name": "31~40",
                        "Value": "298"
                    },
                    {
                        "Name": "41~50",
                        "Value": "239"
                    },
                    {
                        "Name": "51~60",
                        "Value": "130"
                    },
                    {
                        "Name": "60以上",
                        "Value": "15"
                    }
                ]
            },
            {
                "Sex": "女",
                "Ages": [
                    {
                        "Name": "0~20",
                        "Value": "28"
                    },
                    {
                        "Name": "21~30",
                        "Value": "189"
                    },
                    {
                        "Name": "31~40",
                        "Value": "138"
                    },
                    {
                        "Name": "41~50",
                        "Value": "91"
                    },
                    {
                        "Name": "51~60",
                        "Value": "52"
                    },
                    {
                        "Name": "60以上",
                        "Value": "18"
                    }
                ]
            }
        ]
    }
}

接下来是实现代码

package com.faw.xxx.controller.report;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.faw.xxx.common.utils.Result;
import com.faw.xxx.service.report.ReportAgesexcompositionService;
@RestController
@RequestMapping("/V1/JP")
public class ReportAgesexcompositionController {

	@Autowired
	private ReportAgesexcompositionService service;
	/**
	 * 年龄比例及性别构成
	 * @param storeId
	 * @return
	 */
	@RequestMapping(value = "/GetAgeSexComposition", method = RequestMethod.GET)
    public Result getAgeSexComposition(String storeId) {
    	Result result=new Result();
    	return service.getAgeSexComposition(result, storeId);  	
    }
}

package com.faw.xxx.service.report;

import com.faw.xxx.common.utils.Result;

public interface ReportAgesexcompositionService {

	Result getAgeSexComposition(Result result, String storeId);
}

package com.faw.xxx.service.impl.report;

import com.faw.xxx.common.enums.StatusEnum;
import com.faw.xxx.common.utils.Result;
import com.faw.xxx.dao.report.ReportAgesexcompositionDAO;
import com.faw.xxx.entity.report.ReportAgesexcomposition;
import com.faw.xxx.service.report.ReportAgesexcompositionService;
import com.faw.xxx.vo.AgeSexCompositionModelVO;
import com.faw.xxx.vo.AgeSexCompositionResponseVO;
import com.faw.xxx.vo.ChartModelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ReportAgesexcompositionServiceImpl implements ReportAgesexcompositionService {

	@Autowired
	private ReportAgesexcompositionDAO dao;
	@Override
	public Result getAgeSexComposition(Result result, String storeId) {
		List<ReportAgesexcomposition> list = dao.getAgeSexComposition(storeId);
		AgeSexCompositionResponseVO res = new AgeSexCompositionResponseVO();
		List<ChartModelVO> sexs = new ArrayList<>();
		List<AgeSexCompositionModelVO> sexAges = new ArrayList<>();
		if (list != null)
		{
			for(ReportAgesexcomposition item : list)
			{
				ChartModelVO sex = new ChartModelVO();
				AgeSexCompositionModelVO agesex = new AgeSexCompositionModelVO();
				List<ChartModelVO> ages = new ArrayList<>();
				int count = 0;
				String sexName = (item.getSex()!=null&&item.getSex().contains("2")) ? "女" : "男";
				sex.setName(sexName) ;
				agesex.setSex(sexName);
				if (item.getAge20() > 0)
				{
					count = count + item.getAge20();
					ages.add(new ChartModelVO("0~20",String.valueOf(item.getAge20())));
				}
				if (item.getAge30() > 0)
				{
					count = count + item.getAge30();
					ages.add(new ChartModelVO("21~30",String.valueOf(item.getAge30())));
				}
				if (item.getAge40() > 0)
				{
					count = count + item.getAge40();
					ages.add(new ChartModelVO("31~40",String.valueOf(item.getAge40())));
				}
				if (item.getAge50() > 0)
				{
					count = count + item.getAge50();
					ages.add(new ChartModelVO("41~50",String.valueOf(item.getAge50())));
				}
				if (item.getAge60() > 0)
				{
					count = count + item.getAge60();
					ages.add(new ChartModelVO("51~60",String.valueOf(item.getAge60())));
				}
				if (item.getAge70() > 0)
				{
					count = count + item.getAge70();
					ages.add(new ChartModelVO("60以上",String.valueOf(item.getAge70())));
				}
				sex.setValue(String.valueOf(count));
				sexs.add(sex);
				agesex.setAges(ages);
				sexAges.add(agesex);
			}
		}
		res.setSexs(sexs);
		res.setSexAges(sexAges);
		result.put("code", StatusEnum.SUCCESS.getCode()).put("msg", StatusEnum.SUCCESS.getMessage())
				.put("resultData", res);
		return result;

	}

}

package com.faw.xxx.dao.report;

import com.faw.xxx.entity.report.ReportAgesexcomposition;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface ReportAgesexcompositionDAO {
    int insert(ReportAgesexcomposition record);

    int insertSelective(ReportAgesexcomposition record);
    
    List<ReportAgesexcomposition> getAgeSexComposition(String storeId);

}

对应的实体类、VO类、工具类

package com.faw.xxx.entity.report;

import lombok.Data;

import java.io.Serializable;

/**
 * report_agesexcomposition
 * @author 
 */
@Data
public class ReportAgesexcomposition implements Serializable {
    private String storeId;

    private String sex;

    private Integer age20;

    private Integer age30;

    private Integer age40;

    private Integer age50;

    private Integer age60;

    private Integer age70;

    private static final long serialVersionUID = 1L;
}
package com.faw.xxx.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.List;

@Data
public class AgeSexCompositionModelVO {
    @JsonProperty("Sex")
    private String sex;
    @JsonProperty("Ages")
    private List<ChartModelVO> ages;
}

package com.faw.xxx.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class ChartModelVO {
    public ChartModelVO() {
    }

    public ChartModelVO(String name, String value) {
        this.name = name;
        this.value = value;
    }

    @JsonProperty("Name")
    private String name;
    @JsonProperty("Value")
    private String value;
}

<select id="getAgeSexComposition" parameterType="java.lang.String" resultMap="BaseResultMap">
  	SELECT Sex, age20, age30, age40, age50, age60, age70 FROM Report_AgeSexComposition
  	WHERE StoreId = #{storeId,jdbcType=VARCHAR}
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值