package com.mallplus.order.service.impl;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallplus.common.constant.AllEnum;
import com.mallplus.common.constant.OrderStatus;
import com.mallplus.common.entity.oms.*;
import com.mallplus.common.entity.pms.*;
import com.mallplus.common.entity.sms.*;
import com.mallplus.common.entity.ums.UmsIntegrationConsumeSetting;
import com.mallplus.common.entity.ums.UmsMember;
import com.mallplus.common.entity.ums.UmsMemberBlanceLog;
import com.mallplus.common.exception.ApiMallPlusException;
import com.mallplus.common.exception.BusinessException;
import com.mallplus.common.feign.MarkingFeignClinent;
import com.mallplus.common.feign.MemberFeignClient;
import com.mallplus.common.feign.PmsFeignClinent;
import com.mallplus.common.redis.constant.RedisToolsConstant;
import com.mallplus.common.redis.template.RedisUtil;
import com.mallplus.common.utils.CommonResult;
import com.mallplus.common.utils.DateUtils;
import com.mallplus.common.utils.JsonUtil;
import com.mallplus.common.utils.ValidatorUtils;
import com.mallplus.common.vo.*;
import com.mallplus.order.config.WxAppletProperties;
import com.mallplus.order.mapper.OmsCartItemMapper;
import com.mallplus.order.mapper.OmsOrderMapper;
import com.mallplus.order.mapper.OmsOrderOperateHistoryMapper;
import com.mallplus.order.mapper.OmsOrderSettingMapper;
import com.mallplus.order.service.*;
import com.mallplus.order.utils.applet.TemplateData;
import com.mallplus.order.utils.applet.WX_TemplateMsgUtil;
import com.mallplus.order.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* <p>
* 订单表 服务实现类
* </p>
*
* @author zscat
* @since 2019-04-17
*/
@Slf4j
@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements IOmsOrderService {
@Autowired
private WxAppletProperties wxAppletProperties;
@Resource
private WechatApiService wechatApiService;
@Resource
private IUmsMemberReceiveAddressService addressService;
@Resource
private OmsOrderMapper orderMapper;
@Resource
private RedisUtil redisUtil;
@Resource
private IOmsOrderOperateHistoryService orderOperateHistoryDao;
@Resource
private OmsOrderOperateHistoryMapper orderOperateHistoryMapper;
@Resource
private PmsFeignClinent pmsFeignClinent;
@Resource
private MemberFeignClient memberFeignClient;
@Resource
private MarkingFeignClinent markingFeignClinent;
@Resource
private OmsCartItemMapper cartItemMapper;
@Resource
private IOmsOrderService orderService;
@Resource
private IOmsOrderItemService orderItemService;
@Resource
private OmsOrderSettingMapper orderSettingMapper;
@Resource
private IOmsCartItemService cartItemService;
@Autowired
private IOmsOrderOperateHistoryService orderOperateHistoryService;
@Autowired
private ApiContext apiContext;
@Override
public int delivery(List<OmsOrderDeliveryParam> deliveryParamList) {
//批量发货
int count = orderMapper.delivery(deliveryParamList);
//添加操作记录
List<OmsOrderOperateHistory> operateHistoryList = deliveryParamList.stream()
.map(omsOrderDeliveryParam -> {
OmsOrderOperateHistory history = new OmsOrderOperateHistory();
history.setOrderId(omsOrderDeliveryParam.getOrderId());
history.setCreateTime(new Date());
history.setOperateMan("后台管理员");
history.setOrderStatus(2);
history.setNote("完成发货");
return history;
}).collect(Collectors.toList());
orderOperateHistoryDao.saveBatch(operateHistoryList);
return count;
}
@Override
public int close(List<Long> ids, String note) {
OmsOrder record = new OmsOrder();
record.setStatus(4);
int count = orderMapper.update(record, new QueryWrapper<OmsOrder>().eq("delete_status",0).in("id",ids));
List<OmsOrderOperateHistory> historyList = ids.stream().map(orderId -> {
OmsOrderOperateHistory history = new OmsOrderOperateHistory();
history.setOrderId(orderId);
history.setCreateTime(new Date());
history.setOperateMan("后台管理员");
history.setOrderStatus(4);
history.setNote("订单关闭:" + note);
return history;
}).collect(Collectors.toList());
orderOperateHistoryDao.saveBatch(historyList);
return count;
}
@Override
public int updateReceiverInfo(OmsReceiverInfoParam receiverInfoParam) {
OmsOrder order = new OmsOrder();
order.setId(receiverInfoParam.getOrderId());
order.setReceiverName(receiverInfoParam.getReceiverName());
order.setReceiverPhone(receiverInfoParam.getReceiverPhone());
order.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
order.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
order.setReceiverProvince(receiverInfoParam.getReceiverProvince());
order.setReceiverCity(receiverInfoParam.getReceiverCity());
order.setReceiverRegion(receiverInfoParam.getReceiverRegion());
order.setModifyTime(new Date());
int count = orderMapper.updateById(order);
//插入操作记录
OmsOrderOperateHistory history = new OmsOrderOperateHistory();
history.setOrderId(receiverInfoParam.getOrderId());
history.setCreateTime(new Date());
history.setOperateMan("后台管理员");
history.setOrderStatus(receiverInfoParam.getStatus());
history.setNote("修改收货人信息");
orderOperateHistoryMapper.insert(history);
return count;
}
@Override
public int updateMoneyInfo(OmsMoneyInfoParam moneyInfoParam) {
OmsOrder order = new OmsOrder();
order.setId(moneyInfoParam.getOrderId());
order.setFreightAmount(moneyInfoParam.getFreightAmount());
order.setDiscountAmount(moneyInfoParam.getDiscountAmount());
order.setModifyTime(new Date());
int count = orderMapper.updateById(order);
//插入操作记录
OmsOrderOperateHistory history = new OmsOrderOperateHistory();
history.setOrderId(moneyInfoParam.getOrderId());
history.setCreateTime(new Date());
history.setOperateMan("后台管理员");
history.setOrderStatus(moneyInfoParam.getStatus());
history.setNote("修改费用信息");
orderOperateHistoryMapper.insert(history);
return count;
}
@Override
public int updateNote(Long id, String note, Integer status) {
OmsOrder order = new OmsOrder();
order.setId(id);
order.setNote(note);
order.setModifyTime(new Date());
int count = orderMapper.updateById(order);
OmsOrderOperateHistory history = new OmsOrderOperateHistory();
history.setOrderId(id);
history.setCreateTime(new Date());
history.setOperateMan("后台管理员");
history.setOrderStatus(status);
history.setNote("修改备注信息:" + note);
orderOperateHistoryMapper.insert(history);
return count;
}
/**
* 计算购物车中商品的价格
*/