CustomInfoServiceImpl.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. package com.crm.custom.service.impl;
  2. import com.crm.custom.dao.mapper.CustomFileDao;
  3. import com.crm.custom.dao.mapper.CustomInfoDao;
  4. import com.crm.custom.dao.repository.CustomInfoRepository;
  5. import com.crm.custom.dao.repository.CustomLoginHistoryRepository;
  6. import com.crm.custom.dao.repository.UserInfoRepository;
  7. import com.crm.custom.service.*;
  8. import com.crm.custom.service.impl.base.BaseUploadServiceImpl;
  9. import com.crm.rely.backend.configuration.LocaleMessage;
  10. import com.crm.rely.backend.core.constant.*;
  11. import com.crm.rely.backend.core.dto.custom.file.CustomFileDto;
  12. import com.crm.rely.backend.core.entity.base.LongEntitys;
  13. import com.crm.rely.backend.core.entity.system.email.SysEmailSendEntity;
  14. import com.crm.rely.backend.core.entity.web.notice.record.WebNoticeRecordAndEmailMqEntity;
  15. import com.crm.rely.backend.core.exception.ServiceException;
  16. import com.crm.rely.backend.core.pojo.table.CustomLoginHistoryTable;
  17. import com.crm.rely.backend.core.pojo.table.SysConfigTable;
  18. import com.crm.rely.backend.core.pojo.table.SysCountryTable;
  19. import com.crm.rely.backend.model.constant.ConfigConstants;
  20. import com.crm.rely.backend.model.constant.CustomConstants;
  21. import com.crm.rely.backend.model.constant.PayConstants;
  22. import com.crm.rely.backend.model.entity.custom.bank.CustomBankAddEntity;
  23. import com.crm.rely.backend.model.entity.custom.fund.history.CustomFundHistoryAddEntity;
  24. import com.crm.rely.backend.model.entity.custom.info.*;
  25. import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
  26. import com.crm.rely.backend.model.pojo.table.CustomBankTable;
  27. import com.crm.rely.backend.model.pojo.table.CustomInfoTable;
  28. import com.crm.rely.backend.model.pojo.table.UserInfoTable;
  29. import com.crm.rely.backend.model.util.CustomInfoUtil;
  30. import com.crm.rely.backend.service.EmailService;
  31. import com.crm.rely.backend.service.MqSendService;
  32. import com.crm.rely.backend.service.RedisService;
  33. import com.crm.rely.backend.util.*;
  34. import com.google.common.base.Strings;
  35. import lombok.extern.log4j.Log4j;
  36. import org.springframework.beans.BeanUtils;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.stereotype.Service;
  39. import org.springframework.transaction.annotation.Transactional;
  40. import org.springframework.web.multipart.MultipartFile;
  41. import java.math.BigDecimal;
  42. import java.util.*;
  43. /**
  44. * @program: crm-backend
  45. * @description:
  46. * @author: houn
  47. * @create: 2019-07-25 16:14
  48. */
  49. @SuppressWarnings("SpringJavaAutowiringInspection")
  50. @Log4j
  51. @Service
  52. public class CustomInfoServiceImpl extends BaseUploadServiceImpl implements CustomInfoService {
  53. @Autowired
  54. private CustomInfoRepository customInfoRepository;
  55. @Autowired
  56. private UserInfoRepository userInfoRepository;
  57. @Autowired
  58. private CustomLoginHistoryRepository customLoginHistoryRepository;
  59. @Autowired
  60. private SysCountryService sysCountryService;
  61. @Autowired
  62. private EmailService emailService;
  63. @Autowired
  64. private RedisService redisService;
  65. @Autowired
  66. private MqSendService mqSendService;
  67. @Autowired
  68. private CustomInfoDao customInfoDao;
  69. @Autowired
  70. private CustomFileDao customFileDao;
  71. @Autowired
  72. private CustomFileService customFileService;
  73. @Autowired
  74. private SysConfigService sysConfigService;
  75. @Autowired
  76. private CustomBankService customBankService;
  77. @Autowired
  78. private CustomLoginService customLoginService;
  79. @Autowired
  80. private CustomLoginDemoService customLoginDemoService;
  81. @Autowired
  82. private CustomLoginFirmOfferService customLoginFirmOfferService;
  83. @Autowired
  84. private CustomLoginAssetManagementService customLoginAssetManagementService;
  85. @Autowired
  86. private CustomFundHistoryService customFundHistoryService;
  87. @Autowired
  88. private LocaleMessage localeMessage;
  89. /**
  90. * 用户登录
  91. *
  92. * @param entity
  93. * @return
  94. * @throws ServiceException
  95. */
  96. @Override
  97. @Transactional(rollbackFor = Exception.class)
  98. public CustomInfoEntity login(CustomInfoLoginEntity entity) throws ServiceException {
  99. //首先用邮箱和密码验证账号
  100. CustomInfoTable customInfoTable = customInfoRepository.findFirstByEmailAndPassword(entity.getLoginName(),
  101. entity.getPassword());
  102. //如果不存在则判断 输入的是否为id
  103. if (customInfoTable == null) {
  104. Long id = null;
  105. try {
  106. //将登陆输入的账号转为id
  107. id = Long.parseLong(entity.getLoginName());
  108. customInfoTable = customInfoRepository.findFirstByIdAndPassword(id, entity.getPassword());
  109. } finally {
  110. //如果转化id异常或者id和密码不匹配 这提示用户名或密码错误
  111. if (customInfoTable == null || id == null) {
  112. throw new ServiceException(Constants.LOGIN_FAIL);
  113. }
  114. }
  115. }
  116. if (customInfoTable.getValid() == null || !customInfoTable.getValid().equals(1)) {
  117. throw ServiceException.exception(Constants.RESTRICTED_LOGIN);
  118. }
  119. /*
  120. 获取ib信息
  121. */
  122. //List<CustomFileDto> fileDtos = customFileService.getAllFiles(customInfoTable.getId());
  123. //CustomBankTable customBankTable = customBankService.getByCustomId(customInfoTable.getId());
  124. CustomInfoEntity transform = transform(customInfoTable, null, entity);
  125. /**
  126. * 更新登录信息
  127. */
  128. customInfoTable.setLastAddress(entity.getAddress());
  129. customInfoTable.setLastIp(entity.getIp());
  130. customInfoTable.setLastTime(entity.getTime());
  131. customInfoTable.setModifyUser(customInfoTable.getId());
  132. customInfoTable.setModifyIp(entity.getIp());
  133. customInfoTable.setModifyTime(entity.getTime());
  134. customInfoRepository.save(customInfoTable);
  135. //添加登录记录
  136. CustomLoginHistoryTable customLoginHistoryTable = new CustomLoginHistoryTable();
  137. customLoginHistoryTable.setAddress(entity.getAddress());
  138. customLoginHistoryTable.setLoginIp(entity.getIp());
  139. customLoginHistoryTable.setLoginTime(entity.getTime());
  140. customLoginHistoryTable.setCustomId(customInfoTable.getId());
  141. customLoginHistoryTable.setName(CustomInfoUtil.getName(customInfoTable));
  142. customLoginHistoryTable.setAddUser(customInfoTable.getId());
  143. customLoginHistoryTable.setAddIp(entity.getIp());
  144. customLoginHistoryTable.setAddTime(entity.getTime());
  145. customLoginHistoryRepository.save(customLoginHistoryTable);
  146. return transform;
  147. }
  148. /**
  149. * 注册发送邮箱验证码
  150. *
  151. * @param entity
  152. * @throws ServiceException
  153. */
  154. @Override
  155. public void registerSendEmailCode(CustomInfoRegisterSendEmailCodeEntity entity) throws ServiceException {
  156. //验证邮箱是否存在
  157. existByEmail(entity.getEmail());
  158. //构建实体
  159. SysEmailSendEntity sysEmailSendEntity = createRegisterSendEmailCodeSysEmailEntity(entity);
  160. //发送收据
  161. emailService.sendCode(sysEmailSendEntity, sysEmailSendEntity.getMap().get(MapConstants.CODE),
  162. Constants.EMAIL_REGISTER_VALIDATECODE_TIME * 60 * 1000);
  163. }
  164. @Override
  165. @Transactional(rollbackFor = Exception.class)
  166. public CustomInfoTable applyReal(CustomApplyRealEntity entity) throws ServiceException {
  167. CustomInfoTable customInfoTable = findFirstById(entity.getCustomId());
  168. if (StatusConstants.PASSED_STATUS.equals(customInfoTable.getApplyRealStatus())
  169. && StatusConstants.PASSED_STATUS.equals(customInfoTable.getStatus())) {
  170. return customInfoTable;
  171. } else {
  172. customInfoTable.setStatus(StatusConstants.APPLY_STATUS);
  173. customInfoTable.setApplyRealStatus(StatusConstants.APPLY_STATUS);
  174. }
  175. entity.setId(customInfoTable.getId());
  176. BeanUtils.copyProperties(entity, customInfoTable);
  177. customInfoRepository.save(customInfoTable);
  178. sendCustomApplyVerificationSysEmailEntity(customInfoTable);
  179. sendCustomApplyVerificationToCustom(customInfoTable);
  180. return customInfoTable;
  181. }
  182. @Override
  183. public void updateEmailSendEmailCode(CustomInfoUpdateEmailSendEmailCodeEntity entity) throws ServiceException {
  184. //验证邮箱是否存在
  185. existByEmail(entity.getEmail());
  186. //构建实体
  187. SysEmailSendEntity sysEmailSendEntity = createUpdateEmailSendEmailSysEmailEntity(entity);
  188. //发送收据
  189. emailService.sendCode(sysEmailSendEntity, sysEmailSendEntity.getMap().get(MapConstants.CODE));
  190. }
  191. /**
  192. * 添加用户信息
  193. *
  194. * @param entity
  195. * @throws ServiceException
  196. */
  197. @Override
  198. @Transactional(rollbackFor = Exception.class)
  199. public void add(CustomInfoRegisterEntity entity) throws ServiceException, InterruptedException {
  200. //打印注册信息日志
  201. String registerString = String.format("register email:%s,emailCode:%s,agentId:%d",entity.getEmail(),entity.getEmailCode(), entity.getAgentId());
  202. log.info(registerString);
  203. //emailService.validateCode(entity.getEmail(), entity.getEmailCode());
  204. //验证邮箱是否存在
  205. existByEmail(entity.getEmail());
  206. /*if (entity.getAgentId() != null) {
  207. UserInfoTable agent = userInfoRepository.getFirstById(entity.getAgentId());
  208. //不存在代理设置为null 外佣设置为0
  209. if (agent == null) {
  210. log.error("get agent is null,agentId:" + entity.getAgentId());
  211. entity.setAgentId(null);
  212. } else {
  213. }
  214. }
  215. //如果代理链接错误或者没有代理 则将客户挂在公司名下
  216. if (entity.getAgentId() == null) {
  217. SysConfigTable sysConfigTable = sysConfigService.getByCode(GlobalConfigConstants.COMPANY_ACCOUNT_SUPER);
  218. if (sysConfigTable != null) {
  219. Long agentId = Long.parseLong(sysConfigTable.getValue());
  220. entity.setAgentId(agentId);
  221. }
  222. }*/
  223. //获取国家信息
  224. SysCountryTable sysCountryTable = sysCountryService.getByCode(entity.getCountry());
  225. CustomInfoTable customInfoTable = new CustomInfoTable();
  226. BeanUtils.copyProperties(entity, customInfoTable);
  227. //设置国家信息
  228. customInfoTable.setCountryEnName(sysCountryTable.getEnName());
  229. customInfoTable.setCountryName(sysCountryTable.getName());
  230. customInfoTable.setAreaCode(sysCountryTable.getCallingCode());
  231. customInfoTable.setPayStatus(0);
  232. //获取和设置c_id
  233. customInfoTable.setCId(CidUtil.getCid().longValue());
  234. customInfoRepository.save(customInfoTable);
  235. customInfoTable.setAddUser(customInfoTable.getId());
  236. //
  237. // //构建实体
  238. // SysEmailSendEntity sysEmailSendEntity = createRegisterCompleteSysEmailEntity(customInfoTable);
  239. //
  240. // //发送收据
  241. // emailService.sendEmail(sysEmailSendEntity);
  242. //
  243. // WebNoticeRecordAndEmailMqEntity mqEntity = new WebNoticeRecordAndEmailMqEntity(sysEmailSendEntity,
  244. // customInfoTable.getCId(), customInfoTable.getId());
  245. // mqSendService.send(Constants.WEB_NOTICE_PUT_RECORD, mqEntity);
  246. //
  247. // //构建实体
  248. // SysEmailSendEntity sysEmailSendEntityToManger = createRegisterCompleteToMangerSysEmailEntity(customInfoTable);
  249. //
  250. // //发送收据
  251. // emailService.sendEmail(sysEmailSendEntityToManger);
  252. }
  253. /**
  254. * 修改用户信息
  255. *
  256. * @param entity
  257. * @throws ServiceException
  258. */
  259. @Override
  260. @Transactional(rollbackFor = Exception.class)
  261. public void update(CustomInfoUpdateEntity entity) throws ServiceException {
  262. CustomInfoTable customInfoTable = findFirstById(entity.getId());
  263. if (Strings.isNullOrEmpty(entity.getSno())) {
  264. entity.setSno(customInfoTable.getSno());
  265. } else {
  266. if (!entity.getSno().equals(customInfoTable.getSno())) {
  267. existBySno(entity.getSno());
  268. }
  269. }
  270. if (!Strings.isNullOrEmpty(entity.getIdentity()) && !entity.getIdentity().equals(customInfoTable.getIdentity())) {
  271. existByIdentity(entity.getIdentity());
  272. }
  273. BeanUtils.copyProperties(entity, customInfoTable);
  274. //获取国家信息
  275. SysCountryTable sysCountryTable = sysCountryService.getByCode(entity.getCountry());
  276. //设置国家信息
  277. customInfoTable.setCountryEnName(sysCountryTable.getEnName());
  278. customInfoTable.setCountryName(sysCountryTable.getName());
  279. if (entity.getAddressLines() != null) {
  280. customInfoTable.setAddressLine(String.join("@#", entity.getAddressLines()));
  281. } else {
  282. customInfoTable.setAddressLine(null);
  283. }
  284. customInfoRepository.save(customInfoTable);
  285. CustomBankAddEntity customBankAddEntity = new CustomBankAddEntity();
  286. BeanUtils.copyProperties(entity, customBankAddEntity);
  287. customBankAddEntity.setCustomId(entity.getId());
  288. customBankService.add(customBankAddEntity);
  289. }
  290. @Override
  291. @Transactional(rollbackFor = Exception.class)
  292. public void updateEmail(CustomInfoUpdateEmailEntity entity) throws ServiceException {
  293. emailService.validateCode(entity.getEmail(), entity.getEmailCode());
  294. //邮箱
  295. existByEmail(entity.getEmail());
  296. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(entity.getCustomId());
  297. customInfoTable.setEmail(entity.getEmail());
  298. customInfoRepository.save(customInfoTable);
  299. }
  300. @Override
  301. @Transactional(rollbackFor = Exception.class)
  302. public String updateHeadPicture(CustomInfoUpdateHeadPictureEntity entity, MultipartFile file) throws Exception {
  303. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(entity.getCustomId());
  304. if (customInfoTable == null) {
  305. throw ServiceException.exception(Constants.SYSTEM_ERROR);
  306. }
  307. //保存头像
  308. String path = saveFile((entity.getCustomId() == null ? "" : entity.getCustomId().toString()) + "/headPicture"
  309. , file);
  310. customInfoTable.setHeadPicture(path);
  311. customInfoTable.setModifyUser(entity.getUser());
  312. customInfoTable.setModifyTime(entity.getTime());
  313. customInfoTable.setModifyIp(entity.getIp());
  314. customInfoRepository.save(customInfoTable);
  315. return path;
  316. }
  317. @Override
  318. @Transactional(rollbackFor = Exception.class)
  319. public void updatePassword(CustomUpdateLoginPasswordEntity entity) throws ServiceException {
  320. CustomInfoTable customInfoTable = findFirstById(entity.getId());
  321. //验证密码是否正确
  322. if (!entity.getOldPassword().equals(customInfoTable.getPassword())) {
  323. throw new ServiceException(Constants.PASSWORD_ERROR);
  324. }
  325. /**
  326. * 填写修改时间和密码
  327. */
  328. customInfoTable.setPassword(entity.getNewPassword());
  329. customInfoTable.setModifyIp(entity.getModifyIp());
  330. customInfoTable.setModifyTime(entity.getModifyTime());
  331. customInfoTable.setModifyUser(entity.getModifyUser());
  332. customInfoRepository.save(customInfoTable);
  333. sendUpdatePasswordCompleteSendEmailSysEmailEntity(customInfoTable);
  334. }
  335. @Override
  336. public void updatePasswordSendEmail(CustomUpdatePasswordSendEmailEntity entity) throws ServiceException {
  337. CustomInfoTable customInfoTable = customInfoRepository.getFirstByEmailNotNullAndEmail(entity.getEmail());
  338. if (customInfoTable == null) {
  339. return;
  340. // throw new ServiceException(Constants.USER_NOT_EXIST_ERROR);
  341. }
  342. SysEmailSendEntity sysEmailSendEntity = createUpdatePasswordSendEmailSysEmailEntity(entity, customInfoTable);
  343. redisService.save(sysEmailSendEntity.getMap().get(MapConstants.TOKEN), customInfoTable.getId(),
  344. Constants.EMAIL_TOKEN_TIME * 60 * 1000);
  345. mqSendService.send(Constants.ACTIVEMQ_JMS_URL_EMAIL, sysEmailSendEntity);
  346. //系统内部通知
  347. WebNoticeRecordAndEmailMqEntity mqEntity = new WebNoticeRecordAndEmailMqEntity(sysEmailSendEntity,
  348. customInfoTable.getCId(), customInfoTable.getId());
  349. mqSendService.send(Constants.WEB_NOTICE_PUT_RECORD, mqEntity);
  350. }
  351. @Override
  352. @Transactional(rollbackFor = Exception.class)
  353. public void updatePassword(CustomUpdateEmailPasswordEntity entity) throws ServiceException {
  354. Long id = redisService.getEntity(entity.getToken(), Long.class);
  355. if (id == null) {
  356. throw new ServiceException(Constants.EMAIL_EXPIRE);
  357. }
  358. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(id);
  359. if (customInfoTable == null) {
  360. throw new ServiceException(Constants.USER_NOT_EXIST_ERROR);
  361. }
  362. /**
  363. * 填写修改时间和密码
  364. */
  365. customInfoTable.setPassword(entity.getPassword());
  366. customInfoTable.setModifyIp(entity.getModifyIp());
  367. customInfoTable.setModifyTime(entity.getModifyTime());
  368. customInfoTable.setModifyUser(customInfoTable.getModifyUser());
  369. customInfoRepository.save(customInfoTable);
  370. redisService.remove(entity.getToken());
  371. sendUpdatePasswordCompleteSendEmailSysEmailEntity(customInfoTable);
  372. }
  373. @Override
  374. @Transactional(rollbackFor = Exception.class)
  375. public void updateCnyAmountById(Long customId, BigDecimal cnyAmount) throws ServiceException {
  376. updateCnyAmountById(customId, cnyAmount, null);
  377. }
  378. @Override
  379. @Transactional(rollbackFor = Exception.class)
  380. public void updateCnyAmountById(Long customId, BigDecimal cnyAmount, String remarks) throws ServiceException {
  381. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(customId);
  382. if (customInfoTable == null) {
  383. return;
  384. }
  385. BigDecimal amount;
  386. if (customInfoTable.getCnyAmount() == null) {
  387. amount = BigDecimal.ZERO;
  388. } else {
  389. amount = customInfoTable.getCnyAmount();
  390. }
  391. amount = amount.add(cnyAmount);
  392. if (cnyAmount.compareTo(BigDecimal.ZERO) < 0 && amount.compareTo(BigDecimal.ZERO) < 0) {
  393. throw ServiceException.exception(PayConstants.INSUFFICIENT_FUNDS);
  394. }
  395. customInfoTable.setCnyAmount(amount);
  396. customInfoRepository.save(customInfoTable);
  397. addHistory(customInfoTable, 3, cnyAmount, remarks);
  398. }
  399. @Override
  400. @Transactional(rollbackFor = Exception.class)
  401. public void updateCnyAmountByCId(Long cId, BigDecimal cnyAmount) throws ServiceException {
  402. updateCnyAmountByCId(cId, cnyAmount, null);
  403. }
  404. @Override
  405. @Transactional(rollbackFor = Exception.class)
  406. public void updateCnyAmountByCId(Long cId, BigDecimal cnyAmount, String remarks) throws ServiceException {
  407. CustomInfoTable customInfoTable = customInfoRepository.findFirstByCId(cId);
  408. if (customInfoTable == null) {
  409. return;
  410. }
  411. BigDecimal amount;
  412. if (customInfoTable.getCnyAmount() == null) {
  413. amount = BigDecimal.ZERO;
  414. } else {
  415. amount = customInfoTable.getCnyAmount();
  416. }
  417. amount = amount.add(cnyAmount);
  418. if (cnyAmount.compareTo(BigDecimal.ZERO) < 0 && amount.compareTo(BigDecimal.ZERO) < 0) {
  419. throw ServiceException.exception(PayConstants.INSUFFICIENT_FUNDS);
  420. }
  421. customInfoTable.setCnyAmount(amount);
  422. customInfoRepository.save(customInfoTable);
  423. addHistory(customInfoTable, 3, cnyAmount, remarks);
  424. }
  425. @Override
  426. @Transactional(rollbackFor = Exception.class)
  427. public void updateUsdAmountById(Long customId, BigDecimal usdAmount) throws ServiceException {
  428. updateUsdAmountById(customId, usdAmount, null);
  429. }
  430. @Override
  431. @Transactional(rollbackFor = Exception.class)
  432. public void updateUsdAmountById(Long customId, BigDecimal usdAmount, String remarks) throws ServiceException {
  433. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(customId);
  434. if (customInfoTable == null) {
  435. return;
  436. }
  437. BigDecimal amount;
  438. if (customInfoTable.getUsdAmount() == null) {
  439. amount = BigDecimal.ZERO;
  440. } else {
  441. amount = customInfoTable.getUsdAmount();
  442. }
  443. amount = amount.add(usdAmount);
  444. if (usdAmount.compareTo(BigDecimal.ZERO) < 0 && amount.compareTo(BigDecimal.ZERO) < 0) {
  445. throw ServiceException.exception(PayConstants.INSUFFICIENT_FUNDS);
  446. }
  447. customInfoTable.setUsdAmount(amount);
  448. customInfoRepository.save(customInfoTable);
  449. addHistory(customInfoTable, 2, usdAmount, remarks);
  450. }
  451. @Override
  452. @Transactional(rollbackFor = Exception.class)
  453. public void updateUsdAmountByCId(Long cId, BigDecimal usdAmount) throws ServiceException {
  454. updateUsdAmountByCId(cId, usdAmount, null);
  455. }
  456. @Override
  457. @Transactional(rollbackFor = Exception.class)
  458. public void updateUsdAmountByCId(Long cId, BigDecimal usdAmount, String remarks) throws ServiceException {
  459. CustomInfoTable customInfoTable = customInfoRepository.findFirstByCId(cId);
  460. if (customInfoTable == null) {
  461. return;
  462. }
  463. BigDecimal amount;
  464. if (customInfoTable.getUsdAmount() == null) {
  465. amount = BigDecimal.ZERO;
  466. } else {
  467. amount = customInfoTable.getUsdAmount();
  468. }
  469. amount = amount.add(usdAmount);
  470. if (usdAmount.compareTo(BigDecimal.ZERO) < 0 && amount.compareTo(BigDecimal.ZERO) < 0) {
  471. throw ServiceException.exception(PayConstants.INSUFFICIENT_FUNDS);
  472. }
  473. customInfoTable.setUsdAmount(amount);
  474. customInfoRepository.save(customInfoTable);
  475. addHistory(customInfoTable, 2, usdAmount, remarks);
  476. }
  477. private void addHistory(CustomInfoTable customInfoTable, Integer sourceType, BigDecimal amount, String remarks) throws ServiceException {
  478. CustomFundHistoryAddEntity entity = new CustomFundHistoryAddEntity();
  479. entity.setCustomId(customInfoTable.getId());
  480. entity.setCId(customInfoTable.getCId());
  481. entity.setAgentId(customInfoTable.getAgentId());
  482. entity.setAmount(amount);
  483. entity.setRemarks(remarks);
  484. entity.setSourceType(sourceType);
  485. customFundHistoryService.add(entity);
  486. }
  487. @Override
  488. @Transactional(rollbackFor = Exception.class)
  489. public void updateAgentId(CustomInfoUpdateAgentEntity entity) throws ServiceException {
  490. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(entity.getId());
  491. if (customInfoTable == null) {
  492. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  493. }
  494. if (!entity.getAgentId().equals(customInfoTable.getAgentId())) {
  495. BeanUtils.copyProperties(entity, customInfoTable);
  496. customInfoRepository.save(customInfoTable);
  497. customLoginService.updateAgentId(entity);
  498. customLoginDemoService.updateAgentId(entity);
  499. customLoginFirmOfferService.updateAgentId(entity);
  500. customLoginAssetManagementService.updateAgentId(entity);
  501. UserInfoTable userInfoTable = userInfoRepository.findFirstById(customInfoTable.getIbId());
  502. if (userInfoTable != null) {
  503. userInfoTable.setPid(entity.getAgentId());
  504. userInfoTable.setPStamp(entity.getPStamp());
  505. userInfoTable.setStamp(entity.getStamp());
  506. userInfoTable.setSalesId(entity.getSalesId());
  507. userInfoRepository.save(userInfoTable);
  508. }
  509. }
  510. }
  511. /**
  512. * 根据id获取用户信息
  513. *
  514. * @param id
  515. * @return
  516. */
  517. private CustomInfoTable findFirstById(Long id) throws ServiceException {
  518. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(id);
  519. if (customInfoTable == null) {
  520. throw new ServiceException(Constants.USER_NOT_EXIST_ERROR);
  521. }
  522. return customInfoTable;
  523. }
  524. /**
  525. * 验证邮箱是否已存在
  526. *
  527. * @param email
  528. * @throws ServiceException
  529. */
  530. private void existByEmail(String email) throws ServiceException {
  531. boolean existByEmail = customInfoRepository.existsByEmail(email);
  532. if (existByEmail) {
  533. throw new ServiceException(Constants.EMAIL_IS_EXIST_ERROR);
  534. }
  535. }
  536. /**
  537. * 验证证件是否已存在
  538. *
  539. * @param identity
  540. * @throws ServiceException
  541. */
  542. private void existByIdentity(String identity) throws ServiceException {
  543. //如果为空 则直接返回
  544. if (Strings.isNullOrEmpty(identity)) {
  545. return;
  546. }
  547. boolean existsByIdentity = customInfoRepository.existsByIdentity(identity);
  548. if (existsByIdentity) {
  549. throw new ServiceException(Constants.IDENTITY_IS_EXIST_ERROR);
  550. }
  551. }
  552. private void existBySno(String sno) throws ServiceException {
  553. //如果为空 则直接返回
  554. if (Strings.isNullOrEmpty(sno)) {
  555. return;
  556. }
  557. CustomInfoTable customInfoTable = customInfoRepository.getFirstBySno(sno);
  558. if (customInfoTable != null) {
  559. throw new ServiceException(String.format(localeMessage.getMessage("sno_is_exist_error"),
  560. customInfoTable.getSno(), customInfoTable.getEmail()));
  561. }
  562. }
  563. /**
  564. * 构建注册发送邮件实体
  565. *
  566. * @param entity
  567. * @return
  568. */
  569. private SysEmailSendEntity createRegisterSendEmailCodeSysEmailEntity(CustomInfoRegisterSendEmailCodeEntity entity) {
  570. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  571. Map<String, String> map = new HashMap<>(4);
  572. String code = GetRandom.getRandom(6);
  573. map.put(MapConstants.CODE, code);
  574. Date time = DateUtil.operationMinute(new Date(), Constants.EMAIL_REGISTER_VALIDATECODE_TIME);
  575. map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTime(time));
  576. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  577. sysEmailSendEntity.setAddIp(entity.getIp());
  578. sysEmailSendEntity.setAddTime(entity.getTime());
  579. sysEmailSendEntity.setUsers(entity.getEmail());
  580. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.REGISTER_CODE);
  581. if (CountryUtil.isCN(entity.getCountry())) {
  582. sysEmailSendEntity.setTemplateName(GlobalSystemEmailCodeConstants.CUSTOM_REGISTER_SEND_EMAIL_CODE_CN);
  583. } else {
  584. sysEmailSendEntity.setTemplateName(GlobalSystemEmailCodeConstants.CUSTOM_REGISTER_SEND_EMAIL_CODE_EN);
  585. }
  586. sysEmailSendEntity.setMap(map);
  587. sysEmailSendEntity.setMap(map);
  588. return sysEmailSendEntity;
  589. }
  590. private SysEmailSendEntity createRegisterCompleteSysEmailEntity(CustomInfoTable entity) {
  591. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  592. Map<String, String> map = new HashMap<>(3);
  593. map.put(MapConstants.C_ID, entity.getCId() + "");
  594. map.put(MapConstants.EMAIL, entity.getEmail());
  595. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  596. sysEmailSendEntity.setAddIp(entity.getAddIp());
  597. sysEmailSendEntity.setAddTime(entity.getAddTime());
  598. sysEmailSendEntity.setUsers(entity.getEmail());
  599. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.REGISTER_COMPLETE);
  600. sysEmailSendEntity.setTemplateName(GlobalSystemEmailCodeConstants.CUSTOM_REGISTER_COMPLETE_SEND_CN);
  601. sysEmailSendEntity.setMap(map);
  602. return sysEmailSendEntity;
  603. }
  604. // private SysEmailSendEntity createRegisterCompleteToMangerSysEmailEntity(CustomInfoTable entity) throws
  605. // ServiceException {
  606. // SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  607. //
  608. // Map<String, String> map = new HashMap<>(3);
  609. //
  610. // map.put(MapConstants.C_ID, entity.getCId() + "");
  611. // map.put(MapConstants.EMAIL, entity.getEmail());
  612. //
  613. // map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  614. //
  615. // sysEmailSendEntity.setAddIp(entity.getAddIp());
  616. // sysEmailSendEntity.setAddTime(entity.getAddTime());
  617. //
  618. // SysConfigTable sysConfigTable = sysConfigService.getByCode(GlobalConfigConstants.REGISTER_EMAIL_NOTICE);
  619. // if (sysConfigTable != null) {
  620. // sysEmailSendEntity.setUsers(sysConfigTable.getValue());
  621. // }
  622. //
  623. // sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.REGISTER_COMPLETE_MANAGER);
  624. //
  625. // sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.REGISTER_COMPLETE_NOTICE_NAME);
  626. //
  627. // sysEmailSendEntity.setMap(map);
  628. //
  629. // return sysEmailSendEntity;
  630. // }
  631. private SysEmailSendEntity createUpdateEmailSendEmailSysEmailEntity(CustomInfoUpdateEmailSendEmailCodeEntity entity) {
  632. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  633. Map<String, String> map = new HashMap<>(2);
  634. String code = GetRandom.getRandom(6);
  635. map.put(MapConstants.CODE, code);
  636. Date time = DateUtil.operationMinute(new Date(), Constants.EMAIL_VALIDATECODE_TIME);
  637. map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTime(time));
  638. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  639. sysEmailSendEntity.setAddIp(entity.getIp());
  640. sysEmailSendEntity.setAddTime(entity.getTime());
  641. sysEmailSendEntity.setUsers(entity.getEmail());
  642. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.UPDATE_EMAIL_CODE);
  643. sysEmailSendEntity.setTemplateName(GlobalSystemEmailCodeConstants.CUSTOM_UPDATE_EMAIL_CODE_SEND_CN);
  644. sysEmailSendEntity.setMap(map);
  645. return sysEmailSendEntity;
  646. }
  647. /**
  648. * 发送后台申请核验发送邮件实体
  649. *
  650. * @param customInfoTable
  651. * @return
  652. * @throws ServiceException
  653. */
  654. private void sendCustomApplyVerificationSysEmailEntity(CustomInfoTable customInfoTable) throws ServiceException {
  655. if (customInfoTable == null) {
  656. throw ServiceException.exception(Constants.SYSTEM_ERROR);
  657. }
  658. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  659. SysConfigTable sysConfigTable =
  660. sysConfigService.getByCode(ConfigConstants.CUSTOM_SUBMIT_VERIFICATION_MANAGER_EMAIL);
  661. if (sysConfigTable != null) {
  662. sysEmailSendEntity.setUsers(sysConfigTable.getValue());
  663. }
  664. Map<String, String> map = new HashMap<>(6);
  665. String name = CustomInfoUtil.getName(customInfoTable);
  666. map.put(MapConstants.NAME, name);
  667. map.put(MapConstants.C_ID, String.valueOf(customInfoTable.getCId()));
  668. map.put(MapConstants.EMAIL, customInfoTable.getEmail());
  669. map.put(MapConstants.APPLY_TIME, DateUtil.formatTime());
  670. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  671. sysEmailSendEntity.setAddIp(customInfoTable.getModifyIp());
  672. sysEmailSendEntity.setAddTime(new Date());
  673. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.SUBMIT_VERIFICATION);
  674. String templateName = GlobalSystemEmailCodeConstants.SUBMIT_VERIFICATION_NOTICE_NAME;
  675. sysEmailSendEntity.setTemplateName(templateName);
  676. sysEmailSendEntity.setMap(map);
  677. emailService.sendEmail(sysEmailSendEntity);
  678. }
  679. /**
  680. * 发送到客户提交核验
  681. *
  682. * @param customInfoTable
  683. * @throws ServiceException
  684. */
  685. private void sendCustomApplyVerificationToCustom(CustomInfoTable customInfoTable) throws ServiceException {
  686. if (customInfoTable == null) {
  687. throw ServiceException.exception(Constants.SYSTEM_ERROR);
  688. }
  689. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  690. sysEmailSendEntity.setUsers(customInfoTable.getEmail());
  691. Map<String, String> map = new HashMap<>(3);
  692. String name = CustomInfoUtil.getName(customInfoTable);
  693. map.put(MapConstants.NAME, name);
  694. map.put(MapConstants.C_ID, String.valueOf(customInfoTable.getCId()));
  695. map.put(MapConstants.EMAIL, customInfoTable.getEmail());
  696. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  697. sysEmailSendEntity.setAddIp(customInfoTable.getModifyIp());
  698. sysEmailSendEntity.setAddTime(new Date());
  699. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CUSTOM_SUBMIT_VERIFICATION);
  700. String templateName;
  701. templateName = GlobalSystemEmailCodeConstants.CUSTOM_SUBMIT_VERIFICATION_NOTICE_NAME_CN;
  702. sysEmailSendEntity.setTemplateName(templateName);
  703. sysEmailSendEntity.setMap(map);
  704. emailService.sendEmail(sysEmailSendEntity);
  705. WebNoticeRecordAndEmailMqEntity mqEntity = new WebNoticeRecordAndEmailMqEntity(sysEmailSendEntity,
  706. customInfoTable.getCId(), customInfoTable.getId());
  707. mqSendService.send(Constants.WEB_NOTICE_PUT_RECORD, mqEntity);
  708. }
  709. /**
  710. * 构建修改密码发送邮件实体
  711. *
  712. * @param entity
  713. * @param customInfoTable
  714. * @return
  715. */
  716. private SysEmailSendEntity createUpdatePasswordSendEmailSysEmailEntity(CustomUpdatePasswordSendEmailEntity entity
  717. , CustomInfoTable customInfoTable) {
  718. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  719. String token =
  720. MD5Util.getMD5(AESUtil.encrypt(entity.getEmail() + Constants.LOGIN_LINK + UUIDUtil.getUUID() + Constants.LOGIN_LINK + customInfoTable.getId()));
  721. Date time = DateUtil.operationMinute(new Date(), Constants.EMAIL_TOKEN_TIME);
  722. Map<String, String> map = new HashMap<>(4);
  723. String name = CustomInfoUtil.getName(customInfoTable);
  724. map.put(MapConstants.TOKEN, token);
  725. map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTime(time));
  726. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  727. map.put(MapConstants.NAME, name);
  728. sysEmailSendEntity.setAddIp(entity.getIp());
  729. sysEmailSendEntity.setAddTime(entity.getTime());
  730. sysEmailSendEntity.setAddUser(customInfoTable.getAddUser());
  731. sysEmailSendEntity.setUsers(entity.getEmail());
  732. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.FORGET_PASSWORD);
  733. sysEmailSendEntity.setTemplateName(GlobalSystemEmailCodeConstants.CUSTOM_UPDATE_PASSWORD_SEND_CN);
  734. sysEmailSendEntity.setMap(map);
  735. return sysEmailSendEntity;
  736. }
  737. /**
  738. * @param customInfoTable
  739. */
  740. private void sendUpdatePasswordCompleteSendEmailSysEmailEntity(CustomInfoTable customInfoTable) throws ServiceException {
  741. SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
  742. Map<String, String> map = new HashMap<>(2);
  743. String name = CustomInfoUtil.getName(customInfoTable);
  744. map.put(MapConstants.NAME, name);
  745. map.put(MapConstants.DATE_TIME, DateUtil.formatTime());
  746. sysEmailSendEntity.setAddIp(customInfoTable.getModifyIp());
  747. sysEmailSendEntity.setAddTime(customInfoTable.getModifyTime());
  748. sysEmailSendEntity.setAddUser(customInfoTable.getAddUser());
  749. sysEmailSendEntity.setUsers(customInfoTable.getEmail());
  750. sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.FORGET_PASSWORD);
  751. sysEmailSendEntity.setTemplateName(GlobalSystemEmailCodeConstants.CUSTOM_UPDATE_PASSWORD_COMPLETE_SEND_CN);
  752. sysEmailSendEntity.setMap(map);
  753. emailService.sendEmail(sysEmailSendEntity);
  754. }
  755. private CustomInfoEntity transform(CustomInfoTable customInfoTable, UserInfoEntity ibInfoEntity,
  756. CustomInfoLoginEntity entity) {
  757. CustomInfoEntity infoEntity = new CustomInfoEntity();
  758. BeanUtils.copyProperties(customInfoTable, infoEntity);
  759. if (!Strings.isNullOrEmpty(customInfoTable.getAddressLine())) {
  760. infoEntity.setAddressLines(Arrays.asList(customInfoTable.getAddressLine().split("@#")));
  761. }
  762. infoEntity.setIbInfo(ibInfoEntity);
  763. /*infoEntity.setFiles(fileDtos);
  764. if (customBankTable != null) {
  765. infoEntity.setBankAddr(customBankTable.getBankAddr());
  766. infoEntity.setBankBack(customBankTable.getBankBack());
  767. infoEntity.setBankBranchName(customBankTable.getBankBranchName());
  768. infoEntity.setBankCode(customBankTable.getBankCode());
  769. infoEntity.setBankUname(customBankTable.getBankUname());
  770. infoEntity.setBankName(customBankTable.getBankName());
  771. infoEntity.setSwiftCode(customBankTable.getSwiftCode());
  772. infoEntity.setBankFront(customBankTable.getBankFront());
  773. infoEntity.setBankCardNum(customBankTable.getBankCardNum());
  774. }*/
  775. return infoEntity;
  776. }
  777. @Override
  778. @Transactional(rollbackFor = Exception.class)
  779. public void deleteInBatch(LongEntitys entitys) throws Exception {
  780. /*
  781. 删除基本信息
  782. */
  783. customInfoDao.deleteInBatch(entitys);
  784. /*
  785. 删除文件信息
  786. */
  787. List<String> files = customFileDao.getAllPathByCustomId(entitys);
  788. customFileDao.deleteInBatchByCustomId(entitys);
  789. /*
  790. 删除文件
  791. */
  792. FileProcessUtil.deleteFiles(files);
  793. }
  794. @Override
  795. public CustomInfoTable getById(Long id) throws ServiceException {
  796. CustomInfoTable customInfoTable = customInfoRepository.getFirstById(id);
  797. if (customInfoTable == null) {
  798. throw ServiceException.exception(Constants.INFO_NOT_FOUND);
  799. }
  800. return customInfoTable;
  801. }
  802. @Override
  803. @Transactional(rollbackFor = Exception.class)
  804. public void updatePosition(Long customId, Integer position) throws ServiceException {
  805. CustomInfoTable customInfoTable = customInfoRepository.findFirstById(customId);
  806. if (customInfoTable == null) {
  807. throw new ServiceException(Constants.USER_NOT_EXIST_ERROR);
  808. }
  809. customInfoTable.setPosition(position);
  810. customInfoRepository.save(customInfoTable);
  811. }
  812. @Override
  813. @Transactional(rollbackFor = Exception.class)
  814. public void validPosition(Long customId, Integer position) throws ServiceException {
  815. CustomInfoTable customInfoTable = customInfoRepository.getFirstById(customId);
  816. if (customInfoTable == null) {
  817. throw new ServiceException(Constants.USER_NOT_EXIST_ERROR);
  818. }
  819. if (customInfoTable.getPosition() == null) {
  820. customInfoTable.setPosition(CustomConstants.POSITION1);
  821. }
  822. if (!customInfoTable.getPosition().equals(position)) {
  823. throw new ServiceException(Constants.NOT_PERMIT);
  824. }
  825. }
  826. @Override
  827. public Integer getPosition(Long customId) throws ServiceException {
  828. CustomInfoTable customInfoTable = customInfoRepository.getFirstById(customId);
  829. if (customInfoTable == null) {
  830. throw new ServiceException(Constants.USER_NOT_EXIST_ERROR);
  831. }
  832. return customInfoTable.getPosition();
  833. }
  834. }