记一次奇葩的数据结构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>

你可能感兴趣的:(数据结构,数据结构,java)