|
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.module.data.dto.FeignHttpApiDTO;
|
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.common.utils.CommonUtils;
|
import com.iailab.framework.common.util.object.ConvertUtils;
|
import com.iailab.common.utils.HttpsRequest;
|
import com.iailab.module.data.api.IFeignDataApi;
|
import com.iailab.module.prod.dao.PrdPlanMonDao;
|
import com.iailab.module.prod.dto.PrdPlanMonDTO;
|
import com.iailab.module.prod.entity.PrdPlanMonEntity;
|
import com.iailab.module.prod.service.PrdPlanMonService;
|
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 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 PrdPlanMonServiceImpl extends BaseServiceImpl<PrdPlanMonDao, PrdPlanMonEntity> implements PrdPlanMonService {
|
|
private String HTTP_API_CODE = "Prd.YearPEI";
|
|
@Resource
|
private IFeignDataApi feignDataApi;
|
|
@Resource
|
private HttpsRequest httpsRequest;
|
|
@Override
|
public PageData<PrdPlanMonDTO> page(Map<String, Object> params) {
|
IPage<PrdPlanMonEntity> page = baseDao.selectPage(
|
getPage(params, Constant.CREATE_DATE, false),
|
getWrapper(params)
|
);
|
return getPageData(page, PrdPlanMonDTO.class);
|
}
|
|
private QueryWrapper<PrdPlanMonEntity> getWrapper(Map<String, Object> params){
|
String rq = (String)params.get("rq");
|
|
QueryWrapper<PrdPlanMonEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq(StringUtils.isNotBlank(rq), "rq", rq)
|
.orderByAsc("rq");
|
|
return wrapper;
|
}
|
|
@Override
|
public PrdPlanMonDTO get(String id) {
|
PrdPlanMonEntity entity = baseDao.selectById(id);
|
|
return ConvertUtils.sourceToTarget(entity, PrdPlanMonDTO.class);
|
}
|
|
@Override
|
public void save(PrdPlanMonDTO dto) {
|
PrdPlanMonEntity entity = ConvertUtils.sourceToTarget(dto, PrdPlanMonEntity.class);
|
|
insert(entity);
|
}
|
|
@Override
|
public void update(PrdPlanMonDTO dto) {
|
PrdPlanMonEntity entity = ConvertUtils.sourceToTarget(dto, PrdPlanMonEntity.class);
|
|
updateById(entity);
|
}
|
|
public List<PrdPlanMonEntity> list(){
|
List<PrdPlanMonEntity> prdPlanMonList = baseDao.selectList(
|
new QueryWrapper<PrdPlanMonEntity>()
|
.ge("rq",new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
|
.le("rq",new SimpleDateFormat("yyyy-MM-dd").format(LocalDate.now().minusDays(29)))
|
);
|
return prdPlanMonList;
|
}
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void delete(String[] ids) {
|
baseDao.deleteBatchIds(Arrays.asList(ids));
|
}
|
|
@Override
|
public BarLineDTO barLine(String length) {
|
|
BarLineDTO barLineDTO = new BarLineDTO();
|
List<SeriesItem> seriesItemList = new ArrayList<>();
|
List<Object[]> data = new ArrayList();
|
this.list().stream().forEach(
|
item->{
|
Object[] dataItem = new Object[2];
|
dataItem[0] = item.getRq();
|
dataItem[1] = item.getXxPlan();
|
data.add(dataItem);
|
}
|
);
|
|
|
SeriesItem seriesItem = new SeriesItem();
|
seriesItem.setData(data);
|
barLineDTO.setValueName("月计划");
|
List categoriesList = new ArrayList();
|
categoriesList.add("");
|
categoriesList.add("");
|
barLineDTO.setCategories(categoriesList);
|
//barLineDTO.setLegend(this.getLastMonthDates());
|
barLineDTO.setSeries(seriesItemList);
|
return barLineDTO;
|
}
|
|
@Override
|
public BigDecimal currentValue() {
|
return new BigDecimal(1000);
|
}
|
public List<String> getLastMonthDates() {
|
List<String> dateList = new ArrayList<>();
|
LocalDate currentDate = LocalDate.now();
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
for (int i = 29; i >= 0; i--) {
|
dateList.add(currentDate.minusDays(i).format(formatter));
|
}
|
|
return dateList;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void syncData() {
|
FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE);
|
Map<String, String> params = new HashMap<>();
|
String responseStr = httpsRequest.doGet(apiDTO.getUrl(), params, "utf-8", "");
|
|
JSONObject responseObj = JSON.parseObject(responseStr);
|
if (!CommonConstant.STA_TRUE.equals(responseObj.get("sta").toString())) {
|
log.info("接口异常");
|
}
|
JSONObject res = responseObj.getJSONObject(CommonConstant.ZX_RES);
|
JSONObject yearData = res.getJSONObject("yearData");
|
String year = yearData.get("year").toString();
|
JSONArray monthData = res.getJSONArray("monthData");
|
for (int i = 0; i < monthData.size(); i++) {
|
JSONObject item = monthData.getJSONObject(i);
|
String rq = year + "-" + (Integer.parseInt(item.getString("month")) < 10 ? "0" + item.getString("month") : item.getString("month") );
|
this.deleteByRq(rq);
|
PrdPlanMonEntity entity = new PrdPlanMonEntity();
|
entity.setId(UUID.randomUUID().toString());
|
entity.setRq(rq);
|
entity.setXxPlan(CommonUtils.getJSONValue(item.get("xxPlan")));
|
entity.setXxPerformance(CommonUtils.getJSONValue(item.get("xxPerformance")));
|
entity.setZqPlan(CommonUtils.getJSONValue(item.get("zqPlan")));
|
entity.setZqPerformance(CommonUtils.getJSONValue(item.get("zqPerformance")));
|
entity.setCreateDate(new Date());
|
entity.setUpdateDate(new Date());
|
insert(entity);
|
}
|
}
|
|
private void deleteByRq(String rq) {
|
QueryWrapper<PrdPlanMonEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq(StringUtils.isNotBlank(rq), "rq", rq);
|
baseDao.delete(wrapper);
|
}
|
}
|