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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.modules.sys.entity.Office;
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.common.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.iim.entity.LayGroup;
import com.jeeplus.modules.iim.dao.LayGroupDao;
import com.jeeplus.modules.iim.entity.LayGroupUser;
import com.jeeplus.modules.iim.dao.LayGroupUserDao;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 群组Service
 * @author lgf
 * @version 2016-08-07
 */
@Service
@Transactional(readOnly = true)
public class LayGroupService extends CrudService<LayGroupDao, LayGroup> {

	@Autowired
	private LayGroupUserDao layGroupUserDao;

	public LayGroup get(String id) {
		LayGroup layGroup = super.get(id);
		if (layGroup!=null){
			List<LayGroupUser> layGroupUsers = new ArrayList<>();
			layGroupUsers.addAll(layGroupUserDao.findList(new LayGroupUser(layGroup)));
			layGroup.setLayGroupUserList(layGroupUsers);
		}
		return layGroup;
	}
	public LayGroup getNoOwner(String id) {
		LayGroup layGroup = super.get(id);
		if (layGroup!=null) {
			List<LayGroupUser> layGroupUsers = new ArrayList<>();
			LayGroupUser layGroupUser = new LayGroupUser(layGroup);
			layGroupUser.setIsAdmin("1");
			List<LayGroupUser> list = layGroupUserDao.findList(layGroupUser);
			for (LayGroupUser groupUser : list) {
				if (groupUser.getIsAdmin().equals("0")) {
					layGroupUsers.add(groupUser);
				}
			}
			layGroup.setLayGroupUserList(layGroupUsers);
		}
		return layGroup;
	}

	@Transactional(readOnly = false)
	public int saveUsers(String groupid, String ids) {
		String idArray[] = ids.split(",");
		int count = 0;
		LayGroup group = this.get(groupid);
		if (group!=null) {
			for (String id : idArray) {
				User user = UserUtils.get(id);
					LayGroupUser lgUser = new LayGroupUser();
					lgUser.setGroup(group);
					lgUser.setUser(user);
					lgUser.setIsAdmin("0");
					if (layGroupUserDao.findList(lgUser).size() == 0 && !user.getId().equals(group.getCreateBy().getId())) {
						count++;
						lgUser.preInsert();
						layGroupUserDao.insert(lgUser);
				}
			}
		}
		return count;
	}

	public List<LayGroup> findList(LayGroup layGroup) {
		List<LayGroup> layGroupList= new ArrayList<LayGroup>();
		List<LayGroup> list = super.findList(layGroup);
		for(LayGroup u:list){
			layGroupList.add(this.get(u.getId()));
		}
		return layGroupList;
	}

	public List<LayGroup> findGroupList(User user) {
		List<LayGroup> layGroupList= new ArrayList<LayGroup>();
		LayGroupUser layGroupUser = new LayGroupUser();
		layGroupUser.setUser(user);
		List<LayGroupUser> list = layGroupUserDao.findList(layGroupUser);
		for(LayGroupUser u:list){
			layGroupList.add(this.get(u.getGroup().getId()));
		}
		return layGroupList;
	}
	public List<LayGroup> findGroupListNotOwner(User user) {
		List<LayGroup> layGroupList= new ArrayList<LayGroup>();
		LayGroupUser layGroupUser = new LayGroupUser();
		layGroupUser.setUser(user);
		layGroupUser.setIsAdmin("0");
		List<LayGroupUser> list = layGroupUserDao.findList(layGroupUser);
		for(LayGroupUser u:list){
			layGroupList.add(this.get(u.getGroup().getId()));
		}
		return layGroupList;
	}
	public List<LayGroup> findGroupListOwner(User user) {
		List<LayGroup> layGroupList= new ArrayList<LayGroup>();
		LayGroupUser layGroupUser = new LayGroupUser();
		layGroupUser.setUser(user);
		layGroupUser.setIsAdmin("1");
		List<LayGroupUser> list = layGroupUserDao.findList(layGroupUser);
		for(LayGroupUser u:list){
			layGroupList.add(this.get(u.getGroup().getId()));
		}
		return layGroupList;
	}

	public Page<LayGroup> findPage(Page<LayGroup> page, LayGroup layGroup) {
		layGroup.setPage(page);
		page.setList(this.findList(layGroup));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(LayGroup layGroup,MultipartFile[] files) {
		if(layGroup!=null && StringUtils.isNotBlank(layGroup.getAvatar()) &&files!=null){
			if(files.length>0){
				OSSClientUtil ossClientUtil = new OSSClientUtil();
				for (int i = 0; i < files.length; i++) {
					MultipartFile file = files[i];
					if(!file.isEmpty()&&file.getSize()>0){
						String url = ossClientUtil.uploadFile2OSS(file,"im");
						layGroup.setAvatar(url);
					}
				}
			}
		}
		Boolean save = false;
		if (layGroup.getIsNewRecord()){
			save = true;
		}
		super.save(layGroup);
		if (save){
			LayGroupUser layGroupUser = new LayGroupUser();
			layGroupUser.setGroup(layGroup);
			layGroupUser.setUser(UserUtils.getUser());
			layGroupUser.preInsert();
			layGroupUser.setIsAdmin("1");
			layGroupUserDao.insert(layGroupUser);
		}
		for (LayGroupUser layGroupUser : layGroup.getLayGroupUserList()){
			if (layGroupUser.getId() == null){
				continue;
			}
			if (LayGroupUser.DEL_FLAG_NORMAL.equals(layGroupUser.getDelFlag())){
				if (StringUtils.isBlank(layGroupUser.getId())){
					layGroupUser.setGroup(layGroup);
					layGroupUser.setIsAdmin("0");
					layGroupUser.preInsert();
					layGroupUserDao.insert(layGroupUser);
				}else{
					layGroupUser.preUpdate();
					layGroupUserDao.update(layGroupUser);
				}
			}else{
				layGroupUserDao.delete(layGroupUser);
			}
		}
	}



	@Transactional(readOnly = false)
	public void save(LayGroup layGroup) {
		Boolean save = false;
		if (layGroup.getIsNewRecord()){
			save = true;
		}
		super.save(layGroup);
		if (save){
			LayGroupUser layGroupUser = new LayGroupUser();
			layGroupUser.setGroup(layGroup);
			layGroupUser.setIsAdmin("1");
			layGroupUser.setUser(UserUtils.getUser());
			layGroupUser.preInsert();
			layGroupUserDao.insert(layGroupUser);
		}
		for (LayGroupUser layGroupUser : layGroup.getLayGroupUserList()){
			if (LayGroupUser.DEL_FLAG_NORMAL.equals(layGroupUser.getDelFlag())){
				if (StringUtils.isBlank(layGroupUser.getId())){
					layGroupUser.setGroup(layGroup);
					layGroupUser.preInsert();
					layGroupUser.setIsAdmin("0");
					layGroupUserDao.insert(layGroupUser);
				}else{
					layGroupUser.preUpdate();
					layGroupUserDao.update(layGroupUser);
				}
			}else{
				layGroupUserDao.delete(layGroupUser);
			}
		}
	}

	@Transactional(readOnly = false)
	public void insertLayGroup(LayGroup layGroup,User user,Office comOffice) {
		layGroup.setId(IdGen.uuid());
		layGroup.setCreateBy(user);
		layGroup.setUpdateBy(user);
		Date date = new Date();
		layGroup.setCreateDate(date);
		layGroup.setUpdateDate(date);
		layGroup.setRemarks(comOffice.getId());
		if(layGroup==null || StringUtils.isBlank(layGroup.getAvatar()) ){
			layGroup.setAvatar("https://gangwan-app.oss-cn-hangzhou.aliyuncs.com/app-img/photo/static/qunzhu.png");
		}
		dao.insert(layGroup);
		LayGroupUser layGroupUser = new LayGroupUser();
		layGroupUser.setGroup(layGroup);
		layGroupUser.setIsAdmin("1");
		layGroupUser.setUser(user);
		layGroupUser.preInsert();
		layGroupUserDao.insert(layGroupUser);
	}

	@Transactional(readOnly = false)
	public void delete(LayGroup layGroup) {
		super.delete(layGroup);
		layGroupUserDao.delete(new LayGroupUser(layGroup));
	}
	@Transactional(readOnly = false)
	public int deleteGroupUser(LayGroup layGroup,String userIds) {
		int count = 0;
		try {
			String groupId = layGroup.getId();
			for (String userId:userIds.split(",")){
				if (!layGroup.getCreateBy().getId().equals(userId)) {
					LayGroupUser layGroupUser = new LayGroupUser();
					layGroupUser.setUser(new User(userId));
					layGroupUser.setGroup(new LayGroup(groupId));
					layGroupUserDao.deleteUser(layGroupUser);
					count++;
				}
			}
		}catch (Exception e){

		}
		return count;
	}

}