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


import com.kuyuntech.vrv.coreapi.service.core.DeviceAlarmConfigService;
import com.kuyuntech.vrv.coreservice.dao.core.DeviceAlarmConfigDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.DeviceAlarmConfigBean;
import com.kuyuntech.vrv.coreservice.domain.core.DeviceAlarmConfig;
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.*;





/**
* DeviceAlarmConfigService
*
*/
@Service("deviceAlarmConfigService")
@Transactional(rollbackFor = Exception.class,transactionManager = "vrvCoreServiceTransactionManager")
public class DeviceAlarmConfigServiceImpl extends AbstractFastbootService<DeviceAlarmConfig,DeviceAlarmConfigBean> implements DeviceAlarmConfigService {

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

    @Autowired
    DeviceAlarmConfigDao deviceAlarmConfigDao;

    

    @Override
    public DeviceAlarmConfigBean add(DeviceAlarmConfigBean deviceAlarmConfigBean) {

        DeviceAlarmConfig deviceAlarmConfig = new DeviceAlarmConfig();
        beanToDomain(deviceAlarmConfigBean,deviceAlarmConfig,"id");
        deviceAlarmConfigDao.save(deviceAlarmConfig);
        domainToBean(deviceAlarmConfig,deviceAlarmConfigBean);
        return deviceAlarmConfigBean;

    }

    @Override
    public DeviceAlarmConfigBean update(DeviceAlarmConfigBean deviceAlarmConfigBean) {

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

        
        DeviceAlarmConfig deviceAlarmConfig = deviceAlarmConfigDao.findByCodeAndValid(deviceAlarmConfigBean.getCode(),VALID);
        

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

        beanToDomain(deviceAlarmConfigBean,deviceAlarmConfig,"id","code","version","createTime","updateTime","valid");

        deviceAlarmConfigDao.save(deviceAlarmConfig);

        domainToBean(deviceAlarmConfig,deviceAlarmConfigBean);

        return deviceAlarmConfigBean;
    }

    @Override
    public DeviceAlarmConfigBean delete(DeviceAlarmConfigBean deviceAlarmConfigBean) {

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

        
        DeviceAlarmConfig deviceAlarmConfig = deviceAlarmConfigDao.findByCodeAndValid(deviceAlarmConfigBean.getCode(),VALID);
        


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

        
        deviceAlarmConfig.setValid(INVALID);
        deviceAlarmConfigDao.save(deviceAlarmConfig);
        

        domainToBean(deviceAlarmConfig,deviceAlarmConfigBean);

        return deviceAlarmConfigBean;
    }

    @Override
    public DeviceAlarmConfigBean find(DeviceAlarmConfigBean deviceAlarmConfigBean) {
        if(deviceAlarmConfigBean == null){
            return null ;
        }

        
        DeviceAlarmConfig deviceAlarmConfig = deviceAlarmConfigDao.findByCodeAndValid(deviceAlarmConfigBean.getCode(),VALID);
        


        if(deviceAlarmConfig == null){
            return null ;
        }
        BeanUtils.copyProperties(deviceAlarmConfig,deviceAlarmConfigBean);
        return deviceAlarmConfigBean;
    }

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

    @Override
    public List<DeviceAlarmConfigBean> findAll(DeviceAlarmConfigBean deviceAlarmConfigBean, PagerBean pagerBean) {
        List<DeviceAlarmConfigBean> deviceAlarmConfigBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(deviceAlarmConfigBean);
        List<DeviceAlarmConfig> deviceAlarmConfigs = deviceAlarmConfigDao.findAll(detachedCriteria,pagerBean);
        for (DeviceAlarmConfig deviceAlarmConfig : deviceAlarmConfigs) {
            DeviceAlarmConfigBean deviceAlarmConfigBeanTemp = new DeviceAlarmConfigBean();
            domainToBean(deviceAlarmConfig,deviceAlarmConfigBeanTemp);
            deviceAlarmConfigBeans.add(deviceAlarmConfigBeanTemp);
        }
        return deviceAlarmConfigBeans;
    }

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

    @Override
     public Long countAll(DeviceAlarmConfigBean deviceAlarmConfigBean) {
        DetachedCriteria detachedCriteria = createListCriteria(deviceAlarmConfigBean);
        return deviceAlarmConfigDao.countAll(detachedCriteria);
    }

     @Override
     public PagerBean<DeviceAlarmConfigBean> findPager(DeviceAlarmConfigBean deviceAlarmConfigBean, PagerBean pagerBean) {
        List<DeviceAlarmConfigBean> deviceAlarmConfigBeans = this.findAll(deviceAlarmConfigBean, pagerBean);
        Long count = this.countAll(deviceAlarmConfigBean);
        PagerBean<DeviceAlarmConfigBean> deviceAlarmConfigPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, deviceAlarmConfigPageBean);
        deviceAlarmConfigPageBean.setItemCount(count.intValue());
        deviceAlarmConfigPageBean.init();
        deviceAlarmConfigPageBean.setItems(deviceAlarmConfigBeans);
        return  deviceAlarmConfigPageBean ;
     }

     /**
      * 创建列表查询条件
      * @param deviceAlarmConfigBean 查询参数
      * @return
      */
     private static DetachedCriteria createListCriteria(DeviceAlarmConfigBean deviceAlarmConfigBean){
         DetachedCriteria detachedCriteria = DetachedCriteria.forClass(DeviceAlarmConfig.class);
         detachedCriteria.add(Restrictions.eq("valid",VALID));
         return  detachedCriteria;
     }



    @Override
    public void batchDelete(List<DeviceAlarmConfigBean> deviceAlarmConfigBean) {
        if(deviceAlarmConfigBean.isEmpty()){
            return ;
        }
        List<String> codes = new ArrayList<>();
       deviceAlarmConfigBean.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(DeviceAlarmConfigBean.builder().code(code).build()),"batch delete by codes error! ");
        }

    }

    


}
