JcbServiceImpl.java 5.31 KB
Newer Older
谢恒's avatar
谢恒 committed
1 2 3 4 5 6
package com.hs.admin.service.impl;

import com.hs.admin.base.CodeMessageEnum;
import com.hs.admin.base.HsRuntimeException;
import com.hs.admin.base.PageResult;
import com.hs.admin.base.StateEnum;
谢恒's avatar
谢恒 committed
7
import com.hs.admin.bean.DataBase;
谢恒's avatar
谢恒 committed
8 9 10 11 12
import com.hs.admin.bean.Jcb;
import com.hs.admin.dao.JcbDao;
import com.hs.admin.dao.SysTaskDao;
import com.hs.admin.dto.JcbDto;
import com.hs.admin.service.JcbService;
谢恒's avatar
谢恒 committed
13
import com.hs.admin.util.DynamicDataBaseUtil;
谢恒's avatar
谢恒 committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author xieheng
 */
@Service
public class JcbServiceImpl implements JcbService {
    @Resource
    JcbDao jcbDao;
    @Resource
    SysTaskDao sysTaskDao;

    @Override
谢恒's avatar
谢恒 committed
34
    public Jcb saveJcb(JcbDto jcbDto) {
谢恒's avatar
谢恒 committed
35
        Integer id = jcbDto.getId();
谢恒's avatar
谢恒 committed
36
        String jcbId = jcbDto.getJcbId();
谢恒's avatar
谢恒 committed
37 38 39 40 41 42 43
        String jcbName = jcbDto.getJcbName();
        String remark = jcbDto.getRemark();
        String dbName = jcbDto.getDbName();
        String host = jcbDto.getHost();
        String port = jcbDto.getPort();
        String username = jcbDto.getDbUsername();
        String pwd = jcbDto.getDbPwd();
谢恒's avatar
谢恒 committed
44
        Integer type = ObjectUtils.isEmpty(jcbDto.getType()) ? 1 : jcbDto.getType();
谢恒's avatar
谢恒 committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        if (StringUtils.isEmpty(jcbId)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "租户编号不能为空");
        }
        if (StringUtils.isEmpty(jcbName)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "租户名称不能为空");
        }
        if (StringUtils.isEmpty(dbName)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "数据库表名称不能为空");
        }
        if (StringUtils.isEmpty(host)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "数据库连接不能为空");
        }
        if (StringUtils.isEmpty(port)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "数据库端口号不能为空");
        }
        if (StringUtils.isEmpty(username)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "数据库账号不能为空");
        }
        if (StringUtils.isEmpty(pwd)) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "数据库密码不能为空");
        }
谢恒's avatar
谢恒 committed
66 67 68 69 70 71 72 73
        DataBase dataBase = new DataBase();
        dataBase.setHost(host);
        dataBase.setPort(port);
        dataBase.setName(dbName);
        dataBase.setUsername(username);
        dataBase.setPassword(pwd);
        dataBase.setDbType(String.valueOf(type));
        checkDb(dataBase);
谢恒's avatar
谢恒 committed
74 75 76 77 78 79 80 81 82
        Jcb jcb = new Jcb();
        jcb.setJcbId(jcbId);
        jcb.setName(jcbName);
        jcb.setRemark(StringUtils.isEmpty(remark) ? "" : remark);
        jcb.setHost(host);
        jcb.setPort(port);
        jcb.setDbName(dbName);
        jcb.setDbUsername(username);
        jcb.setDbPwd(pwd);
谢恒's avatar
谢恒 committed
83
        jcb.setDbType(ObjectUtils.isEmpty(type) ? 1 : type);
谢恒's avatar
谢恒 committed
84
        if (id == null) {
谢恒's avatar
谢恒 committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            Jcb dbjcb = jcbDao.getCount(jcbId);
            if (dbjcb != null) {
                if (dbjcb.getState() != 3) {
                    throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "租户已经存在,不能重复添加");
                } else {
                    jcb.setGmtCreate(new Date());
                    jcb.setGmtModify(new Date());
                    jcb.setState(StateEnum.ENABLE.getCode());
                    jcb.setId(dbjcb.getId());
                    jcbDao.updateById(jcb);
                }
            } else {
                jcb.setState(StateEnum.ENABLE.getCode());
                jcb.setGmtCreate(new Date());
                jcb.setGmtModify(new Date());
                jcbDao.insert(jcb);
谢恒's avatar
谢恒 committed
101
            }
谢恒's avatar
谢恒 committed
102 103
        } else {
            jcb.setId(id);
谢恒's avatar
谢恒 committed
104
            jcbDao.updateById(jcb);
谢恒's avatar
谢恒 committed
105
        }
谢恒's avatar
谢恒 committed
106
        return jcb;
谢恒's avatar
谢恒 committed
107 108 109 110 111 112 113 114 115 116 117
    }

    /**
     * @description: 删除租户
     * @param: jcbId
     * @return: boolean
     * @author: XieHeng
     * @date: 2021/5/10 9:01 上午
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
谢恒's avatar
谢恒 committed
118 119
    public void delJcb(String id) {
        if (ObjectUtils.isEmpty(id)) {
谢恒's avatar
谢恒 committed
120 121 122
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "参数错误");
        }
        //校验是否有存在正常的任务。否则不能删除租户
谢恒's avatar
谢恒 committed
123 124 125
        int i = jcbDao.delJcb(id);
        if (i != 1) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "删除失败");
谢恒's avatar
谢恒 committed
126
        }
谢恒's avatar
谢恒 committed
127
        sysTaskDao.deleteByJcbId(id);
谢恒's avatar
谢恒 committed
128 129 130 131
    }

    @Override
    public PageResult<Jcb> getJcbList(Integer pageNum, Integer pageSize) {
谢恒's avatar
谢恒 committed
132 133
        Integer index = (pageNum - 1) * pageSize;
        Long totalCount = jcbDao.getTotalCount();
谢恒's avatar
谢恒 committed
134
        List<Jcb> jcbList = jcbDao.findPage(index, pageSize);
谢恒's avatar
谢恒 committed
135
        return new PageResult<>(totalCount, jcbList);
谢恒's avatar
谢恒 committed
136 137
    }

谢恒's avatar
谢恒 committed
138 139 140 141 142 143 144 145
    @Override
    public void checkDb(DataBase dataBase) {
        boolean b = DynamicDataBaseUtil.checkDataBaseConnection(dataBase);
        if (!b) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "数据库校验失败");
        }
    }

谢恒's avatar
谢恒 committed
146
}