Commit 0957bc7b authored by lining's avatar lining

refactor: 通用逻辑提到common模块

parent d64070fd
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* <P></P>
*
* @author 李宁
* @version v1.0
* @since 2022/7/7 15:20
*/
@Data
@ApiModel("取消订单请求模型")
public class CancelServiceOrderDTO {
@ApiModelProperty(value = "订单id", required = true)
@NotNull(message = "请选择要取消的订单")
private Long id;
}
package com.onsiteservice.miniapp.controller.order.dto;
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.Future;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
/**
......
package com.onsiteservice.miniapp.controller.order.dto;
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......
package com.onsiteservice.common.order.dto;
import com.onsiteservice.dao.common.page.PageParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <P></P>
*
* @author 李宁
* @version v1.0
* @since 2022/7/7 14:38
*/
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel("分页查询订单请求模型")
public class PageServiceOrderDTO extends PageParams {
@ApiModelProperty(value = "订单状态: 0表示全部,1表示...", required = true)
private Integer orderStatus;
}
package com.onsiteservice.miniapp.controller.order.dto;
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
/**
* <P></P>
......
package com.onsiteservice.miniapp.controller.order.dto;
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.Future;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
@Data
@ApiModel("订单预约请求模型")
public class ReserveServiceOrderDTO {
@ApiModelProperty(value = "服务子类id", required = true)
@NotNull(message = "请选择服务类型")
private Integer subclassId;
/**
* 服务地址id
*/
@ApiModelProperty(value = "服务地址id", required = true)
@NotNull(message = "请选择服务地址")
private Long addressId;
/**
* 期望上门时间
*/
@ApiModelProperty(value = "期望上门时间", required = true)
@NotNull(message = "请选择上门时间")
@Future(message = "上门时间必须为将来的某个时间点")
private Date expectArrivalTime;
/**
* 服务数量
*/
@ApiModelProperty(value = "服务数量", required = true)
@NotNull(message = "请选择服务数量")
@Min(value = 1, message = "服务数量最低为1")
private Integer num;
/**
* 服务需求图片
*/
@ApiModelProperty("服务需求图片")
private String demandImg;
/**
* 服务需求说明
*/
@ApiModelProperty("服务需求说明")
private String demandDesc;
/**
* 是否阅读协议:1已阅读,0未阅读
*/
@ApiModelProperty("是否阅读协议:1已阅读,0未阅读")
@NotNull(message = "请确认是否阅读协议")
private Boolean readAgreement;
@ApiModelProperty("服务图片数组")
@Size(max = 8, message = "最多上传8张图片")
private List<String> urls;
}
\ No newline at end of file
package com.onsiteservice.miniapp.controller.order.dto;
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......
package com.onsiteservice.miniapp.controller.order.dto;
package com.onsiteservice.common.order.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.checkerframework.checker.units.qual.A;
import javax.validation.constraints.Future;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
/**
......
package com.onsiteservice.miniapp.controller.order.vo;
package com.onsiteservice.common.order.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......
package com.onsiteservice.miniapp.service.order;
package com.onsiteservice.service;
import com.github.pagehelper.PageHelper;
import com.onsiteservice.common.order.dto.DispatchServiceOrderDTO;
import com.onsiteservice.common.order.dto.FinishServiceOrderDTO;
import com.onsiteservice.common.order.dto.SendServiceOrderDTO;
import com.onsiteservice.common.order.dto.ValuationServiceOrderDTO;
import com.onsiteservice.constant.constant.BizConstants;
import com.onsiteservice.constant.enums.BizCodeEnum;
import com.onsiteservice.constant.enums.ServiceOrderStatusEnum;
import com.onsiteservice.constant.enums.ServiceUserTypeEnum;
import com.onsiteservice.core.exception.ServiceException;
import com.onsiteservice.core.result.Result;
import com.onsiteservice.core.result.ResultGenerator;
import com.onsiteservice.dao.common.AbstractMapper;
import com.onsiteservice.dao.common.page.PageInfoVO;
import com.onsiteservice.dao.component.RecordComponent;
import com.onsiteservice.dao.mapper.service.*;
import com.onsiteservice.dao.mapper.user.UserMapper;
import com.onsiteservice.entity.address.ServiceAddress;
import com.onsiteservice.entity.category.ServiceCategory;
import com.onsiteservice.entity.category.ServiceSubclass;
import com.onsiteservice.entity.order.ServiceOrder;
import com.onsiteservice.entity.service.*;
import com.onsiteservice.entity.service.ServiceValuator;
import com.onsiteservice.entity.service.ServiceValuatorAssign;
import com.onsiteservice.entity.service.ServiceWorker;
import com.onsiteservice.entity.service.ServiceWorkerAssign;
import com.onsiteservice.entity.user.User;
import com.onsiteservice.miniapp.controller.order.dto.*;
import com.onsiteservice.miniapp.controller.order.vo.ServiceOrderLogVO;
import com.onsiteservice.miniapp.controller.order.vo.ServiceOrderVO;
import com.onsiteservice.miniapp.mapper.order.ServiceOrderBizMapper;
import com.onsiteservice.util.AttrCopyUtils;
import com.onsiteservice.util.RandomUtils;
import com.onsiteservice.util.aliyun.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -35,9 +29,8 @@ import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.List;
import java.util.Objects;
@Service
@Slf4j
......@@ -53,24 +46,8 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
@Autowired
private SmsUtils smsUtils;
@Resource
private ServiceOrderBizMapper serviceOrderBizMapper;
@Resource
private ServiceOrderMapper serviceOrderMapper;
@Resource
private ServiceOrderImgMapper serviceOrderImgMapper;
@Resource
private ServiceAddressMapper serviceAddressMapper;
@Resource
private ServiceSubclassMapper serviceSubclassMapper;
@Resource
private ServiceCategoryMapper serviceCategoryMapper;
@Resource
private ServiceOrderLogMapper serviceOrderLogMapper;
@Resource
private ServiceWorkerMapper serviceWorkerMapper;
......@@ -89,76 +66,6 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
private RecordComponent recordComponent;
public ServiceOrderVO selectById(Long id, Long userId) {
log.info("order selectById id: {}, userId: {}", id, userId);
// 订单信息
Condition c = new Condition(ServiceOrder.class);
c.createCriteria().andEqualTo("id", id);
List<ServiceOrder> serviceOrderList = this.selectByCondition(c);
if (CollectionUtils.isEmpty(serviceOrderList)) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_NOT_EXIST);
}
return buildServiceOrderVO(serviceOrderList).get(0);
}
public Result<PageInfoVO<ServiceOrderVO>> getPage(PageServiceOrderDTO dto, Long userId) {
log.info("order getPage dto: {}, userId: {}", dto, userId);
PageHelper.startPage(dto.getPage(), dto.getSize());
List<ServiceOrder> serviceOrderList = serviceOrderMapper.selectServiceOrderPage(dto.getOrderStatus());
return ResultGenerator.success(new PageInfoVO<>(buildServiceOrderVO(serviceOrderList)));
}
public int reserve(ReserveServiceOrderDTO dto, Long userId) {
log.info("order reserve dto: {}, userId: {}", dto, userId);
ServiceSubclass serviceSubclass = serviceSubclassMapper.selectByPrimaryKey(dto.getSubclassId());
if (Objects.isNull(serviceSubclass)) {
throw new ServiceException(BizCodeEnum.SERVICE_SUBCLASS_NOT_EXIST);
}
ServiceCategory serviceCategory = serviceCategoryMapper.selectByPrimaryKey(serviceSubclass.getCategoryId());
ServiceAddress serviceAddress = serviceAddressMapper.selectByPrimaryKey(dto.getAddressId());
if (Objects.isNull(serviceAddress)) {
throw new ServiceException(BizCodeEnum.SERVICE_ADDRESS_NOT_EXIST);
}
ServiceOrder serviceOrder = AttrCopyUtils.copy(dto, new ServiceOrder());
// 账户号
serviceOrder.setAccountNo(userId);
// 订单号
serviceOrder.setOrderNo(RandomUtils.orderNum());
// 客户手机号
serviceOrder.setPhone(serviceAddress.getPhone());
// 创建人
serviceOrder.setCreateBy(userId);
// 服务名
serviceOrder.setServiceName(
String.format(BizConstants.FormatConstants.CATEGORY_SUBCLASS_SERVICE_NAME,
serviceCategory.getServiceName(),
serviceSubclass.getServiceName()));
int result = this.insertSelective(serviceOrder);
recordComponent.recordServiceOrderImg(serviceOrder, dto.getUrls());
// TODO 发短信通知所有客服
// 记录流程
if (result == 1) {
recordComponent.recordProcess(serviceOrder.getId(), ServiceOrderStatusEnum.RESERVE.getStatus(), ServiceOrderStatusEnum.RESERVE.getMsg(), null, null);
}
return result;
}
/**
* 指派估价员 管理员操作
*/
......@@ -269,30 +176,6 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
}
public int pay(PayServiceOrderDTO dto, Long userId) {
log.info("order pay dto: {}, userId: {}", dto, userId);
// 判断能发送的状态
ServiceOrder serviceOrder = checkOrder(dto.getId(), userId);
if (!serviceOrder.getOrderStatus().equals(ServiceOrderStatusEnum.SEND.getStatus())) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_CANNOT_PAY);
}
serviceOrder.setOrderStatus(ServiceOrderStatusEnum.PAY.getStatus());
serviceOrder.setModifyBy(userId);
int result = serviceOrderMapper.updateByPrimaryKeySelective(serviceOrder);
// TODO 调用微信支付接口
if (result == 1) {
recordComponent.recordProcess(serviceOrder.getId(), ServiceOrderStatusEnum.PAY.getStatus(), ServiceOrderStatusEnum.PAY.getMsg(), null, null);
}
return result;
}
public int dispatch(DispatchServiceOrderDTO dto, Long userId) {
log.info("order dispatch dto: {}, userId: {}", dto, userId);
......@@ -395,70 +278,8 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
}
public int cancelOrder(CancelServiceOrderDTO dto, Long userId) {
log.info("cancelOrder dto: {}, userId: {}", dto, userId);
ServiceOrder serviceOrder = checkOrder(dto.getId(), userId);
if (serviceOrder.getOrderStatus() >= ServiceOrderStatusEnum.PAY.getStatus()) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_CANNOT_CANCEL);
}
disableServiceValuatorAssign(serviceOrder, userId, null);
serviceOrder.setOrderStatus(ServiceOrderStatusEnum.CANCEL.getStatus());
serviceOrder.setModifyBy(userId);
int result = serviceOrderMapper.updateByPrimaryKeySelective(serviceOrder);
if (result == 1) {
recordComponent.recordProcess(serviceOrder.getId(), ServiceOrderStatusEnum.CANCEL.getStatus(), ServiceOrderStatusEnum.CANCEL.getMsg(), null, null);
}
return result;
}
public List<ServiceOrderLogVO> process(Long id, Long userId) {
log.info("order process id: {}, userId: {}", id, userId);
Condition c = new Condition(ServiceOrderLog.class);
c.createCriteria().andEqualTo(BizConstants.OrderConstants.ORDER_ID, id);
List<ServiceOrderLog> serviceOrderLogList = serviceOrderLogMapper.selectByCondition(c);
return serviceOrderLogList.parallelStream().map(e -> AttrCopyUtils.copy(e, new ServiceOrderLogVO())).sorted(Comparator.comparing(ServiceOrderLogVO::getCreateTime).reversed()).collect(Collectors.toList());
}
/**
* 未派单订单 用户可实时退款
* 已派单订单 需要管理员审批
*/
// public int refundOrder(RefundServiceOrderDTO dto, Long userId) {
// log.info("refundOrder dto: {}, userId: {}", dto, userId);
//
// ServiceOrder serviceOrder = checkOrder(dto.getId(), userId);
// if (!serviceOrder.getOrderStatus().equals(ServiceOrderStatusEnum.PAY.getStatus())) {
// throw new ServiceException(BizCodeEnum.SERVICE_ORDER_CANNOT_REFUND);
// }
//
// // 已申请退款
// serviceOrder.setOrderStatus(ServiceOrderStatusEnum.REFUND_APPLY.getStatus());
// int result = serviceOrderMapper.updateByPrimaryKeySelective(serviceOrder);
//
//
// try {
// // 调用微信接口退款 ??
//
// // 通知web
// } catch (Exception e) {
//
// }
//
//
// return result;
// }
private ServiceOrder checkOrder(Long id, Long userId) {
public ServiceOrder checkOrder(Long id, Long userId) {
Condition c = new Condition(ServiceOrder.class);
c.createCriteria().andEqualTo("id", id);
if (userId != null) {
c.createCriteria().andEqualTo(BizConstants.UserConstants.ACCOUNT_NO, userId);
......@@ -497,6 +318,20 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
return serviceValuatorAssign;
}
private ServiceWorkerAssign buildServiceWorkerAssign(DispatchServiceOrderDTO dto, Long userId) {
ServiceWorkerAssign serviceWorkerAssign = new ServiceWorkerAssign();
// 维修工id
serviceWorkerAssign.setWorkerId(dto.getHostId());
// 订单id
serviceWorkerAssign.setOrderId(dto.getId());
// 维修工上门时间
serviceWorkerAssign.setAssignTime(dto.getExpectArrivalTime());
// 创建人
serviceWorkerAssign.setCreateBy(userId);
return serviceWorkerAssign;
}
/**
* 估价员上门时间失效
......@@ -521,22 +356,6 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
serviceValuatorAssignMapper.updateByConditionSelective(serviceValuatorAssign, c);
}
private ServiceWorkerAssign buildServiceWorkerAssign(DispatchServiceOrderDTO dto, Long userId) {
ServiceWorkerAssign serviceWorkerAssign = new ServiceWorkerAssign();
// 维修工id
serviceWorkerAssign.setWorkerId(dto.getHostId());
// 订单id
serviceWorkerAssign.setOrderId(dto.getId());
// 维修工上门时间
serviceWorkerAssign.setAssignTime(dto.getExpectArrivalTime());
// 创建人
serviceWorkerAssign.setCreateBy(userId);
return serviceWorkerAssign;
}
/**
* 维修工上门时间失效
*
......@@ -560,28 +379,4 @@ public class ServiceOrderService extends AbstractMapper<ServiceOrder> {
}
private List<ServiceOrderVO> buildServiceOrderVO(List<ServiceOrder> serviceOrderList) {
// 地址信息
List<Long> addressIdList = serviceOrderList.parallelStream().map(ServiceOrder::getAddressId).collect(Collectors.toList());
List<ServiceAddress> serviceAddressList = serviceAddressMapper.selectByIdList(addressIdList);
Map<Long, ServiceAddress> serviceAddressMap = serviceAddressList.parallelStream().collect(Collectors.toMap(ServiceAddress::getId, Function.identity()));
// 图片信息
List<Long> orderIdList = serviceOrderList.parallelStream().map(ServiceOrder::getId).collect(Collectors.toList());
List<ServiceOrderImg> serviceOrderImgList = serviceOrderImgMapper.selectByOrderIdList(orderIdList);
Map<Long, List<ServiceOrderImg>> serviceOrderImgMap = serviceOrderImgList.parallelStream().collect(Collectors.groupingBy(ServiceOrderImg::getOrderId));
return serviceOrderList.parallelStream().map(e -> {
ServiceOrderVO serviceOrderVO = AttrCopyUtils.copy(e, new ServiceOrderVO());
ServiceAddress sa = serviceAddressMap.getOrDefault(serviceOrderVO.getAddressId(), new ServiceAddress());
serviceOrderVO.setName(sa.getName());
serviceOrderVO.setAddress(sa.getAddress());
serviceOrderVO.setUrls(serviceOrderImgMap.getOrDefault(serviceOrderVO.getId(), new ArrayList<>()).parallelStream()
.map(ServiceOrderImg::getUrl).collect(Collectors.toList()));
return serviceOrderVO;
}).collect(Collectors.toList());
}
}
package com.onsiteservice.miniapp.controller.order;
import com.onsiteservice.common.annotation.user.CurrentUserId;
import com.onsiteservice.common.order.dto.*;
import com.onsiteservice.common.order.vo.ServiceOrderLogVO;
import com.onsiteservice.common.order.vo.ServiceOrderVO;
import com.onsiteservice.core.result.Result;
import com.onsiteservice.dao.common.page.PageInfoVO;
import com.onsiteservice.miniapp.controller.order.dto.*;
import com.onsiteservice.miniapp.controller.order.vo.ServiceOrderLogVO;
import com.onsiteservice.miniapp.controller.order.vo.ServiceOrderVO;
import com.onsiteservice.miniapp.service.order.ServiceOrderService;
import com.onsiteservice.miniapp.controller.order.dto.CancelServiceOrderDTO;
import com.onsiteservice.miniapp.controller.order.dto.PageServiceOrderDTO;
import com.onsiteservice.miniapp.controller.order.dto.ReserveServiceOrderDTO;
import com.onsiteservice.miniapp.service.order.ServiceOrderBizService;
import com.onsiteservice.service.ServiceOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
......@@ -16,7 +20,6 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.Positive;
import java.util.List;
import static com.onsiteservice.core.result.ResultGenerator.fail;
......@@ -36,6 +39,9 @@ import static com.onsiteservice.core.result.ResultGenerator.success;
public class ServiceOrderController {
@Resource
private ServiceOrderBizService serviceOrderBizService;
@Resource
private ServiceOrderService serviceOrderService;
......@@ -43,19 +49,19 @@ public class ServiceOrderController {
@ApiOperation(value = "根据id查询订单详情")
@GetMapping("get/{id}")
public Result<ServiceOrderVO> getDetail(@ApiParam(name = "id", value = "订单id") @PathVariable @Positive Long id, @CurrentUserId Long userId) {
return success(serviceOrderService.selectById(id, userId));
return success(serviceOrderBizService.selectById(id, userId));
}
@ApiOperation(value = "分页查询订单")
@PostMapping("page")
public Result<PageInfoVO<ServiceOrderVO>> getPage(@RequestBody @NonNull @Validated PageServiceOrderDTO dto, @CurrentUserId Long userId) {
return serviceOrderService.getPage(dto, userId);
return serviceOrderBizService.getPage(dto, userId);
}
@ApiOperation(value = "订单预约")
@PostMapping("reserve")
public Result reserve(@RequestBody @NonNull @Validated ReserveServiceOrderDTO dto, @CurrentUserId Long userId) {
return serviceOrderService.reserve(dto, userId) == 1 ? success() : fail("已预约");
return serviceOrderBizService.reserve(dto, userId) == 1 ? success() : fail("已预约");
}
@ApiOperation(value = "订单估价")
......@@ -82,7 +88,7 @@ public class ServiceOrderController {
@ApiOperation(value = "支付订单")
@PostMapping("pay")
public Result pay(@RequestBody @NonNull @Validated PayServiceOrderDTO dto, @CurrentUserId Long userId) {
return serviceOrderService.pay(dto, userId) == 1 ? success() : fail("支付失败");
return serviceOrderBizService.pay(dto, userId) == 1 ? success() : fail("支付失败");
}
@ApiOperation(value = "派单")
......@@ -121,19 +127,14 @@ public class ServiceOrderController {
@ApiOperation(value = "取消订单")
@PostMapping("cancel")
public Result cancel(@RequestBody @NonNull @Validated CancelServiceOrderDTO dto, @CurrentUserId Long userId) {
return serviceOrderService.cancelOrder(dto, userId) == 1 ? success() : fail("取消失败");
return serviceOrderBizService.cancelOrder(dto, userId) == 1 ? success() : fail("取消失败");
}
@ApiOperation(value = "订单流程图")
@GetMapping("process/{id}")
public Result<List<ServiceOrderLogVO>> process(@ApiParam(name = "id", value = "订单id") @PathVariable @Positive Long id, @CurrentUserId Long userId) {
return success(serviceOrderService.process(id, userId));
return success(serviceOrderBizService.process(id, userId));
}
}
package com.onsiteservice.miniapp.service.order;
import com.github.pagehelper.PageHelper;
import com.onsiteservice.common.order.dto.PayServiceOrderDTO;
import com.onsiteservice.common.order.vo.ServiceOrderLogVO;
import com.onsiteservice.common.order.vo.ServiceOrderVO;
import com.onsiteservice.constant.constant.BizConstants;
import com.onsiteservice.constant.enums.BizCodeEnum;
import com.onsiteservice.constant.enums.ServiceOrderStatusEnum;
import com.onsiteservice.core.exception.ServiceException;
import com.onsiteservice.core.result.Result;
import com.onsiteservice.core.result.ResultGenerator;
import com.onsiteservice.dao.common.AbstractMapper;
import com.onsiteservice.dao.common.page.PageInfoVO;
import com.onsiteservice.dao.component.RecordComponent;
import com.onsiteservice.dao.mapper.service.*;
import com.onsiteservice.entity.address.ServiceAddress;
import com.onsiteservice.entity.category.ServiceCategory;
import com.onsiteservice.entity.category.ServiceSubclass;
import com.onsiteservice.entity.order.ServiceOrder;
import com.onsiteservice.entity.service.ServiceOrderImg;
import com.onsiteservice.entity.service.ServiceOrderLog;
import com.onsiteservice.miniapp.controller.order.dto.CancelServiceOrderDTO;
import com.onsiteservice.miniapp.controller.order.dto.PageServiceOrderDTO;
import com.onsiteservice.miniapp.controller.order.dto.ReserveServiceOrderDTO;
import com.onsiteservice.service.ServiceOrderService;
import com.onsiteservice.util.AttrCopyUtils;
import com.onsiteservice.util.RandomUtils;
import com.onsiteservice.util.aliyun.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ServiceOrderBizService extends AbstractMapper<ServiceOrder> {
/**
* 短信相关
*/
@Value("${aliyun.sms.sign}")
private String sign;
@Autowired
private SmsUtils smsUtils;
@Resource
private ServiceOrderMapper serviceOrderMapper;
@Resource
private ServiceOrderImgMapper serviceOrderImgMapper;
@Resource
private ServiceOrderLogMapper serviceOrderLogMapper;
@Resource
private ServiceOrderService serviceOrderService;
@Resource
private ServiceAddressMapper serviceAddressMapper;
@Resource
private ServiceSubclassMapper serviceSubclassMapper;
@Resource
private ServiceCategoryMapper serviceCategoryMapper;
@Autowired
private RecordComponent recordComponent;
public ServiceOrderVO selectById(Long id, Long userId) {
log.info("order selectById id: {}, userId: {}", id, userId);
// 订单信息
Condition c = new Condition(ServiceOrder.class);
c.createCriteria().andEqualTo("id", id);
List<ServiceOrder> serviceOrderList = this.selectByCondition(c);
if (CollectionUtils.isEmpty(serviceOrderList)) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_NOT_EXIST);
}
return buildServiceOrderVO(serviceOrderList).get(0);
}
public Result<PageInfoVO<ServiceOrderVO>> getPage(PageServiceOrderDTO dto, Long userId) {
log.info("order getPage dto: {}, userId: {}", dto, userId);
PageHelper.startPage(dto.getPage(), dto.getSize());
List<ServiceOrder> serviceOrderList = serviceOrderMapper.selectServiceOrderPage(dto.getOrderStatus());
return ResultGenerator.success(new PageInfoVO<>(buildServiceOrderVO(serviceOrderList)));
}
public int reserve(ReserveServiceOrderDTO dto, Long userId) {
log.info("order reserve dto: {}, userId: {}", dto, userId);
ServiceSubclass serviceSubclass = serviceSubclassMapper.selectByPrimaryKey(dto.getSubclassId());
if (Objects.isNull(serviceSubclass)) {
throw new ServiceException(BizCodeEnum.SERVICE_SUBCLASS_NOT_EXIST);
}
ServiceCategory serviceCategory = serviceCategoryMapper.selectByPrimaryKey(serviceSubclass.getCategoryId());
ServiceAddress serviceAddress = serviceAddressMapper.selectByPrimaryKey(dto.getAddressId());
if (Objects.isNull(serviceAddress)) {
throw new ServiceException(BizCodeEnum.SERVICE_ADDRESS_NOT_EXIST);
}
ServiceOrder serviceOrder = AttrCopyUtils.copy(dto, new ServiceOrder());
// 账户号
serviceOrder.setAccountNo(userId);
// 订单号
serviceOrder.setOrderNo(RandomUtils.orderNum());
// 客户手机号
serviceOrder.setPhone(serviceAddress.getPhone());
// 创建人
serviceOrder.setCreateBy(userId);
// 服务名
serviceOrder.setServiceName(
String.format(BizConstants.FormatConstants.CATEGORY_SUBCLASS_SERVICE_NAME,
serviceCategory.getServiceName(),
serviceSubclass.getServiceName()));
int result = this.insertSelective(serviceOrder);
recordComponent.recordServiceOrderImg(serviceOrder, dto.getUrls());
// TODO 发短信通知所有客服
// 记录流程
if (result == 1) {
recordComponent.recordProcess(serviceOrder.getId(), ServiceOrderStatusEnum.RESERVE.getStatus(), ServiceOrderStatusEnum.RESERVE.getMsg(), null, null);
}
return result;
}
public int pay(PayServiceOrderDTO dto, Long userId) {
log.info("order pay dto: {}, userId: {}", dto, userId);
// 判断能发送的状态
ServiceOrder serviceOrder = serviceOrderService.checkOrder(dto.getId(), userId);
if (!serviceOrder.getOrderStatus().equals(ServiceOrderStatusEnum.SEND.getStatus())) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_CANNOT_PAY);
}
serviceOrder.setOrderStatus(ServiceOrderStatusEnum.PAY.getStatus());
serviceOrder.setModifyBy(userId);
int result = serviceOrderMapper.updateByPrimaryKeySelective(serviceOrder);
// TODO 调用微信支付接口
if (result == 1) {
recordComponent.recordProcess(serviceOrder.getId(), ServiceOrderStatusEnum.PAY.getStatus(), ServiceOrderStatusEnum.PAY.getMsg(), null, null);
}
return result;
}
public int cancelOrder(CancelServiceOrderDTO dto, Long userId) {
log.info("cancelOrder dto: {}, userId: {}", dto, userId);
ServiceOrder serviceOrder = serviceOrderService.checkOrder(dto.getId(), userId);
if (serviceOrder.getOrderStatus() >= ServiceOrderStatusEnum.PAY.getStatus()) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_CANNOT_CANCEL);
}
serviceOrderService.disableServiceValuatorAssign(serviceOrder, userId, null);
serviceOrder.setOrderStatus(ServiceOrderStatusEnum.CANCEL.getStatus());
serviceOrder.setModifyBy(userId);
int result = serviceOrderMapper.updateByPrimaryKeySelective(serviceOrder);
if (result == 1) {
recordComponent.recordProcess(serviceOrder.getId(), ServiceOrderStatusEnum.CANCEL.getStatus(), ServiceOrderStatusEnum.CANCEL.getMsg(), null, null);
}
return result;
}
public List<ServiceOrderLogVO> process(Long id, Long userId) {
log.info("order process id: {}, userId: {}", id, userId);
Condition c = new Condition(ServiceOrderLog.class);
c.createCriteria().andEqualTo(BizConstants.OrderConstants.ORDER_ID, id);
List<ServiceOrderLog> serviceOrderLogList = serviceOrderLogMapper.selectByCondition(c);
return serviceOrderLogList.parallelStream().map(e -> AttrCopyUtils.copy(e, new ServiceOrderLogVO())).sorted(Comparator.comparing(ServiceOrderLogVO::getCreateTime).reversed()).collect(Collectors.toList());
}
/**
* 未派单订单 用户可实时退款
* 已派单订单 需要管理员审批
*/
/*public int refundOrder(RefundServiceOrderDTO dto, Long userId) {
log.info("refundOrder dto: {}, userId: {}", dto, userId);
ServiceOrder serviceOrder = checkOrder(dto.getId(), userId);
if (!serviceOrder.getOrderStatus().equals(ServiceOrderStatusEnum.PAY.getStatus())) {
throw new ServiceException(BizCodeEnum.SERVICE_ORDER_CANNOT_REFUND);
}
// 已申请退款
serviceOrder.setOrderStatus(ServiceOrderStatusEnum.REFUND_APPLY.getStatus());
int result = serviceOrderMapper.updateByPrimaryKeySelective(serviceOrder);
try {
// 调用微信接口退款 ??
// 通知web
} catch (Exception e) {
}
return result;
}*/
private List<ServiceOrderVO> buildServiceOrderVO(List<ServiceOrder> serviceOrderList) {
// 地址信息
List<Long> addressIdList = serviceOrderList.parallelStream().map(ServiceOrder::getAddressId).collect(Collectors.toList());
List<ServiceAddress> serviceAddressList = serviceAddressMapper.selectByIdList(addressIdList);
Map<Long, ServiceAddress> serviceAddressMap = serviceAddressList.parallelStream().collect(Collectors.toMap(ServiceAddress::getId, Function.identity()));
// 图片信息
List<Long> orderIdList = serviceOrderList.parallelStream().map(ServiceOrder::getId).collect(Collectors.toList());
List<ServiceOrderImg> serviceOrderImgList = serviceOrderImgMapper.selectByOrderIdList(orderIdList);
Map<Long, List<ServiceOrderImg>> serviceOrderImgMap = serviceOrderImgList.parallelStream().collect(Collectors.groupingBy(ServiceOrderImg::getOrderId));
return serviceOrderList.parallelStream().map(e -> {
ServiceOrderVO serviceOrderVO = AttrCopyUtils.copy(e, new ServiceOrderVO());
ServiceAddress sa = serviceAddressMap.getOrDefault(serviceOrderVO.getAddressId(), new ServiceAddress());
serviceOrderVO.setName(sa.getName());
serviceOrderVO.setAddress(sa.getAddress());
serviceOrderVO.setUrls(serviceOrderImgMap.getOrDefault(serviceOrderVO.getId(), new ArrayList<>()).parallelStream()
.map(ServiceOrderImg::getUrl).collect(Collectors.toList()));
return serviceOrderVO;
}).collect(Collectors.toList());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment