package com.dxhy.admin.sys.service.impl;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.admin.sys.dao.SysIpDao;
import com.dxhy.admin.sys.entity.SysIpEntity;
import com.dxhy.admin.sys.service.SysIpService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@Service("sysIpService")
@Slf4j
public class SysIpServiceImpl extends ServiceImpl<SysIpDao, SysIpEntity> implements SysIpService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String deptId = (String)params.get("deptId");

        IPage<SysIpEntity> page = this.page(new Query<SysIpEntity>().getPage(params),
            new QueryWrapper<SysIpEntity>().eq(StringUtils.isNotBlank(deptId), "dept_id", deptId));

        return new PageUtils(page);
    }

    @Override
    public void saveIp(SysIpEntity ip) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        ip.setCreateTime(DateUtil.date());
        ip.setUpdateTime(DateUtil.date());
        Aops.getSelf(this).insert(ip);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(SysIpEntity ip) {
        ip.setCreateTime(new Date());
        baseMapper.insert(ip);
    }

    @Override
    public void update(SysIpEntity ip) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        ip.setUpdateTime(DateUtil.date());
        Aops.getSelf(this).updateId(ip);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateId(SysIpEntity ip) {
        return baseMapper.updateById(ip);
    }

    @Override
    public void deleteBatch(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteId(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteId(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public SysIpEntity queryById(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectById(id);
    }

}
