From f6a1bf1d9b19dd8b3750034048f3876d086db1f1 Mon Sep 17 00:00:00 2001
From: ZQN <364596817@qq.com>
Date: 星期三, 14 八月 2024 17:11:49 +0800
Subject: [PATCH] 企业二维码添加

---
 project-system/src/main/java/com/project/system/service/impl/SysCompanyServiceImpl.java |  285 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 267 insertions(+), 18 deletions(-)

diff --git a/project-system/src/main/java/com/project/system/service/impl/SysCompanyServiceImpl.java b/project-system/src/main/java/com/project/system/service/impl/SysCompanyServiceImpl.java
index 421b46c..08f121d 100644
--- a/project-system/src/main/java/com/project/system/service/impl/SysCompanyServiceImpl.java
+++ b/project-system/src/main/java/com/project/system/service/impl/SysCompanyServiceImpl.java
@@ -1,22 +1,31 @@
 package com.project.system.service.impl;
 
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import lombok.RequiredArgsConstructor;
-import org.springframework.beans.factory.annotation.Autowired;
 import cn.hutool.core.convert.Convert;
-import com.project.common.utils.StringUtils;
-import org.springframework.stereotype.Service;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
-import org.springframework.transaction.annotation.Transactional;
-import com.project.system.domain.vo.SysCompanyVo;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.project.common.constant.UserConstants;
+import com.project.common.core.domain.entity.SysUser;
+import com.project.common.core.domain.model.ImportError;
+import com.project.common.exception.base.BaseException;
+import com.project.common.utils.SecurityUtils;
+import com.project.common.utils.StringUtils;
+import com.project.common.utils.file.ImageUtils;
+import com.project.system.domain.SysCompany;
 import com.project.system.domain.bo.editBo.SysCompanyBo;
 import com.project.system.domain.bo.queryBo.SysCompanyQueryBo;
-import com.project.system.domain.SysCompany;
+import com.project.system.domain.vo.SysCompanyResultVo;
+import com.project.system.domain.vo.SysCompanyVo;
 import com.project.system.mapper.SysCompanyMapper;
 import com.project.system.service.ISysCompanyService;
+import com.project.system.service.ISysUserService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
@@ -29,7 +38,7 @@
 @RequiredArgsConstructor(onConstructor_ = @Autowired)
 public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements ISysCompanyService {
 
-
+    private final ISysUserService userService;
 
     @Override//鍒楄〃鏌ヨ
     public List<SysCompanyVo> queryList(SysCompanyQueryBo bo)
@@ -43,6 +52,11 @@
     public SysCompanyVo queryById(Long companyId)
     {
         SysCompany db = this.baseMapper.selectById(companyId);
+        if (StringUtils.isEmpty(db.getQrcodeImg())){
+            String qrcode = ImageUtils.createQrcode(db.getCompanyCode(), db.getCompanyName(), "2");
+            db.setQrcodeImg(qrcode);
+            this.updateById(db);
+        }
         return Convert.convert(SysCompanyVo.class , db);
     }
 
@@ -52,39 +66,267 @@
     public Boolean insertByBo(SysCompanyBo bo)
     {
         SysCompany add = Convert.convert(SysCompany.class, bo);
+        add.setCompanyStatus(1);
         validEntityBeforeSave(add);
-        return this.save(add);
+        String qrcode = ImageUtils.createQrcode(add.getCompanyCode(), add.getCompanyName(), "2");
+        add.setQrcodeImg(qrcode);
+
+        boolean save = this.save(add);
+        if (isAddUser(bo.getCompanyPhone())){
+            addCompanyUser(add, 0);
+        }
+        return save;
     }
 
     @Override//淇敼
     @Transactional
     public Boolean updateByBo(SysCompanyBo bo)
     {
+        SysCompany old = this.getById(bo.getCompanyId());
         SysCompany update = Convert.convert(SysCompany.class, bo);
         validEntityBeforeSave(update);
+        if (!old.getCompanyPhone().equals(update.getCompanyPhone())){
+            if (this.count(SysCompany::getCompanyPhone, old.getCompanyPhone())==1){
+                boolean delUser = delCompanyUser(old.getCompanyPhone());
+                if (!delUser) {
+                    throw new BaseException("鍘熶紒涓氱敤鎴锋竻闄ゅけ璐ワ紝璇疯仈绯荤鐞嗗憳锛�");
+                }
+            }
+            if (isAddUser(update.getCompanyPhone())){
+                addCompanyUser(update, update.getCompanyStatus()==0 ? 1 : 0);
+            }
+        }
         return this.updateById(update);
+    }
+
+    @Override//鐘舵�佸紑鍏�
+    @Transactional
+    public Boolean updStatus(SysCompanyBo bo)
+    {
+        if (bo.getCompanyId()==null || bo.getCompanyStatus()==null) {
+            throw new BaseException("鍙傛暟鏈夎锛岀姸鎬佷慨鏀瑰け璐ワ紒");
+        }
+        SysCompany company = this.getById(bo.getCompanyId());
+        company.setCompanyStatus(bo.getCompanyStatus());
+        updCompanyUserStatus(company.getCompanyPhone(), bo.getCompanyStatus());
+        return this.updateById(company);
     }
 
     @Override//鍒犻櫎
     @Transactional
     public Boolean deleteByIds(Collection<Long> ids)
     {
-
-        //鍋氫竴浜涗笟鍔′笂鐨勬牎楠�,鍒ゆ柇鏄惁闇�瑕佹牎楠�
-
+        for (Long id : ids) {
+            boolean b = delCompanyUser(this.getById(id).getCompanyPhone());
+            if (!b) {
+                throw new BaseException(String.format("鍘熶紒涓� %1$s 鐢ㄦ埛娓呴櫎澶辫触锛岃鑱旂郴绠$悊鍛橈紒", id));
+            }
+        }
         return this.removeByIds(ids);
+    }
+
+
+    @Override
+    public SysCompanyResultVo doImport(List<SysCompanyVo> list)
+    {
+        if (list==null || list.size()<1){
+            throw new BaseException("鏈幏鍙栧埌瀵煎叆淇℃伅锛�");
+        }
+        SysCompanyResultVo resultVo = new SysCompanyResultVo();
+        resultVo.setVoList(list);
+        return this.checkImport(resultVo);
+
+    }
+
+    @Override
+    public SysCompanyResultVo checkImport(SysCompanyResultVo resultVo)
+    {
+        if (resultVo==null || StringUtils.isEmpty(resultVo.getVoList())){
+            throw new BaseException("鏈幏鍙栧埌瀵煎叆淇℃伅锛�");
+        }
+        for (SysCompanyVo vo : resultVo.getVoList()) {
+            List<ImportError> errors = new ArrayList<>();
+            validEntityBeforeImport(vo, errors);
+            vo.setErrorList(errors);
+        }
+        return  resultVo;
+    }
+
+    @Override
+    @Transactional
+    public Boolean saveImport(SysCompanyResultVo resultVo)
+    {
+        if (resultVo==null || StringUtils.isEmpty(resultVo.getVoList())){
+            throw new BaseException("鏈幏鍙栧埌瀵煎叆淇℃伅锛�");
+        }
+        resultVo.getVoList().forEach(e-> {
+            if (StringUtils.isNotEmpty(e.getErrorList())) {
+                throw new BaseException("杩樻湁鏈鐞嗛敊璇俊鎭紒");
+            }
+        });
+        List<SysCompany> companies = Convert.toList(SysCompany.class, resultVo.getVoList());
+        companies.forEach(e->{
+            String qrcode = ImageUtils.createQrcode(e.getCompanyCode(), e.getCompanyName(), "2");
+            e.setQrcodeImg(qrcode);
+        });
+        boolean b = this.saveOrUpdateBatch(companies);
+        for (SysCompany company : companies) {
+            if (isAddUser(company.getCompanyPhone())){
+                addCompanyUser(company,1);
+            }
+        }
+        return b;
     }
 
 
 //-------------------------------------------------------------------------------------
 
-    //淇濆瓨鍓嶆牎楠�
+
+    /**
+     * 淇濆瓨鍓嶆牎楠�
+     * @param entity    鍙傛暟
+     */
     private void validEntityBeforeSave(SysCompany entity)
     {
-        //鍋氫竴浜涙暟鎹牎楠�,濡傚敮涓�绾︽潫
+        entity.setIsImport(0);
+
+        if (StringUtils.isEmpty(entity.getCompanyImg())){
+            throw new BaseException(String.format("%1$s锛岃涓婁紶钀ヤ笟鎵х収", entity.getCompanyName()));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyCode())){
+            throw new BaseException(String.format("%1$s锛岃濉啓浼佷笟绀句細缂栫爜锛�", entity.getCompanyName()));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyUser())){
+            throw new BaseException(String.format("%1$s锛岃濉啓浼佷笟鑱旂郴浜猴紒", entity.getCompanyName()));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyPhone())){
+            throw new BaseException(String.format("%1$s锛岃濉啓浼佷笟鑱旂郴浜虹數璇濓紒", entity.getCompanyName()));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyAddress())){
+            throw new BaseException(String.format("%1$s锛岃濉啓浼佷笟鍦板潃锛�", entity.getCompanyName()));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyName())){
+            throw new BaseException("璇峰~鍐欎紒涓氬悕锛�");
+        }
+        if (entity.getCompanyId()==null){ //鏂板
+            int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()));
+            if (codeCount>0){
+                throw new BaseException(String.format("%1$s锛屼紒涓氱ぞ浼氱紪鐮佸凡瀛樺湪锛�", entity.getCompanyName()));
+            }
+            int nameCount = this.count(lq().eq(SysCompany::getCompanyName, entity.getCompanyName()));
+            if (nameCount>0){
+                throw new BaseException(String.format("%1$s锛屼紒涓氬悕宸插瓨鍦紒", entity.getCompanyName()));
+            }
+            SysUser user = new SysUser();
+            user.setPhonenumber(entity.getCompanyPhone());
+        } else {
+            int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
+            if (codeCount>0){
+                throw new BaseException(String.format("%1$s锛屼紒涓氱ぞ浼氱紪鐮佸凡瀛樺湪锛�", entity.getCompanyName()));
+            }
+            int nameCount = this.count(lq().eq(SysCompany::getCompanyName, entity.getCompanyName()).ne(SysCompany::getCompanyId,entity.getCompanyId()));
+            if (nameCount>0){
+                throw new BaseException(String.format("%1$s锛屼紒涓氬悕宸插瓨鍦紒", entity.getCompanyName()));
+            }
+        }
     }
 
-    //鑾峰彇鏌ヨ鍙傛暟
+
+    private void validEntityBeforeImport(SysCompanyVo entity, List<ImportError> errorList)
+    {
+        entity.setIsImport(1);
+        if (StringUtils.isEmpty(entity.getCompanyName())){
+            errorList.add(new ImportError(0, "璇峰~鍐欎紒涓氬悕锛�"));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyCode())){
+            errorList.add(new ImportError(1, "璇峰~鍐欎紒涓氱ぞ浼氱紪鐮侊紒"));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyUser())){
+            errorList.add(new ImportError(2, "璇峰~鍐欎紒涓氳仈绯讳汉锛�"));
+        }
+        if (StringUtils.isEmpty(entity.getCompanyPhone())){
+            errorList.add(new ImportError(3, "璇峰~鍐欎紒涓氳仈绯讳汉鐢佃瘽锛�"));
+        } else {
+            if (entity.getCompanyPhone().length()!=11){
+                errorList.add(new ImportError(3, "璇峰~鍐欐纭墜鏈哄彿锛�"));
+            }
+        }
+        if (StringUtils.isEmpty(entity.getCompanyAddress())){
+            errorList.add(new ImportError(4, "璇峰~鍐欎紒涓氬湴鍧�锛�"));
+        }
+        int codeCount = this.count(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()));
+        if (codeCount>0){
+            errorList.add(new ImportError(1, "浼佷笟绀句細缂栫爜宸插瓨鍦紒"));
+        }
+        int nameCount = this.count(lq().eq(SysCompany::getCompanyName, entity.getCompanyName()));
+        if (nameCount>0){
+            errorList.add(new ImportError(0, "浼佷笟鍚嶅凡瀛樺湪锛�"));
+        }
+        entity.setCheckStatus(2);
+    }
+
+    /**
+     * 娣诲姞浼佷笟鐢ㄦ埛
+     * @param entity    浼佷笟淇℃伅
+     */
+    @Async
+    @Override
+    public void addCompanyUser(SysCompany entity, Integer isImport)
+    {
+
+        SysUser user = new SysUser();
+        if (isImport==1){
+            user.setStatus("0");
+        } else {
+            user.setStatus("1");
+        }
+        SysCompany one = this.getOne(lq().eq(SysCompany::getCompanyCode, entity.getCompanyCode()));
+        String phone = entity.getCompanyPhone();
+        user.setPhonenumber(phone);
+        user.setUserName(phone);
+        user.setNickName(entity.getCompanyUser());
+        user.setPassword(phone);
+        user.setDeptId(one.getCompanyId());
+        user.setUserType("02");
+        user.setRecommendUser(user.getPassword());
+        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user)))
+        {
+            return;
+        }
+        else if (StringUtils.isNotEmpty(user.getPhonenumber())
+                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
+        {
+            return;
+        }
+        else if (StringUtils.isNotEmpty(user.getEmail())
+                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
+        {
+            return;
+        }
+        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
+        userService.insertUser(user);
+    }
+
+    @Async
+    protected void updCompanyUserStatus(String phone, Integer status)
+    {
+        userService.update(userService.lu().set(SysUser::getStatus,status).eq(SysUser::getPhonenumber, phone));
+    }
+
+    /**
+     * 鍒犻櫎浼佷笟鐢ㄦ埛
+     * @param companyPhone    浼佷笟id
+     */
+    protected boolean delCompanyUser(String companyPhone)
+    {
+        return userService.deleteUserByCompanyPhone(companyPhone)>0;
+    }
+
+    /**
+     * 鑾峰彇鏌ヨ鍙傛暟
+     * @param bo    鍙傛暟
+     * @return  鏌ヨ鍙傛暟
+     */
     private QueryWrapper<SysCompany> getQw(SysCompanyQueryBo bo)
     {
         QueryWrapper<SysCompany> qw = Wrappers.query();
@@ -95,6 +337,8 @@
             qw.eq(StringUtils.isNotEmpty(bo.getCompanyPhone()), "company_phone", bo.getCompanyPhone());
             qw.eq(StringUtils.isNotEmpty(bo.getCompanyAddress()), "company_address", bo.getCompanyAddress());
             qw.eq(StringUtils.isNotEmpty(bo.getCompanyImg()), "company_img", bo.getCompanyImg());
+            qw.eq(StringUtils.isNotEmpty(bo.getCompanyType()), "company_type", bo.getCompanyType());
+            qw.eq(bo.getIsImport() != null, "is_import", bo.getIsImport());
             qw.eq(bo.getCompanyStatus() != null, "company_status", bo.getCompanyStatus());
             qw.eq(StringUtils.isNotEmpty(bo.getRegionCode()), "region_code", bo.getRegionCode());
             qw.like(StringUtils.isNotEmpty(bo.getRegionName()), "region_name", bo.getRegionName());
@@ -109,4 +353,9 @@
         }
         return qw;
     }
+
+    private Boolean isAddUser(String phone)
+    {
+       return userService.count(userService.lq().eq(SysUser::getUserName, phone).eq(SysUser::getDelFlag,0))==0;
+    }
 }

--
Gitblit v1.9.1