532 lines
21 KiB
Java
532 lines
21 KiB
Java
package com.gxwebsoft.open.controller;
|
|
|
|
import cn.hutool.core.bean.copier.BeanCopier;
|
|
import cn.hutool.core.bean.copier.CopyOptions;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alipay.api.AlipayApiException;
|
|
import com.alipay.api.DefaultAlipayClient;
|
|
import com.alipay.api.domain.AlipayOpenAppQrcodeCreateModel;
|
|
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
|
|
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.gxwebsoft.apps.entity.Equipment;
|
|
import com.gxwebsoft.apps.entity.EquipmentRecord;
|
|
import com.gxwebsoft.apps.param.EquipmentParam;
|
|
import com.gxwebsoft.apps.service.EquipmentRecordService;
|
|
import com.gxwebsoft.apps.service.EquipmentService;
|
|
import com.gxwebsoft.common.core.annotation.OperationLog;
|
|
import com.gxwebsoft.common.core.constants.OrderConstants;
|
|
import com.gxwebsoft.common.core.enums.ESmsType;
|
|
import com.gxwebsoft.common.core.utils.AlipayConfigUtil;
|
|
import com.gxwebsoft.common.core.utils.CacheClient;
|
|
import com.gxwebsoft.common.core.utils.ImageUtil;
|
|
import com.gxwebsoft.common.core.web.*;
|
|
import com.gxwebsoft.common.system.entity.User;
|
|
import com.gxwebsoft.oa.service.IdcardService;
|
|
import com.gxwebsoft.oa.vo.IdcardRespVO;
|
|
import com.gxwebsoft.shop.entity.Order;
|
|
import com.gxwebsoft.shop.entity.OrderPay;
|
|
import com.gxwebsoft.shop.entity.OrderRefund;
|
|
import com.gxwebsoft.shop.param.OrderParam;
|
|
import com.gxwebsoft.shop.param.OrderReceiptParam;
|
|
import com.gxwebsoft.shop.service.OrderPayService;
|
|
import com.gxwebsoft.shop.service.OrderRefundService;
|
|
import com.gxwebsoft.shop.service.OrderService;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiModelProperty;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.validation.Valid;
|
|
import java.math.BigDecimal;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import static com.gxwebsoft.apps.constants.EquipmentConstants.*;
|
|
import static com.gxwebsoft.common.core.constants.OrderConstants.*;
|
|
|
|
/**
|
|
* 设备管理控制器
|
|
*
|
|
* @author 科技小王子
|
|
* @since 2022-11-30 02:11:16
|
|
*/
|
|
@Slf4j
|
|
@Api(tags = "设备管理管理")
|
|
@RestController
|
|
@RequestMapping("/api/open/equipment")
|
|
public class OpenEquipmentController extends BaseController {
|
|
@Resource
|
|
private EquipmentService equipmentService;
|
|
@Resource
|
|
private OrderService orderService;
|
|
@Resource
|
|
private AlipayConfigUtil alipayConfig;
|
|
@Resource
|
|
private EquipmentRecordService equipmentRecordService;
|
|
@Resource
|
|
private OrderRefundService orderRefundService;
|
|
|
|
@Resource
|
|
private OrderPayService orderPayService;
|
|
|
|
@Resource
|
|
private IdcardService idcardService;
|
|
|
|
@Resource
|
|
private RestTemplate restTemplate;
|
|
|
|
@Resource
|
|
private CacheClient cacheClient;
|
|
|
|
@PreAuthorize("hasAuthority('apps:equipment:list')")
|
|
@ApiOperation("分页查询设备管理")
|
|
@GetMapping("/page")
|
|
public ApiResult<PageResult<Equipment>> page(EquipmentParam param) {
|
|
// 使用关联查询
|
|
if (getMerchantCode() != null) {
|
|
param.setMerchantCode(getMerchantCode());
|
|
}
|
|
return success(equipmentService.pageRel(param));
|
|
}
|
|
|
|
@ApiOperation("查询全部设备管理")
|
|
@GetMapping()
|
|
public ApiResult<List<Equipment>> list(EquipmentParam param) {
|
|
PageParam<Equipment, EquipmentParam> page = new PageParam<>(param);
|
|
// page.setDefaultOrder("create_time desc");
|
|
// return success(equipmentService.list(page.getOrderWrapper()));
|
|
// 使用关联查询
|
|
return success(equipmentService.listRel(param));
|
|
}
|
|
|
|
@OperationLog
|
|
@ApiOperation("根据id查询设备管理")
|
|
@GetMapping("/{id}")
|
|
public ApiResult<Equipment> get(@PathVariable("id") Integer id) {
|
|
// return success(equipmentService.getById(id));
|
|
// 使用关联查询
|
|
return success(equipmentService.getByIdRel(id));
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('apps:equipment:save')")
|
|
@OperationLog
|
|
@ApiOperation("添加设备管理")
|
|
@PostMapping()
|
|
public ApiResult<?> save(@RequestBody Equipment equipment) throws AlipayApiException {
|
|
// 记录当前登录用户id、租户id
|
|
User loginUser = getLoginUser();
|
|
if (loginUser != null && getMerchantCode() != null) {
|
|
equipment.setMerchantCode(getMerchantCode());
|
|
}
|
|
if (equipmentService.count(new LambdaQueryWrapper<Equipment>()
|
|
.eq(Equipment::getEquipmentCode, equipment.getEquipmentCode())) > 0) {
|
|
return fail("设备编号已存在");
|
|
}
|
|
if (equipmentService.save(equipment)) {
|
|
// 生成二维码
|
|
String qrcode = createQrcode(equipment);
|
|
equipment.setQrcode(qrcode);
|
|
equipmentService.saveOrUpdate(equipment);
|
|
return success("添加成功");
|
|
}
|
|
return fail("添加失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('apps:equipment:update')")
|
|
@OperationLog
|
|
@ApiOperation("修改设备管理")
|
|
@PutMapping()
|
|
public ApiResult<?> update(@RequestBody Equipment equipment) throws AlipayApiException {
|
|
if (equipmentService.updateById(equipment)) {
|
|
// 生成二维码
|
|
String qrcode = createQrcode(equipment);
|
|
equipment.setQrcode(qrcode);
|
|
equipmentService.saveOrUpdate(equipment);
|
|
return success("修改成功");
|
|
}
|
|
return fail("修改失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('apps:equipment:remove')")
|
|
@OperationLog
|
|
@ApiOperation("删除设备管理")
|
|
@DeleteMapping("/{id}")
|
|
public ApiResult<?> remove(@PathVariable("id") Integer id) {
|
|
if (equipmentService.removeById(id)) {
|
|
return success("删除成功");
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('apps:equipment:save')")
|
|
@OperationLog
|
|
@ApiOperation("批量添加设备管理")
|
|
@PostMapping("/batch")
|
|
public ApiResult<?> saveBatch(@RequestBody List<Equipment> list) {
|
|
if (equipmentService.saveBatch(list)) {
|
|
return success("添加成功");
|
|
}
|
|
return fail("添加失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('apps:equipment:update')")
|
|
@OperationLog
|
|
@ApiOperation("批量修改设备管理")
|
|
@PutMapping("/batch")
|
|
public ApiResult<?> removeBatch(@RequestBody BatchParam<Equipment> batchParam) {
|
|
if (batchParam.update(equipmentService, "equipment_id")) {
|
|
return success("修改成功");
|
|
}
|
|
return fail("修改失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('apps:equipment:remove')")
|
|
@OperationLog
|
|
@ApiOperation("批量删除设备管理")
|
|
@DeleteMapping("/batch")
|
|
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
|
|
if (equipmentService.removeByIds(ids)) {
|
|
return success("删除成功");
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
|
|
// 生成支付宝小程序码
|
|
private String createQrcode(Equipment equipment) throws AlipayApiException {
|
|
// 实例化客户端
|
|
DefaultAlipayClient alipayClient = alipayConfig.alipayClient(getTenantId());
|
|
|
|
AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
|
|
AlipayOpenAppQrcodeCreateModel model = new AlipayOpenAppQrcodeCreateModel();
|
|
model.setUrlParam("pages/checkout/checkout");
|
|
model.setQueryParam("equipmentId=".concat(equipment.getEquipmentId().toString()).concat("&equipmentCode=").concat(equipment.getEquipmentCode()));
|
|
model.setDescribe("扫码租赁电池");
|
|
request.setBizModel(model);
|
|
AlipayOpenAppQrcodeCreateResponse response = alipayClient.certificateExecute(request);
|
|
log.info("支付宝扫码返回:{}", response.getBody());
|
|
if (response.isSuccess()) {
|
|
final JSONObject jsonObject = JSONObject.parseObject(response.getBody());
|
|
final String alipay_open_app_qrcode_create_response = jsonObject.getString("alipay_open_app_qrcode_create_response");
|
|
final JSONObject jsonObject1 = JSONObject.parseObject(alipay_open_app_qrcode_create_response);
|
|
return jsonObject1.getString("qr_code_url");
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@ApiOperation("绑定设备")
|
|
@OperationLog
|
|
@PostMapping("/bind")
|
|
@Transactional(rollbackFor = {Exception.class})
|
|
public ApiResult<?> bindEquipment(@RequestBody Equipment equipment) {
|
|
|
|
User loginUser = getLoginUser();
|
|
// 验证签名
|
|
isCheckSign();
|
|
final Integer orderId = equipment.getOrderId();
|
|
final String equipmentCode = equipment.getEquipmentCode();
|
|
final Order order = orderService.getById(orderId);
|
|
Equipment one = equipmentService.getByEquipmentCode(equipmentCode);
|
|
if (one == null) {
|
|
return fail("设备不存在");
|
|
}
|
|
if (!one.getUserId().equals(0)) {
|
|
return fail("该设备已被绑定");
|
|
}
|
|
// 绑定设备
|
|
Equipment saveData = new Equipment();
|
|
saveData.setEquipmentId(one.getEquipmentId());
|
|
saveData.setUserId(getLoginUserId());
|
|
saveData.setOrderId(orderId);
|
|
if (equipmentService.updateById(saveData)) {
|
|
// 记录明细
|
|
EquipmentRecord record = new EquipmentRecord();
|
|
record.setEquipmentCode(one.getEquipmentCode());
|
|
record.setUserId(getLoginUserId());
|
|
record.setOrderId(orderId);
|
|
record.setEventType(EVENT_TYPE_BIND);
|
|
record.setComments("使用电池:");
|
|
record.setMerchantCode(one.getMerchantCode());
|
|
equipmentRecordService.save(record);
|
|
// 订单发货
|
|
order.setDeliveryStatus(DELIVERY_STATUS_ACCEPT);
|
|
order.setOrderStatus(ORDER_STATUS_COMPLETED);
|
|
order.setReceiptStatus(RECEIPT_STATUS_YES);
|
|
order.setExpirationTime(DateUtil.nextMonth());
|
|
order.setEquipmentId(one.getEquipmentId());
|
|
|
|
JSONObject param = new JSONObject();
|
|
param.put("userId", loginUser.getUserId());
|
|
param.put("userName", loginUser.getNickname());
|
|
param.put("userPhone", loginUser.getPhone());
|
|
param.put("battery_sn", one.getEquipmentCode());
|
|
System.out.println("param = " + param);
|
|
try {
|
|
ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("http://battery.zfdliot.com/api/battery/batteryBindUser", param, JSONObject.class);
|
|
JSONObject body = responseEntity.getBody();
|
|
log.info("电池绑定调用第三方返回:{}", body.toString());
|
|
}catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
|
|
if(order.getOrderSource() == 10) {
|
|
order.setOrderStatus(ORDER_STATUS_OVER);
|
|
}
|
|
orderService.updateById(order);
|
|
|
|
orderPayService.lambdaUpdate().set(OrderPay::getEquipmentId, one.getEquipmentId()).update();
|
|
return success("绑定成功");
|
|
}
|
|
return fail("绑定失败");
|
|
}
|
|
|
|
@OperationLog
|
|
@ApiOperation("更换设备")
|
|
@PostMapping("/change")
|
|
@Transactional(rollbackFor = {Exception.class})
|
|
public ApiResult<?> changeEquipment(@RequestBody Equipment equipment) {
|
|
// 验证签名
|
|
isCheckSign();
|
|
String equipmentCode = equipment.getEquipmentCode();
|
|
Integer loginUserId = getLoginUserId();
|
|
|
|
// 订单信息
|
|
Integer orderId = equipment.getOrderId();
|
|
Order order = orderService.getById(orderId);
|
|
Integer oldEqId = order.getEquipmentId();
|
|
|
|
// 新电池
|
|
Equipment one = equipmentService.getByEquipmentCode(equipmentCode);
|
|
if (one == null) {
|
|
return fail("新设备不存在");
|
|
}
|
|
|
|
String newMerchantCode = one.getMerchantCode();
|
|
// 旧电池
|
|
Equipment old = equipmentService.getById(oldEqId);
|
|
String oldMerchantCode = order.getMerchantCode();
|
|
|
|
Integer userId = one.getUserId();
|
|
if (null != userId && !userId.equals(0)) {
|
|
return fail("该设备已被绑定");
|
|
}
|
|
|
|
Integer orderUserId = order.getUserId();
|
|
if(null == orderUserId || 0 == orderUserId){
|
|
orderUserId = loginUserId;
|
|
}
|
|
|
|
// 绑定新电池
|
|
Equipment saveData = new Equipment();
|
|
saveData.setEquipmentId(one.getEquipmentId());
|
|
saveData.setUserId(orderUserId);
|
|
saveData.setOrderId(orderId);
|
|
saveData.setMerchantCode(oldMerchantCode);
|
|
boolean b = equipmentService.updateById(saveData);
|
|
// 记录新电池明细
|
|
EquipmentRecord record = new EquipmentRecord();
|
|
record.setEquipmentCode(equipmentCode);
|
|
record.setEventType(EVENT_TYPE_BIND);
|
|
record.setUserId(orderUserId);
|
|
record.setOrderId(orderId);
|
|
record.setMerchantCode(oldMerchantCode);
|
|
equipmentRecordService.save(record);
|
|
|
|
if (b) {
|
|
// 解绑旧电池
|
|
old.setUserId(0);
|
|
old.setOrderId(0);
|
|
old.setMerchantCode(newMerchantCode);
|
|
equipmentService.updateById(old);
|
|
// 记录明细
|
|
EquipmentRecord record2 = new EquipmentRecord();
|
|
record2.setEquipmentCode(old.getEquipmentCode());
|
|
record2.setUserId(orderUserId);
|
|
record2.setOrderId(orderId);
|
|
record2.setEventType(EVENT_TYPE_UNBIND);
|
|
record2.setMerchantCode(one.getMerchantCode());
|
|
equipmentRecordService.save(record2);
|
|
// 更新订单
|
|
order.setEquipmentId(one.getEquipmentId());
|
|
orderService.updateById(order);
|
|
|
|
User loginUser = getLoginUser();
|
|
// 通知第三方
|
|
/**
|
|
* {
|
|
* "userId": "1",
|
|
* "userName": "2",
|
|
* "userPhone": "3",
|
|
* "battery_sn": "YXW-BMS4-ABCD-1234567890"
|
|
* }
|
|
*/
|
|
JSONObject param = new JSONObject();
|
|
param.put("userId", loginUser.getUserId());
|
|
param.put("userName", loginUser.getNickname());
|
|
param.put("userPhone", loginUser.getPhone());
|
|
param.put("battery_sn", one.getEquipmentCode());
|
|
System.out.println("param2 = " + param);
|
|
ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("http://battery.zfdliot.com/api/battery/batteryBindUser", param, JSONObject.class);
|
|
JSONObject body = responseEntity.getBody();
|
|
log.info("电池更换调用第三方返回:{}", body);
|
|
return success("换电成功");
|
|
|
|
}
|
|
return fail("换电失败");
|
|
}
|
|
|
|
@ApiOperation("确认收货")
|
|
@PostMapping("/receipt")
|
|
@OperationLog
|
|
public ApiResult<?> receipt(@RequestBody @Valid OrderReceiptParam receiptParam) {
|
|
// 验证签名
|
|
isCheckSign();
|
|
|
|
Order order = orderService.getById(receiptParam.getOrderId());
|
|
Integer receiptStatus = order.getReceiptStatus();
|
|
if(receiptStatus == 20){
|
|
return fail("订单已确认收货,不能重复确认!");
|
|
}
|
|
// 验证身份证真实性
|
|
if(StringUtils.hasText(receiptParam.getOrderSourceData())) {
|
|
String orderSourceDataString = receiptParam.getOrderSourceData();
|
|
List<String> images = JSONObject.parseArray(orderSourceDataString, String.class);
|
|
String front = ImageUtil.ImageBase64(images.get(0) + "?x-oss-process=image/resize,w_750/quality,Q_80");
|
|
|
|
String back = ImageUtil.ImageBase64(images.get(1) + "?x-oss-process=image/resize,w_750/quality,Q_80");
|
|
|
|
IdcardRespVO verify = idcardService.verify(front, back);
|
|
if(!"FP00000".equals(verify.getCode())) {
|
|
return fail("请上传身份证正面照片");
|
|
}
|
|
if(verify.getIssueDate() == null || verify.getIssueOrg() == null || verify.getExpireDate() == null) {
|
|
return fail("请上传身份证反面照片");
|
|
}
|
|
order.setRealName(verify.getName());
|
|
order.setIdCode(verify.getIdCardNo());
|
|
order.setAddress(verify.getAddress());
|
|
|
|
}
|
|
|
|
// 短信验证码校验
|
|
String receiptPhone = receiptParam.getReceiptPhone();
|
|
if(StrUtil.isNotBlank(receiptPhone)){
|
|
String captcha = receiptParam.getCaptcha();
|
|
String code = cacheClient.get(ESmsType.RECEIPT.name() + ":" + receiptPhone);
|
|
if (!StrUtil.equals(code, captcha)) {
|
|
return fail("验证码不正确");
|
|
}
|
|
}
|
|
|
|
BeanCopier.create(receiptParam, order, CopyOptions.create().ignoreNullValue()).copy();
|
|
order.setOrderStatus(ORDER_STATUS_COMPLETED);
|
|
order.setDeliveryStatus(DELIVERY_STATUS_YES);
|
|
order.setReceiptStatus(RECEIPT_STATUS_YES);
|
|
orderService.updateById(order);
|
|
return success("确认收货成功");
|
|
}
|
|
|
|
/*@ApiOperation("确认收货")
|
|
@PostMapping("/receipt")
|
|
public ApiResult<?> receipt(@RequestBody Order order) {
|
|
// 验证签名
|
|
isCheckSign();
|
|
// 验证身份证真实性
|
|
if(StringUtils.hasText(order.getOrderSourceData())) {
|
|
String orderSourceDataString = order.getOrderSourceData();
|
|
List<String> images = JSONObject.parseArray(orderSourceDataString, String.class);
|
|
String front = ImageUtil.ImageBase64(images.get(0) + "?x-oss-process=image/resize,w_750/quality,Q_80");
|
|
|
|
String back = ImageUtil.ImageBase64(images.get(1) + "?x-oss-process=image/resize,w_750/quality,Q_80");
|
|
|
|
IdcardRespVO verify = idcardService.verify(front, back);
|
|
if(!"FP00000".equals(verify.getCode())) {
|
|
return fail("请上传身份证正面照片");
|
|
}
|
|
if(verify.getIssueDate() == null || verify.getIssueOrg() == null || verify.getExpireDate() == null) {
|
|
return fail("请上传身份证反面照片");
|
|
}
|
|
}
|
|
|
|
orderService.updateById(order);
|
|
return success("确认收货成功");
|
|
}*/
|
|
|
|
@OperationLog
|
|
@ApiModelProperty("退租")
|
|
@PostMapping("/rentingOut")
|
|
public ApiResult<?> rentingOut(@RequestBody Order order) {
|
|
// 验证签名
|
|
isCheckSign();
|
|
OrderRefund refund = orderRefundService.getOne(new LambdaQueryWrapper<OrderRefund>()
|
|
.eq(OrderRefund::getOrderId, order.getOrderId()).last("limit 1"));
|
|
// 已有记录 取消退租
|
|
Integer isRefund = order.getIsRefund();
|
|
if (refund != null) {
|
|
if(isRefund == 1){
|
|
return fail("已申请退租!");
|
|
}
|
|
|
|
Integer auditStatus = refund.getAuditStatus();
|
|
if(auditStatus != 10){
|
|
return fail("未申请退租!");
|
|
}
|
|
orderRefundService.removeById(refund.getOrderRefundId());
|
|
order.setReceiptStatus(RECEIPT_STATUS_YES);
|
|
orderService.updateById(order);
|
|
return success("取消退租成功");
|
|
|
|
} else {
|
|
if(isRefund == 2){
|
|
return fail("找不到申请退租记录!");
|
|
}
|
|
|
|
refund = new OrderRefund();
|
|
refund.setOrderId(order.getOrderId());
|
|
refund.setOrderNo(order.getOrderNo());
|
|
refund.setOrderGoodsId(order.getGoodsId());
|
|
refund.setUserId(getLoginUserId());
|
|
refund.setType(10);
|
|
refund.setApplyDesc("申请退租");
|
|
refund.setRefundMoney(new BigDecimal(0));
|
|
refund.setMerchantCode(order.getMerchantCode());
|
|
}
|
|
refund.setAuditStatus(10);
|
|
refund.setOrderNo(order.getOrderNo());
|
|
orderRefundService.saveOrUpdate(refund);
|
|
// 更新订单状态
|
|
Order updateOrder = new Order();
|
|
updateOrder.setReceiptStatus(RECEIPT_STATUS_APPLY);
|
|
updateOrder.setOrderId(order.getOrderId());
|
|
orderService.updateById(updateOrder);
|
|
return success("申请成功,请等待客服人员审核");
|
|
}
|
|
|
|
// @ApiModelProperty("退租")
|
|
// @PostMapping("/rentingOut")
|
|
// public ApiResult<?> rentingOut(@RequestBody Order order){
|
|
// // 验证签名
|
|
// isCheckSign();
|
|
// order.setReceiptStatus(RECEIPT_STATUS_RETURN);
|
|
// if(orderService.updateById(order)){
|
|
// final Equipment equipment = equipmentService.getById(order.getEquipmentId());
|
|
// equipment.setUserId(0);
|
|
// equipmentService.updateById(equipment);
|
|
// return success("操作成功,请等待客服人员审核");
|
|
// }
|
|
// return fail("退租失败");
|
|
// }
|
|
}
|