/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workreview.service;

import java.util.*;

import com.google.common.collect.Lists;
import com.jeeplus.common.service.TreeService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workreview.dao.WorkReviewAuditDao;
import com.jeeplus.modules.workreview.entity.WorkReviewAudit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.modules.workreview.entity.WorkReviewStandard;
import com.jeeplus.modules.workreview.dao.WorkReviewStandardDao;

/**
 * 复核标准Service
 * @author ssrh
 * @version 2018-06-01
 */
@Service
@Transactional(readOnly = true)
public class WorkReviewStandardService extends TreeService<WorkReviewStandardDao, WorkReviewStandard> {

    @Autowired
    private WorkReviewAuditDao workReviewAuditDao;

	public WorkReviewStandard get(String id) {
		return super.get(id);
	}
	public WorkReviewAudit getReviewAudit(String id,String companyId,String type) {
		return workReviewAuditDao.getReviewAudit(id,companyId,type);
	}
	public void delReviewAudit(String reportId) {
		workReviewAuditDao.delReviewAudit(reportId);
	}
	public WorkReviewAudit getAudit(String id) {
		return workReviewAuditDao.get(id);
	}

	public List<WorkReviewStandard> findList(WorkReviewStandard workReviewStandard) {
        List<WorkReviewStandard> workReviewStandards = new ArrayList<>();
        List<WorkReviewStandard> listReturn = Lists.newArrayList();
        List<WorkReviewStandard> listAll = super.findList(workReviewStandard);
        Office company = UserUtils.getSelectCompany();
        workReviewStandard.setCompanyId(company.getId());
        List<WorkReviewStandard> list = super.findList(workReviewStandard);
        if (listAll!=null && listAll.size()!=0){
            workReviewStandards.addAll(listAll);
        }
        if (list!=null && list.size()!=0){
            workReviewStandards.addAll(list);
        }

        if(workReviewStandards!=null&&workReviewStandards.size()>0&&workReviewStandard!=null&& (StringUtils.isNotBlank(workReviewStandard.getStandardDetail())||StringUtils.isNotBlank(workReviewStandard.getOrderNum()))){
            //标准内容模糊查询，查询所有父节点
            Set<String> parentIdSet = new HashSet<>();
            for (WorkReviewStandard reviewStandard : workReviewStandards) {
                String parentIds = reviewStandard.getParentIds();
                if (StringUtils.isNotBlank(parentIds)){
                    parentIdSet.addAll(Arrays.asList(parentIds.split(",")));
                }
            }
            for (WorkReviewStandard reviewStandard : workReviewStandards) {
                parentIdSet.remove(reviewStandard.getId());
            }
            workReviewStandards.addAll(0,this.fingIds(parentIdSet,"0"));
            workReviewStandards.addAll(0,this.fingIds(parentIdSet,UserUtils.getSelectCompany().getId()));
        }
        sortList(listReturn,workReviewStandards,"0",true);
        return listReturn;
    }
    public List<WorkReviewStandard> findListByType(WorkReviewStandard workReviewStandard) {
        List<WorkReviewStandard> workReviewStandards = new ArrayList<>();
        List<WorkReviewStandard> listReturn = Lists.newArrayList();
        List<WorkReviewStandard> listAll = new ArrayList<>();
        Office company = UserUtils.getSelectCompany();
        if(workReviewStandard!=null&& StringUtils.isNotBlank(workReviewStandard.getStandardDetail())){
            //标准内容模糊查询，查询所有父节点
            Set<String> parentIdSet = new HashSet<>();
            listAll.addAll(0,this.fingByIds(parentIdSet,"0",workReviewStandard.getParentIds()));
        }
        workReviewStandard.setCompanyId(company.getId());
        List<WorkReviewStandard> list = new ArrayList<>();
        if(workReviewStandard!=null&& StringUtils.isNotBlank(workReviewStandard.getStandardDetail())){
            //标准内容模糊查询，查询所有父节点
            Set<String> parentIdSet = new HashSet<>();
            list.addAll(0,this.fingByIds(parentIdSet,UserUtils.getSelectCompany().getId(),workReviewStandard.getParentIds()));
        }
        if (listAll!=null && listAll.size()!=0){
            workReviewStandards.addAll(listAll);
        }
        if (list!=null && list.size()!=0){
            workReviewStandards.addAll(list);
        }
        if (workReviewStandards!=null && workReviewStandards.size()!=0){
            workReviewStandards.add(get(workReviewStandard.getType()));
        }
        sortList(listReturn,workReviewStandards,"0",true);
        return listReturn;
    }

    public List<WorkReviewAudit> findAuditList(WorkReviewAudit workReviewAudit) {
        List<WorkReviewAudit> workReviewStandards = Lists.newArrayList();
        Office company = UserUtils.getSelectCompany();
        workReviewAudit.setCompanyId(company.getId());
        try {
            List<WorkReviewAudit> list = workReviewAuditDao.findList(workReviewAudit);
            if(workReviewAudit!=null&& StringUtils.isNotBlank(workReviewAudit.getStandardDetail())){
                //标准内容模糊查询，查询所有父节点
                Set<String> parentIdSet = new HashSet<>();
                for (WorkReviewAudit reviewStandard : list) {
                    String parentIds = reviewStandard.getParentIds();
                    if (StringUtils.isNotBlank(parentIds)){
                        parentIdSet.addAll(Arrays.asList(parentIds.split(",")));
                    }
                }
            }
            sortAuditList(workReviewStandards,list,"0",true);
            return workReviewStandards;
        }catch (Exception e){
            logger.error("-----------Exception e"+e);
            return null;
        }

    }

    public static void sortList(List<WorkReviewStandard> list, List<WorkReviewStandard> sourcelist, String parentId, boolean cascade){
        for (int i=0; i<sourcelist.size(); i++){
            WorkReviewStandard e = sourcelist.get(i);
            if (e.getParent()!=null && StringUtils.isNotBlank(e.getParent().getId()) && e.getParent().getId().equals(parentId)){
                list.add(e);
                if (cascade){
                    // 判断是否还有子节点, 有则继续获取子节点
                    for (int j=0; j<sourcelist.size(); j++){
                        WorkReviewStandard child = sourcelist.get(j);
                        if (child.getParent()!=null && StringUtils.isNotBlank(child.getParent().getId()) && child.getParent().getId().equals(e.getId())){
                            sortList(list, sourcelist, e.getId(), true);
                            break;
                        }
                    }
                }
            }
        }
    }
    public static void sortAuditList(List<WorkReviewAudit> list, List<WorkReviewAudit> sourcelist, String parentId, boolean cascade){
        for (int i=0; i<sourcelist.size(); i++){
            WorkReviewAudit e = sourcelist.get(i);
            if (e.getParent()!=null && StringUtils.isNotBlank(e.getParent().getId()) && e.getParent().getId().equals(parentId)){
                list.add(e);
                if (cascade){
                    // 判断是否还有子节点, 有则继续获取子节点
                    for (int j=0; j<sourcelist.size(); j++){
                        WorkReviewAudit child = sourcelist.get(j);
                        if (child.getParent()!=null && StringUtils.isNotBlank(child.getParent().getId()) && child.getParent().getId().equals(e.getId())){
                            sortAuditList(list, sourcelist, e.getId(), true);
                            break;
                        }
                    }
                }
            }
        }
    }

    private List<WorkReviewStandard> fingIds(Set<String> parentIdSet,String companyId) {
        return dao.fingIds(parentIdSet,companyId);
    }
    private List<WorkReviewStandard> fingByIds(Set<String> parentIdSet,String companyId,String parentIds) {
        return dao.fingByIds(parentIdSet,companyId,parentIds);
    }

    public Page<WorkReviewStandard> findPage(Page<WorkReviewStandard> page, WorkReviewStandard workReviewStandard) {
		return super.findPage(page, workReviewStandard);
	}
	
	@Transactional(readOnly = false)
    public void saveAudit(WorkReviewAudit workReviewAudit) {
        //设置序号
        if(workReviewAudit.getIsNewRecord() && StringUtils.isBlank(workReviewAudit.getType())) {
            workReviewAudit.setParent(getAudit(workReviewAudit.getParent().getId()));
            long s3 = System.currentTimeMillis();
            String orderNum = this.selectOrderNum(workReviewAudit.getParent().getId());
            long s4 = System.currentTimeMillis();
            if (StringUtils.isBlank(orderNum)) {
                workReviewAudit.setOrderNum(workReviewAudit.getParent().getOrderNum() + "01");
            } else {
                workReviewAudit.setOrderNum(String.valueOf(Integer.valueOf(orderNum) + 1));
            }
        }
        workReviewAudit.setCompanyId(UserUtils.getSelectCompany().getId());
        workReviewAuditDao.insert(workReviewAudit);
	}
	@Transactional(readOnly = false)
    public void updateAudit(WorkReviewAudit workReviewAudit) {
        workReviewAudit.preUpdate();
        workReviewAuditDao.update(workReviewAudit);
	}
	@Transactional(readOnly = false)
    public void reportMaxDeduct(String id,String maxDeduct) {
        workReviewAuditDao.reportMaxDeduct(id,maxDeduct);
	}
	@Transactional(readOnly = false)
	public void save(WorkReviewStandard workReviewStandard) {
        //设置序号
        if(workReviewStandard.getIsNewRecord()) {
            workReviewStandard.setParent(this.get(workReviewStandard.getParent().getId()));
            String orderNum = this.selectOrderNum(workReviewStandard.getParent().getId());
            if (StringUtils.isBlank(orderNum)) {
                workReviewStandard.setOrderNum(workReviewStandard.getParent().getOrderNum() + "01");
            } else {
                workReviewStandard.setOrderNum(String.valueOf(Integer.valueOf(orderNum) + 1));
            }
        }
        workReviewStandard.setCompanyId(UserUtils.getSelectCompany().getId());
		super.save(workReviewStandard);
	}

	@Transactional(readOnly = false)
	public void delete(WorkReviewStandard workReviewStandard) {
		super.delete(workReviewStandard);
	}
	@Transactional(readOnly = false)
	public void deleteAuditById(String id) {
		workReviewAuditDao.deleteAuditById(id);
	}
	@Transactional(readOnly = false)
	public void deleteAudit(String reportId) {
		workReviewAuditDao.deleteAudit(reportId);
	}
	@Transactional(readOnly = false)
	public void deleteAuditType(String reportId) {
		workReviewAuditDao.deleteAuditType(reportId);
	}
	@Transactional(readOnly = false)
	public void deleteAuditChangeType(String reportId) {
		workReviewAuditDao.deleteAuditChangeType(reportId);
	}


    public String selectOrderNum(String parentId) {
        return dao.selectOrderNum(parentId);
    }
    public String selectOrderNumAudit(String parentId) {
        return workReviewAuditDao.selectOrderNum(parentId);
    }

    public List<WorkReviewStandard> findTreeList(WorkReviewStandard workReviewStandard) {
        List<WorkReviewStandard> workReviewStandards = new ArrayList<>();
        List<WorkReviewStandard> listReturn = Lists.newArrayList();
        List<WorkReviewStandard> listAll = super.findList(workReviewStandard);
        Office company = UserUtils.getSelectCompany();
        workReviewStandard.setCompanyId(company.getId());
        workReviewStandard.setProofread(1);
        List<WorkReviewStandard> list = super.findList(workReviewStandard);
        if (listAll!=null && listAll.size()!=0){
            workReviewStandards.addAll(listAll);
        }
        if (list!=null && list.size()!=0){
            workReviewStandards.addAll(list);
        }
        sortList(listReturn,workReviewStandards,"0",true);
        this.queryProofread(listReturn);
        return listReturn;
    }

    public void queryProofread(List<WorkReviewStandard> listReturn){
	    List<WorkReviewStandard> standards = dao.getByDetail();
        if(standards==null||listReturn==null){
            return;
        }
        for (WorkReviewStandard standard : standards) {
            for (WorkReviewStandard reviewStandard : listReturn) {
                if(reviewStandard!=null&&((reviewStandard.getParent()!=null&&standard.getId().equals(reviewStandard.getParent().getId()))
                        ||standard.getId().equals(reviewStandard.getId())
                        || (StringUtils.isNotBlank(reviewStandard.getParentIds())&&reviewStandard.getParentIds().contains(standard.getId()))
                )){
                    reviewStandard.setProofread(1);
                }
            }
        }

    }
}