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

import com.duowenlvshi.duowenlszp.model.jobintension.entity.Jobintension;
import com.duowenlvshi.duowenlszp.app.util.UUIDUtil;
import com.duowenlvshi.duowenlszp.model.Push.server.PushServer;
import com.duowenlvshi.duowenlszp.model.jobintension.dao.JobintensionMapper;
import com.duowenlvshi.duowenlszp.model.lawyer.dao.LawyerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yang fang yu
 * @since 2019/8/21 11:35
 */
@Service
public class JobintensionServer {
    @Autowired(required = false)
    JobintensionMapper jobintensionMapper;
    @Autowired(required = false)
    LawyerMapper lawyerMapper;
    @Autowired
    JobintensionServer jobintensionServer;
    @Autowired
    PushServer pushServer;


    public int delete(String id){
        return jobintensionMapper.delete(id);
    }
    public List selectByLawyerId(String lawyerId){
        return jobintensionMapper.selectByLawyerId(lawyerId);
    }

    public int save(Jobintension jobintension){
        int i =0;
        String sex = jobintensionServer.judgeSex(jobintension.getLawyerId());

        if("".equals(jobintension.getJobintensionId())){
            //新增
            jobintension.setJobintensionId(UUIDUtil.GetUUID());
            jobintension.setCreatedate(new Date());
            jobintension.setUpdatedate(new Date());
            jobintension.setSex(sex);
            jobintension.setIsvalid("1");
            i =jobintensionMapper.insertSelective(jobintension);
        }else{
            //修改
            jobintension.setUpdatedate(new Date());
            jobintension.setSex(sex);
            i =jobintensionMapper.updateByPrimaryKeySelective(jobintension);
        }
        return i;
    }
    /**
     * 获取精准推送名单
     * */
    public List<Map> selectPush(Map map){
        List<Map> list = jobintensionMapper.selectPush(map);
        if(list.size()<100){
            String yearsOfPractice =map.get("yearsOfPractice")+"";
            String sex =map.get("sex")+"";
            String city =map.get("city")+"";
            if(yearsOfPractice !="" || yearsOfPractice !=null){
                if("yearsOfPractice2".equals(yearsOfPractice)){
                    map.put("yearsSmall",0);
                    map.put("yearsBig",3);
                }else if("yearsOfPractice3".equals(yearsOfPractice)){
                    map.put("yearsSmall",6);
                    map.put("yearsBig",3);
                }else if("yearsOfPractice4".equals(yearsOfPractice)){
                    map.put("yearsSmall",6);
                    map.put("yearsBig",10);
                }else if("yearsOfPractice5".equals(yearsOfPractice)){
                    map.put("yearsSmall",10);
                    map.put("yearsBig",100);
                }
            }
            if(sex !="" || sex !=null){
                if("sex2".equals(sex)){
                    map.put("sex","1");
                }else if("sex3".equals(sex)){
                    map.put("sex","0");
                }
            }
            String[] csArray = city.split(",");
            map.put("city",csArray[1]);
            int i=0;
            while(list.size()<100){
                ++i;
                int pageSize = 100-list.size();
                map.put("pageSize",pageSize);
                List<Map> list2 = jobintensionMapper.selectPush2(map);
                list.removeAll(list2);
                list.addAll(list2);
                if(map.get("sex")!=null){
                    map.remove("sex");
                }else if(map.get("yearsOfPractice")!=null){
                    map.remove("yearsOfPractice");
                }else if(map.get("city")!=null){
                    map.remove("city");
                }
                if(i>5){
                    break;
                }
            }
        }
        return list;
    }

    public int updatePushSet(Map map){
        return jobintensionMapper.updatePushSet(map);
    }
    public String selectPushSet(String lawyerId){
        return jobintensionMapper.selectPushSet(lawyerId);
    }
    public Map selectPushSetOrUpdate(String lawyerId){
        return jobintensionMapper.selectPushSetOrUpdate(lawyerId);
    }

    public int updateYear(String yearsOfPractice,String lawyer_id){
        return jobintensionMapper.updateYear(yearsOfPractice,lawyer_id);

    }
    /**
     * 添加性别
     * */
    public String judgeSex(String id){
        String s = lawyerMapper.selectSex(id);
        if(s!=null && s!=""){
            String sex ;
            int a = Integer.parseInt(s);
            if(a % 2 == 0){
                sex = "sex3";
            }else {
                sex = "sex2";
            }
            return sex;
        }
        return "";
    }
}
