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


import com.duowenlvshi.duowenlszp.app.util.AllPush;
import com.duowenlvshi.duowenlszp.app.util.SelectRedis;
import com.duowenlvshi.duowenlszp.app.util.TypeConversion;

import com.duowenlvshi.duowenlszp.model.Job.server.JobServer;
import com.duowenlvshi.duowenlszp.model.Order.server.OrderServer;
import com.duowenlvshi.duowenlszp.model.Push.entity.Push;
import com.duowenlvshi.duowenlszp.model.Push.server.PushServer;
import com.duowenlvshi.duowenlszp.model.lawyer.server.LawyerServer;

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/27 11:42
 */
@Transactional
@Component
public class PushController {
    @Autowired
    PushServer pushServer;
    @Autowired
    AllPush allPush;
    @Autowired
    JobServer jobServer;
    @Autowired
    OrderServer orderServer;
    @Autowired
    SelectRedis selectRedis;
    @Autowired
    LawyerServer lawyerServer;
    @Autowired
    StringRedisTemplate redisTemplate;



    /**
     * 保存推送名单
     * */
    public int insertPushList(Map<String,Object> map){
        int i=0;
        Map map1 =(Map)map.get("job");
        List<Map> list =(List)map.get("list");
        Push push =(Push) TypeConversion.mapToObject(map1,Push.class);
        List<Map> list2 = orderServer.selectMeal(push.getLawyerIda());
        if(list2.size()>0){
            for (Map map2 : list2) {
                long l =new Date().getTime();
                Date d = (Date)map2.get("endTime");
                long ll =d.getTime();
                if(l>ll){
                    return -3;//返回false 套餐已过期
                }
            }
        }else if(list2.size()==0){
            return -4;//返回false 未购买套餐
        }
        Map map4 =  jobServer.selectByIdPush(push.getJobId());
        List<Map> list1  = new ArrayList();
        list1.add(map4);
        selectRedis.SelectList(list1,"");
        Map<String,String> pushMap =list1.get(0);
        pushMap.put("Text",pushMap.get("real_name")+"律师想邀请您面试"+pushMap.get("postVal")+"岗位,您是否有兴趣呢?快去看看吧!");
        pushMap.put("Alert",pushMap.get("real_name")+"律师想邀请您面试"+pushMap.get("postVal")+"岗位,您是否有兴趣呢?快去看看吧!");//设置ios内容
        pushMap.put("NcOrTrans","3");//设置华为透传还是消息栏 1消息栏 2透传 3全部
        pushMap.put("type","38");//设置跳转参数
        for (Map<String,String> map2 : list) {
            pushMap.put("Title","招聘: "+pushMap.get("real_name")+"律师");//设置律师名字
            push.setLawyerIdb(map2.get("id"));
            pushMap.put("lawyerId",map2.get("id"));//把被推送者lawyerId放入推送map中
            Map extraFieldMap = new HashMap();//设置消息盒子信息
            extraFieldMap.put("jobId",pushMap.get("jobId"));
            allPush.AllPush(pushMap,extraFieldMap);
            i= pushServer.save(push);
            i+=i;
        }
        String s = redisTemplate.opsForValue().get("pushAll");//redis储存推送数
        long   l = Long.parseLong(s);
        l +=list.size();
        redisTemplate.opsForValue().set("pushAll",l+"");
        return i;
    }
    /**
     * 查询精准推送岗位列表
     * */
    public List<Map> selectPushList(Map<String,Object> map){
        String lawyerIdb =(String) map.get("lawyerIdb");
        int page =(int) map.get("page");
        int pageSize =(int) map.get("pageSize");
        Map map1 = new HashMap();
        page = (page - 1 )* pageSize;
        map1.put("lawyerIdb",lawyerIdb);
        map1.put("page",page);
        map1.put("pageSize",pageSize);
        return pushServer.selectByLawyerIdBList(map1);
    }

    /**
     * 查询精准推送岗位列表总数
     * */
    public int selectPushListCount(Map<String,Object> map){
        String lawyerIdb =(String) map.get("lawyerIdb");
        return pushServer.selectByLawyerIdBListCount(lawyerIdb);
    }


}
