package com.iailab.module.cons.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.cons.dao.ConsDayDataDao; import com.iailab.module.cons.entity.ConsDayDataEntity; import com.iailab.module.cons.service.ConsDayDataService; 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.util.*; /** * @author PanZhibao * @Description * @createTime 2024年05月27日 */ @Slf4j @Service public class ConsDayDataServiceImpl extends BaseServiceImpl implements ConsDayDataService { private String HTTP_API_CODE = "Cons.MonthData"; @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 monthstr = DateUtils.format(calendar.getTime(), DateUtils.DATE_PATTERN_MON); String type = "主洗系统"; FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE); Map params = new HashMap<>(); params.put("monthstr", monthstr); params.put("type", type); 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 dataObject = responseObj.getJSONObject(CommonConstant.ZX_RES); if (dataObject == null) { return; } this.deleteByMonth(monthstr); JSONArray rows = dataObject.getJSONArray("rows"); List dataList = rows.toJavaList(ConsDayDataEntity.class); dataList.forEach(entity -> { entity.setId(UUID.randomUUID().toString()); entity.setMonthstr(monthstr); entity.setType(type); entity.setCreateDate(new Date()); insert(entity); }); } private void deleteByMonth(String monthstr) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(monthstr), "monthstr", monthstr); baseDao.delete(wrapper); } }