diff --git a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/domain/entity/WxInstrumentSerialLog.java b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/domain/entity/WxInstrumentSerialLog.java new file mode 100644 index 0000000..1ee77b8 --- /dev/null +++ b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/domain/entity/WxInstrumentSerialLog.java @@ -0,0 +1,174 @@ +package com.flossom.common.core.domain.entity; + +import com.flossom.common.core.annotation.Excel; +import com.flossom.common.core.web.domain.BaseEntity; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +/** + * 仪器序列号关联日志对象 wx_instrument_serial_log + * + * @author flossom + * @date 2024-01-22 + */ +public class WxInstrumentSerialLog extends BaseEntity { + private static final long serialVersionUID = 1L; + + /** + * $column.columnComment + */ + private Long id; + + /** + * 会员id + */ + @Excel(name = "会员id") + private Long userId; + + /** + * 会员名称 + */ + @Excel(name = "会员名称") + private String userName; + + /** + * 会员手机号码 + */ + @Excel(name = "会员手机号码") + private String userMobile; + + /** + * 会员头像 + */ + @Excel(name = "会员头像") + private String userHeadimg; + + /** + * 仪器id + */ + @Excel(name = "仪器id") + private Long instrumentId; + + /** + * 仪器名称 + */ + @Excel(name = "仪器名称") + private String instrumentName; + + /** + * 序列号 + */ + @Excel(name = "序列号") + private String serial; + + /** + * 绑定状态,1未绑定,2已解绑 + */ + @Excel(name = "绑定状态,1未绑定,2已解绑") + private Integer bindingStatus; + + /** + * 状态(0正常 1停用) + */ + @Excel(name = "状态", readConverterExp = "0=正常,1=停用") + private Long status; + + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public void setUserId(Long userId) { + this.userId = userId; + } + + public Long getUserId() { + return userId; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserName() { + return userName; + } + + public void setUserMobile(String userMobile) { + this.userMobile = userMobile; + } + + public String getUserMobile() { + return userMobile; + } + + public void setUserHeadimg(String userHeadimg) { + this.userHeadimg = userHeadimg; + } + + public String getUserHeadimg() { + return userHeadimg; + } + + public void setInstrumentId(Long instrumentId) { + this.instrumentId = instrumentId; + } + + public Long getInstrumentId() { + return instrumentId; + } + + public void setInstrumentName(String instrumentName) { + this.instrumentName = instrumentName; + } + + public String getInstrumentName() { + return instrumentName; + } + + public void setSerial(String serial) { + this.serial = serial; + } + + public String getSerial() { + return serial; + } + + public void setBindingStatus(Integer bindingStatus) { + this.bindingStatus = bindingStatus; + } + + public Integer getBindingStatus() { + return bindingStatus; + } + + public void setStatus(Long status) { + this.status = status; + } + + public Long getStatus() { + return status; + } + + @Override + public String toString() { + return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) + .append("id", getId()) + .append("userId", getUserId()) + .append("userName", getUserName()) + .append("userMobile", getUserMobile()) + .append("userHeadimg", getUserHeadimg()) + .append("instrumentId", getInstrumentId()) + .append("instrumentName", getInstrumentName()) + .append("serial", getSerial()) + .append("bindingStatus", getBindingStatus()) + .append("status", getStatus()) + .append("createBy", getCreateBy()) + .append("createTime", getCreateTime()) + .append("remark", getRemark()) + .toString(); + } +} diff --git a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/enums/BindingStatusEnums.java b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/enums/BindingStatusEnums.java new file mode 100644 index 0000000..c78cd36 --- /dev/null +++ b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/enums/BindingStatusEnums.java @@ -0,0 +1,27 @@ +package com.flossom.common.core.enums; + +/** + * 序列号的绑定状态 + * 0已经绑定,1未绑定,2已解绑 + * + * @author flossom + */ +public enum BindingStatusEnums { + BINDED(0, "已经绑定"), NO_BOUND(1, "未绑定"), UN_BOUND(2, "已解绑"); + + private final Integer code; + private final String info; + + BindingStatusEnums(Integer code, String info) { + this.code = code; + this.info = info; + } + + public Integer getCode() { + return code; + } + + public String getInfo() { + return info; + } +} diff --git a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/enums/SerialValidStatusEnums.java b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/enums/SerialValidStatusEnums.java new file mode 100644 index 0000000..bc664f7 --- /dev/null +++ b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/enums/SerialValidStatusEnums.java @@ -0,0 +1,27 @@ +package com.flossom.common.core.enums; + +/** + * wx_instrument_serial:valid_status + * + * @author flossom + */ +public enum SerialValidStatusEnums { + + INVALID(0, "无效"), EFFECTIVE(1, "有效"); + + private final Integer code; + private final String info; + + SerialValidStatusEnums(Integer code, String info) { + this.code = code; + this.info = info; + } + + public Integer getCode() { + return code; + } + + public String getInfo() { + return info; + } +} diff --git a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/exception/ServiceReturnCodeException.java b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/exception/ServiceReturnCodeException.java new file mode 100644 index 0000000..509553b --- /dev/null +++ b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/exception/ServiceReturnCodeException.java @@ -0,0 +1,64 @@ +package com.flossom.common.core.exception; + +/** + * 业务正常返回响应码,不是异常 + * + * @author flossom + */ +public final class ServiceReturnCodeException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * 错误码 + */ + private Integer code; + + /** + * 错误提示 + */ + private String message; + + /** + * 错误明细,内部调试错误 + */ + private String detailMessage; + + /** + * 空构造方法,避免反序列化问题 + */ + public ServiceReturnCodeException() { + } + + public ServiceReturnCodeException(String message) { + this.message = message; + } + + public ServiceReturnCodeException(String message, Integer code) { + this.message = message; + this.code = code; + } + + public String getDetailMessage() { + return detailMessage; + } + + @Override + public String getMessage() { + return message; + } + + public Integer getCode() { + return code; + } + + public ServiceReturnCodeException setMessage(String message) { + this.message = message; + return this; + } + + public ServiceReturnCodeException setDetailMessage(String detailMessage) { + this.detailMessage = detailMessage; + return this; + } +} diff --git a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxInstrumentSerialLogMapper.java b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxInstrumentSerialLogMapper.java new file mode 100644 index 0000000..0c82050 --- /dev/null +++ b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxInstrumentSerialLogMapper.java @@ -0,0 +1,62 @@ +package com.flossom.common.core.mapper; + +import com.flossom.common.core.domain.entity.WxInstrumentSerialLog; + +import java.util.List; + + +/** + * 仪器序列号关联日志Mapper接口 + * + * @author flossom + * @date 2024-01-22 + */ +public interface WxInstrumentSerialLogMapper { + /** + * 查询仪器序列号关联日志 + * + * @param id 仪器序列号关联日志主键 + * @return 仪器序列号关联日志 + */ + public WxInstrumentSerialLog selectWxInstrumentSerialLogById(Long id); + + /** + * 查询仪器序列号关联日志列表 + * + * @param wxInstrumentSerialLog 仪器序列号关联日志 + * @return 仪器序列号关联日志集合 + */ + public List selectWxInstrumentSerialLogList(WxInstrumentSerialLog wxInstrumentSerialLog); + + /** + * 新增仪器序列号关联日志 + * + * @param wxInstrumentSerialLog 仪器序列号关联日志 + * @return 结果 + */ + public int insertWxInstrumentSerialLog(WxInstrumentSerialLog wxInstrumentSerialLog); + + /** + * 修改仪器序列号关联日志 + * + * @param wxInstrumentSerialLog 仪器序列号关联日志 + * @return 结果 + */ + public int updateWxInstrumentSerialLog(WxInstrumentSerialLog wxInstrumentSerialLog); + + /** + * 删除仪器序列号关联日志 + * + * @param id 仪器序列号关联日志主键 + * @return 结果 + */ + public int deleteWxInstrumentSerialLogById(Long id); + + /** + * 批量删除仪器序列号关联日志 + * + * @param ids 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteWxInstrumentSerialLogByIds(Long[] ids); +} diff --git a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxInstrumentSerialMapper.java b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxInstrumentSerialMapper.java index ffbbf81..41832a7 100644 --- a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxInstrumentSerialMapper.java +++ b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxInstrumentSerialMapper.java @@ -73,4 +73,8 @@ public interface WxInstrumentSerialMapper * @return 结果 */ public int updateAll(WxInstrumentSerial wxInstrumentSerial); + + WxInstrumentSerial selectEntityListBySerial(WxInstrumentSerial wxInstrumentSerial); + + void updateBySerial(WxInstrumentSerial wxInstrumentSerial); } diff --git a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxUserInstrumentMapper.java b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxUserInstrumentMapper.java index 152dda1..859348e 100644 --- a/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxUserInstrumentMapper.java +++ b/flossom-common/flossom-common-core/src/main/java/com/flossom/common/core/mapper/WxUserInstrumentMapper.java @@ -59,4 +59,8 @@ public interface WxUserInstrumentMapper * @return 结果 */ public int deleteWxUserInstrumentByIds(Long[] ids); + + Integer selectUiByInstrumentId(WxUserInstrument wxUserInstrument); + + List selectListByUserIdAndInstrumentId(WxUserInstrument wxUserInstrument); } diff --git a/flossom-common/flossom-common-core/src/main/resources/mapper/WxInstrumentSerialLogMapper.xml b/flossom-common/flossom-common-core/src/main/resources/mapper/WxInstrumentSerialLogMapper.xml new file mode 100644 index 0000000..ee22f26 --- /dev/null +++ b/flossom-common/flossom-common-core/src/main/resources/mapper/WxInstrumentSerialLogMapper.xml @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + + + + select id, user_id, user_name, user_mobile, user_headimg, instrument_id, instrument_name, serial, binding_status, status, create_by, create_time, remark from wx_instrument_serial_log + + + + + + + + insert into wx_instrument_serial_log + + user_id, + user_name, + user_mobile, + user_headimg, + instrument_id, + instrument_name, + serial, + binding_status, + status, + create_by, + create_time, + remark, + + + #{userId}, + #{userName}, + #{userMobile}, + #{userHeadimg}, + #{instrumentId}, + #{instrumentName}, + #{serial}, + #{bindingStatus}, + #{status}, + #{createBy}, + #{createTime}, + #{remark}, + + + + + update wx_instrument_serial_log + + user_id = #{userId}, + user_name = #{userName}, + user_mobile = #{userMobile}, + user_headimg = #{userHeadimg}, + instrument_id = #{instrumentId}, + instrument_name = #{instrumentName}, + serial = #{serial}, + binding_status = #{bindingStatus}, + status = #{status}, + create_by = #{createBy}, + create_time = #{createTime}, + remark = #{remark}, + + where id = #{id} + + + + delete from wx_instrument_serial_log where id = #{id} + + + + delete from wx_instrument_serial_log where id in + + #{id} + + + \ No newline at end of file diff --git a/flossom-common/flossom-common-core/src/main/resources/mapper/WxInstrumentSerialMapper.xml b/flossom-common/flossom-common-core/src/main/resources/mapper/WxInstrumentSerialMapper.xml index 53dddc7..7d39cf5 100644 --- a/flossom-common/flossom-common-core/src/main/resources/mapper/WxInstrumentSerialMapper.xml +++ b/flossom-common/flossom-common-core/src/main/resources/mapper/WxInstrumentSerialMapper.xml @@ -44,6 +44,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" order by create_time desc + + + + + + + + SELECT LAST_INSERT_ID() + insert into wx_user_instrument user_id, diff --git a/flossom-common/flossom-common-security/src/main/java/com/flossom/common/security/handler/GlobalExceptionHandler.java b/flossom-common/flossom-common-security/src/main/java/com/flossom/common/security/handler/GlobalExceptionHandler.java index 30f6a6b..b2e3548 100644 --- a/flossom-common/flossom-common-security/src/main/java/com/flossom/common/security/handler/GlobalExceptionHandler.java +++ b/flossom-common/flossom-common-security/src/main/java/com/flossom/common/security/handler/GlobalExceptionHandler.java @@ -4,6 +4,7 @@ import com.flossom.common.core.constant.HttpStatus; import com.flossom.common.core.exception.DemoModeException; import com.flossom.common.core.exception.InnerAuthException; import com.flossom.common.core.exception.ServiceException; +import com.flossom.common.core.exception.ServiceReturnCodeException; import com.flossom.common.core.exception.auth.NotPermissionException; import com.flossom.common.core.exception.auth.NotRoleException; import com.flossom.common.core.utils.StringUtils; @@ -73,6 +74,18 @@ public class GlobalExceptionHandler return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage()); } + + /** + * 业务正常返回响应码,不是异常 + */ + @ExceptionHandler(ServiceReturnCodeException.class) + public AjaxResult handleServiceReturnCodeException(ServiceReturnCodeException e, HttpServletRequest request) + { + log.info(e.getMessage()); + Integer code = e.getCode(); + return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage()); + } + /** * 请求路径中缺少必需的路径变量 */ diff --git a/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/controller/WxInstrumenController.java b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/controller/WxInstrumenController.java deleted file mode 100644 index 67a46fd..0000000 --- a/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/controller/WxInstrumenController.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.flossom.miniProgram.controller; - -import com.flossom.common.core.web.controller.BaseController; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * 仪器关联正品控产品名Controller - * - * @author flossom - * @date 2024-01-15 - */ -@RestController -@RequestMapping("/instrument") -public class WxInstrumenController extends BaseController { - - -} diff --git a/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/controller/WxInstrumentController.java b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/controller/WxInstrumentController.java new file mode 100644 index 0000000..1677139 --- /dev/null +++ b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/controller/WxInstrumentController.java @@ -0,0 +1,58 @@ +package com.flossom.miniProgram.controller; + +import com.flossom.common.core.domain.R; +import com.flossom.common.core.exception.ServiceException; +import com.flossom.common.core.utils.StringUtils; +import com.flossom.common.core.web.controller.BaseController; +import com.flossom.common.core.web.domain.AjaxResult; +import com.flossom.miniProgram.service.IWxInstrumentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import javax.validation.constraints.NotBlank; + +/** + * 仪器列Controller + * + * @author flossom + * @date 2024-01-06 + */ +@RestController +@RequestMapping("/instrument") +public class WxInstrumentController extends BaseController { + + @Autowired + private IWxInstrumentService wxInstrumentService; + + /** + * 根据序列号获取仪器信息 + * + * @return 当返回 203 时,需要页面跳转到联系客服 + */ + @GetMapping(value = "/getInstrumentInfoBySerial") + public R getInstrumentInfoBySerial(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial) { + return R.ok(wxInstrumentService.getInstrumentInfoBySerial(serial)); + } + + /** + * 用户、序列号与仪器绑定 + */ + @GetMapping(value = "/binding") + public R binding(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial) { + wxInstrumentService.binding(serial); + return R.ok(); + } + + /** + * 换绑 + */ + @GetMapping(value = "/exchangeBinding") + public R exchangeBinding(@NotBlank(message = "序列号不能为空") @RequestParam("serial") String serial) { + wxInstrumentService.exchangeBinding(serial); + return R.ok(); + } + + +} diff --git a/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/IWxInstrumentService.java b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/IWxInstrumentService.java new file mode 100644 index 0000000..2f25be1 --- /dev/null +++ b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/IWxInstrumentService.java @@ -0,0 +1,17 @@ +package com.flossom.miniProgram.service; + +import com.flossom.common.core.domain.entity.WxInstrumentSerial; + +/** + * 仪器列Service接口 + * + * @author flossom + * @date 2024-01-06 + */ +public interface IWxInstrumentService { + WxInstrumentSerial getInstrumentInfoBySerial(String serial); + + void binding(String serial); + + void exchangeBinding(String serial); +} diff --git a/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/impl/WxInstrumentSerialServiceImpl.java b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/impl/WxInstrumentSerialServiceImpl.java new file mode 100644 index 0000000..69626d9 --- /dev/null +++ b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/impl/WxInstrumentSerialServiceImpl.java @@ -0,0 +1,20 @@ +package com.flossom.miniProgram.service.impl; + +import com.flossom.common.core.mapper.WxInstrumentSerialMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * 仪器序列号关联Service业务层处理 + * + * @author flossom + * @date 2024-01-10 + */ +@Service +public class WxInstrumentSerialServiceImpl { + + @Autowired + private WxInstrumentSerialMapper wxInstrumentSerialMapper; + + +} diff --git a/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/impl/WxInstrumentServiceImpl.java b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/impl/WxInstrumentServiceImpl.java new file mode 100644 index 0000000..6acfde5 --- /dev/null +++ b/flossom-modules/flossom-mini-program/src/main/java/com/flossom/miniProgram/service/impl/WxInstrumentServiceImpl.java @@ -0,0 +1,254 @@ +package com.flossom.miniProgram.service.impl; + +import com.flossom.common.core.domain.entity.*; +import com.flossom.common.core.enums.BindingStatusEnums; +import com.flossom.common.core.enums.SerialValidStatusEnums; +import com.flossom.common.core.enums.Status; +import com.flossom.common.core.exception.ServiceException; +import com.flossom.common.core.exception.ServiceReturnCodeException; +import com.flossom.common.core.mapper.*; +import com.flossom.common.core.utils.DateUtils; +import com.flossom.common.core.utils.StringUtils; +import com.flossom.common.security.utils.SecurityUtils; +import com.flossom.miniProgram.service.IWxInstrumentService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Date; +import java.util.List; + +/** + * 仪器列Service业务层处理 + * + * @author flossom + * @date 2024-01-06 + */ +@Service +public class WxInstrumentServiceImpl implements IWxInstrumentService { + + protected final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private WxInstrumentMapper wxInstrumentMapper; + + @Autowired + private WxInstrumentSerialMapper wxInstrumentSerialMapper; + + @Autowired + private WxUserInstrumentMapper wxUserInstrumentMapper; + + @Autowired + private WxUserInstrumentLogMapper wxUserInstrumentLogMapper; + + + @Override + public WxInstrumentSerial getInstrumentInfoBySerial(String serial) { + WxInstrumentSerial query = new WxInstrumentSerial(); + query.setSerial(serial); + query.setValidStatus(SerialValidStatusEnums.EFFECTIVE.getCode()); + query.setStatus(Status.OK.getCode().longValue()); + WxInstrumentSerial wxInstrumentSerial = wxInstrumentSerialMapper.selectEntityListBySerial(query); + // 序列号唯一 + if (wxInstrumentSerial == null) { + logger.info("serial:{}, 没有对应的数据", serial); + throw new ServiceReturnCodeException("没有对应的序列号,跳转联系客服", 203); + } + return wxInstrumentSerial; + } + + @Override + @Transactional + public void binding(String serial) { + WxUserMember wxUserMember = SecurityUtils.getLoginUser().getWxUserMember(); + /* 1、获取序列号信息 */ + WxInstrumentSerial wxInstrumentSerial = getInstrumentInfoBySerial(serial); + + /** + * 2、判断当前用户是否绑定了序列号对应仪器的序列号 + * 逻辑:查询‘当前用户’是否绑定了扫码序列号对应的仪器 + * 绑定了: + * 1、序列号一致,直接获取仪器列表 + * 2、序列号不一致,询问是否对该仪器进行换绑 + * 没有绑定: + * 1、是否有其他用户绑定,如果没有,开启绑定流程 + * 2、其他用户绑定了,返回联系客服 + */ + // 获取用户绑定了对应仪器的序列号 + WxUserInstrument userBindInstrumentQuery = new WxUserInstrument(); + userBindInstrumentQuery.setUserId(wxUserMember.getId()); + userBindInstrumentQuery.setInstrumentId(wxInstrumentSerial.getInstrumentId()); + userBindInstrumentQuery.setStatus(Status.OK.getCode().longValue()); + List wxUserInstrumentList = wxUserInstrumentMapper.selectListByUserIdAndInstrumentId(userBindInstrumentQuery); + // 2.1、当前用户绑定了序列号对应的仪器的序列号 + if (wxUserInstrumentList != null) { + if (wxUserInstrumentList.size() != 1) { + logger.error("根据 用户id:{} 和 仪器id:{} 查询出来的数据为空或者数据量不止一条", wxUserMember.getId(), wxInstrumentSerial.getInstrumentId()); + throw new ServiceException("绑定失败"); + } + WxUserInstrument wxUserInstrument = wxUserInstrumentList.get(0); + /* 2.1.1、判断扫码获取的序列号和用户绑定的序列号是否一致 */ + if (StringUtils.equals(wxUserInstrument.getSerial(), serial)) { + logger.info("相同序列号,页面重新发起查询用户拥有的仪器列表"); + throw new ServiceReturnCodeException("扫码的序列号已被当前用户绑定,请查询仪器列表", 200); + } else { + /* 2.1.2、绑定过仪器,但不是同一个序列号,询问是否需要换绑 */ + logger.info("绑定过该类型仪器,但当前扫码不是以前绑定的仪器,询问是否换绑仪器"); + throw new ServiceReturnCodeException("绑定过该类型仪器,但当前扫码不是以前绑定的仪器,询问是否换绑仪器", 202); + } + } + // 2.2、用户没有绑定过序列号对应的仪器 + else { + /* 2.2.1、判断序列号是否被别人绑定了 */ + WxUserInstrument query = new WxUserInstrument(); + query.setUserId(wxUserMember.getId()); + query.setInstrumentId(wxInstrumentSerial.getInstrumentId()); + query.setStatus(Status.OK.getCode().longValue()); + query.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + Integer num = wxUserInstrumentMapper.selectUiByInstrumentId(query); + if (num != null && num > 0) { + logger.info("被别人绑定了,页面跳转联系客服"); + throw new ServiceReturnCodeException("当前序列码被别人绑定了,页面跳转联系客服", 203); + } + /* 2.2.2、绑定仪器 */ + else { + WxInstrument wxInstrument = wxInstrumentMapper.selectWxInstrumentById(wxInstrumentSerial.getInstrumentId()); + WxUserInstrument saveEntity = new WxUserInstrument(); + saveEntity.setUserId(wxUserMember.getId()); + saveEntity.setSerial(serial); + saveEntity.setInstrumentId(wxInstrumentSerial.getInstrumentId()); + saveEntity.setInstrumentName(wxInstrumentSerial.getInstrumentName()); + saveEntity.setGuarantee(wxInstrument.getGuarantee()); + if (wxInstrument.getGuarantee() != null && wxInstrument.getGuarantee() >= 0) { + Instant instant = LocalDateTime.now().plusYears(wxInstrument.getGuarantee()).atZone(ZoneId.systemDefault()).toInstant(); + saveEntity.setGuaranteeEndtime(Date.from(instant)); + } + saveEntity.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + saveEntity.setStatus(Status.OK.getCode().longValue()); + saveEntity.setCreateBy(wxUserMember.getNickname()); + saveEntity.setCreateTime(DateUtils.getNowDate()); + wxUserInstrumentMapper.insertWxUserInstrument(saveEntity); + + // 更新仪器序列号绑定状态 + WxInstrumentSerial saveSerial = new WxInstrumentSerial(); + saveSerial.setId(wxInstrumentSerial.getId()); + saveSerial.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + saveSerial.setUpdateBy(wxUserMember.getNickname()); + saveSerial.setUpdateTime(DateUtils.getNowDate()); + wxInstrumentSerialMapper.updateWxInstrumentSerial(saveSerial); + + // 保存绑定记录 + WxUserInstrumentLog wxUserInstrumentLog = new WxUserInstrumentLog(); + wxUserInstrumentLog.setUserInstrumentId(saveEntity.getId()); + wxUserInstrumentLog.setUserId(wxUserMember.getId()); + wxUserInstrumentLog.setSerial(serial); + wxUserInstrumentLog.setInstrumentId(wxInstrument.getId()); + wxUserInstrumentLog.setInstrumentName(wxInstrument.getName()); + wxUserInstrumentLog.setGuarantee(wxInstrument.getGuarantee()); + wxUserInstrumentLog.setGuaranteeEndtime(saveEntity.getGuaranteeEndtime()); + wxUserInstrumentLog.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + wxUserInstrumentLog.setStatus(Status.OK.getCode().longValue()); + wxUserInstrumentLog.setCreateBy(wxUserMember.getNickname()); + wxUserInstrumentLog.setCreateTime(DateUtils.getNowDate()); + wxUserInstrumentLogMapper.insertWxUserInstrumentLog(wxUserInstrumentLog); + } + } + } + + @Override + @Transactional + public void exchangeBinding(String serial) { + WxUserMember wxUserMember = SecurityUtils.getLoginUser().getWxUserMember(); + /* 1、获取序列号信息 */ + WxInstrumentSerial newInstrumentSerial = getInstrumentInfoBySerial(serial); + + /* 2、判断序列号是否被别人绑定了 */ + WxUserInstrument query = new WxUserInstrument(); + query.setUserId(wxUserMember.getId()); + query.setSerial(serial); + query.setStatus(Status.OK.getCode().longValue()); + query.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + Integer num = wxUserInstrumentMapper.selectUiByInstrumentId(query); + if (num != null && num > 0) { + logger.info("被别人绑定了,页面跳转联系客服"); + throw new ServiceReturnCodeException("当前序列码被别人绑定了,页面跳转联系客服", 203); + } + + /* 3、换绑 */ + // 3.1、查询原记录 + WxUserInstrument userInstrumentQuery = new WxUserInstrument(); + userInstrumentQuery.setUserId(wxUserMember.getId()); + userInstrumentQuery.setInstrumentId(newInstrumentSerial.getInstrumentId()); + userInstrumentQuery.setStatus(Status.OK.getCode().longValue()); + List wxUserInstrumentList = wxUserInstrumentMapper.selectListByUserIdAndInstrumentId(userInstrumentQuery); + if (wxUserInstrumentList == null || wxUserInstrumentList.size() != 1) { + logger.error("根据 用户id:{} 和 仪器id:{} 查询出来的数据为空或者数据量不止一条", wxUserMember.getId(), newInstrumentSerial.getInstrumentId()); + throw new ServiceException("换绑失败!"); + } + WxUserInstrument wxUserInstrument = wxUserInstrumentList.get(0); + + // 3.2、更新旧的序列号状态为已解绑 + // 当后管解除了用户和仪器的绑定,状态变为已解绑,无需再次修改序列号的绑定状态 + if (BindingStatusEnums.UN_BOUND.getCode() == wxUserInstrument.getBindingStatus()) { + WxInstrumentSerial oldUpdate = new WxInstrumentSerial(); + oldUpdate.setBindingStatus(BindingStatusEnums.UN_BOUND.getCode()); + oldUpdate.setSerial(wxUserInstrument.getSerial()); + wxInstrumentSerialMapper.updateBySerial(oldUpdate); + } + // 3.3、更新新的序列号状态为已绑定 + WxInstrumentSerial newUpdate = new WxInstrumentSerial(); + newUpdate.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + newUpdate.setSerial(serial); + wxInstrumentSerialMapper.updateBySerial(newUpdate); + // 3.4、更新用户仪器关联表 + WxUserInstrument updateEntity = new WxUserInstrument(); + updateEntity.setId(wxUserInstrument.getId()); + updateEntity.setSerial(serial); + if (wxUserInstrument.getGuarantee() != null && wxUserInstrument.getGuarantee() >= 0) { + Instant instant = LocalDateTime.now().plusYears(wxUserInstrument.getGuarantee()).atZone(ZoneId.systemDefault()).toInstant(); + updateEntity.setGuaranteeEndtime(Date.from(instant)); + } + // 当后管解除了用户和仪器的绑定,状态变为已解绑,此时就需改为绑定状态 + updateEntity.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + updateEntity.setUpdateBy(wxUserMember.getNickname()); + updateEntity.setUpdateTime(DateUtils.getNowDate()); + wxUserInstrumentMapper.updateWxUserInstrument(updateEntity); + // 3.5、用户仪器关联记录表 + // 当后管解除了用户和仪器的绑定,操作记录已记录,无需再次记录 + if (BindingStatusEnums.UN_BOUND.getCode() == wxUserInstrument.getBindingStatus()) { + WxUserInstrumentLog oldUserInstrumentLog = new WxUserInstrumentLog(); + oldUserInstrumentLog.setUserInstrumentId(wxUserInstrument.getId()); + oldUserInstrumentLog.setUserId(wxUserMember.getId()); + oldUserInstrumentLog.setSerial(wxUserInstrument.getSerial()); + oldUserInstrumentLog.setInstrumentId(wxUserInstrument.getInstrumentId()); + oldUserInstrumentLog.setInstrumentName(wxUserInstrument.getInstrumentName()); + oldUserInstrumentLog.setSerialImage(wxUserInstrument.getSerialImage()); + oldUserInstrumentLog.setGuarantee(wxUserInstrument.getGuarantee()); + oldUserInstrumentLog.setGuaranteeEndtime(wxUserInstrument.getGuaranteeEndtime()); + oldUserInstrumentLog.setBindingStatus(BindingStatusEnums.UN_BOUND.getCode()); + oldUserInstrumentLog.setStatus(Status.OK.getCode().longValue()); + oldUserInstrumentLog.setCreateBy(wxUserMember.getNickname()); + oldUserInstrumentLog.setCreateTime(DateUtils.getNowDate()); + wxUserInstrumentLogMapper.insertWxUserInstrumentLog(oldUserInstrumentLog); + } + WxUserInstrumentLog newUserInstrumentLog = new WxUserInstrumentLog(); + newUserInstrumentLog.setUserInstrumentId(wxUserInstrument.getId()); + newUserInstrumentLog.setUserId(wxUserMember.getId()); + newUserInstrumentLog.setSerial(serial); + newUserInstrumentLog.setInstrumentId(wxUserInstrument.getInstrumentId()); + newUserInstrumentLog.setInstrumentName(wxUserInstrument.getInstrumentName()); + newUserInstrumentLog.setSerialImage(wxUserInstrument.getSerialImage()); + newUserInstrumentLog.setGuarantee(wxUserInstrument.getGuarantee()); + newUserInstrumentLog.setGuaranteeEndtime(updateEntity.getGuaranteeEndtime()); + newUserInstrumentLog.setBindingStatus(BindingStatusEnums.BINDED.getCode()); + newUserInstrumentLog.setStatus(Status.OK.getCode().longValue()); + newUserInstrumentLog.setCreateBy(wxUserMember.getNickname()); + newUserInstrumentLog.setCreateTime(DateUtils.getNowDate()); + wxUserInstrumentLogMapper.insertWxUserInstrumentLog(newUserInstrumentLog); + } +}