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


import com.kuyuntech.vrv.coreapi.service.core.RoleService;
import com.kuyuntech.vrv.coreservice.dao.core.RoleDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.RoleBean;
import com.kuyuntech.vrv.coreservice.domain.core.Role;
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.*;





/**
* RoleService
*
*/
@Service("roleService")
@Transactional(rollbackFor = Exception.class,transactionManager = "vrvCoreServiceTransactionManager")
public class RoleServiceImpl extends AbstractFastbootService<Role,RoleBean> implements RoleService {

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

    @Autowired
    RoleDao roleDao;

    

    @Override
    public RoleBean add(RoleBean roleBean) {

        Role role = new Role();
        beanToDomain(roleBean,role,"id");
        roleDao.save(role);
        domainToBean(role,roleBean);
        return roleBean;

    }

    @Override
    public RoleBean update(RoleBean roleBean) {

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

        
        Role role = roleDao.findByCodeAndValid(roleBean.getCode(),VALID);
        

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

        beanToDomain(roleBean,role,"id","code","version","createTime","updateTime","valid");

        roleDao.save(role);

        domainToBean(role,roleBean);

        return roleBean;
    }

    @Override
    public RoleBean delete(RoleBean roleBean) {

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

        
        Role role = roleDao.findByCodeAndValid(roleBean.getCode(),VALID);
        


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

        
        role.setValid(INVALID);
        roleDao.save(role);
        

        domainToBean(role,roleBean);

        return roleBean;
    }

    @Override
    public RoleBean find(RoleBean roleBean) {
        if(roleBean == null){
            return null ;
        }

        
        Role role = roleDao.findByCodeAndValid(roleBean.getCode(),VALID);
        


        if(role == null){
            return null ;
        }
        BeanUtils.copyProperties(role,roleBean);
        return roleBean;
    }

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

    @Override
    public List<RoleBean> findAll(RoleBean roleBean, PagerBean pagerBean) {
        List<RoleBean> roleBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(roleBean);
        List<Role> roles = roleDao.findAll(detachedCriteria,pagerBean);
        for (Role role : roles) {
            RoleBean roleBeanTemp = new RoleBean();
            domainToBean(role,roleBeanTemp);
            roleBeans.add(roleBeanTemp);
        }
        return roleBeans;
    }

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

    @Override
     public Long countAll(RoleBean roleBean) {
        DetachedCriteria detachedCriteria = createListCriteria(roleBean);
        return roleDao.countAll(detachedCriteria);
    }

     @Override
     public PagerBean<RoleBean> findPager(RoleBean roleBean, PagerBean pagerBean) {
        List<RoleBean> roleBeans = this.findAll(roleBean, pagerBean);
        Long count = this.countAll(roleBean);
        PagerBean<RoleBean> rolePageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, rolePageBean);
        rolePageBean.setItemCount(count.intValue());
        rolePageBean.init();
        rolePageBean.setItems(roleBeans);
        return  rolePageBean ;
     }

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



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

    }

    


}
