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

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import com.jeeplus.modules.workstaff.entity.WorkStaffEducation;
import com.jeeplus.modules.workstaffachiveslog.entity.WorkStaffAchivesLog;
import com.jeeplus.modules.workstaffachiveslog.service.WorkStaffAchivesLogService;
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.workstaff.entity.WorkStaffExperience;
import com.jeeplus.modules.workstaff.dao.WorkStaffExperienceDao;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工作经历Service
 * @author ssrh
 * @version 2018-07-27
 */
@Service
@Transactional(readOnly = true)
public class WorkStaffExperienceService extends CrudService<WorkStaffExperienceDao, WorkStaffExperience> {
    @Autowired
    private WorkStaffAchivesLogService workStaffAchivesLogService;
    @Autowired
    private UserDao userDao;
	public WorkStaffExperience get(String id) {
		return super.get(id);
	}
	
	public List<WorkStaffExperience> findList(WorkStaffExperience workStaffExperience) {
		return super.findList(workStaffExperience);
	}
	
	public Page<WorkStaffExperience> findPage(Page<WorkStaffExperience> page, WorkStaffExperience workStaffExperience) {
		return super.findPage(page, workStaffExperience);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkStaffExperience workStaffExperience) {
		super.save(workStaffExperience);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkStaffExperience workStaffExperience) {
		super.delete(workStaffExperience);
	}


    public boolean save(WorkStaffBasicInfo workStaffBasicInfo,boolean first) {
        if(workStaffBasicInfo==null|| StringUtils.isBlank(workStaffBasicInfo.getId())){
            return false;
        }
        List<WorkStaffExperience> experienceList = workStaffBasicInfo.getExperienceList();
        if(experienceList==null||experienceList.size()<=0){
            return false;
        }
        boolean flag = false;
        for (WorkStaffExperience entity : experienceList) {
            if(entity!=null&&"1".equals(entity.getDelFlag())&&StringUtils.isBlank(entity.getId())){
                continue;
            }
            if(entity!=null&&"1".equals(entity.getDelFlag())){
                dao.deleteByLogic(entity);
                continue;
            }
            if (entity!=null){
                entity.setStaffId(workStaffBasicInfo.getId());
                this.save(entity);
                if(first){
                    entity.setId(null);
                }
                flag =true;
            }
        }
        return flag;
    }

    public void query(WorkStaffBasicInfo workStaffBasicInfo) {
        if(workStaffBasicInfo==null|| StringUtils.isBlank(workStaffBasicInfo.getId())){
            return;
        }
        WorkStaffExperience entity = new WorkStaffExperience();
        entity.setStaffId(workStaffBasicInfo.getId());
        workStaffBasicInfo.setExperienceList(this.findList(entity));
    }
    private static Map<String,String> EDU_MAP = Maps.newHashMap();//工作经历

    static{//教育经历
        EDU_MAP.put("company", "所在单位");
        EDU_MAP.put("dept", "工作部门");
        EDU_MAP.put("duty", "职务或岗位");
        EDU_MAP.put("startDate", "起始日期");
        EDU_MAP.put("endDate", "终止日期");
        EDU_MAP.put("certifier", "证明人");
        EDU_MAP.put("certifierTel", "证明人联系方式");
    }
    public boolean saveApply(WorkStaffBasicInfo workStaffBasicInfo,boolean first) {
        if(workStaffBasicInfo==null|| StringUtils.isBlank(workStaffBasicInfo.getId())){
            return false;
        }
        List<WorkStaffExperience> list = workStaffBasicInfo.getExperienceList();
        if(list==null||list.size()<=0){
            return false;
        }
        boolean flag = false;
        User u=userDao.get(workStaffBasicInfo.getUserId());
        for (WorkStaffExperience entity : list) {
            entity.setStaffId(workStaffBasicInfo.getId());
            WorkStaffExperience workStaffExperience=dao.get(entity.getId());
            if(workStaffExperience==null){
                entity.setCreateBy(u);
                entity.setCreateDate(new Date());
                entity.setUpdateBy(u);
                entity.setUpdateDate(new Date());
                dao.insert(entity);
            }else{
                this.save(entity);
            }
            if(entity!=null&&"1".equals(entity.getDelFlag())){
                dao.deleteByLogic(entity);
                continue;
            }
            flag =true;
        }
        dao.updateFalt(workStaffBasicInfo.getId());
        return flag;
    }
    public void querys(WorkStaffBasicInfo workStaffBasicInfo) {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        if(workStaffBasicInfo==null|| StringUtils.isBlank(workStaffBasicInfo.getId())){
            return;
        }
        WorkStaffExperience entity = new WorkStaffExperience();
        entity.setStaffId(workStaffBasicInfo.getId());
        List<WorkStaffExperience> list=this.findList(entity);
        List<WorkStaffExperience> newList=Lists.newArrayList();
        if(list!=null&&list.size()>0){//修改
            for(WorkStaffExperience workStaffExperience:list){
                Class<? extends WorkStaffExperience> newClass = workStaffExperience.getClass();
                Field[] declaredFields = newClass.getDeclaredFields();
                for (Field field : declaredFields) {
                    String name = field.getName();
                    if("company".equals(name)||"dept".equals(name)||"duty".equals(name)||"startDate".equals(name)||"endDate".equals(name)
                            || "certifier".equals(name)||"certifierTel".equals(name)){
                        String setMethod = "set"+name.substring(0,1).toUpperCase()+name.substring(1);
                        String getMethod = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
                        WorkStaffAchivesLog workStaffAchivesLog=new WorkStaffAchivesLog();
                        workStaffAchivesLog.setStaffId(workStaffBasicInfo.getId());
                        workStaffAchivesLog.setModule("工作经历");
                        workStaffAchivesLog.setSonId(workStaffExperience.getId());
                        workStaffAchivesLog.setFields(name);
                        workStaffAchivesLog.setType("修改");
                        workStaffAchivesLog.setState("1");
                        List<WorkStaffAchivesLog> logs=workStaffAchivesLogService.findList(workStaffAchivesLog);
                        try {
                            Object newInvoke = newClass.getMethod(getMethod).invoke(workStaffExperience);//修改后
                            if(newInvoke instanceof String){
                                Method setReadOnly = workStaffExperience.getClass().getMethod(setMethod, String.class);
                                if(logs!=null&&logs.size()>0){
                                    setReadOnly.invoke(workStaffExperience,logs.get(0).getNewKey());
                                }
                            }
                            if(newInvoke instanceof Date){//日期
                                Method setReadOnly = workStaffExperience.getClass().getMethod(setMethod, Date.class);
                                if(logs!=null&&logs.size()>0){
                                    Date myDate = dateFormat1.parse(logs.get(0).getNewKey());
                                    setReadOnly.invoke(workStaffExperience,myDate);
                                }
                            }
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
                newList.add(workStaffExperience);
            }
        }
        //新增
        WorkStaffAchivesLog workStaffAchivesLog=new WorkStaffAchivesLog();
        workStaffAchivesLog.setStaffId(workStaffBasicInfo.getId());
        workStaffAchivesLog.setModule("工作经历");
        workStaffAchivesLog.setType("新增");
        workStaffAchivesLog.setState("1");
        List<WorkStaffAchivesLog> sonIds=workStaffAchivesLogService.findSonId(workStaffAchivesLog);
        if(sonIds!=null&&sonIds.size()>0){
            for(WorkStaffAchivesLog w:sonIds){
                workStaffAchivesLog.setSonId(w.getSonId());
                WorkStaffExperience workStaffExperience = new WorkStaffExperience();
                workStaffExperience.setId(w.getSonId());
                workStaffExperience.setDelFlag("0");
                workStaffExperience.setStaffId(workStaffBasicInfo.getId());
                Class<? extends WorkStaffExperience> newClass = workStaffExperience.getClass();
                Field[] declaredFields = newClass.getDeclaredFields();
                for (Field field : declaredFields) {
                    String name = field.getName();
                    if("company".equals(name)||"dept".equals(name)||"duty".equals(name)||"startDate".equals(name)||"endDate".equals(name)
                            || "certifier".equals(name)||"certifierTel".equals(name)){
                        String setMethod = "set"+name.substring(0,1).toUpperCase()+name.substring(1);
                        String getMethod = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
                        workStaffAchivesLog.setFields(name);
                        List<WorkStaffAchivesLog> logs=workStaffAchivesLogService.findList(workStaffAchivesLog);
                        try {
                            if("startDate".equals(name)||"endDate".equals(name)){//日期
                                Method setReadOnly = workStaffExperience.getClass().getMethod(setMethod, Date.class);
                                if(logs!=null&&logs.size()>0){
                                    Date myDate = dateFormat1.parse(logs.get(0).getNewKey());
                                    setReadOnly.invoke(workStaffExperience,myDate);
                                }
                            }else{
                                Method setReadOnly = workStaffExperience.getClass().getMethod(setMethod, String.class);
                                if(logs!=null&&logs.size()>0){
                                    setReadOnly.invoke(workStaffExperience,logs.get(0).getNewKey());
                                }
                            }
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
                newList.add(workStaffExperience);
            }
        }
        //删除
        WorkStaffAchivesLog delLog=new WorkStaffAchivesLog();
        delLog.setStaffId(workStaffBasicInfo.getId());
        delLog.setModule("工作经历");
        delLog.setType("删除");
        workStaffAchivesLog.setState("1");
        List<WorkStaffAchivesLog> dellogs=workStaffAchivesLogService.findList(delLog);
        if(dellogs!=null&&dellogs.size()>0){
            for(WorkStaffAchivesLog w:dellogs){
                newList.remove(dao.get(w.getSonId()));
            }
        }
        workStaffBasicInfo.setExperienceList(newList);
    }
    @Transactional(readOnly = false)
    public void saveEdu(WorkStaffBasicInfo workStaffBasicInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<WorkStaffExperience> experienceList =workStaffBasicInfo.getExperienceList();
        if(experienceList!=null&&experienceList.size()>0){
            for(WorkStaffExperience newExperience:experienceList){
                Class<? extends WorkStaffExperience> newClass = newExperience.getClass();
                Field[] declaredFields = newClass.getDeclaredFields();
                if(newExperience.getId()!=null&&!"1".equals(newExperience.getDelFlag())&&!"".equals(newExperience.getId())){
                    WorkStaffExperience oldExperience=this.get(newExperience.getId());
                    if(oldExperience!=null){
                        Class<? extends WorkStaffExperience> oldClass = oldExperience.getClass();
                        for (Field field : declaredFields) {
                            String name = field.getName();
                            if("company".equals(name)||"dept".equals(name)||"duty".equals(name)||"startDate".equals(name)||"endDate".equals(name)
                                    || "certifier".equals(name)||"certifierTel".equals(name)){
                                String methodName = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
                                WorkStaffAchivesLog workStaffAchivesLog=new WorkStaffAchivesLog();
                                workStaffAchivesLog.setStaffId(workStaffBasicInfo.getId());
                                workStaffAchivesLog.setModule("工作经历");
                                workStaffAchivesLog.setSonId(newExperience.getId());
                                workStaffAchivesLog.setFields(name);
                                workStaffAchivesLog.setState("1");
                                try {
                                    Object newInvoke = newClass.getMethod(methodName).invoke(newExperience);//修改后
                                    Object oldInvoke = oldClass.getMethod(methodName).invoke(oldExperience);//修改前
                                    String describes=EDU_MAP.get(name);
                                    if(newInvoke instanceof String){
                                        String newValue=newInvoke.toString();
                                        String oldValue=oldInvoke.toString();
                                        if(!newValue.equals(oldValue)){
                                                workStaffAchivesLogService.saveStaffLog(workStaffAchivesLog,workStaffBasicInfo.getId(),
                                                        "工作经历",describes,name,
                                                        oldInvoke.toString(),newInvoke.toString(),
                                                        oldInvoke.toString(),newInvoke.toString(),newExperience.getId(),"修改");

                                        }
                                    }
                                    if(newInvoke instanceof Date){//日期
                                        String newDate="";
                                        String oldDate="";
                                        if(newInvoke!=null){
                                            newDate=sdf.format(newInvoke);
                                        }
                                        if (oldInvoke!=null){
                                            oldDate=sdf.format(oldInvoke);
                                        }
                                        if(!newDate.equals(oldDate)){
                                            workStaffAchivesLogService.saveStaffLog(workStaffAchivesLog,workStaffBasicInfo.getId(),
                                                    "工作经历",describes,name,
                                                    oldDate,newDate,oldDate,newDate,newExperience.getId(),"修改");
                                        }
                                    }
                                } catch (NoSuchMethodException e) {
                                    e.printStackTrace();
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                } catch (InvocationTargetException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }
                }else if((newExperience.getId()==null||"".equals(newExperience.getId()))&&"0".equals(newExperience.getDelFlag())){//新增
                    String newId=IdGen.uuid();
                    for (Field field : declaredFields) {
                        String name = field.getName();
                        if("company".equals(name)||"dept".equals(name)||"duty".equals(name)||"startDate".equals(name)||"endDate".equals(name)
                                || "certifier".equals(name)||"certifierTel".equals(name)){
                            String methodName = "get"+name.substring(0,1).toUpperCase()+name.substring(1);
                            WorkStaffAchivesLog workStaffAchivesLog=new WorkStaffAchivesLog();
                            workStaffAchivesLog.setStaffId(workStaffBasicInfo.getId());
                            workStaffAchivesLog.setModule("工作经历");
                            workStaffAchivesLog.setSonId(newId);
                            workStaffAchivesLog.setFields(name);
                            workStaffAchivesLog.setState("1");
                            try {
                                Object newInvoke = newClass.getMethod(methodName).invoke(newExperience);//修改后
                                String describes=EDU_MAP.get(name);
                                if(newInvoke instanceof String){
                                    String newValue=newInvoke.toString();
                                        workStaffAchivesLogService.saveStaffLog(workStaffAchivesLog,workStaffBasicInfo.getId(),
                                                "工作经历",describes,name,
                                                "",newInvoke.toString(),
                                                "",newInvoke.toString(),newId,"新增");

                                }
                                if(newInvoke instanceof Date){//日期
                                    String newDate=sdf.format(newInvoke);
                                    workStaffAchivesLogService.saveStaffLog(workStaffAchivesLog,workStaffBasicInfo.getId(),
                                            "工作经历",describes,name,
                                            "",newDate,"",newDate,newId,"新增");
                                };
                            } catch (NoSuchMethodException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }else if(newExperience.getId()!=null&&"1".equals(newExperience.getDelFlag())&&!"".equals(newExperience.getId())){//删除
                    WorkStaffAchivesLog workStaffAchivesLog=new WorkStaffAchivesLog();
                    workStaffAchivesLog.setStaffId(workStaffBasicInfo.getId());
                    workStaffAchivesLog.setModule("工作经历");
                    workStaffAchivesLog.setSonId(newExperience.getId());
                    workStaffAchivesLog.setType("删除");
                    workStaffAchivesLog.setState("1");
                    workStaffAchivesLogService.saveStaffLog(workStaffAchivesLog,workStaffBasicInfo.getId(),
                            "工作经历","","",
                            "","",
                            "","",newExperience.getId(),"删除");
                }
            }
        }
    }
}