xiaoyong931011
2022-07-08 2384ab59fbebd5bf40826a88420a736984316b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.xcong.farmer.cms.modules.system.service.Impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddMessageDto;
import com.xcong.farmer.cms.modules.system.dto.AdminDeleteDto;
import com.xcong.farmer.cms.modules.system.dto.AdminMessageBoardDto;
import com.xcong.farmer.cms.modules.system.entity.CompanyEntity;
import com.xcong.farmer.cms.modules.system.entity.MessageBoardEntity;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper;
import com.xcong.farmer.cms.modules.system.mapper.MessageBoardMapper;
import com.xcong.farmer.cms.modules.system.service.IMessageBoardService;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import com.xcong.farmer.cms.modules.system.vo.AdminMessageBoardVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.date.DateUtil;
 
import cn.hutool.core.collection.CollUtil;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
 
@Service
@Slf4j
public class MessageBoardServiceImpl extends ServiceImpl<MessageBoardMapper, MessageBoardEntity> implements IMessageBoardService {
 
    @Resource
    private CompanyMapper companyMapper;
 
    @Override
    public Result getMessageInPage(AdminMessageBoardDto adminMessageBoardDto) {
        MessageBoardEntity messageBoardEntity = new MessageBoardEntity();
        UserEntity userlogin = LoginUserUtil.getLoginUser();
        Long companyId = userlogin.getCompanyId();
        if(UserEntity.USER_BELONG_TOP != companyId){
            messageBoardEntity.setCompanyId(companyId);
        }
        String queryKey = adminMessageBoardDto.getQueryKey();
        if(StrUtil.isNotEmpty(queryKey)){
            messageBoardEntity.setName(queryKey);
            messageBoardEntity.setRemark(queryKey);
        }
        Page<AdminMessageBoardVo> page = new Page<>(adminMessageBoardDto.getPageNum(), adminMessageBoardDto.getPageSize());
        IPage<AdminMessageBoardVo> list = this.baseMapper.selectAdminMessageBoardInPage(page, messageBoardEntity);
        return Result.ok(list);
    }
 
    @Override
    @Transactional
    public Result delObjs(AdminDeleteDto adminDeleteDto) {
        String ids = adminDeleteDto.getIds();
        if(StrUtil.isNotEmpty(ids)){
            String[] messageIds = ids.split(StringPool.COMMA);
            for(String messageIdStr : messageIds){
                Long messageId = Long.valueOf(messageIdStr);
                this.baseMapper.deleteById(messageId);
            }
        }
        return Result.ok("删除成功");
    }
 
    @Override
    public Result addMessage(HttpServletRequest request, AdminAddMessageDto adminAddMessageDto) {
        String website = request.getHeader(AppContants.WEBSITE_HEADER);
        Long companyId = getCompanyIdFromWebsite(website);
 
        String title = adminAddMessageDto.getTitle();
        String remark = adminAddMessageDto.getRemark();
        if(StrUtil.isEmpty(title) && StrUtil.isEmpty(remark)){
            return Result.fail("请输入留言");
        }
        MessageBoardEntity messageBoardEntity = new MessageBoardEntity();
        messageBoardEntity.setName(adminAddMessageDto.getName());
        messageBoardEntity.setContactValue(adminAddMessageDto.getContactValue());
        messageBoardEntity.setTitle(adminAddMessageDto.getTitle());
        messageBoardEntity.setRemark(adminAddMessageDto.getRemark());
        messageBoardEntity.setCreateTime(DateUtil.date());
        messageBoardEntity.setParamOne(adminAddMessageDto.getParamOne());
        messageBoardEntity.setParamTwo(adminAddMessageDto.getParamTwo());
        if(companyId > 0L){
            messageBoardEntity.setCompanyId(companyId);
        }
        this.baseMapper.insert(messageBoardEntity);
        return Result.ok("留言成功");
    }
 
    private Long getCompanyIdFromWebsite(String website){
        List<CompanyEntity> companyEntities = companyMapper.selectList(new QueryWrapper<>());
        Long companyId = 0L;
        if(CollUtil.isNotEmpty(companyEntities)){
            for(CompanyEntity companyEntity : companyEntities){
                boolean contains = StrUtil.contains(companyEntity.getWebAddress(), website);
                if(contains){
                    companyId = companyEntity.getId();
                }
            }
        }
        return companyId;
    }
}