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


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


/**
 * ProjectService
 */
@Service("projectService")
@Transactional(rollbackFor = Exception.class, transactionManager = "vrvCoreServiceTransactionManager")
public class ProjectServiceImpl extends AbstractFastbootService<Project, ProjectBean> implements ProjectService {

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

    @Autowired
    ProjectDao projectDao;


    @Override
    public ProjectBean add(ProjectBean projectBean) {

        Project project = new Project();
        beanToDomain(projectBean, project, "id");
        projectDao.save(project);
        domainToBean(project, projectBean);
        return projectBean;

    }

    @Override
    public ProjectBean update(ProjectBean projectBean) {

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


        Project project = projectDao.findByCodeAndValid(projectBean.getCode(), VALID);


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

        beanToDomain(projectBean, project, "id", "code", "version", "createTime", "updateTime", "valid");

        projectDao.save(project);

        domainToBean(project, projectBean);

        return projectBean;
    }

    @Override
    public ProjectBean delete(ProjectBean projectBean) {

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


        Project project = projectDao.findByCodeAndValid(projectBean.getCode(), VALID);


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


        project.setValid(INVALID);
        projectDao.save(project);


        domainToBean(project, projectBean);

        return projectBean;
    }

    @Override
    public ProjectBean find(ProjectBean projectBean) {
        if (projectBean == null) {
            return null;
        }


        Project project = projectDao.findByCodeAndValid(projectBean.getCode(), VALID);


        if (project == null) {
            return null;
        }
        BeanUtils.copyProperties(project, projectBean);
        return projectBean;
    }

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

    @Override
    public List<ProjectBean> findAll(ProjectBean projectBean, PagerBean pagerBean) {
        List<ProjectBean> projectBeans = new ArrayList<>();
        DetachedCriteria detachedCriteria = createListCriteria(projectBean);
        List<Project> projects = projectDao.findAll(detachedCriteria, pagerBean);
        for (Project project : projects) {
            ProjectBean projectBeanTemp = new ProjectBean();
            domainToBean(project, projectBeanTemp);
            projectBeans.add(projectBeanTemp);
        }
        return projectBeans;
    }

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

    @Override
    public Long countAll(ProjectBean projectBean) {
        DetachedCriteria detachedCriteria = createListCriteria(projectBean);
        return projectDao.countAll(detachedCriteria);
    }

    @Override
    public PagerBean<ProjectBean> findPager(ProjectBean projectBean, PagerBean pagerBean) {
        List<ProjectBean> projectBeans = this.findAll(projectBean, pagerBean);
        Long count = this.countAll(projectBean);
        PagerBean<ProjectBean> projectPageBean = new PagerBean<>();
        BeanUtils.copyProperties(pagerBean, projectPageBean);
        projectPageBean.setItemCount(count.intValue());
        projectPageBean.init();
        projectPageBean.setItems(projectBeans);
        return projectPageBean;
    }

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

        if(projectBean != null){
           if(StringUtils.isNotBlank(projectBean.getName())) {
               detachedCriteria.add(Restrictions.like("name", projectBean.getName(), MatchMode.ANYWHERE));
           }
            if(StringUtils.isNotBlank(projectBean.getProvince())) {
                detachedCriteria.add(Restrictions.eq("province", projectBean.getProvince()));
            }
            if(StringUtils.isNotBlank(projectBean.getCity())) {
                detachedCriteria.add(Restrictions.eq("city", projectBean.getCity()));
            }
            if(StringUtils.isNotBlank(projectBean.getAddress())) {
                detachedCriteria.add(Restrictions.like("address", projectBean.getAddress(),MatchMode.ANYWHERE));
            }
            if(StringUtils.isNotBlank(projectBean.getCustomer())) {
                detachedCriteria.add(Restrictions.like("customer", projectBean.getCustomer(), MatchMode.ANYWHERE));
            }
            if(StringUtils.isNotBlank(projectBean.getCustomerTel())) {
                detachedCriteria.add(Restrictions.like("customerTel ", projectBean.getCustomerTel(), MatchMode.ANYWHERE));
            }
        }


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


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

    }

}
