package com.gyee.frame.controller.gen;


import com.gyee.frame.model.auto.ChatDetail;
import com.gyee.frame.model.auto.Gyeeuser;
import com.gyee.frame.model.custom.DatasVo;
import com.gyee.frame.service.ChatDetailService;
import com.gyee.frame.service.GyeeuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户Controller
 * @ClassName: UserController
 * @author gyee
 * @date 2019-11-20 22:35
 */

@Controller
@RequestMapping("/chatDetailController")
public class ChatDetailController {


    @Autowired
    private ChatDetailService chatDetailService;
    @Autowired
    private GyeeuserService gyeeuserService;

    @GetMapping("/selectByUserId")
    @ResponseBody
    public List<DatasVo> selectByUserId(String send, String receive) {

        List<ChatDetail> result=chatDetailService.selectByUserId(send,receive);
        List<DatasVo> ls=new ArrayList<>();
        if(!result.isEmpty())
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(ChatDetail po:result)
            {
                DatasVo vo=new DatasVo();
                vo.setSend(po.getSendingid());
                vo.setReceive(po.getPointtoid());
                vo.setContent(po.getChatcontent());
                vo.setType(po.getChattypes());
                vo.setTime(dateFormat.format(po.getChattime()));
                ls.add(vo);
            }
        }
        return  ls;
    }

    @GetMapping("/selectLastInfoByUserId")
    @ResponseBody
    public List<DatasVo> selectLastInfoByUserId(String userid) {

        List<ChatDetail>vos=new ArrayList<>();
        List<Gyeeuser> results=gyeeuserService.findContactUserlist(userid);
        if(!results.isEmpty())
        {
            for(Gyeeuser user:results)
            {
               if(!user.getId().equals(userid))
               {
                   List<ChatDetail> result=chatDetailService.selectLastInfoByUserId(userid,user.getId());
                   if(!result.isEmpty())
                   {
                       if(result.get(0).getChattypes().equals("pic"))
                       {
                           result.get(0).setChatcontent("");
                       }
                       vos.add(result.get(0));
                   }
               }
            }
        }
        List<DatasVo> ls=new ArrayList<>();
        if(!vos.isEmpty())
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(ChatDetail po:vos)
            {
                DatasVo vo=new DatasVo();
                vo.setSend(po.getSendingid());
                vo.setReceive(po.getPointtoid());
                vo.setContent(po.getChatcontent());
                vo.setType(po.getChattypes());
                vo.setTime(dateFormat.format(po.getChattime()));
                ls.add(vo);
            }
        }
        return  ls;
    }

}
