package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.whyc.dto.Response; import com.whyc.mapper.AttachLockMapper; import com.whyc.pojo.AttachLock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service public class AttachLockService { @Autowired(required = false) private AttachLockMapper mapper; @Transactional public Response attachLock(List list) { if(list!=null&&list.size()>0){} list.stream().forEach(attachLock -> { //1.查询是否存在该记录 QueryWrapper qwrapper=new QueryWrapper(); qwrapper.eq("material_id",attachLock.getMaterialId()); qwrapper.eq("attach_name",attachLock.getAttachName()); qwrapper.last("limit 1"); AttachLock attach=mapper.selectOne(qwrapper); if(attach!=null){ UpdateWrapper uwrapper=new UpdateWrapper(); uwrapper.set("lock_flag",attachLock.getLockFlag()); uwrapper.set("local_reason",attachLock.getLocalReason()); uwrapper.eq("attach_name",attachLock.getAttachName()); uwrapper.eq("material_id",attachLock.getMaterialId()); mapper.update(null,uwrapper); }else{ mapper.insert(attachLock); } }); return new Response().set(1,true,""); } @Transactional public Response updateProductLock(List list) { if(list!=null&&list.size()>0){} list.stream().forEach(attachLock -> { //1.查询是否存在该记录 QueryWrapper qwrapper=new QueryWrapper(); qwrapper.eq("product_id",attachLock.getProductId()); qwrapper.eq("attach_name",attachLock.getAttachName()); qwrapper.last("limit 1"); AttachLock attach=mapper.selectOne(qwrapper); if(attach!=null){ UpdateWrapper uwrapper=new UpdateWrapper(); uwrapper.set("lock_flag",attachLock.getLockFlag()); uwrapper.set("local_reason",attachLock.getLocalReason()); uwrapper.eq("attach_name",attachLock.getAttachName()); uwrapper.eq("product_id",attachLock.getProductId()); mapper.update(null,uwrapper); }else{ mapper.insert(attachLock); } }); return new Response().set(1,true,""); } public void getAndInsert(Integer materialIdInDB, Integer materialId) { QueryWrapper query = Wrappers.query(); query.eq("material_id",materialIdInDB).eq("lock_flag",1).eq("product_id",0); List attachLocksInDB = mapper.selectList(query); attachLocksInDB.forEach(attachLock -> { attachLock.setId(null); attachLock.setMaterialId(materialId); }); if(attachLocksInDB.size()!=0) { mapper.insertBatchSomeColumn(attachLocksInDB); } } }