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

import com.duowenlvshi.duowenlszp.model.Dialogue.server.DialogueServer;
import com.duowenlvshi.duowenlszp.model.Job.entity.Job;
import com.duowenlvshi.duowenlszp.model.Job.server.JobServer;
import com.duowenlvshi.duowenlszp.model.Order.server.OrderServer;
import com.duowenlvshi.duowenlszp.model.jobintension.server.JobintensionServer;
import com.duowenlvshi.duowenlszp.app.util.DeletEmoji;
import com.duowenlvshi.duowenlszp.app.util.RedisUtill;
import com.duowenlvshi.duowenlszp.app.util.SelectRedis;
import com.duowenlvshi.duowenlszp.app.util.TypeConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author yang fang yu
 * @since 2019/8/21 15:43
 */
@Transactional
@Component
public class JobController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    JobServer jobServer;
    @Autowired
    SelectRedis selectRedis;
    @Autowired
    RedisUtill redisUtill;
    @Autowired
    DeletEmoji deletEmoji;
    @Autowired
    OrderServer orderServer;
    @Autowired
    DialogueServer dialogueServer;
    @Autowired
    JobintensionServer jobintensionServer;
    /**
     * 新增或修改招聘信息
     * */
    public int insertJob(Map<String,Object> map){
        Map map1 =(Map) map.get("job");
        int i;
        Job job =(Job) TypeConversion.mapToObject(map1,Job.class);
        String upperOrLower = job.getUpperOrLower();
        if ("upperOrLower1".equals(upperOrLower)){
            String lawyerId = job.getLawyerId();
            List<Map> list = orderServer.selectMeal(lawyerId);
            if(list.size()>0){
                for (Map map2 : list) {
                    long l =new Date().getTime();
                    Date d = (Date)map2.get("endTime");
                    long ll =d.getTime();
                    if(l>ll){
                        return -3;//返回false 套餐已过期
                    }
                }
            }else{
                return -4;//返回false 未购买套餐
            }
        }
        job.setJobrequirements(deletEmoji.filterEmoji(job.getJobrequirements(),""));
        job.setJobresponsibilities(deletEmoji.filterEmoji(job.getJobresponsibilities(),""));
        job.setRecruitmentprofile(jobServer.selectProfile(job.getLawyerId()));
        i =jobServer.save(job);
        return i;
    }
    /**
     * 删除招聘信息
     * */
    public int deleteJob(Map<String,Object> map){
        String id =(String) map.get("id");
        int i =jobServer.delete(id);
        return i;
    }
    /**
     * 查询岗位详情
     * */
    public List selectJob(Map<String,Object> map){
        String id =(String) map.get("jobId");
        String token =(String) map.get("token");
        List<Map> list =jobServer.selectByLawyerId(id);
        for (Map map1 : list) {
           String topic = token+map1.get("lawyer_id")+id;
           map1.put("topic",topic);
           int i = dialogueServer.selectNewTopic(topic);
           if(i>0){
               map1.put("topicState",true);
           }else{
               map1.put("topicState",false);
           }
        }
        return list;
    }
    /**
     * 查询岗位详情(招聘人员版)
     * */
    public List selectJobMy(Map<String,Object> map){
        String jobId =(String) map.get("jobId");
        String lawyerId =(String) map.get("token");
        List<Map> list =jobServer.selectByLawyerIdMy(jobId,lawyerId);
        return list;
    }
    /**
     * 查询招聘岗位列表
     * */
    public List<Map> selectJobList(Map<String,Object> map){
        String lawyerId =(String) map.get("lawyerId");
        int page =(int) map.get("page");
        int pageSize =(int) map.get("pageSize");
        Map map1 = new HashMap();
        map1.put("pageSize",pageSize);
        map1.put("lawyerId",lawyerId);
        if(page==0 && pageSize==0){
            map1.put("pageSize",3);
        }else{
            page = (page-1)*pageSize;
        }
        map1.put("page",page);
        List list =jobServer.selectByLawyerIdList(map1);
        return list;
    }

    /**
     * 首页查询招聘岗位列表
     * */
    public List<Map> selectJobListDefault(Map<String,Object> map){
        List<Map> list;
        String lawyerId =(String) map.get("token");
        String city =(String) map.get("city");
        String yearlySalary = (String) map.get("yearlySalary");
        int page = (int) map.get("page");
        int pageSize = (int) map.get("pageSize");
        page = (page-1)*pageSize;
        Map map1 = new HashMap();
        map1.put("page",page);
        map1.put("pageSize",pageSize);
        if(!"0000".equals(yearlySalary)){
            map1.put("yearlySalary",yearlySalary);
        }
        if(!"0000".equals(city)){
            map1.put("city",city);
        }else{
            List l = new ArrayList();
            List<Map> jobintensionList = jobintensionServer.selectByLawyerId(lawyerId);
            if(jobintensionList.size()>0){
                Map map2 = jobintensionList.get(0);
                String cs =(String) map2.get("city");
                String[] csArray = cs.split(",");
                int length =csArray.length;
                int ll =length/2;
                int i=0;
                int j=0;
                while (length>0){
                    if(length>ll){
                        l.add(csArray[i]+","+csArray[i+1]);
                        i+=2;
                    }else{
                        l.add(csArray[j]+",");
                        j+=2;
                    }
                    --length;
                }
                map1.put("list",l);
                map1.put("listSize",l.size()/2);
            }

        }
        list =jobServer.selectConditionList(map1);
        for (Map map2 : list) {
            String topic = lawyerId+map2.get("lawyer_id")+map2.get("jobId");
            map2.put("topic",topic);
            int i = dialogueServer.selectNewTopic(topic);
            if(i>0){
                map2.put("topicState",true);
            }else{
                map2.put("topicState",false);
            }

        }
        return list;
    }
    /**
     * 查询列表总数据
     * */
    public Integer selectCount(Map<String,Object> map,String code){
        int i =0;
        if("B236".equals(code)){
            String lawyerId =(String) map.get("lawyerId");
            Map map1 = new HashMap();
            map1.put("lawyerId",lawyerId);
             i = jobServer.selectByLawyerIdListCount(map1);
        }else if("B238".equals(code)){
            String city =(String) map.get("city");
            String yearlySalary = (String) map.get("yearlySalary");
            Map map1 = new HashMap();
            if(!"0000".equals(city)){
                map1.put("city",city);
            }
            if(!"0000".equals(yearlySalary)){
                map1.put("yearlySalary",yearlySalary);
            }
             i = jobServer.selectConditionListCount(map1);
        }else if("B251".equals(code)){
            String lawyerId =(String) map.get("token");
            Map map1 = new HashMap();
            map1.put("lawyerId",lawyerId);
            map1.put("upperOrLower","upperOrLower1");
            i = jobServer.selectByLawyerIdListCount(map1);
        }
        return i;
    }

    /**
     * 批量修改招聘简介
     * */
    public int updateProfile(Map<String,Object> map){
        String id =(String) map.get("token");
        String recruitmentProfile =(String) map.get("recruitmentProfile");
        recruitmentProfile = deletEmoji.filterEmoji(recruitmentProfile,"");
        Map map1 = new HashMap<>();
        map1.put("id",id);
        map1.put("recruitmentProfile",recruitmentProfile);
        int i = jobServer.updateProfile(map1);
        return i;
    }
    /**
     * 查询招聘简介
     * */
    public String selectProfile(Map<String,Object> map){
        String id =(String) map.get("token");
        String str = jobServer.selectProfile(id);
        return str;
    }

    /**
     * 查询上架岗位列表信息
     * */
    public List selectPost(Map<String,Object> map){
        String lawyerId =(String) map.get("token");
        int page =(int) map.get("page");
        int pageSize =(int) map.get("pageSize");
        page  = (page-1) * pageSize;
        Map map1 = new HashMap();
        map1.put("lawyerId",lawyerId);
        map1.put("page",page);
        map1.put("pageSize",pageSize);
        List list = jobServer.selectPost(map1);
        return list;
    }
    /**
     * 查询聊天历史记录所需岗位信息
     * */
    public List<Map> selectJobDialongus(Map<String,Object> map){
        String jobId =(String) map.get("jobId");
        return jobServer.selectJobDialongus(jobId);
    }

















































































    /**
     * 查询省code存入Redis
     * */
    public void ProvinceToRedis(){
      List<Map> list  =   jobServer.selectProvince();
        for (Map<String,Integer> map : list) {
            redisTemplate.opsForValue().set(map.get("N_PROVID")+"",map.get("S_PROVNAME")+"");
            System.out.println(map);
        }
    }
    /**
     * 查询城市code存入Redis
     * */
    public void CityToRedis(){
      List<Map> list  =   jobServer.selectCity();
        for (Map map : list) {
            redisTemplate.opsForValue().set(map.get("N_CITYID")+"",map.get("S_CITYNAME")+"");
            System.out.println(map);
        }
    }
    /**
     * 查询学校code存入Redis
     * */
    public void SchoolToRedis(){
        List<Map> list  =   jobServer.selectSchool();
        for (Map map : list) {
            redisTemplate.opsForValue().set(map.get("CODE")+"",map.get("NAME")+"");
        }
    }
    /**
     * 查询code文件存入Redis
     * */
    public void test(){
        redisUtill.RedisRun();
    }

    /**
     * 创建数据统计key
     * */
    public void addKey(){
        redisTemplate.opsForValue().set("pushAll",0+"");//推送数
        redisTemplate.opsForValue().set("matchingAll",0+"");//匹配数
        redisTemplate.opsForValue().set("setUnlimited",0+"");//设置推送不限数
        redisTemplate.opsForValue().set("setDay",0+"");//设置推送每天数
        redisTemplate.opsForValue().set("setWeek",0+"");//设置推送每周数
        redisTemplate.opsForValue().set("setOff",0+"");//设置推送关闭数

    }
    /**
     * 查询key数据
     * */
    public Map selectKey(){
        String pushAll =redisTemplate.opsForValue().get("pushAll");//推送数
        String matchingAll =redisTemplate.opsForValue().get("matchingAll");//匹配数
        String setUnlimited =redisTemplate.opsForValue().get("setUnlimited");//设置推送不限数
        String setDay =redisTemplate.opsForValue().get("setDay");//设置推送每天数
        String setWeek =redisTemplate.opsForValue().get("setWeek");//设置推送每周数
        String setOff =redisTemplate.opsForValue().get("setOff");//设置推送关闭数
        Map map = new HashMap();
        map.put("pushAll",pushAll);
        map.put("matchingAll",matchingAll);
        map.put("setUnlimited",setUnlimited);
        map.put("setDay",setDay);
        map.put("setWeek",setWeek);
        map.put("setOff",setOff);
        return map;
    }
    /**
     * 设置redis值
     * */
    public String setVal(Map<String,Object> map){
        String code =(String) map.get("code");
        String key =(String) map.get("key");
        String val =(String) map.get("val");
        String setOff=null;
        if("1".equals(code)){//设置redis值
            redisTemplate.opsForValue().set(key,val);
            setOff =redisTemplate.opsForValue().get(key);
        }else if("2".equals(code)){//查看redis值
            setOff =redisTemplate.opsForValue().get(key);
        }


        return setOff;
    }
    /**
     * 查询redis岗位
     * */
    public Map setKeyVal(){
        Map<String,Map<String,String>> valMap = new LinkedHashMap();
        Map postMap = new LinkedHashMap();
        Map msgTypeMap = new LinkedHashMap();
        Map yearlySalaryMap = new LinkedHashMap();
        Map sexMap = new LinkedHashMap();
        Map yearsOfPracticeMap = new LinkedHashMap();
        Map businessTypeMap = new LinkedHashMap();
        Map expertiseMap = new LinkedHashMap();
        Map upperOrLowerMap = new LinkedHashMap();
        Map educationMap = new LinkedHashMap();
        valMap.put("postMap",postMap);
        valMap.put("msgTypeMap",msgTypeMap);
        valMap.put("yearlySalaryMap",yearlySalaryMap);
        valMap.put("sexMap",sexMap);
        valMap.put("yearsOfPracticeMap",yearsOfPracticeMap);
        valMap.put("businessTypeMap",businessTypeMap);
        valMap.put("expertiseMap",expertiseMap);
        valMap.put("upperOrLowerMap",upperOrLowerMap);
        valMap.put("educationMap",educationMap);
        List<String> list  = new ArrayList();
        list.add("post");
        list.add("msgType");
        list.add("yearlySalary");
        list.add("sex");
        list.add("yearsOfPractice");
        list.add("businessType");
        list.add("expertise");
        list.add("upperOrLower");
        list.add("education");
        for (String s : list) {
            int i=1;
            while (true){
                String val =redisTemplate.opsForValue().get(s+i);
                if(val==null){
                    break;
                }
                Map map3 =valMap.get(s+"Map");
                map3.put(s+i,val);
                i++;
            }
        }
        return valMap;
    }
}
