Commit 4ea322a3 by tyfanchz

Merge remote-tracking branch 'origin/dev' into dev

2 parents 29f231f1 f083bc7e
......@@ -9,12 +9,16 @@ package com.glsx.smart.constant;
*/
public class Constants {
public final static String DEFAULT_PASSWORD = "123456";
public final static String DEFAULT_DEVICE = "万能遥控器";
public final static String SCENE_CONDITION_STATUS = "scene_condition_status";
public final static String SCENE_CONTENT_DEVICE = "scene_content_device";
public final static long REDIS_CACHE_TIME = 3;
public final static Byte USER_CHANNEL_TUYA = 1; //用户渠道涂鸦
public final static Byte USER_CHANNEL_AQARA = 2; //用户渠道绿米
}
......@@ -5,14 +5,16 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.smart.dao.entity.SmartUserDevice;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.pojo.response.RemoteDeviceResp;
import com.glsx.smart.service.SmartProductService;
import com.glsx.smart.service.SmartProductTypeService;
import com.glsx.smart.service.SmartUserDeviceService;
import com.glsx.smart.service.SmartUserRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
......@@ -34,16 +36,13 @@ public class RemoteProductController {
private String remoteUrl;
@Autowired
private SmartProductService productService;
@Autowired
private SmartProductTypeService productTypeService;
@Autowired
private SmartUserRelationService userRelationService;
@Autowired
private SmartProductTypeService smartProductTypeService;
private SmartUserDeviceService userDeviceService;
/**
* 用户获取远程产品列表数据
......@@ -54,14 +53,13 @@ public class RemoteProductController {
@RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
public List<RemoteDeviceResp> getList(@RequestBody SmartUserRelation request) {
log.info("用户参数:{}", request);
// request.setUserId(91712228L);
SmartUserRelation user = userRelationService.getSingle(request);
if(user == null){
return null;
}
Byte status = request.getStatus() != null ? request.getStatus() : -1;
return this.getRemoteDevice(user.getThirdId(), status);
// return this.getRemoteDevice(user.getThirdId(), status);
return this.getRemoteDeviceV2(user.getId(), status);
}
private List<RemoteDeviceResp> getRemoteDevice(String thirdId, Byte status) {
......@@ -89,6 +87,24 @@ public class RemoteProductController {
}
/**
* 获取用户家设备
* @description 车家智能V1.0.1 用户远程设备获取通过读取数据库获取
* @param userId
* @param status
* @return
*/
private List<RemoteDeviceResp> getRemoteDeviceV2(Long userId, Byte status) {
List<RemoteDeviceResp> deviceRespList = new ArrayList<>();
SmartUserDevice record = new SmartUserDevice();
record.setUserId(userId);
List<SmartUserDevice> list = userDeviceService.getList(record);
if(CollectionUtils.isNotEmpty(list)){
return deviceRespList;
}
return productTypeService.remoteDeviceHandlerV2(list, status);
}
/**
* 查询红外设备下的遥控器
*
* @return 结果
......@@ -97,7 +113,7 @@ public class RemoteProductController {
@RequestMapping(value = "/findRemotes", method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(value = "查询红外设备下的遥控器")
public String findRemotes(@RequestParam String infrared_id) {
return smartProductTypeService.findRemotesById(infrared_id);
return productTypeService.findRemotesById(infrared_id);
}
}
......@@ -126,7 +126,7 @@ public class UserKafkaController {
SmartUserRelation record = new SmartUserRelation();
record.setUserId(userId);
record.setPhone(phone);
record.setChannel((byte)1);
// record.setChannel((byte)1);
return userRelationService.getSingle(record);
}
......
package com.glsx.smart.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.glsx.plat.redis.utils.RedisUtils;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.dao.entity.SmartUserChannel;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.service.SmartUserRelationService;
import lombok.extern.slf4j.Slf4j;
......@@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
......@@ -36,6 +37,12 @@ public class UserRelationController {
@Value("${tuya_user_sync}")
private String syncUser;
@Value("${device_remote_url}")
private String aqaraSyncUserUrl;
@Autowired
private RedisUtils redisUtils;
@Autowired
private SmartUserRelationService userRelationService;
......@@ -63,60 +70,61 @@ public class UserRelationController {
@RequestMapping(value = "/login", method = {RequestMethod.POST})
public R login(@RequestBody SmartUserRelation userRelation){
log.info("/user/login保存数据参数:{}", JSON.toJSONString(userRelation));
String key = userRelation.getPhone();
if(redisUtils.hasKey(key)){
log.info("用户手机号 {} 重复请求", key);
return R.ok();
}
redisUtils.setex(key, key, Constants.REDIS_CACHE_TIME);
//获取用户数据
SmartUserRelation request = new SmartUserRelation();
request.setPhone(userRelation.getPhone());
request.setChannel(userRelation.getChannel()); //涂鸦
SmartUserRelation user = userRelationService.getSingle(request);
//用户数据封装
request.setUserId(userRelation.getUserId());
String password = MD5Utils.md5Hex(request.getPhone(), "utf-8");
request.setPassword(password);
request.setName(userRelation.getName());
if(user != null){
String tuYaId = user.getThirdId();
if(StringUtils.isEmpty(tuYaId)){
if(request.getChannel() == 1){
tuYaId = this.syncUser(user.getPhone(), user.getPassword());
request.setThirdId(tuYaId);
request.setId(user.getId());
}
}
userRelationService.updateByPrimaryKeySelective(request);
return R.ok().data(tuYaId);
return R.ok().data(this.updateChannelUser(user));
}
// String password = MD5.create().digestHex(Constants.DEFAULT_PASSWORD);
String password = MD5Utils.md5Hex(request.getPhone(), "utf-8");
request.setPassword(password);
request.setCreateTime(new Date());
if(request.getChannel() == 1){ //渠道为涂鸦
String tuYaId = this.syncUser(request.getPhone(), password);
request.setThirdId(tuYaId);
Long id = userRelationService.insertSelective(request);
if(id != null && id > 0){
return R.ok().data(tuYaId);
}
Long userId = userRelationService.insertSelective(request);
if(userId > 0L){
userRelationService.syncAqaraUser(request.getPhone(), userId);
String tuYaId = userRelationService.syncTuYaUser(request.getPhone(), password, userId);
return R.ok().data(tuYaId);
}
return R.error();
}
private String syncUser(String mobile, String password){
Map<String, String> object = new HashMap<>();
object.put("country_code", "86");
object.put("username", mobile);
object.put("username_type", "1");
object.put("password", password);
try {
StringBuffer syncUserUrl = new StringBuffer(remoteUrl);
syncUserUrl.append(syncUser);
log.info("请求注册涂鸦平台接口地址:{},参数:{}", syncUserUrl.toString(), JSON.toJSONString(object));
String result = HttpUtils.post(syncUserUrl.toString(), JSON.toJSONString(object));
log.info("注册涂鸦平台返回结果:{}", result);
if (!StringUtils.isEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code").equals(200)) {
return json.getJSONObject("message").getString("uid");
}
/**
* 用户更新操作
* @param user
* @return
*/
private String updateChannelUser(SmartUserRelation user){
List<SmartUserChannel> list = user.getChannelList();
String tuYaId;
if(list.isEmpty()){
userRelationService.syncAqaraUser(user.getPhone(), user.getId());
tuYaId = userRelationService
.syncTuYaUser(user.getPhone(), user.getPassword(), user.getId());
return tuYaId;
} else{
Map<Byte, String> map = new HashMap<>();
list.forEach(index -> map.put(index.getChannel(), index.getThirdId()));
tuYaId = map.get(Constants.USER_CHANNEL_TUYA);
if(tuYaId == null){
tuYaId = userRelationService
.syncTuYaUser(user.getPhone(), user.getPassword(), user.getId());
}
if(map.get(Constants.USER_CHANNEL_AQARA) == null){
userRelationService.syncAqaraUser(user.getPhone(), user.getId());
}
} catch (Exception e) {
log.error("同步涂鸦平台失败:{}", e);
}
return null;
return tuYaId;
}
}
......@@ -8,6 +8,7 @@ import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* <p>Description:
......@@ -80,4 +81,9 @@ public class SmartUserRelation implements Serializable {
* 创建时间
*/
private Date createTime;
/**
* 用户渠道数据
*/
private List<SmartUserChannel> channelList;
}
......@@ -2,6 +2,9 @@ package com.glsx.smart.dao.mapper;
import com.glsx.smart.dao.entity.SmartUserChannel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* <p>Description:
......@@ -13,4 +16,6 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SmartUserChannelMapper extends tk.mybatis.mapper.common.Mapper<SmartUserChannel> {
List<SmartUserChannel> selectByUserId(@Param("userId") Long userId);
}
......@@ -3,6 +3,7 @@ package com.glsx.smart.service;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.glsx.smart.dao.entity.SmartProductType;
import com.glsx.smart.dao.entity.SmartUserDevice;
import com.glsx.smart.pojo.response.RemoteDeviceResp;
import java.util.List;
......@@ -34,4 +35,6 @@ public interface SmartProductTypeService {
List<RemoteDeviceResp> remoteDeviceHandler(JSONArray array, Byte status);
List<RemoteDeviceResp> remoteDeviceHandlerV2(List<SmartUserDevice> deviceList, Byte status);
}
......@@ -29,4 +29,8 @@ public interface SmartUserRelationService {
int batchInsert(List<SmartUserRelation> list);
String syncTuYaUser(String mobile, String password, Long userId);
String syncAqaraUser(String mobile, Long userId);
}
......@@ -7,6 +7,7 @@ import com.github.pagehelper.PageInfo;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.dao.entity.SmartProduct;
import com.glsx.smart.dao.entity.SmartProductType;
import com.glsx.smart.dao.entity.SmartUserDevice;
import com.glsx.smart.dao.mapper.SmartProductMapper;
import com.glsx.smart.dao.mapper.SmartProductTypeMapper;
import com.glsx.smart.exception.BusinessException;
......@@ -179,24 +180,7 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
if(CollectionUtils.isEmpty(productList)){
return deviceRespList;
}
//获取设备类型列表
List<SmartProductType> list;
if(status == 1){
list = productTypeMapper.getAllConditionList();
} else if(status == 2){
list = productTypeMapper.getAllContentList();
} else {
//获取平台产品类型id列表
List<Long> typeIdList = new ArrayList<>(productList.size());
for (SmartProduct product : productList) {
typeIdList.add(product.getTypeId());
}
list = productTypeMapper.getByIdList(typeIdList);
}
Map<Long, SmartProductType> typeMap = new HashMap<>(list.size());
list.stream().forEach(index ->{
typeMap.put(index.getId(), index);
});
Map<Long, SmartProductType> typeMap = this.getProductTypeMap(status, productList);
//远程设备数据封装
for (int i = 0; i < len; i++) {
JSONObject object = array.getJSONObject(i);
......@@ -210,7 +194,7 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
RemoteDeviceResp resp = new RemoteDeviceResp();
resp.setProductId(productId);
resp.setUuid(object.getString("id"));
resp.setProductName(object.get("name")+"");
resp.setProductName(object.getString("name"));
resp.setIconUrl(product.getIconUrl());
boolean flag = productType.getTypeName().contains(Constants.DEFAULT_DEVICE) ? true : false;
resp.setFlag(flag);
......@@ -220,6 +204,71 @@ public class SmartProductTypeServiceImpl extends BaseService implements SmartPro
return deviceRespList.stream().sorted(Comparator.comparing(RemoteDeviceResp::getProductId).reversed()).collect(Collectors.toList());
}
/**
* 获取用户家设备
*
* @param deviceList
* @param status 1触发条件 2执行内容 -1 全部
* @return
*/
@Override
public List<RemoteDeviceResp> remoteDeviceHandlerV2(List<SmartUserDevice> deviceList, Byte status) {
List<RemoteDeviceResp> deviceRespList = new ArrayList<>();
//获取平台产品id列表
List<String> prodIdList = new ArrayList<>(deviceList.size());
deviceList.forEach(index -> prodIdList.add(index.getPlatformProductId()));
List<SmartProduct> productList = productMapper.getByPlatProductIdList(prodIdList);
if(CollectionUtils.isEmpty(productList)){
return deviceRespList;
}
Map<Long, SmartProductType> typeMap = this.getProductTypeMap(status, productList);
//用户设备数据封装
deviceList.forEach(index ->{
SmartProduct product = this.compareProductId(productList, index.getPlatformProductId());
if(product != null){
SmartProductType productType = typeMap.get(product.getTypeId());
if(productType != null){
RemoteDeviceResp resp = new RemoteDeviceResp();
resp.setProductId(index.getPlatformProductId());
resp.setUuid(index.getDeviceId());
resp.setProductName(index.getDeviceName());
resp.setIconUrl(product.getIconUrl());
//判断设备是否为万能遥控器
boolean flag = productType.getTypeName().contains(Constants.DEFAULT_DEVICE) ? true : false;
resp.setFlag(flag);
deviceRespList.add(resp);
}
}
});
return deviceRespList.stream().sorted(Comparator.comparing(RemoteDeviceResp::getProductId).reversed()).collect(Collectors.toList());
}
/**
* 获取设备类型map
*
* @param status
* @param productList
* @return
*/
private Map<Long, SmartProductType> getProductTypeMap(Byte status, List<SmartProduct> productList){
//获取设备类型列表
List<SmartProductType> list;
if(status == 1){
list = productTypeMapper.getAllConditionList();
} else if(status == 2){
list = productTypeMapper.getAllContentList();
} else {
//获取平台产品类型id列表
List<Long> typeIdList = new ArrayList<>(productList.size());
for (SmartProduct product : productList) {
typeIdList.add(product.getTypeId());
}
list = productTypeMapper.getByIdList(typeIdList);
}
Map<Long, SmartProductType> typeMap = new HashMap<>(list.size());
list.stream().forEach(index ->typeMap.put(index.getId(), index));
return typeMap;
}
/**
* 比较获取产品
......
package com.glsx.smart.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.dao.entity.SmartUserChannel;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.dao.mapper.SmartUserChannelMapper;
import com.glsx.smart.dao.mapper.SmartUserRelationMapper;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
import com.glsx.smart.service.SmartUserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>Description:
......@@ -22,6 +32,19 @@ import java.util.List;
@Slf4j
@Service
public class SmartUserRelationServiceImpl implements SmartUserRelationService {
@Value("${device_remote_url}")
private String remoteUrl;
@Value("${tuya_user_sync}")
private String syncUser;
@Value("${device_remote_url}")
private String aqaraSyncUserUrl;
@Resource
private SmartUserChannelMapper userChannelMapper;
@Resource
private SmartUserRelationMapper relationMapper;
......@@ -46,23 +69,17 @@ public class SmartUserRelationServiceImpl implements SmartUserRelationService {
}
@Override
@Transactional
public Long insertSelective(SmartUserRelation record) {
SmartUserRelation request = new SmartUserRelation();
request.setPhone(record.getPhone());
request.setChannel(record.getChannel()); //涂鸦
SmartUserRelation user = relationMapper.getSingle(request);
if(user != null && user.getId() > 0L){
record.setId(user.getId());
relationMapper.updateByPrimaryKeySelective(record);
} else{
relationMapper.insertSelective(record);
}
return record.getId();
int i = relationMapper.insertSelective(record);
return i > 0 ? record.getId() : 0L;
}
@Override
public SmartUserRelation getSingle(SmartUserRelation record) {
SmartUserRelation userRelation = relationMapper.getSingle(record);
if(userRelation != null){
userRelation.setChannelList(userChannelMapper.selectByUserId(userRelation.getId()));
}
return relationMapper.getSingle(record);
}
......@@ -86,4 +103,92 @@ public class SmartUserRelationServiceImpl implements SmartUserRelationService {
return relationMapper.batchInsert(list);
}
@Override
public String syncTuYaUser(String mobile, String password, Long userId) {
String thirdId = this.syncTuYaUser(mobile, password);
if(thirdId != null){
this.saveUserChannel(userId, thirdId,Constants.USER_CHANNEL_TUYA);
}
return thirdId;
}
@Override
public String syncAqaraUser(String mobile, Long userId) {
String thirdId = this.syncAqaraUser(mobile);
if(thirdId != null){
//保存绿米渠道用户数据
this.saveUserChannel(userId, thirdId,Constants.USER_CHANNEL_AQARA);
}
return thirdId;
}
/**
* 保存用户渠道数据
* @param userId
* @param thirdId
* @param channel
*/
private void saveUserChannel(Long userId, String thirdId, Byte channel){
SmartUserChannel userChannel = new SmartUserChannel();
userChannel.setUserId(userId);
userChannel.setCreateTime(new Date());
userChannel.setThirdId(thirdId);
userChannel.setChannel(channel);
userChannelMapper.insertSelective(userChannel);
}
/**
* 同步用户到涂鸦
* @param mobile
* @param password
* @return
*/
private String syncTuYaUser(String mobile, String password){
Map<String, String> object = new HashMap<>();
object.put("country_code", "86");
object.put("username", mobile);
object.put("username_type", "1");
object.put("password", password);
try {
StringBuffer syncUserUrl = new StringBuffer(remoteUrl);
syncUserUrl.append(syncUser);
log.info("请求注册涂鸦平台接口地址:{},参数:{}", syncUserUrl.toString(), JSON.toJSONString(object));
String result = HttpUtils.post(syncUserUrl.toString(), JSON.toJSONString(object));
log.info("注册涂鸦平台返回结果:{}", result);
if (!StringUtils.isEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code").equals(200)) {
return json.getJSONObject("message").getString("uid");
}
}
} catch (Exception e) {
log.error("同步涂鸦平台失败:{}", e);
}
return null;
}
/**
* 同步用户到绿米
* @param mobile
* @return
*/
private String syncAqaraUser(String mobile){
Map<String, String> object = new HashMap<>();
object.put("account", mobile);
object.put("password", mobile);
try {
log.info("请求绿米平台用户接口地址:{},参数:{}", aqaraSyncUserUrl, JSON.toJSONString(object));
String result = HttpUtils.post(aqaraSyncUserUrl, JSON.toJSONString(object));
log.info("请求绿米平台用户返回结果:{}", result);
if (!StringUtils.isEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code").equals(200)) {
return json.getJSONObject("result").getString("openId");
}
}
} catch (Exception e) {
log.error("请求绿米平台用户失败:{}", e);
}
return null;
}
}
......@@ -15,4 +15,9 @@
<!--@mbg.generated generated on Fri Apr 23 15:24:29 CST 2021.-->
id, user_id, channel, third_id, create_time
</sql>
<select id="selectByUserId" parameterType="java.lang.Long" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/> FROM smart_user_channel
WHERE 1 = 1 AND user_id = #{userId,jdbcType=BIGINT}
</select>
</mapper>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!