日期和sql优化

This commit is contained in:
季圣华
2021-06-09 20:29:36 +08:00
parent f04dcd220c
commit 99e7db36d3
30 changed files with 361 additions and 243 deletions

View File

@@ -13,6 +13,14 @@ public class BusinessConstants {
* 默认的日期格式
*/
public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 一天的初始时间
*/
public static final String DAY_FIRST_TIME = " 00:00:00";
/**
* 一天的结束时间
*/
public static final String DAY_LAST_TIME = " 23:59:59";
/**
* 默认的分页起始页页码
*/

View File

@@ -1,6 +1,7 @@
package com.jsh.erp.controller;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.AccountHeadVo4Body;
import com.jsh.erp.datasource.entities.AccountHeadVo4ListEx;
@@ -76,7 +77,7 @@ public class AccountHeadController {
Map<String, Object> map = new HashMap<String, Object>();
try {
JSONObject outer = new JSONObject();
endTime = endTime + " 23:59:59";
endTime = endTime + BusinessConstants.DAY_LAST_TIME;
BigDecimal sum = accountHeadService.findTotalPay(supplierId, endTime, supType);
outer.put("getAllMoney", sum);
map.put("rows", outer);

View File

@@ -2,6 +2,7 @@ package com.jsh.erp.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.DepotHead;
import com.jsh.erp.datasource.entities.DepotHeadVo4Body;
@@ -96,6 +97,8 @@ public class DepotHeadController {
Map<String, Object> map = new HashMap<String, Object>();
try {
List<DepotHeadVo4InDetail> resList = new ArrayList<DepotHeadVo4InDetail>();
beginTime = Tools.parseDayToTime(beginTime, BusinessConstants.DAY_FIRST_TIME);
endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
List<DepotHeadVo4InDetail> list = depotHeadService.findByAll(beginTime, endTime, type, materialParam, depotId, oId, (currentPage-1)*pageSize, pageSize);
int total = depotHeadService.findByAllCount(beginTime, endTime, type, materialParam, depotId, oId);
map.put("total", total);
@@ -143,8 +146,8 @@ public class DepotHeadController {
Map<String, Object> map = new HashMap<String, Object>();
try {
List<DepotHeadVo4InOutMCount> resList = new ArrayList<>();
beginTime = beginTime + " 00:00:00";
endTime = endTime + " 23:59:59";
beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
List<DepotHeadVo4InOutMCount> list = depotHeadService.findInOutMaterialCount(beginTime, endTime, type, materialParam, depotId, oId, (currentPage-1)*pageSize, pageSize);
int total = depotHeadService.findInOutMaterialCountTotal(beginTime, endTime, type, materialParam, depotId, oId);
map.put("total", total);
@@ -193,8 +196,8 @@ public class DepotHeadController {
} else if (supType.equals("供应商")) { //供应商
j = -1;
}
beginTime = beginTime + " 00:00:00";
endTime = endTime + " 23:59:59";
beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
List<DepotHeadVo4StatementAccount> resList = new ArrayList<DepotHeadVo4StatementAccount>();
List<DepotHeadVo4StatementAccount> list = depotHeadService.findStatementAccount(beginTime, endTime, organId, supType, (currentPage-1)*pageSize, pageSize);
int total = depotHeadService.findStatementAccountCount(beginTime, endTime, organId, supType);
@@ -277,7 +280,7 @@ public class DepotHeadController {
Map<String, Object> map = new HashMap<String, Object>();
try {
JSONObject outer = new JSONObject();
endTime = endTime + " 23:59:59";
endTime = endTime + BusinessConstants.DAY_LAST_TIME;
BigDecimal sum = depotHeadService.findTotalPay(supplierId, endTime, supType);
outer.put("getAllMoney", sum);
map.put("rows", outer);
@@ -369,8 +372,8 @@ public class DepotHeadController {
BaseResponseInfo res = new BaseResponseInfo();
Map<String, Object> map = new HashMap<String, Object>();
try {
String today = Tools.getNow() + " 00:00:00";
String firstDay = Tools.getCurrentMonth() + "-01 00:00:00";
String today = Tools.getNow() + BusinessConstants.DAY_FIRST_TIME;
String firstDay = Tools.firstDayOfMonth(Tools.getCurrentMonth()) + BusinessConstants.DAY_FIRST_TIME;
BigDecimal todaySale = depotHeadService.getBuyAndSaleStatistics("出库", "销售",
1, today, getNow3()); //今日销售出库
BigDecimal todayRetailSale = depotHeadService.getBuyAndSaleRetailStatistics("出库", "零售",

View File

@@ -265,8 +265,8 @@ public class DepotItemController {
BaseResponseInfo res = new BaseResponseInfo();
Map<String, Object> map = new HashMap<String, Object>();
Long tenantId = redisService.getTenantId(request);
String timeA = monthTime+"-01 00:00:00";
String timeB = Tools.lastDayOfMonth(monthTime)+" 23:59:59";
String timeA = Tools.firstDayOfMonth(monthTime) + BusinessConstants.DAY_FIRST_TIME;
String timeB = Tools.lastDayOfMonth(monthTime) + BusinessConstants.DAY_LAST_TIME;
try {
List<DepotItemVo4WithInfoEx> dataList = depotItemService.findByAll(StringUtil.toNull(materialParam),
timeB,(currentPage-1)*pageSize, pageSize);
@@ -335,8 +335,8 @@ public class DepotItemController {
@RequestParam("materialParam") String materialParam,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Long tenantId = redisService.getTenantId(request);
String timeA = monthTime+"-01 00:00:00";
String timeB = Tools.lastDayOfMonth(monthTime)+" 23:59:59";
String timeA = Tools.firstDayOfMonth(monthTime) + BusinessConstants.DAY_FIRST_TIME;
String timeB = Tools.lastDayOfMonth(monthTime) + BusinessConstants.DAY_LAST_TIME;
try {
List<DepotItemVo4WithInfoEx> dataList = depotItemService.findByAll(StringUtil.toNull(materialParam),
timeB, null, null);
@@ -385,7 +385,7 @@ public class DepotItemController {
BaseResponseInfo res = new BaseResponseInfo();
Map<String, Object> map = new HashMap<String, Object>();
Long tenantId = redisService.getTenantId(request);
String endTime = Tools.lastDayOfMonth(monthTime)+" 23:59:59";
String endTime = Tools.lastDayOfMonth(monthTime) + BusinessConstants.DAY_LAST_TIME;
try {
List<DepotItemVo4WithInfoEx> dataList = depotItemService.findByAll(StringUtil.toNull(materialParam),
endTime, null, null);
@@ -431,7 +431,7 @@ public class DepotItemController {
HttpServletRequest request)throws Exception {
BaseResponseInfo res = new BaseResponseInfo();
Map<String, Object> map = new HashMap<String, Object>();
String endTime = Tools.lastDayOfMonth(monthTime)+" 23:59:59";
String endTime = Tools.lastDayOfMonth(monthTime) + BusinessConstants.DAY_LAST_TIME;
try {
List<DepotItemVo4WithInfoEx> dataList = depotItemService.findByAll(StringUtil.toNull(materialParam),
endTime, (currentPage-1)*pageSize, pageSize);
@@ -493,7 +493,7 @@ public class DepotItemController {
HttpServletRequest request)throws Exception {
BaseResponseInfo res = new BaseResponseInfo();
Map<String, Object> map = new HashMap<String, Object>();
String endTime = Tools.lastDayOfMonth(monthTime)+" 23:59:59";
String endTime = Tools.lastDayOfMonth(monthTime) + BusinessConstants.DAY_LAST_TIME;
try {
List<DepotItemVo4WithInfoEx> dataList = depotItemService.findByAll(StringUtil.toNull(materialParam),
endTime,(currentPage-1)*pageSize, pageSize);

View File

@@ -272,9 +272,9 @@ public class AccountService {
try {
DepotHeadExample example = new DepotHeadExample();
if (!timeStr.equals("")) {
Date bTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date eTime = StringUtil.getDateByString(timeStr + "-31 00:00:00", null);
Date mTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date bTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
Date eTime = StringUtil.getDateByString(Tools.lastDayOfMonth(timeStr) + BusinessConstants.DAY_LAST_TIME, null);
Date mTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
if (type.equals("month")) {
example.createCriteria().andAccountIdEqualTo(id).andPayTypeNotEqualTo("预付款")
.andOperTimeGreaterThanOrEqualTo(bTime).andOperTimeLessThanOrEqualTo(eTime)
@@ -317,9 +317,9 @@ public class AccountService {
try {
AccountHeadExample example = new AccountHeadExample();
if (!timeStr.equals("")) {
Date bTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date eTime = StringUtil.getDateByString(timeStr + "-31 00:00:00", null);
Date mTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date bTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
Date eTime = StringUtil.getDateByString(Tools.lastDayOfMonth(timeStr) + BusinessConstants.DAY_LAST_TIME, null);
Date mTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
if (type.equals("month")) {
example.createCriteria().andAccountIdEqualTo(id)
.andBillTimeGreaterThanOrEqualTo(bTime).andBillTimeLessThanOrEqualTo(eTime)
@@ -363,9 +363,9 @@ public class AccountService {
try {
AccountHeadExample example = new AccountHeadExample();
if (!timeStr.equals("")) {
Date bTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date eTime = StringUtil.getDateByString(timeStr + "-31 00:00:00", null);
Date mTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date bTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
Date eTime = StringUtil.getDateByString(Tools.lastDayOfMonth(timeStr) + BusinessConstants.DAY_LAST_TIME, null);
Date mTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
if (type.equals("month")) {
example.createCriteria().andBillTimeGreaterThanOrEqualTo(bTime).andBillTimeLessThanOrEqualTo(eTime)
.andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
@@ -422,9 +422,9 @@ public class AccountService {
try {
DepotHeadExample example = new DepotHeadExample();
if (!timeStr.equals("")) {
Date bTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date eTime = StringUtil.getDateByString(timeStr + "-31 00:00:00", null);
Date mTime = StringUtil.getDateByString(timeStr + "-01 00:00:00", null);
Date bTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
Date eTime = StringUtil.getDateByString(Tools.lastDayOfMonth(timeStr) + BusinessConstants.DAY_LAST_TIME, null);
Date mTime = StringUtil.getDateByString(Tools.firstDayOfMonth(timeStr) + BusinessConstants.DAY_FIRST_TIME, null);
if (type.equals("month")) {
example.createCriteria().andAccountIdListLike("%" +id.toString() + "%")
.andOperTimeGreaterThanOrEqualTo(bTime).andOperTimeLessThanOrEqualTo(eTime)

View File

@@ -15,6 +15,7 @@ import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.orgaUserRel.OrgaUserRelService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@@ -88,6 +89,8 @@ public class AccountHeadService {
List<AccountHeadVo4ListEx> list=null;
try{
String [] creatorArray = getCreatorArray(roleType);
beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
list = accountHeadMapperEx.selectByConditionAccountHead(type, creatorArray, billNo, beginTime, endTime, offset, rows);
}catch(Exception e){
JshException.readFail(logger, e);
@@ -111,6 +114,8 @@ public class AccountHeadService {
Long result=null;
try{
String [] creatorArray = getCreatorArray(roleType);
beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
result = accountHeadMapperEx.countsByAccountHead(type, creatorArray, billNo, beginTime, endTime);
}catch(Exception e){
JshException.readFail(logger, e);

View File

@@ -24,6 +24,7 @@ import com.jsh.erp.service.serialNumber.SerialNumberService;
import com.jsh.erp.service.supplier.SupplierService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@@ -101,6 +102,8 @@ public class DepotHeadService {
String [] creatorArray = getCreatorArray(roleType);
Map<Long,String> personMap = personService.getPersonMap();
Map<Long,String> accountMap = accountService.getAccountMap();
beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
list=depotHeadMapperEx.selectByConditionDepotHead(type, subType, creatorArray, status, number, beginTime, endTime, materialParam, depotIds, offset, rows);
if (null != list) {
for (DepotHeadVo4List dh : list) {
@@ -144,6 +147,8 @@ public class DepotHeadService {
try{
String depotIds = depotService.findDepotStrByCurrentUser();
String [] creatorArray = getCreatorArray(roleType);
beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
result=depotHeadMapperEx.countsByDepotHead(type, subType, creatorArray, status, number, beginTime, endTime, materialParam, depotIds);
}catch(Exception e){
JshException.readFail(logger, e);

View File

@@ -263,8 +263,8 @@ public class DepotItemService {
public BigDecimal buyOrSale(String type, String subType, Long MId, String monthTime, String sumType) throws Exception{
BigDecimal result= BigDecimal.ZERO;
try{
String beginTime = monthTime + "-01 00:00:00";
String endTime = Tools.lastDayOfMonth(monthTime) +" 23:59:59";
String beginTime = Tools.firstDayOfMonth(monthTime) + BusinessConstants.DAY_FIRST_TIME;
String endTime = Tools.lastDayOfMonth(monthTime) + BusinessConstants.DAY_LAST_TIME;
if (SUM_TYPE.equals(sumType)) {
result= depotItemMapperEx.buyOrSaleNumber(type, subType, MId, beginTime, endTime, sumType);
} else {
@@ -288,8 +288,8 @@ public class DepotItemService {
public BigDecimal inOrOutPrice(String type, String subType, String month) throws Exception{
BigDecimal result= BigDecimal.ZERO;
try{
String beginTime = month + "-01 00:00:00";
String endTime = Tools.lastDayOfMonth(month) +" 23:59:59";
String beginTime = Tools.firstDayOfMonth(month) + BusinessConstants.DAY_FIRST_TIME;
String endTime = Tools.lastDayOfMonth(month) + BusinessConstants.DAY_LAST_TIME;
result = depotItemMapperEx.inOrOutPrice(type, subType, beginTime, endTime);
}catch(Exception e){
JshException.readFail(logger, e);

View File

@@ -85,6 +85,14 @@ public class Tools {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
public static String parseDayToTime(String day, String timeStr) {
if(StringUtil.isNotEmpty(day)){
return day + timeStr;
} else {
return null;
}
}
/**
* 获得指定时间格式为mm:ss
*
@@ -134,7 +142,7 @@ public class Tools {
/**
* 判断字符串是否全部为数字
*
* @param accountWaste
* @param checkStr
* @return boolean值
*/
public static boolean checkStrIsNum(String checkStr) {
@@ -434,7 +442,7 @@ public class Tools {
/**
* 获取当前日期的前XX个月
*
* @param 之前的第几个月
* @param beforeMonth
* @return 前XX个月字符串
*/
public static String getBeforeMonth(int beforeMonth) {
@@ -443,6 +451,16 @@ public class Tools {
return new SimpleDateFormat("yyyy-MM").format(c.getTime());
}
/**
* 根据月份获取当月第一天
* @param monthTime
* @return
* @throws ParseException
*/
public static String firstDayOfMonth(String monthTime) throws ParseException {
return monthTime + "-01";
}
/**
* 根据月份获取当月最后一天
* @param monthTime
@@ -461,7 +479,7 @@ public class Tools {
/**
* 获取email用户姓名
*
* @param args
* @param emailAddress
*/
public static String getEmailUserName(String emailAddress) {
return emailAddress.substring(0, emailAddress.lastIndexOf("@"));
@@ -470,7 +488,7 @@ public class Tools {
/**
* 获取中文编码,邮件附件乱码问题解决
*
* @param str
* @param emailAttchmentTitle
* @return
*/
public static String getChineseString(String emailAttchmentTitle) {
@@ -501,7 +519,7 @@ public class Tools {
/**
* 模糊判断电话号码是否合法,只能是数字
*
* @param macAddress
* @param userTel
* @return
*/
public static boolean isTelNumberBySlur(String userTel) {