package com.ebts.generator.service.impl;

import com.ebts.common.constant.ReturnConstants;
import com.ebts.common.core.entity.Result;
import com.ebts.common.utils.SecurityUtils;
import com.ebts.generator.dao.UniQueryDao;
import com.ebts.generator.entity.UniQuery;
import com.ebts.generator.service.UniQueryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 万能查询Service业务层处理
 *
 * @author binlin
 * @date 2021-01-30
 */
@Service
public class UniQueryServiceImpl implements UniQueryService {

    @Resource
    private UniQueryDao uniQueryDao;

    /**
     * 查询万能查询
     *
     * @param id 万能查询ID
     * @return 万能查询
     */
    @Override
    public Result<UniQuery> selectUniQueryById(Long id) {
        UniQuery uniQuery = uniQueryDao.selectUniQueryById(id);
        if (uniQuery != null) {
            return Result.ok(uniQuery);
        } else {
            return Result.info(ReturnConstants.RESULT_EMPTY);
        }
    }

    /**
     * 查询万能查询列表
     *
     * @param uniQuery 万能查询
     * @return 万能查询
     */
    @Override
    public Result<List<UniQuery>> selectUniQueryList(UniQuery uniQuery) {
        List<UniQuery> uniQueryList = uniQueryDao.selectUniQueryList(uniQuery);
        if (uniQueryList.size() > 0) {
            return Result.ok(uniQueryList);
        } else {
            return Result.info(ReturnConstants.RESULT_EMPTY);
        }
    }

    /**
     * 新增万能查询
     *
     * @param uniQuery 万能查询
     * @return 结果
     */
    @Override
    public Result<Integer> insertUniQuery(UniQuery uniQuery) {
        uniQuery.setCreateBy(SecurityUtils.getUserId());
        uniQueryDao.insertUniQuery(uniQuery);
        return Result.ok();
    }

    /**
     * 修改万能查询
     *
     * @param uniQuery 万能查询
     * @return 结果
     */
    @Override
    public Result<Integer> updateUniQuery(UniQuery uniQuery) {
        uniQuery.setUpdateBy(SecurityUtils.getUserId());
        uniQueryDao.updateUniQuery(uniQuery);
        return Result.ok();
    }

    /**
     * 批量删除万能查询
     *
     * @param ids 需要删除的万能查询ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Integer> deleteUniQueryByIds(Long[] ids) {
        uniQueryDao.deleteUniQueryByIds(ids);
        uniQueryDao.deleteUniQueryByUqIds(ids);
        return Result.ok();
    }

    /**
     * 删除万能查询信息
     *
     * @param id 万能查询ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Integer> deleteUniQueryById(Long id) {
        uniQueryDao.deleteUniQueryById(id);
        uniQueryDao.deleteUniQueryByUqId(id);
        return Result.ok();
    }
}