package com.dian.service;

import com.dian.domain.Jilu;
import com.dian.mapper.JiluMapper;
import com.dian.util.DateUtil;
import com.dian.vo.Tongji;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Service class for managing Jilu records.
 */
@Service
public class JiluService {

    @Resource
    private JiluMapper jiluMapper;

    public boolean deleteByPrimaryKey(Integer id) {
        return jiluMapper.deleteByPrimaryKey(id) == 1;
    }

    public boolean insert(Jilu entify) {
        return jiluMapper.insert(entify) == 1;
    }

    public boolean insertSelective(Jilu entify) {
        return jiluMapper.insertSelective(entify) == 1;
    }

    public Jilu selectByPrimaryKey(Integer id) {
        return jiluMapper.selectByPrimaryKey(id);
    }

    public boolean updateByPrimaryKeySelective(Jilu entify) {
        return jiluMapper.updateByPrimaryKeySelective(entify) == 1;
    }

    public boolean updateByPrimaryKey(Jilu entify) {
        return jiluMapper.updateByPrimaryKey(entify) == 1;
    }

    public Page<Jilu> selectByPage(Integer startId, Integer pageIndex, Integer pageSize, String searchKey) {
        Page<Jilu> pageVenderList = PageHelper.startPage(pageIndex, pageSize);
        jiluMapper.query(pageIndex, pageSize, searchKey);
        return pageVenderList;
    }

    public Tongji getTongjiByDay(Integer days) {
        Tongji tongji = new Tongji();
        List<Map<String, Double>> jilus = jiluMapper.queryByDay(days);
        if(null != jilus && jilus.size() > 0){
            String[] x = new String[jilus.size()];
            String[] y = new String[jilus.size()];
            for(int i =0;i<jilus.size();i++){
                Map<String, Double> map = jilus.get(i);
                   try {
                       x[i] = DateUtil.formatWeb(map.get('riqi')+"",DateUtil.WEB_FORMAT);
                       y[i] = map.get('danjia')+"";
                   } catch (ParseException e) {
                       e.printStackTrace();
                   }
            }
            tongji.setY(y);
            tongji.setX(x);
        }
        return tongji;
    }
}

This code represents a Java service class named JiluService. It provides methods for managing Jilu records, including basic CRUD operations (Create, Read, Update, Delete), pagination, and statistical analysis. The class relies on the JiluMapper for database interactions and uses PageHelper for efficient pagination. The getTongjiByDay method stands out as it retrieves daily statistics for Jilu records, specifically focusing on their price information.

Key aspects:

  • CRUD operations: The service offers methods for deleting, inserting, updating, and retrieving Jilu records based on their primary key (id).
  • Pagination: The selectByPage method utilizes PageHelper to enable pagination for retrieving Jilu records, allowing for efficient retrieval of large datasets in manageable chunks.
  • Statistical analysis: The getTongjiByDay method calculates daily statistics for Jilu records, extracting the date and price information from a list of maps returned by the queryByDay method of JiluMapper. It then organizes this data into arrays for Tongji object, providing a structured representation of daily price variations.

Important notes:

  • Database Interaction: The JiluMapper is assumed to be responsible for handling database interactions, with methods like deleteByPrimaryKey, insert, selectByPrimaryKey, updateByPrimaryKeySelective, updateByPrimaryKey, query, and queryByDay. These methods are not shown in the provided code but are critical for the service's functionality.
  • Tongji object: The Tongji object is used to encapsulate the daily statistics, storing the dates (x array) and corresponding prices (y array) for visualization or further processing.
  • Error handling: The getTongjiByDay method includes basic error handling using a try-catch block to handle potential ParseException during date formatting. However, more robust error handling strategies may be needed depending on the application context.

Overall, this JiluService demonstrates a well-structured service class providing core functionality for managing and analyzing Jilu records. It incorporates best practices like dependency injection (@Resource), clear method names, and basic error handling to ensure maintainability and robustness.

JiluService - Java Service for Jilu Management

原文地址: https://www.cveoy.top/t/topic/kLTp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录