public class DiffListUtils {
public static List<SceneTemplateRelationDO> getDifferent(List<SceneTemplateRelationDO> preList,List<SceneTemplateRelationDO> curList){
ArrayList<SceneTemplateRelationDO> diffList = new ArrayList<>();
Map<SceneTemplateRelationDO,Integer> map = new HashMap<>();
for (SceneTemplateRelationDO relationDO : curList){
map.put(relationDO,1);
}
for (SceneTemplateRelationDO relationDO : preList){
if (map.get(relationDO) != null){
map.put(relationDO,2);
continue;
}
diffList.add(relationDO);
}
for (Map.Entry<SceneTemplateRelationDO,Integer> entry : map.entrySet()){
if (entry.getValue() == 1){
diffList.add(entry.getKey());
}
}
return diffList;
}
}
/**
*重写实体类
*/
@Override
public boolean equals(Object obj){
if (obj instanceof SceneTemplateRelationDO){
SceneTemplateRelationDO str = (SceneTemplateRelationDO)obj;
return (getSceneId().equals(str.getSceneId()) && getTemplateId().equals(str.getTemplateId()));
} else {
return super.equals(obj);
}
}
@Override
public int hashCode(){
return Objects.hash(getSceneId(),getTemplateId());
}
//逻辑
public class ServiceTemplateService {
@Test
public void test(){
ServiceTemplateDAO serviceTemplateDAO = new ServiceTemplateDAOImpl();
SceneTemplateRelationDAO sceneTemplateRelationDAO = new SceneTemplateRelationDAOImpl();
try {
List<Long> list = new ArrayList<Long>();
list.add(2L);
list.add(6L);
list.add(7L);
list.add(8L);
System.out.println(list);
ServiceTemplateDTO templateDTO = new ServiceTemplateDTO();
templateDTO.setId(2L);
templateDTO.setCode("ceshicode2");
templateDTO.setName("前端模板2");
templateDTO.setServiceLineId(1L);
templateDTO.setServiceSceneId(list);
ServiceTemplateDO templateDO = copy(templateDTO);
List<SceneTemplateRelationDO> qrelationDOList = new ArrayList<SceneTemplateRelationDO>();
List<Long> sceneIdList = templateDTO.getServiceSceneId();
for (Long sceneId : sceneIdList){
SceneTemplateRelationDO relationDO = new SceneTemplateRelationDO();
relationDO.setTemplateId(templateDTO.getId());
relationDO.setSceneId(sceneId);
qrelationDOList.add(relationDO);
}
System.out.println(qrelationDOList);
Integer rowCount = serviceTemplateDAO.update(templateDO);
System.out.println(rowCount);
List<SceneTemplateRelationDO> crelationDOList = sceneTemplateRelationDAO.selectByTemplateId(templateDTO.getId());
System.out.println(crelationDOList);
if(crelationDOList != null){
List<SceneTemplateRelationDO> diffList = DiffListUtils.getDifferent(qrelationDOList, crelationDOList);
for (SceneTemplateRelationDO sceneTemplateRelationDO : diffList){
System.out.println("sceneTemplateRelationDO----->"+sceneTemplateRelationDO);
System.out.println("qrelationDOList----->"+qrelationDOList);
System.out.println("crelationDOList----->"+crelationDOList);
System.out.println("diffList----->"+ diffList);
Integer status = sceneTemplateRelationDO.getStatus();
System.out.println("status++++++"+status);
if(null == status){
Long insert = sceneTemplateRelationDAO.insert(sceneTemplateRelationDO);
System.out.println("insert++++++"+insert);
continue;
}
if (status == 1){
sceneTemplateRelationDO.setStatus(0);
Integer update = sceneTemplateRelationDAO.update(sceneTemplateRelationDO);
System.out.println("update===status==1------>"+update);
continue;
}
sceneTemplateRelationDO.setStatus(1);
Integer update = sceneTemplateRelationDAO.update(sceneTemplateRelationDO);
System.out.println("update===status==0------->"+update);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static ServiceTemplateDO copy(ServiceTemplateDTO serviceTemplateDTO){
ServiceTemplateDO serviceTemplateDO = new ServiceTemplateDO();
serviceTemplateDO.setId(serviceTemplateDTO.getId());
serviceTemplateDO.setCode(serviceTemplateDTO.getCode());
serviceTemplateDO.setName(serviceTemplateDTO.getName());
serviceTemplateDO.setStatus(serviceTemplateDTO.getStatus());
serviceTemplateDO.setServiceLineId(serviceTemplateDTO.getServiceLineId());
return serviceTemplateDO;
}
}