package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysTableSet;
import com.ruoyi.system.domain.SysTableSetQuery;
import com.ruoyi.system.mapper.SysTableSetMapper;
import com.ruoyi.system.service.ISysTableSetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 列宽修改Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-30
 */
@Service
public class SysTableSetServiceImpl implements ISysTableSetService {
    @Autowired
    private SysTableSetMapper sysTableSetMapper;

    @Override
    public List<SysTableSet> queryList(SysTableSet sysTableSet) {
        return sysTableSetMapper.selectSysTableSetList(sysTableSet);
    }

    @Override
    @Transactional
    public AjaxResult saveTableSet(SysTableSetQuery sysTableSetQuery) {
        boolean flag = false;
        //查询用户对应的表信息是否存在
        SysTableSet stableSet = new SysTableSet();
        stableSet.setUserId(sysTableSetQuery.getUserId());
        stableSet.setTableName(sysTableSetQuery.getTableName());
        List<SysTableSet> sysTableSetList = sysTableSetMapper.selectSysTableSetList(stableSet);
        if (CollectionUtils.isNotEmpty(sysTableSetList)) {
            if (sysTableSetMapper.deleteSysTableSet(stableSet) <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("删除数据失败");
            }
        }
        for (SysTableSet tableSet : sysTableSetQuery.getSysTableSetList()) {
            tableSet.setTableName(sysTableSetQuery.getTableName());
            tableSet.setUserId(sysTableSetQuery.getUserId());
            if (sysTableSetMapper.insertSysTableSet(tableSet) <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("新增数据失败");
            }
        }
        return AjaxResult.success();
    }

    @Override
    public List<SysTableSet> selectTableSet(SysTableSet sysTableSet) {
        List<SysTableSet> list = new ArrayList<>();
        if (sysTableSet.getUserId() != null && StringUtils.isNoneEmpty(sysTableSet.getTableName())) {
            SysTableSet stableSet = new SysTableSet();
            stableSet.setUserId(sysTableSet.getUserId());
            stableSet.setTableName(sysTableSet.getTableName());
            list = sysTableSetMapper.selectSysTableSetList(stableSet);
        }
        return list;
    }

    @Override
    @Transactional
    public boolean updateTableSetMessage(SysTableSet sysTableSet) {
        Integer integer = sysTableSetMapper.updateSysTableSet(sysTableSet);
        if (Objects.equals(integer, 0) || integer < 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 重置当前用户、当前模块的信息
     * @param sysTableSet   条件
     * @return  结果
     */
    @Override
    @Transactional
    public AjaxResult resetModule(SysTableSet sysTableSet) {
        sysTableSetMapper.deleteSysTableSet(sysTableSet);
        return AjaxResult.success();
    }
}
