package com.kuyuntech.vrv.coreservice.service.core.impl;


import com.kuyuntech.vrv.coreapi.service.core.GatewayService;
import com.kuyuntech.vrv.coreservice.dao.core.GatewayDao;
import com.kuyuntech.vrv.coreservice.domain.core.Project;
import org.hibernate.criterion.MatchMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.GatewayBean;
import com.kuyuntech.vrv.coreservice.domain.core.Gateway;
import com.wbspool.fastboot.core.common.bean.PagerBean;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;

import org.hibernate.criterion.Order;
import com.wbspool.fastboot.core.jpa.service.AbstractFastbootService;
import org.springframework.util.Assert;

import static com.wbspool.fastboot.core.jpa.constant.DataValidTypes.*;


/**
 * GatewayService
 */
@Service("gatewayService")
@Transactional(rollbackFor = Exception.class, transactionManager = "vrvCoreServiceTransactionManager")
public class GatewayServiceImpl extends AbstractFastbootService<Gateway, GatewayBean> implements GatewayService {

    private static final Logger logger = LoggerFactory.getLogger(GatewayServiceImpl.class);

    @Autowired
    GatewayDao gatewayDao;


    @Override
    public GatewayBean add(GatewayBean gatewayBean) {

        Gateway gateway = new Gateway();
        beanToDomain(gatewayBean, gateway, "id");
        gatewayDao.save(gateway);
        domainToBean(gateway, gatewayBean);
        return gatewayBean;

    }

    @Override
    public GatewayBean update(GatewayBean gatewayBean) {

        if (gatewayBean == null) {
            return null;
        }


        Gateway gateway = gatewayDao.findByCodeAndValid(gatewayBean.getCode(), VALID);


        if (gateway == null) {
            return null;
        }

        beanToDomain(gatewayBean, gateway, "id", "code", "version", "createTime", "updateTime", "valid");

        gatewayDao.save(gateway);

        domainToBean(gateway, gatewayBean);

        return gatewayBean;
    }

    @Override
    public GatewayBean delete(GatewayBean gatewayBean) {

        if (gatewayBean == null) {
            return null;
        }


        Gateway gateway = gatewayDao.findByCodeAndValid(gatewayBean.getCode(), VALID);


        if (gateway == null) {
            return null;
        }


        gateway.setValid(INVALID);
        gatewayDao.save(gateway);


        domainToBean(gateway, gatewayBean);

        return gatewayBean;
    }

    @Override
    public GatewayBean find(GatewayBean gatewayBean) {
        if (gatewayBean == null) {
            return null;
        }


        Gateway gateway = gatewayDao.findByCodeAndValid(gatewayBean.getCode(), VALID);


        if (gateway == null) {
            return null;
        }
        BeanUtils.copyProperties(gateway, gatewayBean);
        return gatewayBean;
    }

    @Override
    public GatewayBean find(String code) {
        return this.find(GatewayBean.builder().code(code).build());
    }

    @Override
    public List<GatewayBean> findAll(GatewayBean gatewayBean, PagerBean pagerBean) {
        List<GatewayBean> gatewayBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(gatewayBean);
        List<Gateway> gateways = gatewayDao.findAll(detachedCriteria, pagerBean);
        for (Gateway gateway : gateways) {
            GatewayBean gatewayBeanTemp = new GatewayBean();
            domainToBean(gateway, gatewayBeanTemp);
            gatewayBeans.add(gatewayBeanTemp);
        }
        return gatewayBeans;
    }

    @Override
    public List<GatewayBean> findAll(GatewayBean gatewayBean) {
        return this.findAll(gatewayBean, null);
    }

    @Override
    public Long countAll(GatewayBean gatewayBean) {
        DetachedCriteria detachedCriteria = createListCriteria(gatewayBean);
        return gatewayDao.countAll(detachedCriteria);
    }

    @Override
    public PagerBean<GatewayBean> findPager(GatewayBean gatewayBean, PagerBean pagerBean) {
        List<GatewayBean> gatewayBeans = this.findAll(gatewayBean, pagerBean);
        Long count = this.countAll(gatewayBean);
        PagerBean<GatewayBean> gatewayPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, gatewayPageBean);
        gatewayPageBean.setItemCount(count.intValue());
        gatewayPageBean.init();
        gatewayPageBean.setItems(gatewayBeans);
        return gatewayPageBean;
    }

    /**
     * 创建列表查询条件
     *
     * @param gatewayBean 查询参数
     * @return
     */
    private static DetachedCriteria createListCriteria(GatewayBean gatewayBean) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Gateway.class);

        if (gatewayBean != null) {
            if (StringUtils.isNotBlank(gatewayBean.getNumber())) {
                detachedCriteria.add(Restrictions.like("number", gatewayBean.getNumber(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(gatewayBean.getIp())) {
                detachedCriteria.add(Restrictions.like("ip", gatewayBean.getIp(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(gatewayBean.getLocation())) {
                detachedCriteria.add(Restrictions.like("location", gatewayBean.getLocation(), MatchMode.ANYWHERE));
            }
        }

        detachedCriteria.add(Restrictions.eq("valid", VALID));
        return detachedCriteria;
    }


    @Override
    public void batchDelete(List<GatewayBean> gatewayBean) {
        if (gatewayBean.isEmpty()) {
            return;
        }
        List<String> codes = new ArrayList<>();
        gatewayBean.forEach((e) -> {
            codes.add(e.getCode());
        });
        batchDeleteByCodes(codes);

    }


    @Override
    public void batchDeleteByCodes(List<String> codes) {

        if (codes.isEmpty()) {
            return;
        }

        for (String code : codes) {
            Assert.notNull(this.delete(GatewayBean.builder().code(code).build()), "batch delete by codes error! ");
        }

    }

}
