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


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


/**
 * OperatorLogService
 */
@Service("operatorLogService")
@Transactional(rollbackFor = Exception.class, transactionManager = "vrvCoreServiceTransactionManager")
public class OperatorLogServiceImpl extends AbstractFastbootService<OperatorLog, OperatorLogBean> implements OperatorLogService {

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

    @Autowired
    OperatorLogDao operatorLogDao;


    @Override
    public OperatorLogBean add(OperatorLogBean operatorLogBean) {

        OperatorLog operatorLog = new OperatorLog();
        beanToDomain(operatorLogBean, operatorLog, "id");
        operatorLogDao.save(operatorLog);
        domainToBean(operatorLog, operatorLogBean);
        return operatorLogBean;

    }

    @Override
    public OperatorLogBean update(OperatorLogBean operatorLogBean) {

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


        OperatorLog operatorLog = operatorLogDao.findByCodeAndValid(operatorLogBean.getCode(), VALID);


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

        beanToDomain(operatorLogBean, operatorLog, "id", "code", "version", "createTime", "updateTime", "valid");

        operatorLogDao.save(operatorLog);

        domainToBean(operatorLog, operatorLogBean);

        return operatorLogBean;
    }

    @Override
    public OperatorLogBean delete(OperatorLogBean operatorLogBean) {

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


        OperatorLog operatorLog = operatorLogDao.findByCodeAndValid(operatorLogBean.getCode(), VALID);


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


        operatorLog.setValid(INVALID);
        operatorLogDao.save(operatorLog);


        domainToBean(operatorLog, operatorLogBean);

        return operatorLogBean;
    }

    @Override
    public OperatorLogBean find(OperatorLogBean operatorLogBean) {
        if (operatorLogBean == null) {
            return null;
        }


        OperatorLog operatorLog = operatorLogDao.findByCodeAndValid(operatorLogBean.getCode(), VALID);


        if (operatorLog == null) {
            return null;
        }
        BeanUtils.copyProperties(operatorLog, operatorLogBean);
        return operatorLogBean;
    }

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

    @Override
    public List<OperatorLogBean> findAll(OperatorLogBean operatorLogBean, PagerBean pagerBean) {
        List<OperatorLogBean> operatorLogBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(operatorLogBean);
        List<OperatorLog> operatorLogs = operatorLogDao.findAll(detachedCriteria, pagerBean);
        for (OperatorLog operatorLog : operatorLogs) {
            OperatorLogBean operatorLogBeanTemp = new OperatorLogBean();
            domainToBean(operatorLog, operatorLogBeanTemp);
            operatorLogBeans.add(operatorLogBeanTemp);
        }
        return operatorLogBeans;
    }

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

    @Override
    public Long countAll(OperatorLogBean operatorLogBean) {
        DetachedCriteria detachedCriteria = createListCriteria(operatorLogBean);
        return operatorLogDao.countAll(detachedCriteria);
    }

    @Override
    public PagerBean<OperatorLogBean> findPager(OperatorLogBean operatorLogBean, PagerBean pagerBean) {
        List<OperatorLogBean> operatorLogBeans = this.findAll(operatorLogBean, pagerBean);
        Long count = this.countAll(operatorLogBean);
        PagerBean<OperatorLogBean> operatorLogPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, operatorLogPageBean);
        operatorLogPageBean.setItemCount(count.intValue());
        operatorLogPageBean.init();
        operatorLogPageBean.setItems(operatorLogBeans);
        return operatorLogPageBean;
    }

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

        if (operatorLogBean != null) {
            if (StringUtils.isNotBlank(operatorLogBean.getUserRealname())) {
                detachedCriteria.add(Restrictions.like("userRealname", operatorLogBean.getUserRealname(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(operatorLogBean.getUserAccount())) {
                detachedCriteria.add(Restrictions.like("userAccount", operatorLogBean.getUserAccount(), MatchMode.ANYWHERE));
            }
        }


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


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

    }


}
