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


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


/**
 * OperatorService
 */
@Service("operatorService")
@Transactional(rollbackFor = Exception.class, transactionManager = "vrvCoreServiceTransactionManager")
public class OperatorServiceImpl extends AbstractFastbootService<Operator, OperatorBean> implements OperatorService {

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

    @Autowired
    OperatorDao operatorDao;


    @Override
    public OperatorBean add(OperatorBean operatorBean) {

        Operator operator = new Operator();
        beanToDomain(operatorBean, operator, "id");
        operatorDao.save(operator);
        domainToBean(operator, operatorBean);
        return operatorBean;

    }

    @Override
    public OperatorBean update(OperatorBean operatorBean) {

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


        Operator operator = operatorDao.findByCodeAndValid(operatorBean.getCode(), VALID);


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

        beanToDomain(operatorBean, operator, "id", "code", "version", "createTime", "updateTime", "valid");

        operatorDao.save(operator);

        domainToBean(operator, operatorBean);

        return operatorBean;
    }

    @Override
    public OperatorBean delete(OperatorBean operatorBean) {

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


        Operator operator = operatorDao.findByCodeAndValid(operatorBean.getCode(), VALID);


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


        operator.setValid(INVALID);
        operatorDao.save(operator);


        domainToBean(operator, operatorBean);

        return operatorBean;
    }

    @Override
    public OperatorBean find(OperatorBean operatorBean) {
        if (operatorBean == null) {
            return null;
        }


        Operator operator = operatorDao.findByCodeAndValid(operatorBean.getCode(), VALID);


        if (operator == null) {
            return null;
        }
        BeanUtils.copyProperties(operator, operatorBean);
        return operatorBean;
    }

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

    @Override
    public List<OperatorBean> findAll(OperatorBean operatorBean, PagerBean pagerBean) {
        List<OperatorBean> operatorBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(operatorBean);
        List<Operator> operators = operatorDao.findAll(detachedCriteria, pagerBean);
        for (Operator operator : operators) {
            OperatorBean operatorBeanTemp = new OperatorBean();
            domainToBean(operator, operatorBeanTemp);
            operatorBeans.add(operatorBeanTemp);
        }
        return operatorBeans;
    }

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

    @Override
    public Long countAll(OperatorBean operatorBean) {
        DetachedCriteria detachedCriteria = createListCriteria(operatorBean);
        return operatorDao.countAll(detachedCriteria);
    }

    @Override
    public PagerBean<OperatorBean> findPager(OperatorBean operatorBean, PagerBean pagerBean) {
        List<OperatorBean> operatorBeans = this.findAll(operatorBean, pagerBean);
        Long count = this.countAll(operatorBean);
        PagerBean<OperatorBean> operatorPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, operatorPageBean);
        operatorPageBean.setItemCount(count.intValue());
        operatorPageBean.init();
        operatorPageBean.setItems(operatorBeans);
        return operatorPageBean;
    }

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

        if (operatorBean != null) {
            if (StringUtils.isNotBlank(operatorBean.getName())) {
                detachedCriteria.add(Restrictions.like("name", operatorBean.getName(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(operatorBean.getProvince())) {
                detachedCriteria.add(Restrictions.eq("province", operatorBean.getProvince()));
            }
            if (StringUtils.isNotBlank(operatorBean.getCity())) {
                detachedCriteria.add(Restrictions.eq("city", operatorBean.getCity()));
            }
            if (StringUtils.isNotBlank(operatorBean.getAddress())) {
                detachedCriteria.add(Restrictions.like("address", operatorBean.getAddress(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(operatorBean.getContact())) {
                detachedCriteria.add(Restrictions.like("customer", operatorBean.getContact(), MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotBlank(operatorBean.getTel())) {
                detachedCriteria.add(Restrictions.like("tel", operatorBean.getTel(), MatchMode.ANYWHERE));
            }
        }

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


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

    }


}
