import com.xzx.gc.GcUserApplication; import com.xzx.gc.entity.AddressInfo; import com.xzx.gc.entity.OrderDetailInfo; import com.xzx.gc.entity.OrderInfo; import com.xzx.gc.entity.UserTargetInfo; import com.xzx.gc.user.mapper.*; import com.xzx.gc.user.service.AddressService; import com.xzx.gc.user.service.UserService; import com.xzx.gc.user.service.UserTargetInfoService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import tk.mybatis.mapper.entity.Example; import java.util.ArrayList; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = {GcUserApplication.class}) @ActiveProfiles("fix") public class FixTest { @Autowired private UserTargetInfoService userTargetInfoService; @Autowired private UserTargetInfoMapper userTargetInfoMapper; @Autowired private UserService userService; @Autowired private AddressService addressService; @Autowired private AddressMapper addressMapper; @Autowired private OrderMapper orderMapper; @Autowired private OrderDetailMapper orderDetailMapper; @Autowired private OrderDetailBatchMapper orderDetailBatchMapper; @Test public void fixtaeget(){ //修复意向客户的地址问题 Example example=new Example(UserTargetInfo.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("regsterType",3); criteria.andIsNull("provinceId"); List userTargetInfo1 = userTargetInfoMapper.selectByExample(example); for (UserTargetInfo userTargetInfo : userTargetInfo1) { String mobile = userTargetInfo.getMobile(); AddressInfo addressInfo=new AddressInfo(); addressInfo.setDelFlag("0"); addressInfo.setFlag("1"); addressInfo.setMobilePhone(mobile); AddressInfo addressInfo1 = addressMapper.selectOne(addressInfo); if(addressInfo1!=null){ userTargetInfoService.updateTargetUserAddr(addressInfo1); } } System.out.println("修复了"+userTargetInfo1.size()+"条记录"); } @Test public void fixOrderAdd(){ //修复订单地址id Example example=new Example(OrderDetailInfo.class); Example.Criteria criteria = example.createCriteria(); criteria.andIsNull("addressId"); List orderDetailInfos = orderDetailMapper.selectByExample(example); List list=new ArrayList<>(); for (OrderDetailInfo orderDetailInfo : orderDetailInfos) { OrderDetailInfo detailInfo=new OrderDetailInfo(); String orderId = orderDetailInfo.getOrderId(); OrderInfo orderInfo = orderMapper.selectByPrimaryKey(orderId); String createUserId = orderInfo.getCreateUserId(); Long addressId; AddressInfo addressInfo=new AddressInfo(); addressInfo.setUserId(createUserId); addressInfo.setFlag("1"); addressInfo.setDelFlag("0"); AddressInfo addressInfo1 = addressMapper.selectOne(addressInfo); if(addressInfo1!=null){ addressId=addressInfo1.getAddressId(); }else { //找删除的数据里最新的那条 Example example1=new Example(AddressInfo.class); Example.Criteria criteria1 = example1.createCriteria(); criteria1.andEqualTo("userId",createUserId); criteria1.andEqualTo("flag","1"); example1.orderBy("createTime").desc(); List addressInfos = addressMapper.selectByExample(example1); addressId=addressInfos.get(0).getAddressId(); } detailInfo.setOrderId(orderDetailInfo.getOrderId()); detailInfo.setAddressId(addressId); list.add(detailInfo); } orderDetailBatchMapper.updateBatchByPrimaryKeySelective(list); } }