package com.duowenlvshi.duowenlszp.model.work.controller;

import com.duowenlvshi.duowenlszp.model.jobintension.server.JobintensionServer;
import com.duowenlvshi.duowenlszp.app.util.DeletEmoji;
import com.duowenlvshi.duowenlszp.app.util.TypeConversion;
import com.duowenlvshi.duowenlszp.model.work.Server.WorkServer;
import com.duowenlvshi.duowenlszp.model.work.entity.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yang fang yu
 * @since 2019/8/21 15:44
 */
@Transactional
@Component
public class WorkController {
    @Autowired
    WorkServer workServer;
    @Autowired
    JobintensionServer jobintensionServer;
    @Autowired
    DeletEmoji deletEmoji;

    /**
     * 新增或修改工作经历
     * */
    public int insertWork(Map<String,Object> map){
        List<Map> list =(List) map.get("list");
        int i=0;
        for (Map map1 : list) {
            Work work =(Work) TypeConversion.mapToObject(map1,Work.class);
            //过滤表情
            work.setCompany(deletEmoji.filterEmoji(work.getCompany(),""));
            work.setPosition(deletEmoji.filterEmoji(work.getPosition(),""));
            String start =  work.getStartTime();
            String end   = work.getEndingTime();
            double startDate=0;
            double endDate=0;
            SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM");
            try {
                startDate = formatter.parse(start).getTime();
                if("至今".equals(end)){
                    endDate =(double) System.currentTimeMillis();
                }else{
                    endDate =(double) formatter.parse(end).getTime();
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Double year = ((endDate - startDate)/(1000 * 60 * 60 * 24 * 365l));
            work.setYear(year);
             i =workServer.save(work);
            double yearSum = workServer.selectYear(work.getLawyerId());
            //向上取整
            /*yearSum = Math.ceil(yearSum);*/
            String yearsOfPractice=null;
            if(yearSum<3){
                yearsOfPractice = "yearsOfPractice2";
            }else if(yearSum<6){
                yearsOfPractice = "yearsOfPractice3";
            }else if(yearSum<10){
                yearsOfPractice = "yearsOfPractice4";
            }else{
                yearsOfPractice = "yearsOfPractice5";
            }
            jobintensionServer.updateYear(yearsOfPractice,work.getLawyerId());
        }
        return i;
    }
    /**
     * 删除工作经历
     * */
    public int deleteWork(Map<String,Object> map){
        String id =(String) map.get("id");
        int i =workServer.delete(id);
        return i;
    }
    /**
     * 查询律师工作经历
     * */
    public List<Map> selectWork(Map<String,Object> map){
        String lawyerId =(String) map.get("lawyerId");
        List list =workServer.selectByLawyerId(lawyerId);
        return list;
    }
    /**
     * 查询学校code
     * */
    public List<Map> selectSchoolCode(Map<String,Object> map){
        String name =(String) map.get("name");
        String code =(String) map.get("code");
        List<Map> list ;
        if("1".equals(code)){
             list =workServer.selectSchool(name);
        }else{
             list =workServer.selectSchoolVague(name);
        }

        return list;
    }
    /**
     * 删除
     * */
    public int deleteAll(Map<String,Object> map){
        String id =(String) map.get("id");
        String code =(String) map.get("code");
        String token =(String) map.get("token");
        Map map1 = new HashMap();
        map1.put("id","'"+id+"'");
        switch (code){
            case "work":{
                map1.put("surface","lszp_work");
                double yearSum = workServer.selectYear(token);
                String yearsOfPractice;
                if(yearSum<3){
                    yearsOfPractice = "yearsOfPractice2";
                }else if(yearSum<6){
                    yearsOfPractice = "yearsOfPractice3";
                }else if(yearSum<10){
                    yearsOfPractice = "yearsOfPractice4";
                }else{
                    yearsOfPractice = "yearsOfPractice5";
                }
                jobintensionServer.updateYear(yearsOfPractice,token);
                break;
            }
            case "education":{
                map1.put("surface","lszp_education");
                break;
            }
            default:{
                break;
            }
        }
        int i =workServer.deleteAll(map1);
        return i;

    }
}






















