|
package com.iailab.module.prod.service.impl;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.iailab.framework.common.constant.Constant;
|
import com.iailab.common.dto.echarts.BarLineDTO;
|
import com.iailab.common.dto.echarts.SeriesItem;
|
import com.iailab.common.enums.CommonConstant;
|
import com.iailab.framework.common.page.PageData;
|
import com.iailab.framework.common.service.impl.BaseServiceImpl;
|
import com.iailab.framework.common.util.object.ConvertUtils;
|
import com.iailab.common.utils.DateUtils;
|
import com.iailab.common.utils.HttpsRequest;
|
import com.iailab.module.data.dto.FeignHttpApiDTO;
|
import com.iailab.module.data.api.IFeignDataApi;
|
import com.iailab.module.prod.dao.PrdCurrentSaleDao;
|
import com.iailab.module.prod.dto.PrdCurrentSaleDTO;
|
import com.iailab.module.prod.entity.PrdCurrentSaleEntity;
|
import com.iailab.module.prod.service.PrdCurrentSaleService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import javax.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDate;
|
import java.time.format.DateTimeFormatter;
|
import java.util.*;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2024年05月14日
|
*/
|
@Slf4j
|
@Service
|
public class PrdCurrentSaleServiceImpl extends BaseServiceImpl<PrdCurrentSaleDao, PrdCurrentSaleEntity> implements PrdCurrentSaleService {
|
|
private String HTTP_API_CODE = "Prd.SaleTrendDay";
|
|
@Resource
|
private IFeignDataApi feignDataApi;
|
|
@Resource
|
private HttpsRequest httpsRequest;
|
|
@Resource
|
private PrdCurrentSaleDao prdCurrentSaleDao;
|
|
@Override
|
public PageData<PrdCurrentSaleDTO> page(Map<String, Object> params) {
|
IPage<PrdCurrentSaleEntity> page = baseDao.selectPage(
|
getPage(params, Constant.CREATE_DATE, false),
|
getWrapper(params)
|
);
|
return getPageData(page, PrdCurrentSaleDTO.class);
|
}
|
|
private QueryWrapper<PrdCurrentSaleEntity> getWrapper(Map<String, Object> params){
|
String rq = (String)params.get("rq");
|
|
QueryWrapper<PrdCurrentSaleEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq(StringUtils.isNotBlank(rq), "rq", rq)
|
.orderByAsc("rq");
|
|
return wrapper;
|
}
|
|
@Override
|
public PrdCurrentSaleDTO get(String id) {
|
PrdCurrentSaleEntity entity = baseDao.selectById(id);
|
|
return ConvertUtils.sourceToTarget(entity, PrdCurrentSaleDTO.class);
|
}
|
|
@Override
|
public void save(PrdCurrentSaleDTO dto) {
|
PrdCurrentSaleEntity entity = ConvertUtils.sourceToTarget(dto, PrdCurrentSaleEntity.class);
|
|
insert(entity);
|
}
|
|
@Override
|
public void update(PrdCurrentSaleDTO dto) {
|
PrdCurrentSaleEntity entity = ConvertUtils.sourceToTarget(dto, PrdCurrentSaleEntity.class);
|
|
updateById(entity);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void delete(String[] ids) {
|
baseDao.deleteBatchIds(Arrays.asList(ids));
|
}
|
|
@Override
|
public BarLineDTO barLine(String length,String type) {
|
|
BarLineDTO barLineDTO = new BarLineDTO();
|
List<SeriesItem> seriesItemList = new ArrayList<>();
|
List<Object[]> data = new ArrayList();
|
|
switch (type) {
|
case "groundsales":
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getGroundsales();
|
if(item.getGroundsales()!=null){
|
data.add(dataItem);
|
}
|
barLineDTO.setValueName("地销混煤");
|
}
|
);
|
break;
|
case "medblock":
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getMedblock();
|
if(item.getMedblock()!=null){
|
data.add(dataItem);
|
}
|
barLineDTO.setValueName("洗中块外运");
|
}
|
);
|
break;
|
case "nubmeasure":
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getNubmeasure();
|
if(item.getNubmeasure()!=null){
|
data.add(dataItem);
|
}
|
barLineDTO.setValueName("洗小块外运");
|
}
|
);
|
break;
|
case "gangue":
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getGangue();
|
if(item.getGangue()!=null){
|
data.add(dataItem);
|
}
|
barLineDTO.setValueName("矸石外运");
|
}
|
);
|
break;
|
case "reshipped":
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getReshipped();
|
if(item.getReshipped()!=null){
|
data.add(dataItem);
|
}
|
barLineDTO.setValueName("小块转载");
|
}
|
);
|
break;
|
case "trainTon":
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getTrainTon();
|
if(item.getGangue()!=null){
|
data.add(dataItem);
|
}
|
barLineDTO.setValueName("火车外运吨数");
|
}
|
);
|
break;
|
case "trainCount":
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getTrainCount();
|
if(item.getGangue()!=null){
|
data.add(dataItem);
|
}
|
barLineDTO.setValueName("火车外运列数");
|
}
|
);
|
break;
|
default:
|
break;
|
}
|
|
SeriesItem seriesItem = new SeriesItem();
|
seriesItem.setData(data);
|
seriesItemList.add(seriesItem);
|
//barLineDTO.setLegend(this.getLastMonthDates(Integer.parseInt(length)));
|
barLineDTO.setSeries(seriesItemList);
|
return barLineDTO;
|
}
|
|
@Override
|
public BigDecimal currentValue(String type) {
|
switch (type) {
|
case "groundsales":
|
//地销混煤
|
return baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date())))==null
|
?new BigDecimal(0)
|
:baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))).getGroundsales();
|
case "medblock":
|
//洗中块外运
|
return baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date())))==null
|
?new BigDecimal(0)
|
:baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))).getMedblock();
|
case "nubmeasure":
|
//洗小块外运
|
return baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date())))==null
|
?new BigDecimal(0)
|
:baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))).getNubmeasure();
|
case "gangue":
|
//矸石外运
|
return baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date())))==null
|
?new BigDecimal(0)
|
:baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))).getGangue();
|
case "reshipped":
|
//小块转载
|
return baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date())))==null
|
?new BigDecimal(0)
|
:baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))).getReshipped();
|
case "trainTon":
|
//火车外运吨数
|
return baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date())))==null
|
?new BigDecimal(0)
|
:baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))).getTrainTon();
|
case "trainCount":
|
//火车外运列数
|
return baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date())))==null
|
?new BigDecimal(0)
|
:baseDao.selectOne(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.eq("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))).getTrainCount();
|
|
default:
|
break;
|
}
|
return null;
|
}
|
public List<String> getLastMonthDates(int length) {
|
List<String> dateList = new ArrayList<>();
|
LocalDate currentDate = LocalDate.now();
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
for (int i = length-1; i >= 0; i--) {
|
dateList.add(currentDate.minusDays(i).format(formatter));
|
}
|
|
return dateList;
|
}
|
|
public List<PrdCurrentSaleEntity> list(){
|
List<PrdCurrentSaleEntity> prdCurrentSaleList = baseDao.selectList(
|
new QueryWrapper<PrdCurrentSaleEntity>()
|
.le("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
|
.ge("rq",LocalDate.now().minusDays(29).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
|
.orderByAsc("rq")
|
);
|
return prdCurrentSaleList;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void syncData() {
|
Calendar calendar = Calendar.getInstance();
|
calendar.add(Calendar.DAY_OF_YEAR, -30);
|
String startDate = DateUtils.format(calendar.getTime());
|
FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE);
|
Map<String, String> params = new HashMap<>();
|
String responseStr = httpsRequest.doGet(apiDTO.getUrl() + "&startDate=" + startDate, params, "utf-8", "");
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (!CommonConstant.STA_TRUE.equals(responseObj.get("sta").toString())) {
|
log.info("接口异常");
|
}
|
JSONArray dataArray = responseObj.getJSONArray(CommonConstant.ZX_RES);
|
if (CollectionUtils.isEmpty(dataArray)) {
|
return;
|
}
|
List<PrdCurrentSaleEntity> entities = dataArray.toJavaList(PrdCurrentSaleEntity.class);
|
|
entities.forEach(entity -> {
|
deleteByRq(entity.getDate());
|
entity.setRq(entity.getDate());
|
entity.setId(UUID.randomUUID().toString());
|
entity.setCreateDate(new Date());
|
entity.setUpdateDate(new Date());
|
insert(entity);
|
});
|
}
|
|
private void deleteByRq(String rq) {
|
QueryWrapper<PrdCurrentSaleEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq(StringUtils.isNotBlank(rq), "rq", rq);
|
baseDao.delete(wrapper);
|
}
|
}
|