package com.sunyard.ssp.modules.sysconf.cipherunit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunyard.ssp.common.exception.SspwebException;
import com.sunyard.ssp.modules.sysconf.cipherunit.entity.ScCipherMachine;
import com.sunyard.ssp.modules.sysconf.cipherunit.entity.ScCipherMachineVo;
import com.sunyard.ssp.modules.sysconf.cipherunit.mapper.ScCipherMachineMapper;
import com.sunyard.ssp.modules.sysconf.cipherunit.service.ScCipherMachineService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author admin
* @description 针对表【SC_CIPHER_MACHINE(密码部件配置)】的数据库操作Service实现
* @createDate 2024-05-16 14:20:57
*/
@Service
public class ScCipherMachineServiceImpl extends ServiceImpl<ScCipherMachineMapper, ScCipherMachine>
    implements ScCipherMachineService{

    @Override
    public ScCipherMachineVo queryCipherMachine() {
        ScCipherMachine scCipherMachine = baseMapper.selectOne(new QueryWrapper<>());
        ScCipherMachineVo scCipherMachineVo = new ScCipherMachineVo();
        BeanUtil.copyProperties(scCipherMachine,scCipherMachineVo);
        return scCipherMachineVo;
    }

    @Override
    public ScCipherMachine addCipherMachine(ScCipherMachine scCipherMachine) {
        if (Objects.isNull(scCipherMachine)){
            throw new SspwebException("必要参数不能为空");
        }
        Long count = baseMapper.selectCount(new QueryWrapper<>());
        if (count>=1){
            throw new SspwebException("已经存在密码部件配置");
        }
        int insert = baseMapper.insert(scCipherMachine);
        if (insert<=0){
            throw new SspwebException("添加失败");
        }
        return scCipherMachine;
    }

    @Override
    public ScCipherMachine editCipherMachine(ScCipherMachine scCipherMachine) {
        if (Objects.isNull(scCipherMachine)){
            throw new SspwebException("必要参数不能为空");
        }
        Long count = baseMapper.selectCount(new QueryWrapper<ScCipherMachine>().eq("id",scCipherMachine.getId()));
        if (count<1){
            throw new SspwebException("不存在密码部件配置");
        }
        int update = baseMapper.updateById(scCipherMachine);
        if (update<=0){
            throw new SspwebException("修改失败");
        }
        return scCipherMachine;
    }

    @Override
    public void deleteCipherMachine(Long id) {
        if (Objects.isNull(id)){
            throw new SspwebException("必要参数不能为空");
        }
        int i = baseMapper.deleteById(id);
        if (i<1){
            throw new SspwebException("删除失败");
        }
    }
}




