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.iailab.common.enums.CommonConstant; import com.iailab.framework.common.service.impl.BaseServiceImpl; 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.StorageDayDao; import com.iailab.module.prod.entity.StorageDayEntity; import com.iailab.module.prod.service.StorageDayService; 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 java.util.*; /** * @author PanZhibao * @Description * @createTime 2024年06月01日 */ @Slf4j @Service public class StorageDayServiceImpl extends BaseServiceImpl implements StorageDayService { private String HTTP_API_CODE = "Prd.StorageDay"; @Resource private IFeignDataApi feignDataApi; @Resource private HttpsRequest httpsRequest; @Override @Transactional(rollbackFor = Exception.class) public void syncData() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, -1); String endDate = DateUtils.format(calendar.getTime()); calendar.add(Calendar.DAY_OF_YEAR, -5); String startDate = DateUtils.format(calendar.getTime()); FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE); Map params = new HashMap<>(); String responseStr = httpsRequest.doGet(apiDTO.getUrl() + "&startDate=" + startDate + "&endDate=" + endDate, 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 entities = dataArray.toJavaList(StorageDayEntity.class); entities.forEach(entity -> { deleteByDate(entity.getDate()); entity.setId(UUID.randomUUID().toString()); entity.setCreateDate(new Date()); insert(entity); }); } private void deleteByDate(String date) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(date), "date", date); baseDao.delete(wrapper); } }