package com.duowenlvshi.duowenlszp.app.api;

import com.alibaba.fastjson.JSONObject;
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.model.Brief.controller.BriefController;
import com.duowenlvshi.duowenlszp.model.Dialogue.controller.DialogueController;
import com.duowenlvshi.duowenlszp.model.Job.controller.JobController;
import com.duowenlvshi.duowenlszp.model.Meal.controller.MealController;
import com.duowenlvshi.duowenlszp.model.Order.controller.OrderController;
import com.duowenlvshi.duowenlszp.model.Push.controller.PushController;
import com.duowenlvshi.duowenlszp.model.education.controller.EducationController;
import com.duowenlvshi.duowenlszp.model.jobintension.controller.JobintensionController;
import com.duowenlvshi.duowenlszp.model.lawyer.controller.LawyerController;
import com.duowenlvshi.duowenlszp.model.lawyer.entity.Lawyer;
import com.duowenlvshi.duowenlszp.model.lawyer.server.LawyerServer;
import com.duowenlvshi.duowenlszp.model.work.controller.WorkController;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yang fang yu
 * @since 2019/8/20 09:43
 */
@Slf4j
@RestController
@RequestMapping("/app")
@MapperScan("com.duowenlvshi.duowenlszp.model.*.dao")
public class ApiController {
    @Autowired
    LawyerServer lawyerServer;

    @Autowired
    EducationController educationController;
    @Autowired
    JobController jobController;
    @Autowired
    JobintensionController jobintensionController;
    @Autowired
    DialogueController dialogueController;
    @Autowired
    WorkController workController;
    @Autowired
    PushController pushController;
    @Autowired
    RedisUtill redisUtill;
    @Autowired
    SelectRedis selectRedis;
    @Autowired
    BriefController briefController;
    @Autowired
    DeletEmoji deletEmoji;
    @Autowired
    OrderController orderController;
    @Autowired
    MealController mealController;
    @Autowired
    LawyerController lawyerController;

    @RequestMapping(value = "/api",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map api(@RequestBody Map<String,Object> map){
            Map RespondMap = new HashMap();
            List list = new ArrayList();
        try {
            String token =(String) map.get("token");
            int i =-1;
            //验证token
            Lawyer lawyer =lawyerServer.selectLawyer(token);
            if("".equals(lawyer) || lawyer==null){
                RespondMap.put("message","token错误");
                RespondMap.put("success",false);
                return RespondMap;
            }else{

                String actionCode =(String) map.get("actionCode");
                switch (actionCode){
                    //教育信息
                    case "B220":{
                        list = educationController.selectEducation(map);
                        break;
                    }
                    case "B221":{
                        i  = educationController.insertEducation(map);
                        break;
                    }
                    case "B222":{
                        i  = educationController.deletEducation(map);
                        break;
                    }
                    //工作信息
                    case "B223":{
                        i = workController.insertWork(map);
                        break;
                    }
                    case "B224":{
                        i = workController.deleteWork(map);
                        break;
                    }
                    case "B225":{
                        list = workController.selectWork(map);
                        break;
                    }
                    //查询学校
                    case "B239":{
                        list = workController.selectSchoolCode(map);
                        break;
                    }
                    //招聘信息
                    case "B226":{
                        i = jobController.insertJob(map);
                        break;
                    }
                    case "B227":{
                        i = jobController.deleteJob(map);
                        break;
                    }
                    case "B228":{
                        list = jobController.selectJob(map);
                        break;
                    }
                    case "B249":{
                        i = jobController.updateProfile(map);
                        break;
                    }
                    case "B250":{
                        String s = jobController.selectProfile(map);
                        RespondMap.put("recruitmentProfile",s);
                        break;
                    }
                    case "B248":{
                        list = jobController.selectJobMy(map);
                        break;
                    }
                    case "B236":{
                        list = jobController.selectJobList(map);
                        int count = jobController.selectCount(map,"B236");
                        RespondMap.put("total",count);
                        break;
                    }
                    case "B238":{
                        List list2 = jobController.selectJobListDefault(map);
                        int count = jobController.selectCount(map,"B238");
                        selectRedis.SelectList(list2,"");
                        RespondMap.put("list2",list2);
                        RespondMap.put("total",count);
                        break;
                    }
                    case "B251":{
                        list = jobController.selectPost(map);
                        int count = jobController.selectCount(map,"B251");
                        RespondMap.put("total",count);
                        break;
                    }
                    /*城市code存入redis*/
                    case "B240":{
                        jobController.CityToRedis();
                        break;
                    }
                    /*查询省code存入Redis*/
                    case "B247":{
                        jobController.ProvinceToRedis();
                        break;
                    }
                    /*查询学校code存入Redis*/
                    case "B241":{
                        jobController.SchoolToRedis();
                        break;
                    }
                    /*查询code文件存入Redis*/
                    case "B242":{
                        jobController.test();
                        break;
                    }
                    /*创建key*/
                    case "B260":{
                        jobController.addKey();
                        break;
                    }
                    /*查询key数据*/
                    case "B261":{
                        Map map2 =jobController.selectKey();
                        RespondMap.put("map",map2);
                        break;
                    }
                    /*修改redis值*/
                    case "B262":{
                        String val =jobController.setVal(map);
                        RespondMap.put("val",val);
                        break;
                    }
                    /*查询redis岗位*/
                    case "B263":{
                        Map valMap =jobController.setKeyVal();
                        RespondMap.put("valMap",valMap);
                        break;
                    }
                    //求职信息
                    case "B229":{
                        i = jobintensionController.insertJobintension(map);
                        break;
                    }
                    case "B230":{
                        i = jobintensionController.deleteJobintension(map);
                        break;
                    }
                    case "B231":{
                        List list2 = jobintensionController.selectJobintension(map);
                        selectRedis.SelectList(list2,"1");
                        RespondMap.put("list2",list2);
                        break;
                    }
                    case "B246":{
                        list = jobintensionController.selectMatching(map);
                        break;
                    }
                    case "B256":{
                        i = jobintensionController.updatePushSet(map);
                        break;
                    }
                    case "B258":{
                        String pushSet = jobintensionController.selectPushSet(map);
                        RespondMap.put("pushSet",pushSet);
                        break;
                    }
                    //聊天消息
                    case "B232":{
                        i = dialogueController.insertDialogue(map);
                        break;
                    }
                    case "B233":{
                        list = dialogueController.selectDialogueList(map);
                        int count = dialogueController.selectByIdListCount(map);
                        RespondMap.put("total",count);
                        break;
                    }
                    case "B234": {
                        list = dialogueController.selectDialogue(map);//聊天列表
                        List<Map> job = jobController.selectJobDialongus(map);//岗位详情
                        int count = dialogueController.selectDialogueCount(map);//聊天总数
                        selectRedis.SelectList(job,"");//设置redis值
                        RespondMap.put("total",count);
                        RespondMap.put("job",job);
                        if(job.get(0).get("email") !=null){
                            RespondMap.put("defaultMsg","您好，谢谢关注我们的岗位，欢迎投递简历到我们邮箱["+job.get(0).get("email")+"]或留下联系方式。");
                        }else{
                            RespondMap.put("defaultMsg","您好，谢谢关注我们的岗位，方便留下您的联系方式详细沟通吗？");
                        }

                        break;
                    }
                    //推送
                    case "B235":{
                        i = pushController.insertPushList(map);
                        break;
                    }
                    case "B237":{
                        list = pushController.selectPushList(map);
                        int count = pushController.selectPushListCount(map);
                        RespondMap.put("total",count);
                        break;
                    }
                    case "Redis":{
                        redisUtill.RedisRun();
                        break;
                    }
                    //简介
                    case "B243":{
                        i = briefController.insertBrief(map);
                        break;
                    }
                    case "B244":{
                        i = briefController.insertBrief(map);
                        break;
                    }
                    //主页信息
                    case "B245":{
                        List education = educationController.selectEducation(map);
                        List work = workController.selectWork(map);
                        List brief =briefController.selectBrief(map);
                        String  backgroundMap = lawyerController.selectBackgroundMap(map);
                        selectRedis.SelectList(education,"");
                        selectRedis.SelectList(work,"");
                        selectRedis.SelectList(brief,"");
                        RespondMap.put("educationList",education);
                        RespondMap.put("workList",work);
                        RespondMap.put("briefList",brief);
                        RespondMap.put("backgroundMap",backgroundMap);
                        break;
                    }
                    case "B257":{
                        String backgroundMap = lawyerController.updateBackgroundMap(map);
                        RespondMap.put("backgroundMap",backgroundMap);
                        break;
                    }
                    //套餐
                    case "B252":{
                        list = mealController.selectMealList();
                        break;
                    }
                    case "B254":{
                        Map map2  = mealController.selectNum(map);
                        RespondMap.put("meal",map2);
                        RespondMap.put("free",map2.get("free"));
                        i=1;
                        break;
                    }
                    //订单
                    case "B253":{
                        Map  map2 =orderController.insertOrder(map);
                        if(map2.get("num") != null){
                            i=(int)map2.get("num");
                        }else if(map2 != null){
                            RespondMap.put("payMap",map2);
                        }

                        break;
                    }
                    case "B255":{
                        list = orderController.selectMeal(map);
                        if(list.size()>0){
                            for (Object o : list) {
                                Map map2 = JSONObject.parseObject(JSONObject.toJSONString(o), Map.class);
                                boolean b =(boolean)map2.get("code");
                                if(b){
                                    RespondMap.put("code",true);
                                }else{
                                    RespondMap.put("code",false);
                                }
                            }
                        }else{
                            RespondMap.put("code",false);
                        }
                        break;
                    }
                    case "B259":{
                        i = workController.deleteAll(map);
                        break;
                    }
                    default:{
                        break;
                    }
                }
                if(list.size()>0){
                    selectRedis.SelectList(list,"");
                }
                RespondMap.put("list",list);
                if(i==0){
                    RespondMap.put("message","数据操作失败,请稍后再试");
                    RespondMap.put("success",false);
                }else if(i==-2){
                    RespondMap.put("message","请更新至最新版本后再购买!");
                    RespondMap.put("success",false);
                }else if(i==-3){
                    RespondMap.put("message","套餐已过期");
                    RespondMap.put("success",false);
                }else if(i==-4){
                    RespondMap.put("message","未购买套餐");
                    RespondMap.put("success",false);
                }
                else if(i==-5){
                    RespondMap.put("message","请先设置求职意向");
                    RespondMap.put("success",false);
                }
                else{
                    RespondMap.put("message","");
                    RespondMap.put("success",true);
                }
            }
            return RespondMap;
        }catch (Exception e){
            e.printStackTrace();
            RespondMap.put("success",false);
            RespondMap.put("message","服务器繁忙,请稍后再试");
        }finally{
            RespondMap.put("list",list);
            return RespondMap;
        }
    }
    @RequestMapping("/cs")
    public String test5(){
        return "666";
    }
}
