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


import com.kuyuntech.vrv.coreapi.service.core.DeviceAlarmRecordService;
import com.kuyuntech.vrv.coreservice.dao.core.DeviceAlarmRecordDao;
import org.hibernate.criterion.MatchMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.DeviceAlarmRecordBean;
import com.kuyuntech.vrv.coreservice.domain.core.DeviceAlarmRecord;
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.*;


/**
 * DeviceAlarmRecordService
 */
@Service("deviceAlarmRecordService")
@Transactional(rollbackFor = Exception.class, transactionManager = "vrvCoreServiceTransactionManager")
public class DeviceAlarmRecordServiceImpl extends AbstractFastbootService<DeviceAlarmRecord, DeviceAlarmRecordBean> implements DeviceAlarmRecordService {

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

    @Autowired
    DeviceAlarmRecordDao deviceAlarmRecordDao;


    @Override
    public DeviceAlarmRecordBean add(DeviceAlarmRecordBean deviceAlarmRecordBean) {

        DeviceAlarmRecord deviceAlarmRecord = new DeviceAlarmRecord();
        beanToDomain(deviceAlarmRecordBean, deviceAlarmRecord, "id");
        deviceAlarmRecordDao.save(deviceAlarmRecord);
        domainToBean(deviceAlarmRecord, deviceAlarmRecordBean);
        return deviceAlarmRecordBean;

    }

    @Override
    public DeviceAlarmRecordBean update(DeviceAlarmRecordBean deviceAlarmRecordBean) {

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


        DeviceAlarmRecord deviceAlarmRecord = deviceAlarmRecordDao.findByCodeAndValid(deviceAlarmRecordBean.getCode(), VALID);


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

        beanToDomain(deviceAlarmRecordBean, deviceAlarmRecord, "id", "code", "version", "createTime", "updateTime", "valid");

        deviceAlarmRecordDao.save(deviceAlarmRecord);

        domainToBean(deviceAlarmRecord, deviceAlarmRecordBean);

        return deviceAlarmRecordBean;
    }

    @Override
    public DeviceAlarmRecordBean delete(DeviceAlarmRecordBean deviceAlarmRecordBean) {

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


        DeviceAlarmRecord deviceAlarmRecord = deviceAlarmRecordDao.findByCodeAndValid(deviceAlarmRecordBean.getCode(), VALID);


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


        deviceAlarmRecord.setValid(INVALID);
        deviceAlarmRecordDao.save(deviceAlarmRecord);


        domainToBean(deviceAlarmRecord, deviceAlarmRecordBean);

        return deviceAlarmRecordBean;
    }

    @Override
    public DeviceAlarmRecordBean find(DeviceAlarmRecordBean deviceAlarmRecordBean) {
        if (deviceAlarmRecordBean == null) {
            return null;
        }


        DeviceAlarmRecord deviceAlarmRecord = deviceAlarmRecordDao.findByCodeAndValid(deviceAlarmRecordBean.getCode(), VALID);


        if (deviceAlarmRecord == null) {
            return null;
        }
        BeanUtils.copyProperties(deviceAlarmRecord, deviceAlarmRecordBean);
        return deviceAlarmRecordBean;
    }

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

    @Override
    public List<DeviceAlarmRecordBean> findAll(DeviceAlarmRecordBean deviceAlarmRecordBean, PagerBean pagerBean) {
        List<DeviceAlarmRecordBean> deviceAlarmRecordBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(deviceAlarmRecordBean);
        List<DeviceAlarmRecord> deviceAlarmRecords = deviceAlarmRecordDao.findAll(detachedCriteria, pagerBean);
        for (DeviceAlarmRecord deviceAlarmRecord : deviceAlarmRecords) {
            DeviceAlarmRecordBean deviceAlarmRecordBeanTemp = new DeviceAlarmRecordBean();
            domainToBean(deviceAlarmRecord, deviceAlarmRecordBeanTemp);
            deviceAlarmRecordBeans.add(deviceAlarmRecordBeanTemp);
        }
        return deviceAlarmRecordBeans;
    }

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

    @Override
    public Long countAll(DeviceAlarmRecordBean deviceAlarmRecordBean) {
        DetachedCriteria detachedCriteria = createListCriteria(deviceAlarmRecordBean);
        return deviceAlarmRecordDao.countAll(detachedCriteria);
    }

    @Override
    public PagerBean<DeviceAlarmRecordBean> findPager(DeviceAlarmRecordBean deviceAlarmRecordBean, PagerBean pagerBean) {
        List<DeviceAlarmRecordBean> deviceAlarmRecordBeans = this.findAll(deviceAlarmRecordBean, pagerBean);
        Long count = this.countAll(deviceAlarmRecordBean);
        PagerBean<DeviceAlarmRecordBean> deviceAlarmRecordPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, deviceAlarmRecordPageBean);
        deviceAlarmRecordPageBean.setItemCount(count.intValue());
        deviceAlarmRecordPageBean.init();
        deviceAlarmRecordPageBean.setItems(deviceAlarmRecordBeans);
        return deviceAlarmRecordPageBean;
    }

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

        if (deviceAlarmRecordBean != null) {
            if (StringUtils.isNotBlank(deviceAlarmRecordBean.getSn())) {
                detachedCriteria.add(Restrictions.like("sn", deviceAlarmRecordBean.getSn(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(deviceAlarmRecordBean.getAlarmName())) {
                detachedCriteria.add(Restrictions.like("alarmName", deviceAlarmRecordBean.getAlarmName(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(deviceAlarmRecordBean.getVariableId())) {
                detachedCriteria.add(Restrictions.like("variableId", deviceAlarmRecordBean.getVariableId(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(deviceAlarmRecordBean.getAlarmType())) {
                detachedCriteria.add(Restrictions.eq("alarmType", deviceAlarmRecordBean.getAlarmType()));
            }
            if (StringUtils.isNotBlank(deviceAlarmRecordBean.getRecordCondition())) {
                detachedCriteria.add(Restrictions.like("recordCondition", deviceAlarmRecordBean.getRecordCondition(), MatchMode.ANYWHERE));
            }
        }


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


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

    }


}
