package com.goafanti.personnel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.*;
import com.goafanti.common.model.*;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.personnel.bo.*;
import com.goafanti.personnel.service.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonnelServiceImpl  extends BaseMybatisDao<PersonnelDossierMapper> implements PersonnelService {
	@Autowired
	private PersonnelDossierMapper	personnelDossierMapper;
	@Autowired
	private PersonnelContactMapper	personnelContactMapper;
	@Autowired
	private PersonnelEntryMapper	personnelEntryMapper;
	@Autowired
	private PersonnelPromotionMapper	personnelPromotionMapper;
	@Autowired
	private PersonnelContractMapper	personnelContractMapper;
	@Autowired
	private PersonnelChooseMapper	personnelChooseMapper;
	@Autowired
	private PersonnelQuitMapper	personnelQuitMapper;
	@Autowired
	private PersonnelLevelMapper	personnelLevelMapper;
	@Autowired
	private PersonnelWelfareMapper	personnelWelfareMapper;
	@Autowired
	private PersonnelMidMapper	personnelMidMapper;
	@Autowired
	private AdminMapper	adminMapper;
	
	@Override
	public int addPersonnel(InputPersonnelBo i) {
		if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)) {
			Admin a=adminMapper.selectById(TokenManager.getAdminId());
			if (a!=null) {
				i.setCompany(a.getDepartmentId());
			}
		}
		return personnelDossierMapper.insertSelective(i);
	}

	@Override
	public int updatePersonnel(InputPersonnelBo i) {
		return personnelDossierMapper.updateByPrimaryKeySelective(i);
	}

	@Override
	public int updateContact(InputPersonnelContactBo i) {
		PersonnelDossier p=new PersonnelDossier();
		p.setId(i.getId());
		p.setMobile(i.getMobile());
		p.setFixedTel(i.getFixedTel());
		personnelDossierMapper.updateByPrimaryKeySelective(p);
		JSONArray ja = (JSONArray) JSON.parse(i.getContactList());
		List<PersonnelContact> list = new ArrayList<PersonnelContact>();
		if (ja != null && !ja.isEmpty()) {
			PersonnelContact pc = null;
			for (int idx = 0; idx < ja.size(); idx++) {
				pc = ja.getJSONObject(idx).toJavaObject(PersonnelContact.class);
				list.add(pc);
			}
		}
		for (PersonnelContact pc : list) {
			if (pc.getId()!=null) {
				personnelContactMapper.updateByPrimaryKeySelective(pc);
			}else {
				personnelContactMapper.insertSelective(pc);
			}
			
		}
		PersonnelContact pc =list.get(0);
		PersonnelMid m=new PersonnelMid();
		m.setId(i.getId());
		m.setEmergencyContact(pc.getEmergencyContact());
		m.setEmergencyMobile(pc.getEmergencyMobile());
		PersonnelMid pm=personnelMidMapper.selectByPrimaryKey(i.getId());
		if (pm==null||pm.getId()==null) {
			personnelMidMapper.insertSelective(m);
		}else {
			personnelMidMapper.updateByPrimaryKeySelective(m);
			
		}
		
		return 1;
	}

	@Override
	public int updateEntry(InputPersonnelEntryBo i) {
		PersonnelEntry p=personnelEntryMapper.selectByPrimaryKey(i.getId());
		if (p==null||p.getId()==null) {
			personnelEntryMapper.insertSelective(i);
		}else {
			personnelEntryMapper.updateByPrimaryKeySelective(i);
		}
		return 1;
	}

	@Override
	public int updatePromotion(InputPersonnelPromotionBo i) {
		if (i.getId()==null) {
			personnelPromotionMapper.insertSelective(i);
		}else {
			personnelPromotionMapper.updateByPrimaryKeySelective(i);
		}
		PersonnelMid p=new PersonnelMid();
		p.setId(i.getPdId());
		p.setPromotionRemarks(i.getPromotionRemarks());
		p.setPromotionTime(i.getPromotionTime());
		PersonnelMid pm=personnelMidMapper.selectByPrimaryKey(i.getPdId());
		if (pm==null||pm.getId()==null) {
			personnelMidMapper.insertSelective(p);
		}else {
			personnelMidMapper.updateByPrimaryKeySelective(p);
			
		}
		return 1;
	}

	@Override
	public int checkDoorId(Integer doorId,Integer id) {
		int i=personnelDossierMapper.checkDoorId(doorId,id);
		return i;
	}
	@Override
	public int checkEmployeeId(Integer employeeId,Integer id) {
		int i=personnelDossierMapper.checkEmployeeId(employeeId,id);
		return i;
	}

	@Override
	public int updateContract(InputPersonnelContractBo i) {
		if (i.getId()==null) {
			personnelContractMapper.insertSelective(i);
		} else {
			personnelContractMapper.updateByPrimaryKeySelective(i);
		}
		PersonnelMid p=new PersonnelMid();
		p.setId(i.getPdId());
		p.setContractStartTime(i.getStartTime());
		p.setContractEndTime(i.getEndTime());
		PersonnelMid pm=personnelMidMapper.selectByPrimaryKey(i.getPdId());
		if (pm==null||pm.getId()==null) {
			personnelMidMapper.insertSelective(p);
		}else {
			personnelMidMapper.updateByPrimaryKeySelective(p);
			
		}
		return 1;
	}

	@Override
	public int updateChoose(InputPersonnelChooseBo i) {
		if (i.getId()==null) {
			personnelChooseMapper.insertSelective(i);
		} else {
			personnelChooseMapper.updateByPrimaryKeySelective(i);
		}
		return 1;
	}

	@Override
	public int updateQuit(InputPersonnelQuitBo i) {
		if (i.getId()==null) {
			personnelQuitMapper.insertSelective(i);
		} else {
			personnelQuitMapper.updateByPrimaryKeySelective(i);
		}
		PersonnelMid p=new PersonnelMid();
		p.setId(i.getPdId());
		p.setQuitTime(i.getEffectTime());
		PersonnelMid pm=personnelMidMapper.selectByPrimaryKey(i.getPdId());
		if (pm==null||pm.getId()==null) {
			personnelMidMapper.insertSelective(p);
		}else {
			personnelMidMapper.updateByPrimaryKeySelective(p);
			
		}
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OutPersonnelListBo> selectList(InputPersonnelListBo i) {
		Map<String, Object> params = new HashMap<>();
		if (i.getPageNo()==null||i.getPageNo()<1)i.setPageNo(1);
		if (i.getPageSize()==null||i.getPageSize()<1) i.setPageSize(10);
		params.put("i", i);
		if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)) {
			params.put("aid", TokenManager.getAdminId());	
		}
		Pagination<OutPersonnelListBo>p=(Pagination<OutPersonnelListBo>) findPage("selectPersonnelByList", "selectPersonnelByCount",
				params, i.getPageNo(), i.getPageSize());
		return p;
	}

	@Override
	public OutPersonnelBo selectBypdId(Integer id) {
		OutPersonnelBo op=	personnelDossierMapper.selectBypdId(id);
		op.setContactList(personnelContactMapper.selectBypdId(id));
		PersonnelEntry pe=personnelEntryMapper.selectByPrimaryKey(id);
		op.setPersonnelEntry(pe);
		op.setPromotionList(personnelPromotionMapper.selectBypdId(id));
		op.setContractList(personnelContractMapper.selectBypdId(id));
		op.setChooseList(personnelChooseMapper.selectBypdId(id));
		op.setQuitList(personnelQuitMapper.selectBypdId(id));
		op.setWelfareList(personnelWelfareMapper.selectBypdId(id));
		return op;
	}

	

	@Override
	public List<OutPersonnelLvlBo> selectLvlList() {
		return personnelLevelMapper.selectAll();
	}

	@Override
	public int updateLvl(InputPersonnelLvlBo i) {
		if (i.getId()==null) {
			personnelLevelMapper.insertSelective(i);
		}else {
			personnelLevelMapper.updateByPrimaryKeySelective(i);
		}
		return 1;
	}

	@Override
	public int updateWelfare(IntpersonnelWelfareBo i) {
		PersonnelWelfare p=personnelWelfareMapper.selectByPrimaryKey(i.getId());
		if (p==null||p.getId()==null) {
			if (i.getType()!=null) {
				personnelWelfareMapper.insertSelective(i);
			}
		}else {
			personnelWelfareMapper.updateByPrimaryKey(i);
		}
		PersonnelMid m=new PersonnelMid();
		m.setId(i.getPdId());
		m.setWelfareAnnexUrl(i.getAnnexUrl());
		m.setWelfareAnnexName(i.getAnnexName());
		m.setWelfareRemarks(i.getRemarks());
		PersonnelMid pm=personnelMidMapper.selectByPrimaryKey(i.getPdId());
		if (i.getType()!=null) {
			if (i.getType()==0) {
				m.setInsuranceStartTime(i.getStartTime());
				m.setInsuranceEndTime(i.getEndTime());
			}else if (i.getType()==1) {
				m.setFundStartTime(i.getStartTime());
				m.setFundEndTime(i.getEndTime());
			}else if (i.getType()==2) {
				m.setEmployerStartTime(i.getStartTime());
				m.setEmployerEndTime(i.getEndTime());
			}
		}
		if (pm==null||pm.getId()==null) {
			personnelMidMapper.insertSelective(m);
		}else {
			personnelMidMapper.updateByidTime(m,i.getType());
			
		}
		return 1;
	}


}
