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


import com.kuyuntech.vrv.coreapi.service.core.DeviceAlarmRepairRecordService;
import com.kuyuntech.vrv.coreservice.dao.core.DeviceAlarmRepairRecordDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.DeviceAlarmRepairRecordBean;
import com.kuyuntech.vrv.coreservice.domain.core.DeviceAlarmRepairRecord;
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.*;





/**
* DeviceAlarmRepairRecordService
*
*/
@Service("deviceAlarmRepairRecordService")
@Transactional(rollbackFor = Exception.class,transactionManager = "vrvCoreServiceTransactionManager")
public class DeviceAlarmRepairRecordServiceImpl extends AbstractFastbootService<DeviceAlarmRepairRecord,DeviceAlarmRepairRecordBean> implements DeviceAlarmRepairRecordService {

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

    @Autowired
    DeviceAlarmRepairRecordDao deviceAlarmRepairRecordDao;

    

    @Override
    public DeviceAlarmRepairRecordBean add(DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBean) {

        DeviceAlarmRepairRecord deviceAlarmRepairRecord = new DeviceAlarmRepairRecord();
        beanToDomain(deviceAlarmRepairRecordBean,deviceAlarmRepairRecord,"id");
        deviceAlarmRepairRecordDao.save(deviceAlarmRepairRecord);
        domainToBean(deviceAlarmRepairRecord,deviceAlarmRepairRecordBean);
        return deviceAlarmRepairRecordBean;

    }

    @Override
    public DeviceAlarmRepairRecordBean update(DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBean) {

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

        
        DeviceAlarmRepairRecord deviceAlarmRepairRecord = deviceAlarmRepairRecordDao.findByCodeAndValid(deviceAlarmRepairRecordBean.getCode(),VALID);
        

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

        beanToDomain(deviceAlarmRepairRecordBean,deviceAlarmRepairRecord,"id","code","version","createTime","updateTime","valid");

        deviceAlarmRepairRecordDao.save(deviceAlarmRepairRecord);

        domainToBean(deviceAlarmRepairRecord,deviceAlarmRepairRecordBean);

        return deviceAlarmRepairRecordBean;
    }

    @Override
    public DeviceAlarmRepairRecordBean delete(DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBean) {

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

        
        DeviceAlarmRepairRecord deviceAlarmRepairRecord = deviceAlarmRepairRecordDao.findByCodeAndValid(deviceAlarmRepairRecordBean.getCode(),VALID);
        


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

        
        deviceAlarmRepairRecord.setValid(INVALID);
        deviceAlarmRepairRecordDao.save(deviceAlarmRepairRecord);
        

        domainToBean(deviceAlarmRepairRecord,deviceAlarmRepairRecordBean);

        return deviceAlarmRepairRecordBean;
    }

    @Override
    public DeviceAlarmRepairRecordBean find(DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBean) {
        if(deviceAlarmRepairRecordBean == null){
            return null ;
        }

        
        DeviceAlarmRepairRecord deviceAlarmRepairRecord = deviceAlarmRepairRecordDao.findByCodeAndValid(deviceAlarmRepairRecordBean.getCode(),VALID);
        


        if(deviceAlarmRepairRecord == null){
            return null ;
        }
        BeanUtils.copyProperties(deviceAlarmRepairRecord,deviceAlarmRepairRecordBean);
        return deviceAlarmRepairRecordBean;
    }

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

    @Override
    public List<DeviceAlarmRepairRecordBean> findAll(DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBean, PagerBean pagerBean) {
        List<DeviceAlarmRepairRecordBean> deviceAlarmRepairRecordBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(deviceAlarmRepairRecordBean);
        List<DeviceAlarmRepairRecord> deviceAlarmRepairRecords = deviceAlarmRepairRecordDao.findAll(detachedCriteria,pagerBean);
        for (DeviceAlarmRepairRecord deviceAlarmRepairRecord : deviceAlarmRepairRecords) {
            DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBeanTemp = new DeviceAlarmRepairRecordBean();
            domainToBean(deviceAlarmRepairRecord,deviceAlarmRepairRecordBeanTemp);
            deviceAlarmRepairRecordBeans.add(deviceAlarmRepairRecordBeanTemp);
        }
        return deviceAlarmRepairRecordBeans;
    }

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

    @Override
     public Long countAll(DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBean) {
        DetachedCriteria detachedCriteria = createListCriteria(deviceAlarmRepairRecordBean);
        return deviceAlarmRepairRecordDao.countAll(detachedCriteria);
    }

     @Override
     public PagerBean<DeviceAlarmRepairRecordBean> findPager(DeviceAlarmRepairRecordBean deviceAlarmRepairRecordBean, PagerBean pagerBean) {
        List<DeviceAlarmRepairRecordBean> deviceAlarmRepairRecordBeans = this.findAll(deviceAlarmRepairRecordBean, pagerBean);
        Long count = this.countAll(deviceAlarmRepairRecordBean);
        PagerBean<DeviceAlarmRepairRecordBean> deviceAlarmRepairRecordPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, deviceAlarmRepairRecordPageBean);
        deviceAlarmRepairRecordPageBean.setItemCount(count.intValue());
        deviceAlarmRepairRecordPageBean.init();
        deviceAlarmRepairRecordPageBean.setItems(deviceAlarmRepairRecordBeans);
        return  deviceAlarmRepairRecordPageBean ;
     }

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



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

    }

    


}
