|
|
@@ -0,0 +1,256 @@
|
|
|
+package com.usky.vpp.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.usky.common.core.bean.CommonPage;
|
|
|
+import com.usky.common.core.exception.BusinessException;
|
|
|
+import com.usky.vpp.domain.VppCustomer;
|
|
|
+import com.usky.vpp.domain.VppCustomerAccess;
|
|
|
+import com.usky.vpp.domain.VppCustomerContact;
|
|
|
+import com.usky.vpp.mapper.VppCustomerAccessMapper;
|
|
|
+import com.usky.vpp.mapper.VppCustomerContactMapper;
|
|
|
+import com.usky.vpp.mapper.VppCustomerMapper;
|
|
|
+import com.usky.vpp.service.VppCustomerService;
|
|
|
+import com.usky.vpp.service.vo.CustomerAccessAuditRequest;
|
|
|
+import com.usky.vpp.service.vo.CustomerAccessRequest;
|
|
|
+import com.usky.vpp.service.vo.CustomerContactRequest;
|
|
|
+import com.usky.vpp.util.VppAuditHelper;
|
|
|
+import com.usky.vpp.util.VppPageHelper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 客户管理业务实现
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class VppCustomerServiceImpl implements VppCustomerService {
|
|
|
+
|
|
|
+ private static final int ACCESS_PENDING = 0;
|
|
|
+ private static final int ACCESS_PASSED = 1;
|
|
|
+ private static final int ACCESS_REJECTED = 2;
|
|
|
+ private static final int LIFECYCLE_PENDING = 1;
|
|
|
+ private static final int LIFECYCLE_ADMITTED = 2;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private VppCustomerMapper customerMapper;
|
|
|
+ @Autowired
|
|
|
+ private VppCustomerAccessMapper accessMapper;
|
|
|
+ @Autowired
|
|
|
+ private VppCustomerContactMapper contactMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public VppCustomerAccess submitAccess(CustomerAccessRequest request) {
|
|
|
+ validateAccessRequest(request);
|
|
|
+ VppCustomerAccess access = new VppCustomerAccess();
|
|
|
+ access.setApplyNo(VppAuditHelper.nextApplyNo());
|
|
|
+ access.setAccountNo(request.getAccountNo());
|
|
|
+ access.setCustomerName(request.getCustomerName());
|
|
|
+ access.setCustomerType(request.getCustomerType());
|
|
|
+ access.setPowerCompany(request.getPowerCompany());
|
|
|
+ access.setContractCapacity(request.getContractCapacity());
|
|
|
+ access.setBusinessLicenseUrl(request.getBusinessLicenseUrl());
|
|
|
+ access.setCreditStatus(request.getCreditStatus());
|
|
|
+ access.setAccessStatus(ACCESS_PENDING);
|
|
|
+ access.setApplyAt(LocalDateTime.now());
|
|
|
+ VppAuditHelper.fillCreate(access);
|
|
|
+ accessMapper.insert(access);
|
|
|
+ return access;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommonPage<VppCustomerAccess> pageAccess(Map<String, Object> params) {
|
|
|
+ Page<VppCustomerAccess> page = VppPageHelper.of(params);
|
|
|
+ LambdaQueryWrapper<VppCustomerAccess> wrapper = new LambdaQueryWrapper<VppCustomerAccess>()
|
|
|
+ .isNull(VppCustomerAccess::getDeletedAt)
|
|
|
+ .orderByDesc(VppCustomerAccess::getApplyAt);
|
|
|
+ if (params != null && params.get("accessStatus") != null) {
|
|
|
+ wrapper.eq(VppCustomerAccess::getAccessStatus, Integer.parseInt(params.get("accessStatus").toString()));
|
|
|
+ }
|
|
|
+ Page<VppCustomerAccess> result = accessMapper.selectPage(page, wrapper);
|
|
|
+ return toCommonPage(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void auditAccess(Long id, CustomerAccessAuditRequest request) {
|
|
|
+ VppCustomerAccess access = accessMapper.selectById(id);
|
|
|
+ if (access == null || access.getDeletedAt() != null) {
|
|
|
+ throw new BusinessException("准入申请不存在");
|
|
|
+ }
|
|
|
+ if (access.getAccessStatus() != ACCESS_PENDING) {
|
|
|
+ throw new BusinessException("当前状态不允许审核");
|
|
|
+ }
|
|
|
+ if (request.getPassed() == null) {
|
|
|
+ throw new BusinessException("审核结果不能为空");
|
|
|
+ }
|
|
|
+ access.setAuditOpinion(request.getAuditOpinion());
|
|
|
+ access.setAuditAt(LocalDateTime.now());
|
|
|
+ if (Boolean.TRUE.equals(request.getPassed())) {
|
|
|
+ access.setAccessStatus(ACCESS_PASSED);
|
|
|
+ VppCustomer customer = buildCustomerFromAccess(access);
|
|
|
+ VppAuditHelper.fillCreate(customer);
|
|
|
+ customerMapper.insert(customer);
|
|
|
+ access.setCustomerId(customer.getId());
|
|
|
+ } else {
|
|
|
+ access.setAccessStatus(ACCESS_REJECTED);
|
|
|
+ }
|
|
|
+ VppAuditHelper.fillUpdate(access);
|
|
|
+ accessMapper.updateById(access);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommonPage<VppCustomer> pageCustomer(Map<String, Object> params) {
|
|
|
+ Page<VppCustomer> page = VppPageHelper.of(params);
|
|
|
+ LambdaQueryWrapper<VppCustomer> wrapper = new LambdaQueryWrapper<VppCustomer>()
|
|
|
+ .isNull(VppCustomer::getDeletedAt)
|
|
|
+ .orderByDesc(VppCustomer::getCreateTime);
|
|
|
+ if (params != null) {
|
|
|
+ if (params.get("customerName") != null) {
|
|
|
+ wrapper.like(VppCustomer::getCustomerName, params.get("customerName").toString());
|
|
|
+ }
|
|
|
+ if (params.get("accountNo") != null) {
|
|
|
+ wrapper.eq(VppCustomer::getAccountNo, params.get("accountNo").toString());
|
|
|
+ }
|
|
|
+ if (params.get("lifecycleStatus") != null) {
|
|
|
+ wrapper.eq(VppCustomer::getLifecycleStatus, Integer.parseInt(params.get("lifecycleStatus").toString()));
|
|
|
+ }
|
|
|
+ if (params.get("customerType") != null) {
|
|
|
+ wrapper.eq(VppCustomer::getCustomerType, Integer.parseInt(params.get("customerType").toString()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return toCommonPage(customerMapper.selectPage(page, wrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public VppCustomer getCustomer(Long id) {
|
|
|
+ VppCustomer customer = customerMapper.selectById(id);
|
|
|
+ if (customer == null || customer.getDeletedAt() != null) {
|
|
|
+ throw new BusinessException("客户不存在");
|
|
|
+ }
|
|
|
+ return customer;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateCustomer(Long id, VppCustomer customer) {
|
|
|
+ VppCustomer existing = getCustomer(id);
|
|
|
+ customer.setId(existing.getId());
|
|
|
+ customer.setAccountNo(existing.getAccountNo());
|
|
|
+ customer.setCreateTime(existing.getCreateTime());
|
|
|
+ customer.setCreatedBy(existing.getCreatedBy());
|
|
|
+ VppAuditHelper.fillUpdate(customer);
|
|
|
+ customerMapper.updateById(customer);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommonPage<VppCustomerContact> listContact(Long customerId, Map<String, Object> params) {
|
|
|
+ getCustomer(customerId);
|
|
|
+ Page<VppCustomerContact> page = VppPageHelper.of(params);
|
|
|
+ LambdaQueryWrapper<VppCustomerContact> wrapper = new LambdaQueryWrapper<VppCustomerContact>()
|
|
|
+ .eq(VppCustomerContact::getCustomerId, customerId)
|
|
|
+ .isNull(VppCustomerContact::getDeletedAt)
|
|
|
+ .orderByDesc(VppCustomerContact::getIsPrimary);
|
|
|
+ return toCommonPage(contactMapper.selectPage(page, wrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public VppCustomerContact addContact(Long customerId, CustomerContactRequest request) {
|
|
|
+ getCustomer(customerId);
|
|
|
+ validateContactRequest(request);
|
|
|
+ VppCustomerContact contact = new VppCustomerContact();
|
|
|
+ contact.setCustomerId(customerId);
|
|
|
+ contact.setContactName(request.getContactName());
|
|
|
+ contact.setContactPhone(request.getContactPhone());
|
|
|
+ contact.setContactEmail(request.getContactEmail());
|
|
|
+ contact.setIsPrimary(request.getIsPrimary() == null ? 0 : request.getIsPrimary());
|
|
|
+ contact.setPosition(request.getPosition());
|
|
|
+ VppAuditHelper.fillCreate(contact);
|
|
|
+ contactMapper.insert(contact);
|
|
|
+ return contact;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateContact(Long customerId, Long contactId, CustomerContactRequest request) {
|
|
|
+ VppCustomerContact contact = getContact(customerId, contactId);
|
|
|
+ validateContactRequest(request);
|
|
|
+ contact.setContactName(request.getContactName());
|
|
|
+ contact.setContactPhone(request.getContactPhone());
|
|
|
+ contact.setContactEmail(request.getContactEmail());
|
|
|
+ if (request.getIsPrimary() != null) {
|
|
|
+ contact.setIsPrimary(request.getIsPrimary());
|
|
|
+ }
|
|
|
+ contact.setPosition(request.getPosition());
|
|
|
+ VppAuditHelper.fillUpdate(contact);
|
|
|
+ contactMapper.updateById(contact);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void deleteContact(Long customerId, Long contactId) {
|
|
|
+ VppCustomerContact contact = getContact(customerId, contactId);
|
|
|
+ contact.setDeletedAt(LocalDateTime.now());
|
|
|
+ VppAuditHelper.fillUpdate(contact);
|
|
|
+ contactMapper.updateById(contact);
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> CommonPage<T> toCommonPage(Page<T> page) {
|
|
|
+ return new CommonPage<>(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ private VppCustomerContact getContact(Long customerId, Long contactId) {
|
|
|
+ VppCustomerContact contact = contactMapper.selectById(contactId);
|
|
|
+ if (contact == null || contact.getDeletedAt() != null || !customerId.equals(contact.getCustomerId())) {
|
|
|
+ throw new BusinessException("联系人不存在");
|
|
|
+ }
|
|
|
+ return contact;
|
|
|
+ }
|
|
|
+
|
|
|
+ private VppCustomer buildCustomerFromAccess(VppCustomerAccess access) {
|
|
|
+ VppCustomer customer = new VppCustomer();
|
|
|
+ customer.setAccountNo(access.getAccountNo());
|
|
|
+ customer.setCustomerName(access.getCustomerName());
|
|
|
+ customer.setCustomerType(access.getCustomerType());
|
|
|
+ customer.setPowerCompany(access.getPowerCompany());
|
|
|
+ customer.setContractCapacity(access.getContractCapacity());
|
|
|
+ customer.setCreditStatus(access.getCreditStatus());
|
|
|
+ customer.setBusinessLicenseUrl(access.getBusinessLicenseUrl());
|
|
|
+ customer.setLifecycleStatus(LIFECYCLE_ADMITTED);
|
|
|
+ customer.setDrNotifyMinutes(30);
|
|
|
+ return customer;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateAccessRequest(CustomerAccessRequest request) {
|
|
|
+ if (request == null || !StringUtils.hasText(request.getAccountNo())) {
|
|
|
+ throw new BusinessException("电力户号不能为空");
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(request.getCustomerName())) {
|
|
|
+ throw new BusinessException("客户名称不能为空");
|
|
|
+ }
|
|
|
+ if (request.getCustomerType() == null) {
|
|
|
+ throw new BusinessException("客户类型不能为空");
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(request.getPowerCompany())) {
|
|
|
+ throw new BusinessException("供电公司不能为空");
|
|
|
+ }
|
|
|
+ if (request.getContractCapacity() == null) {
|
|
|
+ throw new BusinessException("用电容量不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateContactRequest(CustomerContactRequest request) {
|
|
|
+ if (request == null || !StringUtils.hasText(request.getContactName())) {
|
|
|
+ throw new BusinessException("联系人姓名不能为空");
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(request.getContactPhone())) {
|
|
|
+ throw new BusinessException("联系电话不能为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|