package com.jpsoft.shinestar.modules.base.service.impl;

import java.util.*;
import javax.annotation.Resource;

import com.jpsoft.shinestar.modules.base.dao.MeetingAddressDAO;
import com.jpsoft.shinestar.modules.base.dao.MeetingPersonDAO;
import com.jpsoft.shinestar.modules.base.dao.MeetingTimeDAO;
import com.jpsoft.shinestar.modules.base.entity.MeetingAddress;
import com.jpsoft.shinestar.modules.base.entity.MeetingPerson;
import com.jpsoft.shinestar.modules.base.entity.MeetingTime;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.MeetingInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.MeetingInfo;
import com.jpsoft.shinestar.modules.base.service.MeetingInfoService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="meetingInfoService")
public class MeetingInfoServiceImpl implements MeetingInfoService {
	@Resource(name="meetingInfoDAO")
	private MeetingInfoDAO meetingInfoDAO;

	@Resource(name="meetingPersonDAO")
	private MeetingPersonDAO meetingPersonDAO;

	@Resource(name="meetingTimeDAO")
	private MeetingTimeDAO meetingTimeDAO;

	@Resource(name="meetingAddressDAO")
	private MeetingAddressDAO meetingAddressDAO;

	@Override
	public MeetingInfo get(String id) {
		// TODO Auto-generated method stub
		return meetingInfoDAO.get(id);
	}

	@Override
	public int insert(MeetingInfo model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return meetingInfoDAO.insert(model);
	}

	@Override
	public int insertAndPerson(MeetingInfo model){

		int count = meetingInfoDAO.insert(model);

		if(count>0){
			String addressId = model.getAddressId();
			MeetingAddress meetingAddress = new MeetingAddress();
			meetingAddress.setId(UUID.randomUUID().toString());
			meetingAddress.setAddressId(addressId);
			meetingAddress.setMeetingId(model.getId());
			meetingAddress.setCreateBy(model.getCreateBy());
			meetingAddress.setCreateTime(model.getCreateTime());
			meetingAddress.setDelFlag(false);

			meetingAddressDAO.insert(meetingAddress);


			List<MeetingTime> meetingTimeDataList = model.getMeetingTimeDataList();

			for (MeetingTime meetingTime:meetingTimeDataList) {
				meetingTime.setId(UUID.randomUUID().toString());
				meetingTime.setMeetingId(model.getId());
				meetingTime.setCreateBy(model.getCreateBy());
				meetingTime.setCreateTime(model.getCreateTime());
				meetingTime.setDelFlag(false);

				if(StringUtils.isNotEmpty(meetingTime.getMeetingTimeRange())) {
					String[] array = meetingTime.getMeetingTimeRange().split(",");
					JSONObject jsonObject  = new JSONObject();
					jsonObject.put("start",array[0]);
					jsonObject.put("end",array[1]);
					meetingTime.setMeetingTime(jsonObject.toString());
				}

				if(StringUtils.isNotEmpty(meetingTime.getStartSignInTimeRange())) {
					String[] array = meetingTime.getStartSignInTimeRange().split(",");
					JSONObject jsonObject  = new JSONObject();
					jsonObject.put("start",array[0]);
					jsonObject.put("end",array[1]);
					meetingTime.setStartSignInTime(jsonObject.toString());
				}

				if(StringUtils.isNotEmpty(meetingTime.getEndSignInTimeRange())) {
					String[] array = meetingTime.getEndSignInTimeRange().split(",");
					JSONObject jsonObject  = new JSONObject();
					jsonObject.put("start",array[0]);
					jsonObject.put("end",array[1]);
					meetingTime.setEndSignInTime(jsonObject.toString());
				}

				meetingTimeDAO.insert(meetingTime);
			}


			if(StringUtils.isNotEmpty(model.getSelectPersonKey())){
				String[] selectPersonKeyArray = model.getSelectPersonKey().split(",");
				for (String key :selectPersonKeyArray) {
					MeetingPerson meetingPerson = new MeetingPerson();
					meetingPerson.setId(UUID.randomUUID().toString());
					meetingPerson.setCreateBy(model.getCreateBy());
					meetingPerson.setCreateTime(model.getCreateTime());
					meetingPerson.setDelFlag(false);
					meetingPerson.setPersonId(Long.parseLong(key));
					meetingPerson.setMeetingInfoId(model.getId());
					meetingPersonDAO.insert(meetingPerson);
				}
			}
		}

		return count;
	}

	@Override
	public int update(MeetingInfo model) {
		// TODO Auto-generated method stub
		return meetingInfoDAO.update(model);		
	}

	@Override
	public int updateAndPerson(MeetingInfo model){
		int count = meetingInfoDAO.update(model);

		if(count>0){
			//地址相关
			List<MeetingAddress> meetingAddressList = meetingAddressDAO.findByMeetingId(model.getId());
			for (MeetingAddress meetingAddress:meetingAddressList) {
				meetingAddress.setDelFlag(true);
				meetingAddress.setUpdateBy(model.getUpdateBy());
				meetingAddress.setUpdateTime(model.getUpdateTime());
				meetingAddressDAO.update(meetingAddress);
			}

			String addressId = model.getAddressId();
			MeetingAddress meetingAddress = new MeetingAddress();
			meetingAddress.setId(UUID.randomUUID().toString());
			meetingAddress.setAddressId(addressId);
			meetingAddress.setMeetingId(model.getId());
			meetingAddress.setCreateBy(model.getCreateBy());
			meetingAddress.setCreateTime(model.getCreateTime());
			meetingAddress.setDelFlag(false);

			meetingAddressDAO.insert(meetingAddress);


			//会议时间相关
			List<MeetingTime> curMeetingTimeList = meetingTimeDAO.findByMeetingId(model.getId());

			for (MeetingTime meetingTime:curMeetingTimeList) {
				meetingTime.setDelFlag(true);
				meetingTime.setUpdateBy(model.getUpdateBy());
				meetingTime.setUpdateTime(model.getUpdateTime());
				meetingTimeDAO.update(meetingTime);
			}

			List<MeetingTime> meetingTimeDataList = model.getMeetingTimeDataList();
			for (MeetingTime meetingTime:meetingTimeDataList) {
				meetingTime.setId(UUID.randomUUID().toString());
				meetingTime.setMeetingId(model.getId());
				meetingTime.setCreateBy(model.getCreateBy());
				meetingTime.setCreateTime(model.getCreateTime());
				meetingTime.setDelFlag(false);

				if(StringUtils.isNotEmpty(meetingTime.getMeetingTimeRange())) {
					String[] array = meetingTime.getMeetingTimeRange().split(",");
					JSONObject jsonObject  = new JSONObject();
					jsonObject.put("start",array[0]);
					jsonObject.put("end",array[1]);
					meetingTime.setMeetingTime(jsonObject.toString());
				}

				if(StringUtils.isNotEmpty(meetingTime.getStartSignInTimeRange())) {
					String[] array = meetingTime.getStartSignInTimeRange().split(",");
					JSONObject jsonObject  = new JSONObject();
					jsonObject.put("start",array[0]);
					jsonObject.put("end",array[1]);
					meetingTime.setStartSignInTime(jsonObject.toString());
				}

				if(StringUtils.isNotEmpty(meetingTime.getEndSignInTimeRange())) {
					String[] array = meetingTime.getEndSignInTimeRange().split(",");
					JSONObject jsonObject  = new JSONObject();
					jsonObject.put("start",array[0]);
					jsonObject.put("end",array[1]);
					meetingTime.setEndSignInTime(jsonObject.toString());
				}

				meetingTimeDAO.insert(meetingTime);
			}

			//原参会人员
			List<MeetingPerson> meetingPersonList = meetingPersonDAO.findByMeetingId(model.getId());

			//新参会人员
			Set<Long> selectPersonSet = new HashSet<>();

			if(StringUtils.isNotEmpty(model.getSelectPersonKey())) {
				String[] selectPersonKeyArray = model.getSelectPersonKey().split(",");

				for (String key : selectPersonKeyArray) {
					selectPersonSet.add(Long.valueOf(key));
				}
			}

			Set<Long> meetingPersonSet = new HashSet<>();

			for (MeetingPerson meetingPerson : meetingPersonList) {
				//原先参会人员被删除
				if(!selectPersonSet.contains(meetingPerson.getPersonId())){
					meetingPerson.setDelFlag(true);
					meetingPerson.setUpdateBy(model.getUpdateBy());
					meetingPerson.setUpdateTime(model.getUpdateTime());
					meetingPersonDAO.update(meetingPerson);
				}
				else {
					//当前参会人员
					meetingPersonSet.add(meetingPerson.getPersonId());
				}
			}

			if(StringUtils.isNotEmpty(model.getSelectPersonKey())){
				for (Long personId : selectPersonSet) {
					//新参会人员
					if(!meetingPersonSet.contains(personId)) {
						MeetingPerson meetingPerson = new MeetingPerson();
						meetingPerson.setId(UUID.randomUUID().toString());
						meetingPerson.setCreateBy(model.getCreateBy());
						meetingPerson.setCreateTime(model.getCreateTime());
						meetingPerson.setDelFlag(false);
						meetingPerson.setPersonId(personId);
						meetingPerson.setMeetingInfoId(model.getId());
						meetingPersonDAO.insert(meetingPerson);
					}
				}
			}

		}
		return count;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return meetingInfoDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = meetingInfoDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<MeetingInfo> list() {
		// TODO Auto-generated method stub
		return meetingInfoDAO.list();
	}

	@Override
	public  List<MeetingInfo> notEndList(){
		return meetingInfoDAO.notEndList();
	}
		
	@Override
	public Page<MeetingInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,List<Sort> sortList) {
        Page<MeetingInfo> page = PageHelper.startPage(pageNumber,pageSize).doSelectPage(()->{
            meetingInfoDAO.search(searchParams,sortList);
        });
        
        return page;
	}
}