/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.springblade.modules.discuss.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.Nullable; import org.springblade.common.constant.CommonConstant; import org.springblade.common.utils.SpringUtils; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.SpringUtil; import org.springblade.modules.discuss.entity.PublicDiscussEntity; import org.springblade.modules.discuss.entity.TopicsEntity; import org.springblade.modules.discuss.entity.UserTopicsEntity; import org.springblade.modules.discuss.mapper.UserTopicsMapper; import org.springblade.modules.discuss.service.IPublicDiscussService; import org.springblade.modules.discuss.service.ITopicsService; import org.springblade.modules.discuss.service.IUserTopicsService; import org.springblade.modules.discuss.vo.TopicsVO; import org.springblade.modules.discuss.vo.UserTopicsVO; import org.springblade.modules.district.entity.DistrictEntity; import org.springblade.modules.district.service.IDistrictService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * 用户议题报表 服务实现类 * * @author BladeX * @since 2023-11-22 */ @Service public class UserTopicsServiceImpl extends ServiceImpl implements IUserTopicsService { @Resource private ITopicsService topicsService; @Override public IPage selectUserTopicsPage(IPage page, UserTopicsVO userTopics) { if (StringUtils.isNotBlank(userTopics.getDistrictId())) { List longs = JSON.parseArray(userTopics.getDistrictId()).toJavaList(String.class); IDistrictService bean = SpringUtils.getBean(IDistrictService.class); List list = bean.list(Wrappers.lambdaQuery().in(DistrictEntity::getId, longs)); List collect = list.stream().map(item -> item.getAoiCode() ).collect(Collectors.toList()); if (collect != null) { userTopics.setAoiCodeList(collect); } } return page.setRecords(baseMapper.selectUserTopicsPage(page, userTopics)); } @Override @Transactional(rollbackFor = Exception.class) public Boolean batchSave(List topics) throws Exception { // 判断是否一户一票 还是一人一票 IPublicDiscussService bean = SpringUtil.getBean(IPublicDiscussService.class); PublicDiscussEntity one = bean.getOne(Wrappers.lambdaQuery().eq(PublicDiscussEntity::getArticleId, topics.get(0).getArticleId())); // 一户一票 if (one.getVoteRestrictions().equals(CommonConstant.NUMBER_ONE)) { long count = count(Wrappers.lambdaQuery().eq(UserTopicsEntity::getHouseCode, topics.get(0).getHouseCode())); if (count > 1) { throw new Exception("您的房屋已投票,不能重复投票!"); } } else { // long count = count(Wrappers.lambdaQuery().eq(UserTopicsEntity::getUserId, AuthUtil.getUserId())); if (count > 1) { throw new Exception("您的已投票,不能重复投票!"); } } Boolean userTopics = getaBoolean(topics); if (userTopics != null) return userTopics; return false; } @Nullable private Boolean getaBoolean(List topics) { List objects = new ArrayList<>(); for (TopicsVO topic : topics) { UserTopicsEntity userTopicsEntity = new UserTopicsEntity(); userTopicsEntity.setUserId(AuthUtil.getUserId()); userTopicsEntity.setSelected(topic.getSelected()); userTopicsEntity.setTopicsId(topic.getId()); userTopicsEntity.setPublicDiscussId(topic.getPublicDiscussId()); objects.add(userTopicsEntity); // 单选 if (topic.getOptionRange().equals(0)) { if (StringUtils.isBlank(topic.getSelected())) { break; } UserTopicsEntity userTopics = new UserTopicsEntity(); userTopics.setSelected(topic.getSelected()); userTopics.setUserId(AuthUtil.getUserId()); userTopics.setPublicDiscussId(topic.getPublicDiscussId()); userTopics.setTopicsId(Integer.valueOf(topic.getSelected())); userTopics.setArticleId(topic.getArticleId()); userTopics.setHouseCode(topic.getHouseCode()); userTopics.setSignaturePath(topic.getSignaturePath()); UpdateWrapper objectUpdateWrapper = new UpdateWrapper<>(); objectUpdateWrapper.setSql("number = number + 1"); objectUpdateWrapper.eq("id", topic.getSelected()); topicsService.update(null, objectUpdateWrapper); return save(userTopics); } else { // 多选 if (StringUtils.isBlank(topic.getSelected())) { break; } JSONArray objects1 = JSON.parseArray(topic.getSelected()); List objectsTwo = new ArrayList<>(); for (Object o : objects1) { UserTopicsEntity userTopics = new UserTopicsEntity(); userTopics.setSelected(topic.getSelected()); userTopics.setUserId(AuthUtil.getUserId()); userTopics.setPublicDiscussId(topic.getPublicDiscussId()); userTopics.setArticleId(topic.getArticleId()); userTopics.setHouseCode(topic.getHouseCode()); userTopics.setTopicsId((Integer) o); userTopics.setSignaturePath(topic.getSignaturePath()); objectsTwo.add(userTopics); UpdateWrapper objectUpdateWrapper = new UpdateWrapper<>(); objectUpdateWrapper.setSql("number = number + 1"); objectUpdateWrapper.eq("id", o); topicsService.update(null, objectUpdateWrapper); } return saveBatch(objectsTwo); } } return null; } @Override public Integer getCount(Integer id) { List list = list(Wrappers.lambdaQuery() .eq(UserTopicsEntity::getPublicDiscussId, id) .groupBy(UserTopicsEntity::getUserId)); return list.size(); } }