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


import com.kuyuntech.vrv.coreapi.service.core.PowerService;
import com.kuyuntech.vrv.coreservice.dao.core.PowerDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.PowerBean;
import com.kuyuntech.vrv.coreservice.domain.core.Power;
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.*;





/**
* PowerService
*
*/
@Service("powerService")
@Transactional(rollbackFor = Exception.class,transactionManager = "vrvCoreServiceTransactionManager")
public class PowerServiceImpl extends AbstractFastbootService<Power,PowerBean> implements PowerService {

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

    @Autowired
    PowerDao powerDao;

    

    @Override
    public PowerBean add(PowerBean powerBean) {

        Power power = new Power();
        beanToDomain(powerBean,power,"id");
        powerDao.save(power);
        domainToBean(power,powerBean);
        return powerBean;

    }

    @Override
    public PowerBean update(PowerBean powerBean) {

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

        
        Power power = powerDao.findByCodeAndValid(powerBean.getCode(),VALID);
        

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

        beanToDomain(powerBean,power,"id","code","version","createTime","updateTime","valid");

        powerDao.save(power);

        domainToBean(power,powerBean);

        return powerBean;
    }

    @Override
    public PowerBean delete(PowerBean powerBean) {

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

        
        Power power = powerDao.findByCodeAndValid(powerBean.getCode(),VALID);
        


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

        
        power.setValid(INVALID);
        powerDao.save(power);
        

        domainToBean(power,powerBean);

        return powerBean;
    }

    @Override
    public PowerBean find(PowerBean powerBean) {
        if(powerBean == null){
            return null ;
        }

        
        Power power = powerDao.findByCodeAndValid(powerBean.getCode(),VALID);
        


        if(power == null){
            return null ;
        }
        BeanUtils.copyProperties(power,powerBean);
        return powerBean;
    }

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

    @Override
    public List<PowerBean> findAll(PowerBean powerBean, PagerBean pagerBean) {
        List<PowerBean> powerBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(powerBean);
        List<Power> powers = powerDao.findAll(detachedCriteria,pagerBean);
        for (Power power : powers) {
            PowerBean powerBeanTemp = new PowerBean();
            domainToBean(power,powerBeanTemp);
            powerBeans.add(powerBeanTemp);
        }
        return powerBeans;
    }

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

    @Override
     public Long countAll(PowerBean powerBean) {
        DetachedCriteria detachedCriteria = createListCriteria(powerBean);
        return powerDao.countAll(detachedCriteria);
    }

     @Override
     public PagerBean<PowerBean> findPager(PowerBean powerBean, PagerBean pagerBean) {
        List<PowerBean> powerBeans = this.findAll(powerBean, pagerBean);
        Long count = this.countAll(powerBean);
        PagerBean<PowerBean> powerPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, powerPageBean);
        powerPageBean.setItemCount(count.intValue());
        powerPageBean.init();
        powerPageBean.setItems(powerBeans);
        return  powerPageBean ;
     }

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



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

    }

    


}
