package com.yeshi.fanli.service.impl.homemodule;
|
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import com.yeshi.fanli.entity.SystemEnum;
|
import com.yeshi.fanli.service.inter.user.UserInfoService;
|
import org.springframework.stereotype.Service;
|
|
import com.yeshi.fanli.dao.mybatis.homemodule.HomeNavbarUserMapper;
|
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar;
|
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbarUser;
|
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
|
import com.yeshi.fanli.exception.homemodule.HomeNavbarUserException;
|
import com.yeshi.fanli.exception.user.UserInfoExtraException;
|
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarService;
|
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarUserService;
|
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
|
import com.yeshi.fanli.util.StringUtil;
|
|
@Service
|
public class HomeNavbarUserServiceImpl implements HomeNavbarUserService {
|
|
@Resource
|
private HomeNavbarUserMapper homeNavbarUserMapper;
|
|
@Resource
|
private HomeNavbarService homeNavbarService;
|
|
@Resource
|
private UserInfoExtraService userInfoExtraService;
|
|
|
@Override
|
public List<HomeNavbar> listEffectiveNavbar(Long uid, String device, Integer sex,SystemEnum system) {
|
List<HomeNavbar> list = new ArrayList<HomeNavbar>();
|
if (sex == null || sex == 0) {
|
if (uid != null || !StringUtil.isNullOrEmpty(device)) {
|
if (uid != null) {
|
device = null;
|
}
|
// 自定义导航
|
List<HomeNavbarUser> listUserNavbar = homeNavbarUserMapper.listEffectiveNavbars(uid, device,system);
|
|
if (listUserNavbar != null && listUserNavbar.size() > 0) {
|
// 固定导航
|
List<HomeNavbar> listFixed = homeNavbarService.listQueryFixedNavbar(system);
|
if (listFixed != null && listFixed.size() > 0) {
|
list.addAll(listFixed);
|
}
|
|
// 遍历用户自定义
|
for (HomeNavbarUser uomeNavbarUser : listUserNavbar) {
|
HomeNavbar homeNavbar = uomeNavbarUser.getHomeNavbar();
|
if (homeNavbar == null) {
|
continue;
|
}
|
|
// 是否是固定项
|
boolean notExist = true;
|
|
if (listFixed != null && listFixed.size() > 0) {
|
long id = homeNavbar.getId();
|
for (HomeNavbar fixedbar : listFixed) {
|
long fixedId = fixedbar.getId();
|
if (fixedId == id) {
|
notExist = false;
|
listFixed.remove(fixedbar);
|
break;
|
}
|
}
|
}
|
|
if (notExist) {
|
list.add(homeNavbar);
|
}
|
}
|
}
|
}
|
|
if (list.size() == 0) {
|
List<HomeNavbar> listDefault = homeNavbarService.listQueryDefaultNavbar(sex, system);
|
if (listDefault != null) {
|
list.addAll(listDefault);
|
}
|
}
|
} else {
|
List<HomeNavbar> listDefault = homeNavbarService.listQueryDefaultNavbar(sex, system);
|
if (listDefault != null) {
|
list.addAll(listDefault);
|
}
|
}
|
return list;
|
}
|
|
@Override
|
public List<HomeNavbarUser> listUserNavbar(Long uid, String device,SystemEnum system) throws HomeNavbarUserException {
|
if (uid == null && StringUtil.isNullOrEmpty(device)) {
|
throw new HomeNavbarUserException(1, "参数不正确");
|
}
|
|
if (uid != null && !StringUtil.isNullOrEmpty(device)) {
|
device = null;
|
}
|
|
return homeNavbarUserMapper.listEffectiveNavbars(uid, device,system);
|
}
|
|
@Override
|
public void addNavbarUser(Long uid, String device, List<Long> list) throws HomeNavbarUserException {
|
if (uid == null && StringUtil.isNullOrEmpty(device)) {
|
throw new HomeNavbarUserException(1, "参数不正确");
|
}
|
|
if (list == null || list.size() < 6) {
|
throw new HomeNavbarUserException(1, "至少保留6个分类");
|
}
|
|
if (list.size() > 16) {
|
throw new HomeNavbarUserException(1, "最多显示16个分类");
|
}
|
|
// 登录之后 以uid为准
|
if (uid != null && !StringUtil.isNullOrEmpty(device)) {
|
device = null;
|
}
|
|
// 设置已同步
|
UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
|
if (userInfoExtra != null && !userInfoExtra.getSynchNavbar()) {
|
try {
|
UserInfoExtra updateExtra = new UserInfoExtra();
|
updateExtra.setSynchNavbar(true);
|
updateExtra.setId(userInfoExtra.getId());
|
userInfoExtraService.saveUserInfoExtra(updateExtra);
|
} catch (UserInfoExtraException e) {
|
e.printStackTrace();
|
}
|
}
|
|
List<HomeNavbarUser> listInsert = new ArrayList<HomeNavbarUser>();
|
List<HomeNavbarUser> listUpdate = new ArrayList<HomeNavbarUser>();
|
|
// 之前已经定义的导航数据
|
List<HomeNavbarUser> listMyNavbars = homeNavbarUserMapper.listMyNavbars(uid, device);
|
|
for (int i = 0; i < list.size(); i++) {
|
HomeNavbarUser navbarUser = null;
|
if (list.get(i) == null) {
|
i--;
|
continue;
|
}
|
long homeId = list.get(i);
|
|
if (listMyNavbars != null && listMyNavbars.size() > 0) {
|
for (int j = 0; j < listMyNavbars.size(); j++) {
|
HomeNavbarUser homeNavbarUser = listMyNavbars.get(j);
|
HomeNavbar homeNavbar = homeNavbarUser.getHomeNavbar();
|
|
if (homeNavbar == null) {
|
listMyNavbars.remove(homeNavbarUser);
|
j--;
|
continue;
|
}
|
|
long id = homeNavbar.getId();
|
if (homeId == id) {
|
navbarUser = new HomeNavbarUser();
|
navbarUser.setId(homeNavbarUser.getId());
|
navbarUser.setOrder(homeNavbarUser.getOrder());
|
listMyNavbars.remove(homeNavbarUser);
|
break;
|
}
|
}
|
}
|
|
if (navbarUser != null) {
|
Integer order = navbarUser.getOrder();
|
if (order != null && i + 1 == order.intValue()) {
|
continue;
|
}
|
navbarUser.setOrder(i + 1);
|
listUpdate.add(navbarUser);
|
} else {
|
navbarUser = new HomeNavbarUser();
|
navbarUser.setOrder(i + 1);
|
navbarUser.setUid(uid);
|
navbarUser.setDevice(device);
|
navbarUser.setHomeNavbar(new HomeNavbar(homeId));
|
navbarUser.setCreateTime(new Date());
|
listInsert.add(navbarUser);
|
}
|
}
|
|
// 更新
|
if (listUpdate.size() > 0) {
|
homeNavbarUserMapper.updateSelectiveBatch(listUpdate);
|
}
|
|
// 新增
|
if (listInsert.size() > 0) {
|
homeNavbarUserMapper.insertBatch(listInsert);
|
}
|
|
// 删除
|
if (listMyNavbars != null && listMyNavbars.size() > 0) {
|
List<Long> listId = new ArrayList<Long>();
|
for (HomeNavbarUser homeNavbarUser : listMyNavbars) {
|
listId.add(homeNavbarUser.getId());
|
}
|
homeNavbarUserMapper.deleteByPrimaryKeyBatch(listId);
|
}
|
}
|
|
@Override
|
public List<HomeNavbar> restoreSystemDefault(Long uid, String device,SystemEnum system) throws HomeNavbarUserException {
|
if (uid == null && StringUtil.isNullOrEmpty(device)) {
|
throw new HomeNavbarUserException(1, "参数不正确");
|
}
|
|
// 删除用户自定义导航
|
if (uid != null) {
|
homeNavbarUserMapper.deleteByUid(uid);
|
} else {
|
homeNavbarUserMapper.deleteByDevice(device);
|
}
|
// 返回有效的
|
return homeNavbarService.listQueryEffectiveNavbar(system);
|
}
|
|
@Override
|
public void synchroDeviceToUser(Long uid, String device) {
|
UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
|
if (userInfoExtra == null) {
|
userInfoExtra = new UserInfoExtra();
|
} else if (userInfoExtra.getSynchNavbar()) {
|
return;
|
}
|
|
List<HomeNavbarUser> listDevice = homeNavbarUserMapper.listMyNavbars(null, device);
|
if (listDevice == null || listDevice.size() == 0) {
|
return;
|
}
|
|
// 同步设备定义导航
|
for (HomeNavbarUser homeNavbarUser : listDevice) {
|
homeNavbarUser.setId(null);
|
homeNavbarUser.setDevice(null);
|
homeNavbarUser.setUid(uid);
|
homeNavbarUser.setCreateTime(new Date());
|
}
|
homeNavbarUserMapper.insertBatch(listDevice);
|
|
// 已同步
|
try {
|
UserInfoExtra updateExtra = new UserInfoExtra();
|
updateExtra.setSynchNavbar(true);
|
updateExtra.setId(userInfoExtra.getId());
|
userInfoExtraService.saveUserInfoExtra(updateExtra);
|
} catch (UserInfoExtraException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|