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


import com.kuyuntech.vrv.coreapi.bean.core.VerifyCodeBean;
import com.kuyuntech.vrv.coreapi.service.core.VerifyCodeService;
import com.kuyuntech.vrv.coreservice.dao.core.VerifyCodeDao;
import com.kuyuntech.vrv.coreservice.domain.core.VerifyCode;
import com.wbspool.fastboot.core.common.bean.PagerBean;
import com.wbspool.fastboot.core.jpa.service.AbstractFastbootService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

import static com.wbspool.fastboot.core.jpa.constant.DataValidTypes.INVALID;
import static com.wbspool.fastboot.core.jpa.constant.DataValidTypes.VALID;





/**
* VerifyCodeService
*
*/
@Service("verifyCodeService")
@Transactional(rollbackFor = Exception.class,transactionManager = "authCompCoreServiceTransactionManager")
public class VerifyCodeServiceImpl extends AbstractFastbootService<VerifyCode, VerifyCodeBean> implements VerifyCodeService {

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

    @Autowired
    VerifyCodeDao verifyCodeDao;

    

    @Override
    public VerifyCodeBean add(VerifyCodeBean verifyCodeBean) {

        VerifyCode verifyCode = new VerifyCode();
        beanToDomain(verifyCodeBean,verifyCode,"id");
        verifyCodeDao.save(verifyCode);
        domainToBean(verifyCode,verifyCodeBean);
        return verifyCodeBean;

    }

    @Override
    public VerifyCodeBean update(VerifyCodeBean verifyCodeBean) {

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

        
        VerifyCode verifyCode = verifyCodeDao.findByCodeAndValid(verifyCodeBean.getCode(),VALID);
        

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

        beanToDomain(verifyCodeBean,verifyCode,"id","code","version","createTime","updateTime","valid");

        verifyCodeDao.save(verifyCode);

        domainToBean(verifyCode,verifyCodeBean);

        return verifyCodeBean;
    }

    @Override
    public VerifyCodeBean delete(VerifyCodeBean verifyCodeBean) {

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

        
        VerifyCode verifyCode = verifyCodeDao.findByCodeAndValid(verifyCodeBean.getCode(),VALID);
        


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

        
        verifyCode.setValid(INVALID);
        verifyCodeDao.save(verifyCode);
        

        domainToBean(verifyCode,verifyCodeBean);

        return verifyCodeBean;
    }

    @Override
    public VerifyCodeBean find(VerifyCodeBean verifyCodeBean) {
        if(verifyCodeBean == null){
            return null ;
        }

        
        VerifyCode verifyCode = verifyCodeDao.findByCodeAndValid(verifyCodeBean.getCode(),VALID);
        


        if(verifyCode == null){
            return null ;
        }
        BeanUtils.copyProperties(verifyCode,verifyCodeBean);
        return verifyCodeBean;
    }

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

    @Override
    public List<VerifyCodeBean> findAll(VerifyCodeBean verifyCodeBean, PagerBean pagerBean) {
        List<VerifyCodeBean> verifyCodeBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(verifyCodeBean);
        List<VerifyCode> verifyCodes = verifyCodeDao.findAll(detachedCriteria,pagerBean);
        for (VerifyCode verifyCode : verifyCodes) {
            VerifyCodeBean verifyCodeBeanTemp = new VerifyCodeBean();
            domainToBean(verifyCode,verifyCodeBeanTemp);
            verifyCodeBeans.add(verifyCodeBeanTemp);
        }
        return verifyCodeBeans;
    }

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

    @Override
     public Long countAll(VerifyCodeBean verifyCodeBean) {
        DetachedCriteria detachedCriteria = createListCriteria(verifyCodeBean);
        return verifyCodeDao.countAll(detachedCriteria);
    }

     @Override
     public PagerBean<VerifyCodeBean> findPager(VerifyCodeBean verifyCodeBean, PagerBean pagerBean) {
        List<VerifyCodeBean> verifyCodeBeans = this.findAll(verifyCodeBean, pagerBean);
        Long count = this.countAll(verifyCodeBean);
        PagerBean<VerifyCodeBean> verifyCodePageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, verifyCodePageBean);
        verifyCodePageBean.setItemCount(count.intValue());
        verifyCodePageBean.init();
        verifyCodePageBean.setItems(verifyCodeBeans);
        return  verifyCodePageBean ;
     }

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



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

    }

    


}
