/* * 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.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; 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.redis.cache.BladeRedis; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.Func; 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.excel.UserTopicsExcel; import org.springblade.modules.discuss.excel.holdExcel; 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.springblade.modules.house.entity.HouseholdEntity; import org.springblade.modules.house.excel.HouseHoldExcel; import org.springblade.modules.house.service.IHouseholdService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.io.OutputStream; 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; public static final String SMS_VALIDATE_PHONE = "sms:validate:code:"; @Autowired private BladeRedis redisTemplate; @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); } } IHouseholdService householdService = SpringUtils.getBean(IHouseholdService.class); HouseholdEntity householdEntity = householdService.getOne(Wrappers.lambdaQuery() .eq(HouseholdEntity::getHouseCode, userTopics.getHouseCode()) .eq(HouseholdEntity::getAssociatedUserId, AuthUtil.getUserId()) .eq(HouseholdEntity::getIsDeleted, 0) .last("limit 1")); if (householdEntity != null) { userTopics.setHouseholdId(householdEntity.getId()); } List userTopicsVOS = baseMapper.selectUserTopicsPage(page, userTopics); return page.setRecords(userTopicsVOS); } @Override @Transactional(rollbackFor = Exception.class) public String batchSave(TopicsVO topics) throws Exception { // if (StringUtils.isNotBlank(topics.getPhone())) { // Object validateCode = redisTemplate.get(SMS_VALIDATE_PHONE + topics.getPhone()); // if (validateCode == null) { // return "验证码已过期"; // } // if (!validateCode.toString().equals(topics.getCode())) { // return "验证码错误"; // } // //删除验证码 // redisTemplate.del(SMS_VALIDATE_PHONE + topics.getPhone()); // } // 判断是否一户一票 还是一人一票 List topicsList = topics.getChildren(); IHouseholdService householdService = SpringUtils.getBean(IHouseholdService.class); HouseholdEntity householdEntity = householdService.getOne(Wrappers.lambdaQuery() .eq(HouseholdEntity::getHouseCode, topicsList.get(0).getHouseCode()) .eq(HouseholdEntity::getAssociatedUserId, AuthUtil.getUserId()) .eq(HouseholdEntity::getIsDeleted, 0) .last("limit 1")); IPublicDiscussService bean = SpringUtil.getBean(IPublicDiscussService.class); PublicDiscussEntity one = bean.getOne(Wrappers.lambdaQuery().eq(PublicDiscussEntity::getArticleId, topicsList.get(0).getArticleId())); // 一户一票 if (one.getVoteRestrictions().equals(CommonConstant.NUMBER_ONE)) { long count = count(Wrappers.lambdaQuery() .eq(UserTopicsEntity::getHouseCode, topicsList.get(0).getHouseCode()) .eq(UserTopicsEntity::getArticleId, topicsList.get(0).getArticleId())); if (count > 0) { return "您的房屋已投票,不能重复投票!"; } } else { // long count = count(Wrappers.lambdaQuery() .eq(UserTopicsEntity::getHouseholdId, householdEntity.getId()) .eq(UserTopicsEntity::getArticleId, topicsList.get(0).getArticleId())); if (count > 0) { return "您已投票,不能重复投票!"; } } for (TopicsVO topic : topicsList) { if (topic.getMandatoryFlag().equals(2)) { if (topic.getOptionNumberMin() > 1) { JSONArray objects1 = JSON.parseArray(topic.getSelected()); if (objects1.size() < topic.getOptionNumberMin()) { return topic.getDiscussContent() + ":议题最少需要选择" + topic.getOptionNumberMin() + "项"; } } if (topic.getOptionNumber() > 1) { JSONArray objects1 = JSON.parseArray(topic.getSelected()); if (objects1.size() > topic.getOptionNumber()) { return topic.getDiscussContent() + ":议题最多需要选择" + topic.getOptionNumber() + "项"; } } } } Boolean userTopics = getaBoolean(topicsList, householdEntity); if (userTopics) return "200"; return "操作失败!"; } @Nullable private Boolean getaBoolean(List topics, HouseholdEntity householdEntity) { List objects = new ArrayList<>(); Boolean result = false; for (TopicsVO topic : topics) { UserTopicsEntity userTopicsEntity = new UserTopicsEntity(); userTopicsEntity.setHouseholdId(householdEntity.getId()); 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.setHouseholdId(householdEntity.getId()); 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); result = 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.setHouseholdId(householdEntity.getId()); 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); } result = saveBatch(objectsTwo); } } return result; } @Override public Integer getCount(Integer id) { List list = list(Wrappers.lambdaQuery() .eq(UserTopicsEntity::getPublicDiscussId, id) .groupBy(UserTopicsEntity::getHouseholdId)); return list.size(); } @Override public List exportUser(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); } } List userTopicsExcels = baseMapper.exportUser(userTopics); return userTopicsExcels; } @Override public Boolean saveUserTopicsEntity(UserTopicsEntity userTopics) { IHouseholdService householdService = SpringUtils.getBean(IHouseholdService.class); HouseholdEntity householdEntity = householdService.getOne(Wrappers.lambdaQuery() .eq(HouseholdEntity::getHouseCode, userTopics.getHouseCode()) .eq(HouseholdEntity::getAssociatedUserId, AuthUtil.getUserId()) .eq(HouseholdEntity::getIsDeleted, 0) .last("limit 1")); if (householdEntity != null) { userTopics.setHouseholdId(householdEntity.getId()); } return save(userTopics); } @Override public void handleExcel(OutputStream out, UserTopicsVO userTopics) { // 设置excel工作簿 ExcelWriter excelWriter = EasyExcel.write(out) .build(); // List userList = new ArrayList<>(); // for (int i = 0; i < 10; i++) { // HouseHoldExcel houseHoldExcel = new HouseHoldExcel(); // houseHoldExcel.setHouseCode(i+"1"); // } //设置自动换行 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); contentWriteCellStyle.setWrapped(true); List proposal = getProposal(userTopics); ExcelWriterSheetBuilder userExcelSheet = new ExcelWriterSheetBuilder(); userExcelSheet.registerWriteHandler(new HorizontalCellStyleStrategy(null,contentWriteCellStyle)); userExcelSheet.sheetName("小区投票人员"); userExcelSheet.sheetNo(0); userExcelSheet.head(holdExcel.class); excelWriter.write(proposal, userExcelSheet.build()); // List classInfoList = new ArrayList<>(); // for (int i = 0; i < 10; i++) { // HouseHoldExcel houseHoldExcel = new HouseHoldExcel(); // houseHoldExcel.setHouseCode(i+"1"); // } List publicopinion = getPublicopinion(userTopics); ExcelWriterSheetBuilder classExcelSheet = new ExcelWriterSheetBuilder(); classExcelSheet.registerWriteHandler(new HorizontalCellStyleStrategy(null,contentWriteCellStyle)); classExcelSheet.sheetName("投票记录"); classExcelSheet.sheetNo(1); classExcelSheet.head(UserTopicsExcel.class); excelWriter.write(publicopinion, classExcelSheet.build()); excelWriter.finish(); // ExcelWriter excelWriter = EasyExcelFactory.write(out).build(); // //设置内容样式 // WriteCellStyle contentStyle = new WriteCellStyle(); // contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//居中 // contentStyle.setWrapped(true);//自动换行 // //设置头部样式 // WriteCellStyle headerStyle = new WriteCellStyle(); // headerStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); // //设置策略 // HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headerStyle, contentStyle); // WriteSheet proposalSheet = EasyExcelFactory.writerSheet(0, "委员提案").head(HouseHoldExcel.class).registerWriteHandler(horizontalCellStyleStrategy).build(); // WriteSheet publicopinionSheet = EasyExcelFactory.writerSheet(1, "社情民意").head(HouseHoldExcel.class).registerWriteHandler(horizontalCellStyleStrategy).build(); // excelWriter.write(getProposal(userTopics), proposalSheet); // excelWriter.write(getPublicopinion(userTopics), publicopinionSheet); } //首页-房屋记录 private List getProposal(UserTopicsVO userTopics) { List list = new ArrayList<>(); JSONArray objects1 = JSON.parseArray(userTopics.getDistrictId()); List collect1 = objects1.stream().map(item -> (String) item).collect(Collectors.toList()); IDistrictService bean3 = SpringUtils.getBean(IDistrictService.class); List list3 = bean3.list(Wrappers.lambdaQuery().in(DistrictEntity::getId, collect1)); List aoiCodeList = list3.stream().map(item -> item.getAoiCode()).collect(Collectors.toList()); userTopics.setAoiCodeList(aoiCodeList); List householdList = baseMapper.getHouseholdList(userTopics); householdList.stream().forEach(item -> { if (StringUtils.isBlank(item.getOptionContent())) { item.setVoteFlag("否"); } else { ITopicsService bean = SpringUtils.getBean(ITopicsService.class); List longs = Func.toLongList(item.getOptionContent()); for (Long aLong : longs) { TopicsEntity topics = bean.getById(aLong); if (StringUtils.isNotBlank(topics.getDiscussContent()) && topics.getDiscussContent().equals("管理规约")) { item.setManagementRegulations(topics.getOptionContent()); } if (StringUtils.isNotBlank(topics.getDiscussContent()) && topics.getDiscussContent().equals("议事规则")) { item.setRulesOfProcedure(topics.getOptionContent()); } } item.setVoteFlag("是"); } }); // 添加 list.addAll(householdList); return list; } //首页-议事 private List getPublicopinion(UserTopicsVO userTopics) { List list = new ArrayList<>(); List userTopicsExcels = baseMapper.getresultTwo(userTopics); list.addAll(userTopicsExcels); return list; } }