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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.alibaba.fastjson.JSON;
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.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workreview.entity.WorkReviewStandard;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workjobgrade.entity.WorkJobGrade;
import com.jeeplus.modules.workjobgrade.dao.WorkJobGradeDao;

/**
 * 职级类型Service
 * @author ssrh
 * @version 2018-08-02
 */
@Service
@Transactional(readOnly = true)
public class WorkJobGradeService extends TreeService<WorkJobGradeDao, WorkJobGrade> {

	public WorkJobGrade get(String id) {
        WorkJobGrade workJobGrade = super.get(id);
        if(workJobGrade!=null) {
            WorkJobGrade select = new WorkJobGrade();
            select.setParent(workJobGrade);
            List<WorkJobGrade> jobGradeList = dao.findByParent(select);
            if(jobGradeList!=null&&jobGradeList.size()>0){
                workJobGrade.setIsLeaf("否");
            }else {
                workJobGrade.setIsLeaf("是");
            }
        }
        return workJobGrade;
	}
	
	public List<WorkJobGrade> findList(WorkJobGrade workJobGrade) {
        User user = UserUtils.getUser();
        Office branchOffice = user.getBranchOffice();
        if(branchOffice==null){
            branchOffice=user.getCompany();
        }
        workJobGrade.setCompanyId(user.getCompany().getId());
        workJobGrade.setBranchOffice(branchOffice.getId());
        workJobGrade.getCurrentUser().setBranchOffice(branchOffice);
        List<WorkJobGrade> list = super.findList(workJobGrade);
        if(list!=null&&list.size()>0) {
            List<WorkJobGrade> sortedList = new ArrayList<>(list.size());
            this.sortList(sortedList,list,null);
            return sortedList;
        }
        return list;
	}

    private void sortList(List<WorkJobGrade> sortedList, List<WorkJobGrade> list,WorkJobGrade parent) {
        Iterator<WorkJobGrade> iterator = list.iterator();
        while (iterator.hasNext()){
            WorkJobGrade next = iterator.next();
            if(next==null)continue;
            if(parent==null) {
                sortedList.add(next);
                iterator.remove();
                this.sortList(sortedList,list,next);
                next.getParent().setId("0");
                iterator = list.iterator();
            }else {
                if(parent.getId().equals(next.getParent().getId())){
                    sortedList.add(next);
                    iterator.remove();
                    this.sortList(sortedList,list,next);
                    iterator = list.iterator();
                }
            }
        }
    }

    public Page<WorkJobGrade> findPage(Page<WorkJobGrade> page, WorkJobGrade workJobGrade) {
		return super.findPage(page, workJobGrade);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkJobGrade workJobGrade) {
        User user = UserUtils.getUser();
        if(StringUtils.isBlank(workJobGrade.getCompanyId())){
	        workJobGrade.setCompanyId(user.getComId());
        }
        if(StringUtils.isBlank(workJobGrade.getBranchOffice())){
            workJobGrade.setBranchOffice(user.getBranchOffice()==null?workJobGrade.getCompanyId():user.getBranchOffice().getId());
        }
        if(workJobGrade.getOffice()==null){
            workJobGrade.setOffice(user.getOffice());
        }
		super.save(workJobGrade);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkJobGrade workJobGrade) {
		super.delete(workJobGrade);
	}

    @Transactional(readOnly = false)
    public String changeState(WorkJobGrade workJobGrade) {
        String msg = null;
        if("禁用".equals(workJobGrade.getState())){
            workJobGrade.setState("启用");
            msg = "恢复";
        }else {
            workJobGrade.setState("禁用");
            msg = "移除";
        }
        dao.changeState(workJobGrade);
        return msg;
    }

    public List<WorkJobGrade> findByName(String name) {
	    WorkJobGrade select = new WorkJobGrade();
	    select.setName(name);
        User user = UserUtils.getUser();
        Office branchOffice = user.getBranchOffice();
        if(branchOffice==null){
            branchOffice=user.getCompany();
        }
	    select.setBranchOffice(branchOffice.getId());
        return dao.findByName(select);
    }
}