liujie
昨天 c983a0a398504697a777ffae48e59961b5ee2bd7
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.ruoyi.user.service.impl;
 
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.user.domain.AccountCity;
import com.ruoyi.user.domain.AccountCityCompany;
import com.ruoyi.user.domain.AccountUser;
import com.ruoyi.user.domain.vo.AccountCityCompanyVo;
import com.ruoyi.user.domain.vo.MemberOptionVo;
import com.ruoyi.user.mapper.AccountCityCompanyMapper;
import com.ruoyi.user.mapper.AccountCityMapper;
import com.ruoyi.user.mapper.AccountUserMapper;
import com.ruoyi.user.service.IAccountCityCompanyService;
 
/**
 * 城市公司档案 业务层实现
 *
 * @author ruoyi
 */
@Service
public class AccountCityCompanyServiceImpl implements IAccountCityCompanyService
{
    @Autowired
    private AccountCityCompanyMapper companyMapper;
 
    @Autowired
    private AccountCityMapper cityMapper;
 
    @Autowired
    private AccountUserMapper accountUserMapper;
 
    @Override
    public List<AccountCityCompanyVo> selectCompanyList(AccountCityCompanyVo company)
    {
        return companyMapper.selectCompanyList(company);
    }
 
    @Override
    public AccountCityCompanyVo selectCompanyById(Long id)
    {
        return companyMapper.selectCompanyById(id);
    }
 
    @Override
    public int insertCompany(AccountCityCompany company)
    {
        // current 为 null 即新增:此时城市必须存在且启用
        validateCompany(company, null);
        // 默认启用:不写 status 会落库为 NULL,而 NORMAL 的取值是 '0'
        if (StringUtils.isEmpty(company.getStatus()))
        {
            company.setStatus(AccountCityCompany.STATUS_NORMAL);
        }
        return companyMapper.insertCompany(company);
    }
 
    @Override
    public int updateCompany(AccountCityCompany company)
    {
        if (StringUtils.isNull(company.getId()))
        {
            throw new ServiceException("档案编号不能为空");
        }
        // 先确认存在:update 的 where 带 del_flag = '0',改到已删除的行会返回 0 行,
        // 与「没改动任何字段」无法区分,故先显式查一次。查出来的行同时用于校验——
        // 城市是否被改过要靠它比对(见 validateCompany 里对「城市已禁用」的放行)
        AccountCityCompany current = companyMapper.selectCompanyById(company.getId());
        if (StringUtils.isNull(current))
        {
            throw new ServiceException("公司档案不存在");
        }
        validateCompany(company, current);
        return companyMapper.updateCompany(company);
    }
 
    @Override
    public int changeStatus(AccountCityCompany company)
    {
        String target = company.getStatus();
        if (!AccountCityCompany.STATUS_NORMAL.equals(target) && !AccountCityCompany.STATUS_DISABLED.equals(target))
        {
            throw new ServiceException("档案状态只能改为「正常」或「禁用」");
        }
        if (StringUtils.isNull(companyMapper.selectCompanyById(company.getId())))
        {
            throw new ServiceException("公司档案不存在");
        }
        return companyMapper.updateCompany(company);
    }
 
    @Override
    public List<MemberOptionVo> searchL4Member(String keyword)
    {
        // 空关键字不回结果:L4 会员是全量可搜的集合,放开等于把整张会员表倒进下拉
        if (StringUtils.isEmpty(keyword))
        {
            return Collections.emptyList();
        }
        String kw = keyword.trim();
        // 纯数字时补一个 keywordId 走等值匹配(用户编号),非数字则不参与该列比较——
        // 写成 user_id like 或 cast(user_id as char) 都会让主键索引用不上。
        Long keywordId = null;
        try
        {
            keywordId = Long.valueOf(kw);
        }
        catch (NumberFormatException ignored)
        {
            // 非纯数字:只走手机号/昵称/姓名模糊匹配
        }
        return accountUserMapper.selectL4MemberOptions(kw, keywordId);
    }
 
    /**
     * 写操作的公共校验。
     *
     * @param current 修改前的档案;为 null 表示新增。修改时用于排除自身查重,
     *                并判断城市有没有被改动过
     */
    private void validateCompany(AccountCityCompany company, AccountCityCompany current)
    {
        if (StringUtils.isNull(company.getCityId()))
        {
            throw new ServiceException("所属城市不能为空");
        }
        AccountCity city = cityMapper.selectCityById(company.getCityId());
        if (StringUtils.isNull(city))
        {
            throw new ServiceException("所属城市不存在");
        }
        // 「城市已禁用」只拦新落到该城市上的档案(新增,或修改时把城市换掉)。
        // 城市被禁用后,原本就挂在它下面的档案仍然要能改联系人/公司名,
        // 否则一条禁用就锁死了存量档案,且前端下拉里也选不回原城市。城市只要没变就放行。
        boolean cityChanged = current == null || !company.getCityId().equals(current.getCityId());
        if (cityChanged && !AccountCity.STATUS_NORMAL.equals(city.getStatus()))
        {
            throw new ServiceException("所属城市已禁用,不能建立档案");
        }
 
        if (StringUtils.isEmpty(company.getCompanyName()))
        {
            throw new ServiceException("公司名称不能为空");
        }
        if (StringUtils.isEmpty(company.getContactName()))
        {
            throw new ServiceException("联系人不能为空");
        }
        if (StringUtils.isEmpty(company.getContactPhone()))
        {
            throw new ServiceException("联系电话不能为空");
        }
 
        if (StringUtils.isNull(company.getUserId()))
        {
            throw new ServiceException("关联用户不能为空");
        }
        // selectAccountUserById 的 where 自带 del_flag = '0',查不到即「不存在或已删除」
        AccountUser member = accountUserMapper.selectAccountUserById(company.getUserId());
        if (StringUtils.isNull(member))
        {
            throw new ServiceException("关联用户不存在");
        }
        // 只认当下等级,不看认证状态:等级是运营手工授予的准入依据,认证只是资料完整度
        if (!AccountUser.LEVEL_L4.equals(member.getSystemLevel()))
        {
            throw new ServiceException("关联用户只能是 L4 会员");
        }
 
        // 一城一档:同城市下已存在未删除档案则拦下(修改时排除自身)
        AccountCityCompany ref = new AccountCityCompany();
        ref.setCityId(company.getCityId());
        if (current != null)
        {
            ref.setId(company.getId());
        }
        if (companyMapper.countCompanyByCityId(ref) > 0)
        {
            throw new ServiceException("城市「" + city.getCityName() + "」已存在公司档案,不能重复建立");
        }
    }
}