package com.iailab.module.device.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.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.HttpsRequest; import com.iailab.module.data.dto.FeignHttpApiDTO; import com.iailab.module.data.api.IFeignDataApi; import com.iailab.module.device.dao.DeviceInfoDao; import com.iailab.module.device.dto.DeviceInfoDTO; import com.iailab.module.device.entity.DeviceInfoEntity; import com.iailab.module.device.service.DeviceInfoService; 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年05月27日 */ @Slf4j @Service public class DeviceInfoServiceImpl extends BaseServiceImpl implements DeviceInfoService { private String HTTP_API_CODE = "Dev.DeviceTree"; @Resource private IFeignDataApi feignDataApi; @Resource private HttpsRequest httpsRequest; @Override public List list(Map params) { List list = baseDao.selectList(getWrapper(params)); return ConvertUtils.sourceToTarget(list, DeviceInfoDTO.class); } private QueryWrapper getWrapper(Map params) { String pid = (String) params.get("pid"); String no = (String) params.get("no"); String name = (String) params.get("name"); String isMain = (String) params.get("isMain"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(pid), "pid", pid) .like(StringUtils.isNotBlank(no), "no", no) .like(StringUtils.isNotBlank(name), "name", name) .eq(StringUtils.isNotBlank(isMain), "is_main", isMain); return wrapper; } @Override @Transactional(rollbackFor = Exception.class) public void syncData() { FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE); Map 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("接口异常"); } JSONArray dataArray = responseObj.getJSONArray(CommonConstant.ZX_RES); if (CollectionUtils.isEmpty(dataArray)) { return; } List deviceInfoDTOS = dataArray.toJavaList(DeviceInfoDTO.class); for (int i = 0; i < deviceInfoDTOS.size(); i++) { DeviceInfoEntity entity0 = ConvertUtils.sourceToTarget(deviceInfoDTOS.get(i), DeviceInfoEntity.class); entity0.setId(UUID.randomUUID().toString()); entity0.setCreateDate(new Date()); entity0.setPid("0"); baseDao.insert(entity0); List chd = deviceInfoDTOS.get(i).getChildren(); if (CollectionUtils.isEmpty(chd)) { continue; } for (int j = 0; j < chd.size(); j++) { DeviceInfoEntity entity1 = ConvertUtils.sourceToTarget(chd.get(j), DeviceInfoEntity.class); entity1.setId(UUID.randomUUID().toString()); entity1.setCreateDate(new Date()); entity1.setPid(entity0.getId()); baseDao.insert(entity1); } } } @Override public PageData page(Map params) { params.put("pid","0"); IPage page = baseDao.selectPage(getPage(params, "no", true), getWrapper(params)); return getPageData(page, DeviceInfoDTO.class); } @Override public List getchildrenList(String deviceId) { return baseDao.getchildrenList(deviceId); } @Override public DeviceInfoDTO get(String id) { DeviceInfoEntity entity = baseDao.selectById(id); return ConvertUtils.sourceToTarget(entity, DeviceInfoDTO.class); } @Override public void updateById(DeviceInfoDTO dto) { DeviceInfoEntity entity = ConvertUtils.sourceToTarget(dto, DeviceInfoEntity.class); baseDao.updateById(entity); } @Override public DeviceInfoDTO getByDevNo(String devNo) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("no", devNo); DeviceInfoEntity entity = baseDao.selectOne(wrapper); return ConvertUtils.sourceToTarget(entity, DeviceInfoDTO.class); } @Override public Long getTotal() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("pid","0"); return baseDao.selectCount(queryWrapper); } }