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


import com.kuyuntech.vrv.coreapi.service.core.RolePowerService;
import com.kuyuntech.vrv.coreservice.dao.core.RolePowerDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.kuyuntech.vrv.coreapi.bean.core.RolePowerBean;
import com.kuyuntech.vrv.coreservice.domain.core.RolePower;
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.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;

import com.wbspool.fastboot.core.jpa.service.AbstractFastbootService;
import org.springframework.util.Assert;

import static com.wbspool.fastboot.core.jpa.constant.DataValidTypes.*;


/**
 * RolePowerService
 */
@Service("rolePowerService")
@Transactional(rollbackFor = Exception.class, transactionManager = "vrvCoreServiceTransactionManager")
public class RolePowerServiceImpl extends AbstractFastbootService<RolePower, RolePowerBean> implements RolePowerService {

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

    @Autowired
    RolePowerDao rolePowerDao;

    @Override
    public RolePowerBean add(RolePowerBean rolePowerBean) {

        RolePower rolePower = new RolePower();
        beanToDomain(rolePowerBean, rolePower, "id");
        rolePowerDao.save(rolePower);
        domainToBean(rolePower, rolePowerBean);
        return rolePowerBean;

    }

    @Override
    public RolePowerBean update(RolePowerBean rolePowerBean) {

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


        RolePower rolePower = rolePowerDao.findByCodeAndValid(rolePowerBean.getCode(), VALID);


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

        beanToDomain(rolePowerBean, rolePower, "id", "code", "version", "createTime", "updateTime", "valid");

        rolePowerDao.save(rolePower);

        domainToBean(rolePower, rolePowerBean);

        return rolePowerBean;
    }

    @Override
    public RolePowerBean delete(RolePowerBean rolePowerBean) {

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


        RolePower rolePower = rolePowerDao.findByCodeAndValid(rolePowerBean.getCode(), VALID);


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


        rolePower.setValid(INVALID);
        rolePowerDao.save(rolePower);


        domainToBean(rolePower, rolePowerBean);

        return rolePowerBean;
    }

    @Override
    public RolePowerBean find(RolePowerBean rolePowerBean) {
        if (rolePowerBean == null) {
            return null;
        }


        RolePower rolePower = rolePowerDao.findByCodeAndValid(rolePowerBean.getCode(), VALID);


        if (rolePower == null) {
            return null;
        }
        BeanUtils.copyProperties(rolePower, rolePowerBean);
        return rolePowerBean;
    }

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

    @Override
    public List<RolePowerBean> findAll(RolePowerBean rolePowerBean, PagerBean pagerBean) {
        List<RolePowerBean> rolePowerBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(rolePowerBean);
        List<RolePower> rolePowers = rolePowerDao.findAll(detachedCriteria, pagerBean);
        for (RolePower rolePower : rolePowers) {
            RolePowerBean rolePowerBeanTemp = new RolePowerBean();
            domainToBean(rolePower, rolePowerBeanTemp);
            rolePowerBeans.add(rolePowerBeanTemp);
        }
        return rolePowerBeans;
    }

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

    @Override
    public Long countAll(RolePowerBean rolePowerBean) {
        DetachedCriteria detachedCriteria = createListCriteria(rolePowerBean);
        return rolePowerDao.countAll(detachedCriteria);
    }

    @Override
    public PagerBean<RolePowerBean> findPager(RolePowerBean rolePowerBean, PagerBean pagerBean) {
        List<RolePowerBean> rolePowerBeans = this.findAll(rolePowerBean, pagerBean);
        Long count = this.countAll(rolePowerBean);
        PagerBean<RolePowerBean> rolePowerPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, rolePowerPageBean);
        rolePowerPageBean.setItemCount(count.intValue());
        rolePowerPageBean.init();
        rolePowerPageBean.setItems(rolePowerBeans);
        return rolePowerPageBean;
    }

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


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

    }




}
