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


import com.kuyuntech.vrv.coreapi.service.core.DeviceParamService;
import com.kuyuntech.vrv.coreservice.dao.core.DeviceParamDao;
import com.kuyuntech.vrv.coreservice.domain.core.DeviceParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.DeviceParamBean;
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.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import java.util.ArrayList;

import com.wbspool.fastboot.core.jpa.service.AbstractFastbootService;
import org.springframework.util.Assert;
import static com.wbspool.fastboot.core.jpa.constant.DataValidTypes.*;





/**
* DeviceParamService
*
*/
@Service("deviceParamService")
@Transactional(rollbackFor = Exception.class,transactionManager = "vrvCoreServiceTransactionManager")
public class DeviceParamServiceImpl extends AbstractFastbootService<DeviceParam,DeviceParamBean> implements DeviceParamService {

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

    @Autowired
    DeviceParamDao deviceParamDao;

    

    @Override
    public DeviceParamBean add(DeviceParamBean deviceParamBean) {

        DeviceParam deviceParam = new DeviceParam();
        beanToDomain(deviceParamBean,deviceParam,"id");
        deviceParamDao.save(deviceParam);
        domainToBean(deviceParam,deviceParamBean);
        return deviceParamBean;

    }

    @Override
    public DeviceParamBean update(DeviceParamBean deviceParamBean) {

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

        
        DeviceParam deviceParam = deviceParamDao.findByCodeAndValid(deviceParamBean.getCode(),VALID);
        

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

        beanToDomain(deviceParamBean,deviceParam,"id","code","version","createTime","updateTime","valid");

        deviceParamDao.save(deviceParam);

        domainToBean(deviceParam,deviceParamBean);

        return deviceParamBean;
    }

    @Override
    public DeviceParamBean delete(DeviceParamBean deviceParamBean) {

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

        
        DeviceParam deviceParam = deviceParamDao.findByCodeAndValid(deviceParamBean.getCode(),VALID);
        


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

        
        deviceParam.setValid(INVALID);
        deviceParamDao.save(deviceParam);
        

        domainToBean(deviceParam,deviceParamBean);

        return deviceParamBean;
    }

    @Override
    public DeviceParamBean find(DeviceParamBean deviceParamBean) {
        if(deviceParamBean == null){
            return null ;
        }

        
        DeviceParam deviceParam = deviceParamDao.findByCodeAndValid(deviceParamBean.getCode(),VALID);
        


        if(deviceParam == null){
            return null ;
        }
        BeanUtils.copyProperties(deviceParam,deviceParamBean);
        return deviceParamBean;
    }

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

    @Override
    public List<DeviceParamBean> findAll(DeviceParamBean deviceParamBean, PagerBean pagerBean) {
        List<DeviceParamBean> deviceParamBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(deviceParamBean);
        List<DeviceParam> deviceParams = deviceParamDao.findAll(detachedCriteria,pagerBean);
        for (DeviceParam deviceParam : deviceParams) {
            DeviceParamBean deviceParamBeanTemp = new DeviceParamBean();
            domainToBean(deviceParam,deviceParamBeanTemp);
            deviceParamBeans.add(deviceParamBeanTemp);
        }
        return deviceParamBeans;
    }

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

    @Override
     public Long countAll(DeviceParamBean deviceParamBean) {
        DetachedCriteria detachedCriteria = createListCriteria(deviceParamBean);
        return deviceParamDao.countAll(detachedCriteria);
    }

     @Override
     public PagerBean<DeviceParamBean> findPager(DeviceParamBean deviceParamBean, PagerBean pagerBean) {
        List<DeviceParamBean> deviceParamBeans = this.findAll(deviceParamBean, pagerBean);
        Long count = this.countAll(deviceParamBean);
        PagerBean<DeviceParamBean> deviceParamPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, deviceParamPageBean);
        deviceParamPageBean.setItemCount(count.intValue());
        deviceParamPageBean.init();
        deviceParamPageBean.setItems(deviceParamBeans);
        return  deviceParamPageBean ;
     }

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



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

    }

    


}
