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

import com.duowenlvshi.duowenlszp.app.util.TypeConversion;
import com.duowenlvshi.duowenlszp.model.Dialogue.entity.Dialogue;
import com.duowenlvshi.duowenlszp.model.Dialogue.server.DialogueServer;
import com.duowenlvshi.duowenlszp.model.Job.server.JobServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yang fang yu
 * @since 2019/8/26 17:21
 */
@Transactional
@Component
public class DialogueController {
    @Autowired
    DialogueServer dialogueServer;
    @Autowired
    JobServer jobServer;


    /**
     * 新增聊天记录
     * */
    public int insertDialogue(Map<String,Object> map){
        Date startDate = null;
        Map map1 =(Map) map.get("dialogue");
        map1.put("lawyerIda",map1.get("pusher"));
        map1.remove("pusher");
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        try {
            startDate = formatter.parse((String) map1.get("pushDate"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        map1.remove("pushDate");
        Dialogue dialogue =(Dialogue) TypeConversion.mapToObject(map1,Dialogue.class);
        dialogue.setCreateDate(startDate);
        dialogue.setUpdateDate(startDate);
        int i =dialogueServer.save(dialogue);
        return i;
    }

    /**
     * 查询聊天记录
     * */
    public List selectDialogue(Map<String,Object> map){
        String topic =(String) map.get("topic");
        String lawyerIdB =(String) map.get("token");
        String identity =(String) map.get("identity");
        String jobId =(String) map.get("jobId");
        int page =(int) map.get("page");
        int pageSize =(int) map.get("pageSize");
        page = (page - 1) *pageSize;
        Map map1 = new HashMap();
        map1.put("topic",topic);
        map1.put("page",page);
        map1.put("pageSize",pageSize);
        map1.put("lawyerIdB",lawyerIdB);
        map1.put("identity",identity);
        map1.put("jobId",jobId);
        dialogueServer.updateNewTopic(map1);
        List list =dialogueServer.selectByIdAorIdB(map1);
        Map map2 = new HashMap();
        map2.put("topic",topic);
        map2.put("lawyerIdB",lawyerIdB);
        dialogueServer.updateRead(map2);
        return list;
    }
    /**
     * 查询聊天列表
     * */

    public List<Map> selectDialogueList(Map<String,Object> map){
        Map map1 = new HashMap();
        String lawyerId =(String) map.get("lawyerId");
        String identity =(String) map.get("identity");
        String jobId =(String) map.get("jobId");
        int page =(int) map.get("page");
        int pageSize =(int) map.get("pageSize");
        page = (page - 1) *pageSize;
        map1.put("lawyerId",lawyerId);
        map1.put("identity",identity);
        map1.put("page",page);
        map1.put("pageSize",pageSize);
        map1.put("jobId",jobId);
        /*dialogueServer.updateNewTopic(map1);*/
        List<Map> list =dialogueServer.selectByIdList(map1);
        for (Map map2 : list) {
            if(!lawyerId.equals(map2.get("lawyerIdA"))){
                map2.put("lawyerIdB",map2.get("lawyerIdA"));
                map2.put("lawyerIdA",lawyerId);
                String logo;
                logo =(String) map2.get("logoA");
                map2.put("logoA",map2.get("logoB"));
                map2.put("logoB",logo);
                String name;
                name =(String) map2.get("nameA");
                map2.put("nameA",map2.get("nameB"));
                map2.put("nameB",name);
                map2.put("sincerity",map2.get("sincerityA"));
                map2.remove("sincerityB");
                map2.remove("sincerityA");
            }else{
                map2.put("sincerity",map2.get("sincerityB"));
                map2.remove("sincerityB");
                map2.remove("sincerityA");
            }
        }
        return list;
    }
    /**
     * 查询聊天列表总数
     * */
    public Integer selectByIdListCount(Map<String,Object> map){
        Map map1 = new HashMap();
        String lawyerId =(String) map.get("lawyerId");
        String identity =(String) map.get("identity");
        String jobId =(String) map.get("jobId");
        map1.put("lawyerId",lawyerId);
        map1.put("identity",identity);
        map1.put("jobId",jobId);
        Integer i =dialogueServer.selectByIdListCount(map1);
        return i;
    }
    /**
     * 查询聊天列表总数
     * */
    public Integer selectDialogueCount(Map<String,Object> map){
        String topic =(String) map.get("topic");
        return dialogueServer.selectDialogueCount(topic);
    }
}
