package com.yeejoin.amos.boot.module.common.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.typroject.tyboot.core.foundation.utils.Bean;
import com.yeejoin.amos.boot.biz.common.entity.DataDictionary;
import com.yeejoin.amos.boot.module.common.api.dto.DutyFirstAidDto;
import com.yeejoin.amos.boot.module.common.api.entity.OrgUsr;
import com.yeejoin.amos.boot.module.common.api.service.IDutyFirstAidService;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest;

@Service
public class DutyFirstAidServiceImpl extends DutyCommonServiceImpl implements IDutyFirstAidService {




	@Autowired
	OrgUsrServiceImpl orgUsrServiceImpl;
	
   private static final String POSTTYPENAME="postTypeName";
	private static final String USERNAME= "userName";


	public String getGroupCode() {
		return "dutyFirstAid";
	}

	@Override
	public DutyFirstAidDto save(DutyFirstAidDto dto) {
		Map<String, Object> fireStationDtoResult = dutyPersonShiftMapper.getFireTeamBySequenceNbr(Long.parseLong(dto.getFirstAidId()));
		if(fireStationDtoResult==null || !fireStationDtoResult.containsKey("name")) {
			throw new BadRequest("此120急救站不存在");
		}
		dto.setFirstAid(fireStationDtoResult.get("name").toString());
		Map<String, Object> map = Bean.BeantoMap(dto);
		Long instanceId = excuteDynamicFormInstance(dto.getUserId(), map, this.getGroupCode());
		saveDutyPersonShiftDetail(instanceId, dto.getDutyShift(), dto.getStartTime());
		insertPersonShift(instanceId, dto.getDutyShift());
		return dto;
	}

	@Override
	public DutyFirstAidDto update(Long instanceId, DutyFirstAidDto dto) {
		updateDynamicFormInstance(this.getGroupCode(), instanceId, Bean.BeantoMap(dto));
		insertPersonShift(instanceId, dto.getDutyShift());
		return dto;
	}

	@Override
	public Object getSchedulingDutyForSpecifyDate(String dutyDay, Long shiftId, String postType, String fieldCode) {
		List<Map<String, Object>> equipmentList = dutyPersonShiftMapper.getEquipmentForSpecifyDate(dutyDay,
				this.getGroupCode(), "firstAidId", "firstAid", "teamName", "result.firstAidId");
		if (equipmentList == null || equipmentList.size() < 1 || equipmentList.get(0) == null) {
			return null;
		}
		List<Object> detailList = new ArrayList<Object>();
		for (Map<String, Object> map : equipmentList) {
			List<Object> resultList = new ArrayList<Object>();

			resultList.add( map.get("firstAid").toString());

			String carId = map.get("firstAidId").toString();
			Map<String, Object> instanceMap = dutyPersonShiftMapper.getInstanceIdForSpecifyDateAndEquipment(dutyDay,
					this.getGroupCode(), carId);
			if (instanceMap == null) {
				continue;
			}
			String instanceId = instanceMap.get("instanceIds").toString();
			if (StringUtils.isNotBlank(instanceId)) {
				String[] instanceIds = instanceId.split(",");

						// 获取当前装备ID下的排版数据
						List<Map<String, Object>> specifyDateList = dutyPersonShiftMapper
								.getPositionStaffDutyForSpecifyDate(dutyDay, this.getGroupCode(), instanceIds,
										null, fieldCode);
						if (specifyDateList == null || specifyDateList.size() < 1 || specifyDateList.get(0) == null) {
							continue;
						}

						for (Map<String, Object> specify : specifyDateList) {
							LinkedHashMap<String, Object> infoMap_2 = new LinkedHashMap<String, Object>();

							if( specify.containsKey(POSTTYPENAME)&& specify.get(POSTTYPENAME)!=null && specify.containsKey(USERNAME)&& specify.get(USERNAME)!=null ) {
								infoMap_2.put(specify.get(POSTTYPENAME).toString(),specify.get(USERNAME).toString());
								resultList.add(infoMap_2);
							}

				}
			}
			detailList.add(resultList);
		}
		return detailList;
	}

	public List< Map<String, Object>> getFirstAidExportData(List<String> ids) {
		List< Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		List< Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<String> userNameList= new ArrayList<String>();


		List<String> firstAidSimpleList = new ArrayList<String>();
		List<String> companyNameList = new ArrayList<String>();

		String firstAidCompanyId = dutyPersonShiftMapper.getFirstAidCompanyId();
		if (!"".equals(firstAidCompanyId) && firstAidCompanyId != null){
			ids.add(firstAidCompanyId);
			OrgUsr companyDetail =	orgUsrServiceImpl.getDetailById(Long.parseLong(firstAidCompanyId));
			String companyNameString = companyDetail.getBizOrgName()+ "@" +companyDetail.getSequenceNbr();
			companyNameList.add(companyNameString);
		}

		String typeString = "JJZ";
		ids.stream().forEach(i->{
			Map<String, Object> detailMap = new HashMap<String, Object>();
			if (!i.equals(firstAidCompanyId)){
				List<OrgUsr> personList = orgUsrServiceImpl.getPersonListByParentId(Long.parseLong(i));
				personList.stream().forEach(m -> {
					String userNameString =m.getBizOrgName()+ "@" +m.getSequenceNbr();
					userNameList.add(userNameString);
				});
				detailMap.put(USERNAME, userNameList);
			}

			//单位默认为 消防救援保障部，不可编辑。
			detailMap.put("companyName", companyNameList);

			List<DataDictionary>  dataDicList=	dataDictionaryService.getByType(typeString);
			List<String> dataDicSimpleList = new ArrayList<String>();
				dataDicList.stream().forEach(l->{
					String dataDic = l.getName() + "@" +l.getCode();
					dataDicSimpleList.add(dataDic);
				});

			detailMap.put(POSTTYPENAME,dataDicSimpleList);
			List<Map<String, Object>> list = dutyPersonShiftMapper.getFirstAidForTypeCodeAndCompanyId(
					Long.parseLong(firstAidCompanyId));
			list.stream().forEach(m -> {
				String firstAidNameString = m.get("name").toString() + "@" + m.get("sequence_nbr").toString();
				if (!firstAidSimpleList.contains(firstAidNameString)){
					firstAidSimpleList.add(firstAidNameString);
				}
			});
			if( firstAidSimpleList != null && firstAidSimpleList.size() >= 1 ) {
				detailMap.put("firstAidName", firstAidSimpleList);
			}
			resultList.add(detailMap);
		});
		for (int i = 0; i < resultList.size(); i++) {

				if (resultList.get(i).size() == 4){
					result.add(resultList.get(i));
				}
				if (result.size()>0){
					break;
				}
		}

		return result;
	}

}
