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

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
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.workcommongroup.entity.WorkCommonGroup;
import com.jeeplus.modules.workcommongroup.dao.WorkCommonGroupDao;

/**
 * 常用组管理Service
 * @author ssrh
 * @version 2018-07-16
 */
@Service
@Transactional(readOnly = true)
public class WorkCommonGroupService extends CrudService<WorkCommonGroupDao, WorkCommonGroup> {

	public WorkCommonGroup get(String id) {
        WorkCommonGroup workCommonGroup = super.get(id);
        return workCommonGroup;
	}

    public void queryDetails(WorkCommonGroup workCommonGroup) {
        if(workCommonGroup!=null&& StringUtils.isNotBlank(workCommonGroup.getId())){
            workCommonGroup.setUserList(dao.queryUserList(workCommonGroup.getId()));
        }
    }

    public List<WorkCommonGroup> findList(WorkCommonGroup workCommonGroup) {
		return super.findList(workCommonGroup);
	}
	
	public Page<WorkCommonGroup> findPage(Page<WorkCommonGroup> page, WorkCommonGroup workCommonGroup) {
        if(!UserUtils.getUser().isAdmin()) {
            String dataScopeSql = dataScopeFilter(workCommonGroup.getCurrentUser(), "o", "u", "s", MenuStatusEnum.WORK_COMMON_GROUP.getValue());
            workCommonGroup.getSqlMap().put("dsf", dataScopeSql);
        }
		return super.findPage(page, workCommonGroup);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkCommonGroup workCommonGroup) {
	    if(workCommonGroup.getOffice()==null){
	        workCommonGroup.setOffice(UserUtils.getSelectOffice());
        }
        if(workCommonGroup.getCompany()==null){
	        workCommonGroup.setCompany(UserUtils.getSelectCompany());
        }
		super.save(workCommonGroup);
		saveUserList(workCommonGroup);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkCommonGroup workCommonGroup) {
	    dao.deleteUsers(workCommonGroup.getId());
		super.delete(workCommonGroup);
	}

    private void saveUserList(WorkCommonGroup workCommonGroup) {
        List<User> userList = workCommonGroup.getUserList();
        if(userList ==null|| userList.size()<=0){
            return;
        }
        Iterator<User> iterator = userList.iterator();
        while (iterator.hasNext()){
            User next = iterator.next();
            if(next==null||StringUtils.isBlank(next.getId())){
                iterator.remove();
            }
        }
        dao.deleteUsers(workCommonGroup.getId());
        if(userList ==null|| userList.size()<=0){
            return;
        }
        HashMap<String, Object> map = new HashMap<>(5);
        map.put("groupId", workCommonGroup.getId());
        map.put("userList", userList);
        dao.batchInsert(map);
    }
	
	
}