| package com.xzx.gc.user.service;  | 
|   | 
|   | 
| import cn.hutool.core.collection.CollUtil;  | 
| import cn.hutool.core.convert.Convert;  | 
| import cn.hutool.core.date.DateUtil;  | 
| import cn.hutool.core.util.StrUtil;  | 
| import com.xzx.gc.common.constant.Constants;  | 
| import com.xzx.gc.common.constant.OrderEnum;  | 
| import com.xzx.gc.common.constant.SmsConstant;  | 
| import com.xzx.gc.common.utils.BusinessUtil;  | 
| import com.xzx.gc.common.utils.IdUtils;  | 
| import com.xzx.gc.common.utils.StringUtils;  | 
| import com.xzx.gc.common.utils.ali.SmsUtil;  | 
| import com.xzx.gc.entity.*;  | 
| import com.xzx.gc.model.IdDTO;  | 
| import com.xzx.gc.model.system.ItemUserListDTO;  | 
| import com.xzx.gc.model.system.ItemUserListResDTO;  | 
| import com.xzx.gc.model.user.UserScrapStoreListParamDTO;  | 
| import com.xzx.gc.model.user.UserScrapSubscribeParamDTO;  | 
| import com.xzx.gc.user.mapper.OrderAppraiseInfoMapper;  | 
| import com.xzx.gc.user.mapper.OrderDetailMapper;  | 
| import com.xzx.gc.user.mapper.UserScrapStoreMapper;  | 
| import org.springframework.beans.factory.annotation.Autowired;  | 
| import org.springframework.stereotype.Service;  | 
| import org.springframework.transaction.annotation.Transactional;  | 
| import tk.mybatis.mapper.entity.Example;  | 
|   | 
| import java.util.*;  | 
| import java.util.stream.Collectors;  | 
|   | 
| @Service  | 
| @Transactional  | 
| public class UserScrapStoreService {  | 
|   | 
|   | 
|     @Autowired  | 
|     private UserScrapStoreMapper userScrapStoreMapper;  | 
|   | 
|     @Autowired  | 
|     private UserService userService;  | 
|   | 
|     @Autowired  | 
|     private OrderAppraiseInfoMapper orderAppraiseInfoMapper;  | 
|   | 
|     @Autowired  | 
|     private OrderService orderService;  | 
|   | 
|     @Autowired  | 
|     private IdUtils idUtils;  | 
|   | 
|     @Autowired  | 
|     private OrderDetailMapper orderDetailMapper;  | 
|   | 
|     @Autowired  | 
|     private SysItemUserService sysItemUserService;  | 
|   | 
|     @Autowired  | 
|     private SysEnvironmentalInfoService sysEnvironmentalInfoService;  | 
|   | 
|     @Autowired  | 
|     private BusinessUtil businessUtil;  | 
|   | 
|     @Autowired  | 
|     private AddressLevelService addressLevelService;  | 
|   | 
|     public UserScrapStore findByMobile(String mobile) {  | 
|         UserScrapStore userScrapStore = new UserScrapStore();  | 
|         userScrapStore.setMobilePhone(mobile);  | 
|         userScrapStore.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));  | 
|         return userScrapStoreMapper.selectOne(userScrapStore);  | 
|     }  | 
|   | 
|     public UserScrapStore findById(String id) {  | 
|         return userScrapStoreMapper.selectByPrimaryKey(id);  | 
|     }  | 
|   | 
|     public void add(UserScrapStore userScrapStore) {  | 
|         userScrapStore.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG));  | 
|         userScrapStore.setCreateTime(DateUtil.now());  | 
|         userScrapStoreMapper.insertSelective(userScrapStore);  | 
|     }  | 
|   | 
|     public List<UserScrapStore> list(UserScrapStoreListParamDTO userScrapStoreListParamDTO) {  | 
|         List<UserScrapStore> list = userScrapStoreMapper.list(userScrapStoreListParamDTO);  | 
|         //关联了订单 一定会有部分数据 进行特殊处理  | 
|         long count = list.stream().filter(x -> StrUtil.isNotBlank(x.getId())).count();  | 
|         if (count > 0) {  | 
|             for (UserScrapStore userScrapStore : list) {  | 
|                 //满意度  | 
|                 Example example = new Example(OrderAppraiseInfo.class);  | 
|                 Example.Criteria criteria = example.createCriteria();  | 
|                 criteria.andEqualTo("delFlag", Convert.toShort(Constants.DEL_NOT_FLAG));  | 
|                 criteria.andEqualTo("receiverId", userScrapStore.getId());  | 
|                 List<OrderAppraiseInfo> orderAppraiseInfos = orderAppraiseInfoMapper.selectByExample(example);  | 
|                 if (CollUtil.isNotEmpty(orderAppraiseInfos)) {  | 
|                     double sum = orderAppraiseInfos.stream().mapToDouble(x -> Convert.toDouble(x.getStar(), Convert.toDouble(Constants.SCORE_MAX))).sum();  | 
|                     userScrapStore.setSatisfaction(Convert.toInt(sum / (orderAppraiseInfos.size() * Constants.SCORE_MAX) * 100));  | 
|                 }  | 
|   | 
|                 //区域名称  | 
|                 AddressLevelInfo addressLevelInfo = addressLevelService.find(userScrapStore.getProvinceId(), userScrapStore.getCityId(), userScrapStore.getTownshipId());  | 
|                 if(addressLevelInfo!=null){  | 
|                     userScrapStore.setTownshipName(addressLevelInfo.getLevel3Name());  | 
|                 }  | 
|             }  | 
|             return list;  | 
|         }else {  | 
|             return new ArrayList<>();  | 
|         }  | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 预约  | 
|      */  | 
|     public void subscribe(UserScrapSubscribeParamDTO scrapSubscribeParamDTO) {  | 
|   | 
|         //废品站ID  | 
|         String id = scrapSubscribeParamDTO.getId();  | 
|         UserScrapStore byId = findById(id);  | 
|         UserInfo userInfo = userService.findById(scrapSubscribeParamDTO.getUserId());  | 
|   | 
|         //新建个订单并完成状态  | 
|         OrderInfo orderInfo = new OrderInfo();  | 
|         String orderId = idUtils.generate("DD", 1);  | 
|         orderInfo.setOrderId(orderId);  | 
|         orderInfo.setAddress(byId.getDetailAddress());  | 
|         orderInfo.setOrderStatus(OrderEnum.完成.getValue());  | 
|         orderInfo.setOrderType(OrderEnum.废品站.getValue());  | 
|         orderInfo.setAddressArea(byId.getAddressArea());  | 
|         orderInfo.setCreateUserId(scrapSubscribeParamDTO.getUserId());  | 
|         orderInfo.setCreateTime(DateUtil.now());  | 
|         orderInfo.setRelaName(StringUtils.decode(userInfo.getNickName()));  | 
|         orderInfo.setRelaPhone(userInfo.getMobilePhone());  | 
|         orderInfo.setLatitude(userInfo.getLatitude());  | 
|         orderInfo.setLongitude(userInfo.getLongitude());  | 
|         orderInfo.setCreateType(Convert.toShort(1));  | 
|         orderInfo.setScrapId(id);  | 
|         orderService.add(orderInfo);  | 
|   | 
|         OrderDetailInfo orderDetailInfo = new OrderDetailInfo();  | 
|         orderDetailInfo.setOrderId(orderId);  | 
|         orderDetailInfo.setCreateUserName(StringUtils.decode(userInfo.getNickName()));  | 
|         orderDetailInfo.setMobilePhone(userInfo.getMobilePhone());  | 
|         orderDetailInfo.setReceiverName(byId.getStoreName());  | 
|         orderDetailInfo.setReceiverPhone(byId.getMobilePhone());  | 
|         orderDetailInfo.setCompleteTime(DateUtil.now());  | 
|         orderDetailMapper.insertSelective(orderDetailInfo);  | 
|   | 
|         //发送给废品站短信  | 
|         Map<String, Object> map = new HashMap<>();  | 
|         map.put("name", byId.getStoreName());  | 
|         String content = StrUtil.format("你好,{name},收到送货上门订单,请注意查收。", map);  | 
|         SmsUtil.sendPoint(byId.getMobilePhone(), content);  | 
|   | 
|     }  | 
|   | 
|   | 
|     /**  | 
|      * 显示废品站价格列表  | 
|      *  | 
|      * @param itemUserListDTO  | 
|      * @return  | 
|      */  | 
|     public List<ItemUserListResDTO> itemUserList(ItemUserListDTO itemUserListDTO) {  | 
|         List<ItemUserListResDTO> list = new ArrayList<>();  | 
|         UserScrapStore byId = findById(itemUserListDTO.getUserId());  | 
|         List<SysItemUser> byUserId = sysItemUserService.findByUserId(itemUserListDTO.getUserId());  | 
|         for (Iterator<SysItemUser> iterator = byUserId.iterator(); iterator.hasNext(); ) {  | 
|             SysItemUser next = iterator.next();  | 
|             SysEnvironmentalInfo byCityAndItemType = sysEnvironmentalInfoService.findByItemTypeAndAreaNotDel(next.getItemType(), Convert.toInt(byId.getTownshipId()));  | 
|             if (byCityAndItemType == null) {  | 
|                 iterator.remove();  | 
|             } else {  | 
|   | 
|                 next.setPicture(byCityAndItemType.getPicture());  | 
|                 next.setTitle(byCityAndItemType.getTitle());  | 
|                 //平台价格  | 
|                 next.setPrice(businessUtil.changeMoney(byCityAndItemType.getPrice()));  | 
|   | 
|                 //用父类分类包裹  | 
|                 SysEnvironmentalInfo parent = sysEnvironmentalInfoService.findById(byCityAndItemType.getParentId());  | 
|                 Long id = parent.getId();  | 
|                 long count = list.stream().filter(x -> x.getId().equals(id)).count();  | 
|                 if (count == 0) {  | 
|                     ItemUserListResDTO dto = new ItemUserListResDTO();  | 
|                     dto.setTitle(parent.getTitle());  | 
|                     dto.setId(id);  | 
|                     dto.setItemType(parent.getItemType());  | 
|                     List<SysItemUser> resList = new ArrayList<>();  | 
|                     resList.add(next);  | 
|                     dto.setList(resList);  | 
|                     list.add(dto);  | 
|                 } else {  | 
|                     ItemUserListResDTO itemUserListResDTO = list.stream().filter(x -> x.getId().equals(id)).collect(Collectors.toList()).get(0);  | 
|                     List<SysItemUser> list1 = itemUserListResDTO.getList();  | 
|                     list1.add(next);  | 
|                 }  | 
|             }  | 
|         }  | 
|   | 
|         String filterOrder = itemUserListDTO.getFilterOrder();  | 
|         if (StrUtil.isNotBlank(filterOrder)) {  | 
|             for (ItemUserListResDTO itemUserListResDTO : list) {  | 
|                 List<SysItemUser> list1 = itemUserListResDTO.getList();  | 
|                 if (CollUtil.isNotEmpty(list1)) {  | 
|                     if ("1".equals(filterOrder)) {  | 
|                         CollUtil.sort(list1, (o1, o2) -> {  | 
|                             if (Convert.toBigDecimal(o1.getMaxPrice()).compareTo(Convert.toBigDecimal(o2.getMaxPrice())) > 0) {  | 
|                                 return -1;  | 
|                             } else if (Convert.toBigDecimal(o1.getMaxPrice()).compareTo(Convert.toBigDecimal(o2.getMaxPrice())) == 0) {  | 
|                                 return 0;  | 
|                             } else {  | 
|                                 return 1;  | 
|                             }  | 
|                         });  | 
|                     } else {  | 
|                         CollUtil.sort(list1, (o1, o2) -> {  | 
|                             if (Convert.toBigDecimal(o1.getMaxPrice()).compareTo(Convert.toBigDecimal(o2.getMaxPrice())) > 0) {  | 
|                                 return 1;  | 
|                             } else if (Convert.toBigDecimal(o1.getMaxPrice()).compareTo(Convert.toBigDecimal(o2.getMaxPrice())) == 0) {  | 
|                                 return 0;  | 
|                             } else {  | 
|                                 return -1;  | 
|                             }  | 
|                         });  | 
|                     }  | 
|                 }  | 
|             }  | 
|         }  | 
|   | 
|         return list;  | 
|     }  | 
|   | 
|     public void update(UserScrapStore userScrapStore) {  | 
|         userScrapStoreMapper.updateByPrimaryKeySelective(userScrapStore);  | 
|     }  | 
|   | 
|     public void deleteById(String id) {  | 
|         UserScrapStore userScrapStore = new UserScrapStore();  | 
|         userScrapStore.setId(id);  | 
|         userScrapStore.setDelFlag(Convert.toShort(Constants.DEL_FLAG));  | 
|         userScrapStoreMapper.updateByPrimaryKeySelective(userScrapStore);  | 
|         //删除报价  | 
|         sysItemUserService.deleteByUserId(id);  | 
|     }  | 
| }  |