package com.dxhy.manage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

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

import com.alibaba.fastjson.JSON;
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.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 com.dxhy.manage.dao.SysIpDao;
import com.dxhy.manage.entity.SysDeptEntity;
import com.dxhy.manage.entity.SysIpEntity;
import com.dxhy.manage.service.SysDeptService;
import com.dxhy.manage.service.SysIpService;

/**
 * @author jiaohongyang
 */
@Service
public class SysIpServiceImpl extends ServiceImpl<SysIpDao, SysIpEntity> implements SysIpService {
    @Resource
    private SysDeptService sysDeptService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String deptId = (String)params.get("deptId");
        String company = (String)params.get("company");
        String ip = (String)params.get("ip");
        IPage<SysIpEntity> page = this.page(new Query<SysIpEntity>().getPage(params),
            new QueryWrapper<SysIpEntity>().eq(StringUtils.isNotBlank(deptId), "dept_id", deptId)
                .eq(StringUtils.isNotBlank(company), "company", company).like(StringUtils.isNotBlank(ip), "ip", ip));

        return new PageUtils(page);
    }

    @Override
    public void saveIp(SysIpEntity ip) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        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);
        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);
    }

    public void saveDeptList(List<SysIpEntity> ipList) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).insertDeptList(ipList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertDeptList(List<SysIpEntity> ipList) {
        saveOrUpdateBatch(ipList);
    }

    @Override
    public int uploadIp(String dataList) {
        List<SysIpEntity> list = new ArrayList<>();
        try {
            List<SysIpEntity> ipList = JSON.parseArray(dataList, SysIpEntity.class);
            if (ipList != null && ipList.size() > 0) {
                for (SysIpEntity sysIpEntity : ipList) {
                    // 根基 集团编码获取集团信息
                    SysDeptEntity deptCompany = sysDeptService.queryDept(sysIpEntity.getCompany(), "1");
                    if (deptCompany == null) {
                        return 1;
                    }
                    sysIpEntity.setDeptId(deptCompany.getDeptId());
                    sysIpEntity.setCreateTime(new Date());
                    list.add(sysIpEntity);
                }
            }
            if (list.size() > 0) {
                this.saveDeptList(list);
            }
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }

}
