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

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

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.leavetype.dao.LeaveTypeDao;
import com.jeeplus.modules.leavetype.entity.LeaveType;
import com.jeeplus.modules.sys.dao.OfficeDao;
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.workattendance.dao.WorkAttendanceRuleRecordDao;
import com.jeeplus.modules.workattendance.entity.WorkAttendancePlace;
import com.jeeplus.modules.workattendance.entity.WorkAttendanceRuleRecord;
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.modules.workattendance.entity.WorkAttendanceRule;
import com.jeeplus.modules.workattendance.dao.WorkAttendanceRuleDao;

/**
 * 考勤规则Service
 * @author 杨帆
 * @version 2018-09-05
 */
@Service
@Transactional(readOnly = true)
public class WorkAttendanceRuleService extends CrudService<WorkAttendanceRuleDao, WorkAttendanceRule> {
    @Autowired
	private WorkAttendancePlaceService workAttendancePlaceService;
    @Autowired
	private WorkAttendanceRuleRecordDao workAttendanceRuleRecordDao;
    @Autowired
	private OfficeDao officeDao;
    @Autowired
	private LeaveTypeDao leaveTypeDao;

	public WorkAttendanceRule get(String id) {
		WorkAttendanceRule workAttendanceRule =  super.get(id);
		List<WorkAttendancePlace> workAttendancePlaces = workAttendancePlaceService.getByRuleId(workAttendanceRule.getId());
		workAttendanceRule.setWorkAttendancePlaces(workAttendancePlaces);
		WorkAttendanceRuleRecord workAttendanceRuleRecord = new WorkAttendanceRuleRecord();
		workAttendanceRuleRecord.setWorkAttendanceRule(workAttendanceRule);
		List<WorkAttendanceRuleRecord> workAttendanceRuleRecordList = workAttendanceRuleRecordDao.findUsers(workAttendanceRuleRecord);
		List<WorkAttendanceRuleRecord> workAttendanceRuleRecordOfficeList = workAttendanceRuleRecordDao.findOffices(workAttendanceRuleRecord);
		if (workAttendanceRuleRecordList!=null &&workAttendanceRuleRecordList.size()!=0){
			List<User> users = new ArrayList<>();
			for(WorkAttendanceRuleRecord ruleRecord:workAttendanceRuleRecordList){
				users.add(ruleRecord.getUser());
			}
			workAttendanceRule.setUserList(users);
		}
		if (workAttendanceRuleRecordOfficeList!=null &&workAttendanceRuleRecordOfficeList.size()!=0){
			List<Office> offices = new ArrayList<>();
			for(WorkAttendanceRuleRecord ruleRecord:workAttendanceRuleRecordOfficeList){
				Office office = new Office(ruleRecord.getOfficeId());
				office.setName(ruleRecord.getOfficeName());
				offices.add(office);
			}
			workAttendanceRule.setOfficeList(offices);
		}
		if (workAttendanceRule.getLeaveType()!=null){
			LeaveType leaveType = leaveTypeDao.get(workAttendanceRule.getLeaveType());
			if (leaveType!=null){
				workAttendanceRule.setLeaveTypeName(leaveType.getName());
			}
		}

		return workAttendanceRule;

	}
	
	public List<WorkAttendanceRule> findList(WorkAttendanceRule workAttendanceRule) {
		return super.findList(workAttendanceRule);
	}
	public WorkAttendanceRule findAttendanceRuleByCompany(String companyId,String userId,String officeId) {
		WorkAttendanceRule workAttendanceRule = dao.findAttendanceRuleByCompany(companyId,userId,officeId);
		if (workAttendanceRule!=null && StringUtils.isNotBlank(workAttendanceRule.getId())){
			List<WorkAttendancePlace> workAttendancePlaces = workAttendancePlaceService.getByRuleId(workAttendanceRule.getId());
			workAttendanceRule.setWorkAttendancePlaces(workAttendancePlaces);
			WorkAttendanceRuleRecord workAttendanceRuleRecord = new WorkAttendanceRuleRecord();
			workAttendanceRuleRecord.setWorkAttendanceRule(workAttendanceRule);
			List<WorkAttendanceRuleRecord> workAttendanceRuleRecordList = workAttendanceRuleRecordDao.findUsers(workAttendanceRuleRecord);
			List<WorkAttendanceRuleRecord> workAttendanceRuleRecordOfficeList = workAttendanceRuleRecordDao.findOffices(workAttendanceRuleRecord);
			if (workAttendanceRuleRecordList!=null &&workAttendanceRuleRecordList.size()!=0){
				List<User> users = new ArrayList<>();
				for(WorkAttendanceRuleRecord ruleRecord:workAttendanceRuleRecordList){
					users.add(ruleRecord.getUser());
				}
				workAttendanceRule.setUserList(users);
			}
			if (workAttendanceRuleRecordOfficeList!=null &&workAttendanceRuleRecordOfficeList.size()!=0){
				List<Office> offices = new ArrayList<>();
				for(WorkAttendanceRuleRecord ruleRecord:workAttendanceRuleRecordOfficeList){
					Office office = new Office(ruleRecord.getOfficeId());
					office.setName(ruleRecord.getOfficeName());
					offices.add(office);
				}
				workAttendanceRule.setOfficeList(offices);
			}
		}
		return workAttendanceRule;
	}

	public Page<WorkAttendanceRule> findPage(Page<WorkAttendanceRule> page, WorkAttendanceRule workAttendanceRule) {
		if(workAttendanceRule.getBranchOffice()==null || StringUtils.isBlank(workAttendanceRule.getBranchOffice().getId())){
			Office office = UserUtils.getSelectOffice();
			if (StringUtils.isBlank(office.getBranchOffice())){
				workAttendanceRule.setBranchOfficeId(UserUtils.getSelectCompany().getId());
			}else{
				workAttendanceRule.setBranchOfficeId(office.getBranchOffice());
			}

		}
		return super.findPage(page, workAttendanceRule);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkAttendanceRule workAttendanceRule) {
		workAttendanceRule.setCompanyId(UserUtils.getSelectCompany().getId());
		super.save(workAttendanceRule);
		workAttendancePlaceService.deleteByRuleId(workAttendanceRule.getId());
		List<WorkAttendancePlace> workAttendancePlaces = workAttendanceRule.getWorkAttendancePlaces();
		for (WorkAttendancePlace workAttendancePlace:workAttendancePlaces){
			if (workAttendancePlace!=null && StringUtils.isNotBlank(workAttendancePlace.getDelFlag()) && "0".equals(workAttendancePlace.getDelFlag())){
				if(StringUtils.isNotBlank(workAttendancePlace.getItude())) {
					String[] arr = workAttendancePlace.getItude().split(",");
					String x = arr[0];
					String y = arr[1];
					workAttendancePlace.setLongitude(x);
					workAttendancePlace.setLatitude(y);
				}
				workAttendancePlace.setWorkAttendanceRule(workAttendanceRule);
				workAttendancePlace.preInsert();
				workAttendancePlaceService.insert(workAttendancePlace);
			}
		}
		List<User> users = workAttendanceRule.getUserList();
		List<Office> offices = workAttendanceRule.getOfficeList();
		workAttendanceRule.setworkAttendanceRuleRecordIds(users);
		workAttendanceRule.setworkAttendanceRuleRecordOffices(offices);
		WorkAttendanceRuleRecord workAttendanceRuleRecord = new WorkAttendanceRuleRecord();
		workAttendanceRuleRecord.setWorkAttendanceRule(workAttendanceRule);
		// 更新发送接受人记录
		workAttendanceRuleRecordDao.deleteByworkAttendanceRuleId(workAttendanceRuleRecord);
		if (workAttendanceRule.getWorkAttendanceRuleRecordList().size() > 0){
			workAttendanceRuleRecordDao.insertAll(workAttendanceRule.getWorkAttendanceRuleRecordList());
		}
		if (workAttendanceRule.getWorkAttendanceRuleRecordOfficeList().size() > 0){
			workAttendanceRuleRecordDao.insertOfficeAll(workAttendanceRule.getWorkAttendanceRuleRecordOfficeList());
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkAttendanceRule workAttendanceRule) {
		workAttendancePlaceService.deleteByRuleId(workAttendanceRule.getId());
		workAttendanceRuleRecordDao.deleteByRuleId(workAttendanceRule.getId());
		super.delete(workAttendanceRule);
	}
	@Transactional(readOnly = false)
	public String isSave(String id, String userIds, String officeIds,String branchOffice) {
		boolean save = false;
		if(StringUtils.isNotBlank(id)){
			save = true;
		}
		StringBuffer buffer = new StringBuffer();
		if(StringUtils.isNotBlank(userIds)) {
			for (String userId:userIds.split(",")) {
				int count = workAttendanceRuleRecordDao.findUserCount(id,userId,branchOffice);
				if(save){
					if(count > 1){
						User user = UserUtils.get(userId);
						buffer.append(user.getName()+"已存在考勤规则,</br>");
					}
				}else {
					if(count > 0){
						User user = UserUtils.get(userId);
						buffer.append(user.getName()+"已存在考勤规则,</br>");
					}
				}
			}
		}
		if(StringUtils.isNotBlank(officeIds)) {
			for (String officeId:officeIds.split(",")) {
				int count = workAttendanceRuleRecordDao.findOfficeCount(id,officeId,branchOffice);
				if(save){
					if(count > 1){
						Office office = officeDao.get(officeId);
						buffer.append(office.getTopCompany()+"已存在考勤规则,</br>");
					}
				}else {
					if(count > 0){
						Office office = officeDao.get(officeId);
						buffer.append(office.getTopCompany()+"已存在考勤规则,</br>");
					}
				}
			}
		}
		return buffer.toString();
	}

	
	
	
}