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

import com.duowenlvshi.duowenlszp.app.util.TypeConversion;
import com.duowenlvshi.duowenlszp.model.jobintension.entity.Jobintension;
import com.duowenlvshi.duowenlszp.model.jobintension.server.JobintensionServer;
import lombok.extern.slf4j.Slf4j;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yang fang yu
 * @since 2019/8/21 15:43
 */
@Slf4j
@Transactional
@Component
public class JobintensionController {
    @Autowired
    JobintensionServer jobintensionServer;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 新增或修改求职信息
     * */
    public int insertJobintension(Map<String,Object> map){
        Map map1 =(Map) map.get("jobintension");
        Jobintension jobintension =(Jobintension) TypeConversion.mapToObject(map1,Jobintension.class);
        jobintension.setExpertise(jobintension.getExpertise()+",");
        int i =jobintensionServer.save(jobintension);
        return i;
    }
    /**
     * 删除求职信息
     * */
    public int deleteJobintension(Map<String,Object> map){
        String id =(String) map.get("id");
        int i =jobintensionServer.delete(id);
        return i;
    }
    /**
     * 查询律师求职信息
     * */
    public List<Map> selectJobintension(Map<String,Object> map){
        String lawyerId =(String) map.get("lawyerId");
        List<Map> list =jobintensionServer.selectByLawyerId(lawyerId);
        for (Map map1 : list) {
            String s = map1.get("expertise")+"";
            s = s.substring(0,s.length() - 1);
            map1.put("expertise",s);
        }
        return list;
    }
    /**
     * 获取精准推送名单
     * */
    public List<Map> selectMatching(Map<String,Object> map){
        String lawyerId = map.get("token")+"";
        Map push =(Map)map.get("push");
        push.put("lawyerId",lawyerId);
        if("sex1".equals(push.get("sex"))){
            push.remove("sex");
        }
        if("yearsOfPractice1".equals(push.get("yearsOfPractice"))){
            push.remove("yearsOfPractice");
        }
        String expertise =(String) push.get("expertise");
        String newStrFirst = expertise.replaceAll(",",",|");
        push.put("expertise",newStrFirst);
        List<Map> list =jobintensionServer.selectPush(push);
        String s = redisTemplate.opsForValue().get("matchingAll");//redis储存匹配数
        long   l = Long.parseLong(s);
        l +=list.size();
        redisTemplate.opsForValue().set("matchingAll",l+"");
        redisTemplate.opsForValue().get("matchingAll");
        return list;
    }
    /**
     * 设置求职者推送状态
     * */
    public int updatePushSet(Map<String,Object> map){
        String token =(String) map.get("token");
        String pushSet =(String) map.get("pushSet");
        Map map1   = new HashMap();
        map1.put("lawyerId",token);
        map1.put("pushSet",pushSet);
        int i =jobintensionServer.updatePushSet(map1);
        if(i==0){
            i=-5;
        }
        String str;
        if("0".equals(pushSet)){
            str="setOff";
        }else if("1".equals(pushSet)){
            str="setUnlimited";
        }else if("2".equals(pushSet)){
            str="setDay";
        }else {
            str="setWeek";
        }
        String s = redisTemplate.opsForValue().get(str);
        long   l = Long.parseLong(s);
        ++l;
        redisTemplate.opsForValue().set(str,l+"");
        return i;
    }

    /**
     *查询求职者推送状态
     * */
    public String selectPushSet(Map<String,Object> map){
        String token =(String) map.get("token");
        return jobintensionServer.selectPushSet(token);
    }
}
