JcbServiceImpl.java 4.49 KB
Newer Older
谢恒's avatar
谢恒 committed
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
package com.hs.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.admin.base.CodeMessageEnum;
import com.hs.admin.base.HsRuntimeException;
import com.hs.admin.base.PageResult;
import com.hs.admin.base.StateEnum;
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;
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
    public boolean saveJcb(JcbDto jcbDto) {
        Integer id = jcbDto.getId();
        Integer jcbId = jcbDto.getJcbId();
        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();
        Integer type = 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 74 75 76
        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);
        jcb.setType(ObjectUtils.isEmpty(type) ? 1 : type);
        if (id == null) {
谢恒's avatar
谢恒 committed
77 78 79 80
            int count = jcbDao.getCount(jcbId);
            if (count > 0) {
                throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "租户已经存在,不能重复添加");
            }
谢恒's avatar
谢恒 committed
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
            jcb.setState(StateEnum.ENABLE.getCode());
            jcb.setGmtCreate(new Date());
            return jcbDao.insert(jcb) == 1;
        } else {
            jcb.setId(id);
            return jcbDao.updateById(jcb) == 1;
        }
    }

    /**
     * @description: 删除租户
     * @param: jcbId
     * @return: boolean
     * @author: XieHeng
     * @date: 2021/5/10 9:01 上午
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delJcb(Integer id) {
        if (id == null || id < 1) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "参数错误");
        }
        //校验是否有存在正常的任务。否则不能删除租户
        int i = jcbDao.checkTask(id);
        if (i > 0) {
            throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "租户存在任务,不能删除");
        }
        if (jcbDao.delJcb(id) == 1) {
            sysTaskDao.delTaskById(id);
            return true;
        }
        throw new HsRuntimeException(CodeMessageEnum.REQUEST_ERROR.getCode(), "删除失败");
    }

    @Override
    public PageResult<Jcb> getJcbList(Integer pageNum, Integer pageSize) {
谢恒's avatar
谢恒 committed
117 118 119 120
        Integer index = (pageNum - 1) * pageSize;
        Long totalCount = jcbDao.getTotalCount();
        List<Jcb> jcbList = jcbDao.findAllByState(index, pageSize);
        return new PageResult<>(totalCount, jcbList);
谢恒's avatar
谢恒 committed
121 122 123
    }

}