admin
2024-08-04 bc56870059cca013649077af0e53891cba8dbfd1
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
package com.taoke.autopay.service.impl.agent;
 
import com.taoke.autopay.dao.agent.ChannelAgentMapper;
import com.taoke.autopay.entity.agent.ChannelAgent;
import com.taoke.autopay.exception.ChannelAgentException;
import com.taoke.autopay.service.agent.ChannelAgentService;
import com.taoke.autopay.utils.AgentAliasUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author hxh
 * @title: ChannelAgentServiceImpl
 * @description: TODO
 * @date 2024/7/25 22:28
 */
@Service
public class ChannelAgentServiceImpl implements ChannelAgentService {
 
    @Resource
    private ChannelAgentMapper channelAgentMapper;
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ChannelAgent addChannelAgent(ChannelAgent agent) throws ChannelAgentException {
        ChannelAgentMapper.DaoQuery query = new ChannelAgentMapper.DaoQuery();
        query.account = agent.getAccount();
        long count = channelAgentMapper.count(query);
        if (count > 0) {
            throw new ChannelAgentException("账号已存在");
        }
        if (agent.getStatus() == null) {
            agent.setStatus(ChannelAgent.STATUS_NOMAL);
        }
 
 
        if (agent.getCreateTime() == null) {
            agent.setCreateTime(new Date());
        }
        channelAgentMapper.insertSelective(agent);
 
        // 更新alias
        if (agent.getAlias() == null) {
            ChannelAgent update = ChannelAgent.builder().id(agent.getId()).alias(AgentAliasUtil.createAlias(agent.getId())).updateTime(new Date()).build();
            agent.setAlias(update.getAlias());
            channelAgentMapper.updateByPrimaryKeySelective(update);
        }
 
        return agent;
 
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setAlipayAccount(Long agentId, String name, String account) throws ChannelAgentException {
        ChannelAgent agent = channelAgentMapper.selectByPrimaryKeyForUpdate(agentId);
        if (agent == null) {
            throw new ChannelAgentException("代理ID不存在");
        }
        ChannelAgent update = ChannelAgent.builder().id(agentId).alipayAccount(account).alipayName(name).alipayUpdateTime(new Date()).updateTime(new Date()).build();
        updateSelective(update);
    }
 
    @Override
    public ChannelAgent selectByPrimaryKey(Long agengId) {
        return channelAgentMapper.selectByPrimaryKey(agengId);
    }
 
    @Override
    public void updateSelective(ChannelAgent agent) {
        if (agent.getUpdateTime() == null) {
            agent.setUpdateTime(new Date());
        }
        channelAgentMapper.updateByPrimaryKeySelective(agent);
    }
 
    @Override
    public List<ChannelAgent> list(ChannelAgentMapper.DaoQuery query) {
        return channelAgentMapper.list(query);
    }
 
    @Override
    public long count(ChannelAgentMapper.DaoQuery query) {
        return channelAgentMapper.count(query);
    }
 
    @Override
    public void delete(Long id) {
        channelAgentMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public ChannelAgent login(String account, String pwd) throws ChannelAgentException {
 
        ChannelAgentMapper.DaoQuery query = new ChannelAgentMapper.DaoQuery();
        query.account = account;
        query.count = 1;
        List<ChannelAgent> list = channelAgentMapper.list(query);
        if (list.isEmpty()) {
            throw new ChannelAgentException("账户不存在");
        }
        if (!list.get(0).getPwd().equalsIgnoreCase(StringUtil.Md5(pwd))) {
            throw new ChannelAgentException("密码错误");
        }
 
 
        return list.get(0);
    }
 
    @Override
    public ChannelAgent selectByAlias(String alias) {
        ChannelAgentMapper.DaoQuery query = new ChannelAgentMapper.DaoQuery();
        query.alias = alias;
        query.count = 1;
        List<ChannelAgent> list = channelAgentMapper.list(query);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
 
    @Override
    public List<ChannelAgent> listByIds(List<Long> ids) {
        if(ids==null||ids.size()==0){
            return new ArrayList<>();
        }
        return channelAgentMapper.listByIds(ids);
    }
}