package com.iailab.module.data.ind.service.impl; 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.module.data.common.enums.CommonConstant; import com.iailab.module.data.common.enums.IsEnableEnum; 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.module.data.ind.dto.IndItemAtomDTO; import com.iailab.module.data.ind.service.IndItemCalService; import com.iailab.module.data.ind.service.IndItemService; import com.iailab.module.data.ind.common.IndItemTypeEnum; import com.iailab.module.data.ind.dao.IndItemDao; import com.iailab.module.data.ind.dto.IndItemCalDTO; import com.iailab.module.data.ind.dto.IndItemDTO; import com.iailab.module.data.ind.entity.IndItemEntity; import com.iailab.module.data.ind.service.IndItemAtomService; 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月25日 */ @Service public class IndItemServiceImpl extends BaseServiceImpl implements IndItemService { @Resource private IndItemAtomService indItemAtomService; @Resource private IndItemCalService indItemCalService; @Override public PageData page(Map params) { IPage page = baseDao.selectPage( getPage(params, Constant.CREATE_TIME, false), getWrapper(params) ); return getPageData(page, IndItemDTO.class); } @Override public List getList(Map params) { List list = baseDao.selectList(getWrapper(params)); return ConvertUtils.sourceToTarget(list, IndItemDTO.class); } @Override public void enableByIds(String[] ids) { if (CollectionUtils.isEmpty(Arrays.asList(ids))) { return; } Arrays.asList(ids).forEach(item -> { IndItemEntity entity = new IndItemEntity(); entity.setId(item); entity.setIsEnable(IsEnableEnum.ENABLE.value()); baseDao.updateById(entity); }); } @Override public void disableByIds(String[] ids) { if (CollectionUtils.isEmpty(Arrays.asList(ids))) { return; } Arrays.asList(ids).forEach(item -> { IndItemEntity entity = new IndItemEntity(); entity.setId(item); entity.setIsEnable(IsEnableEnum.DISABLE.value()); baseDao.updateById(entity); }); } @Override public IndItemDTO getItemByItemNo(String itemNo) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("item_no", itemNo); return ConvertUtils.sourceToTarget(baseDao.selectOne(wrapper),IndItemDTO.class); } private QueryWrapper getWrapper(Map params){ String itemNo = (String)params.get("itemNo"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.like(StringUtils.isNotBlank(itemNo), "item_no", itemNo); return wrapper; } @Override public IndItemDTO get(String id) { IndItemEntity entity = baseDao.selectById(id); if (entity == null) { return null; } IndItemDTO result = ConvertUtils.sourceToTarget(entity, IndItemDTO.class); if (IndItemTypeEnum.ATOM_ITEM.getCode().equals(result.getItemType())) { IndItemAtomDTO dto = indItemAtomService.getItemId(id); result.setIndItemAtom(dto); } else if (IndItemTypeEnum.CAL_ITEM.getCode().equals(result.getItemType())) { IndItemCalDTO dto = indItemCalService.getItemId(id); result.setIndItemCal(dto); } return result; } @Override public void save(IndItemDTO dto) { IndItemEntity entity = ConvertUtils.sourceToTarget(dto, IndItemEntity.class); entity.setId(UUID.randomUUID().toString()); if (IndItemTypeEnum.ATOM_ITEM.getName().equals(entity.getItemType())) { IndItemAtomDTO indItemAtomDTO = new IndItemAtomDTO(); indItemAtomDTO.setId(UUID.randomUUID().toString()); indItemAtomDTO.setItemId(entity.getId()); indItemAtomDTO.setDataSource(dto.getDataSource()); indItemAtomDTO.setQuerySql(dto.getQuerySql()); indItemAtomService.save(indItemAtomDTO); }else if (IndItemTypeEnum.CAL_ITEM.getName().equals(entity.getItemType())){ IndItemCalDTO indItemCalDTO = new IndItemCalDTO(); indItemCalDTO.setId(UUID.randomUUID().toString()); indItemCalDTO.setItemId(entity.getId()); indItemCalDTO.setExpression(dto.getExpression()); indItemCalService.save(indItemCalDTO); } entity.setIsEnable(CommonConstant.IS_ENABLE); entity.setCreateTime(new Date()); insert(entity); } @Override public void update(IndItemDTO dto) { IndItemEntity entity = ConvertUtils.sourceToTarget(dto, IndItemEntity.class); entity.setUpdateTime(new Date()); updateById(entity); System.out.println(IndItemTypeEnum.ATOM_ITEM.getName()+IndItemTypeEnum.CAL_ITEM.getName()); if (IndItemTypeEnum.ATOM_ITEM.getName().equals(entity.getItemType())) { dto.getIndItemAtom().setDataSource(dto.getDataSource()); dto.getIndItemAtom().setQuerySql(dto.getQuerySql()); indItemAtomService.update(dto.getIndItemAtom()); }else if (IndItemTypeEnum.CAL_ITEM.getName().equals(entity.getItemType())){ dto.getIndItemCal().setExpression(dto.getExpression()); indItemCalService.update(dto.getIndItemCal()); } } @Override @Transactional(rollbackFor = Exception.class) public void delete(String[] ids) { baseDao.deleteBatchIds(Arrays.asList(ids)); indItemAtomService.deleteByItemId(ids); indItemCalService.deleteByItemId(ids); } @Override public List getItemAtom(List itemNos) { Map params = new HashMap<>(); params.put("itemType", IndItemTypeEnum.ATOM_ITEM.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("itemNos", itemNos); return baseDao.getItemAtom(params); } @Override public IndItemDTO getItemAtom(String itemNo) { IndItemDTO result = null; Map params = new HashMap<>(); params.put("itemType", IndItemTypeEnum.ATOM_ITEM.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("itemNo", itemNo); List list = baseDao.getItemAtom(params); if (!CollectionUtils.isEmpty(list)) { result = list.get(0); } return result; } @Override public List getItemCal(List itemNos) { Map params = new HashMap<>(); params.put("itemType", IndItemTypeEnum.CAL_ITEM.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("itemNos", itemNos); return baseDao.getItemCal(params); } @Override public IndItemDTO getItemCal(String itemNo) { IndItemDTO result = null; Map params = new HashMap<>(); params.put("itemType", IndItemTypeEnum.CAL_ITEM.getCode()); params.put("isEnable", CommonConstant.IS_ENABLE); params.put("itemNo", itemNo); List list = baseDao.getItemCal(params); if (!CollectionUtils.isEmpty(list)) { result = list.get(0); } return result; } }