mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.panzhihua.service_community.service.impl;
 
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panzhihua.common.constants.UserConstants;
import com.panzhihua.common.model.vos.user.SysUserVO;
import com.panzhihua.common.utlis.Snowflake;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.community.dpc.AddDpcDTO;
import com.panzhihua.common.model.dtos.community.dpc.EditDpcDTO;
import com.panzhihua.common.model.dtos.community.dpc.PageDpcDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.service_community.dao.ComActDpcDAO;
import com.panzhihua.service_community.entity.ComActDpc;
import com.panzhihua.service_community.service.ComActDpcService;
 
import javax.annotation.Resource;
import java.util.Date;
 
/**
 * 人大代表(ComActDpc)表服务实现类
 *
 * @author makejava
 * @since 2022-06-07 10:55:32
 */
@Service("comActDpcService")
public class ComActDpcServiceImpl extends ServiceImpl<ComActDpcDAO, ComActDpc> implements ComActDpcService {
 
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ComActDpcDAO comActDpcDAO;
 
    /**
     * 新增人大代表
     * @param addDpcDTO
     * @return
     */
    @Override
    public R addDpc(AddDpcDTO addDpcDTO) {
        ComActDpc dpc = this.baseMapper.selectOne(new LambdaQueryWrapper<ComActDpc>()
                .eq(ComActDpc::getPhone, addDpcDTO.getPhone())
                .eq(ComActDpc::getCommunityId, addDpcDTO.getCommunityId()));
        if (nonNull(dpc)) {
            if (dpc.getIsDel()) {
                this.baseMapper.deleteById(dpc.getId());
            } else {
                return R.fail("手机号已存在");
            }
        }
        dpc = new ComActDpc();
        dpc.setId(Snowflake.getId());
        dpc.setCreatedAt(new Date());
        BeanUtils.copyProperties(addDpcDTO, dpc);
        int result = comActDpcDAO.insertComActDpc(dpc);
        if (result > 0) {
            refreshAssociateUserCash(addDpcDTO.getPhone());
            return R.ok();
        }
        return R.fail("操作失败,请重新尝试");
    }
 
    /**
     * 修改人大代表
     * @param editDpcDTO
     * @return
     */
    @Override
    public R editDpc(EditDpcDTO editDpcDTO) {
        ComActDpc dpc = this.baseMapper.selectById(editDpcDTO.getId());
        if (isNull(dpc)) {
            return R.fail("资源不存在");
        }
        if (!dpc.getPhone().equals(editDpcDTO.getPhone())) {
            ComActDpc dpc1 = this.baseMapper.selectOne(new LambdaQueryWrapper<ComActDpc>()
                    .eq(ComActDpc::getPhone, editDpcDTO.getPhone())
                    .eq(ComActDpc::getCommunityId, editDpcDTO.getCommunityId()));
            if (nonNull(dpc1)) {
                if (dpc1.getIsDel()) {
                    this.baseMapper.deleteById(dpc1.getId());
                } else {
                    return R.fail("手机号已存在");
                }
            }
        }
        BeanUtils.copyProperties(editDpcDTO, dpc);
        int result = this.baseMapper.updateById(dpc);
        if (result > 0) {
            refreshAssociateUserCash(editDpcDTO.getPhone());
            return R.ok();
        }
        return R.fail("操作失败,请重新尝试");
    }
 
    /**
     * 删除人大代表
     * @param id
     * @return
     */
    @Override
    public R deleteDpc(Long id) {
        ComActDpc dpc = this.baseMapper.selectById(id);
        if (isNull(dpc)) {
            return R.fail("资源不存在");
        }
        dpc.setIsDel(true);
        int result = this.baseMapper.updateById(dpc);
        if (result > 0) {
            refreshAssociateUserCash(dpc.getPhone());
            return R.ok();
        }
        return R.fail("操作失败,请重新尝试");
    }
 
    private void refreshAssociateUserCash(String phone) {
        SysUserVO sysUser = this.baseMapper.selectUser(phone);
        if (nonNull(sysUser)) {
            String userKey = UserConstants.LOGIN_USER_INFO + sysUser.getUserId();
            stringRedisTemplate.delete(userKey);
        }
    }
 
    /**
     * 获取人大代表详情
     * @param id
     * @return
     */
    @Override
    public R detailDpc(Long id) {
        return R.ok(this.baseMapper.detailDpc(id));
    }
 
    /**
     * 分页查询人大代表
     * @param pageDpcDTO
     * @return
     */
    @Override
    public R pageDpc(PageDpcDTO pageDpcDTO) {
        Page page = new Page<>();
        page.setSize(pageDpcDTO.getPageSize());
        page.setCurrent(pageDpcDTO.getPageNum());
        return R.ok(this.baseMapper.pageDpc(page, pageDpcDTO));
    }
}