package com.gyee.frame.controller.gen;


import com.gyee.frame.common.conf.V2Config;
import com.gyee.frame.common.dataSources.DataSource;
import com.gyee.frame.common.dataSources.DataSourceType;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.ChatDetail;
import com.gyee.frame.model.auto.GyeeBraceletuser;
import com.gyee.frame.model.auto.Gyeeuser;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.DatasVo;
import com.gyee.frame.service.ChatDetailService;
import com.gyee.frame.service.GyeeBraceletuserService;
import com.gyee.frame.service.GyeeuserService;
import com.gyee.frame.util.IpUtil;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import com.gyee.frame.util.golden.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 用户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;
    @Autowired
    private GyeeBraceletuserService gyeeBraceletuserService;



    @DataSource(value = DataSourceType.TICKET)
    @GetMapping("/chooseImage")
    @ResponseBody
    public int chooseImage(String userid, String url) {

        if(StringUtils.isNotEmpty(userid)  && StringUtils.isNotEmpty(url))
        {
            GyeeBraceletuser vo = gyeeBraceletuserService.queryBraceletuserByUserId(userid);
            if(null !=vo)
            {
                if(InitialRunner.braceletuserMap.containsKey(vo.getUserid()))
                {
                    GyeeBraceletuser gb=InitialRunner.braceletuserMap.get(vo.getUserid());
                    gb.setImgurl(getUrl(url));
                }
                vo.setImgurl(getUrl(url));
                gyeeBraceletuserService.updateByPrimaryKeySelective(vo);
            }

        }

        return  0;
    }

    private String getUrl(String url)
    {
        if(url.indexOf("boy")!=-1)
        {
            url="/static/image/boy.jpg";
        }else if(url.indexOf("fhnv")!=-1)
        {
            url="/static/image/fhnv.jpg";
        }else if(url.indexOf("girl")!=-1)
        {
            url="/static/image/girl.jpg";
        }else if(url.indexOf("huge")!=-1)
        {
            url="/static/image/huge.jpg";
        }
        return url;
    }

    @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("/selectLimitByUserId")
    @ResponseBody
    public List<DatasVo> selectLimitByUserId(String send, String receive,int begin,int end) {

        List<ChatDetail> result=chatDetailService.selectLimitByUserId(send,receive,begin,end);
        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()));
                vo.setContentDuration(po.getRemark1());
                vo.setImgurl(po.getRemark2());
                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("1"))
                       {
                           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;
    }


    @PostMapping("/insertSelective")
    @ResponseBody
    public int insertSelective(HttpServletRequest request, @RequestBody DatasVo vo) {


        ChatDetail po=new ChatDetail();
        po.setId(SnowflakeIdWorker.getUUID());
        po.setPointtoid(vo.getReceive());
        if(InitialRunner.laborsmap.containsKey(vo.getReceive()))
        {
            po.setPointto(InitialRunner.laborsmap.get(vo.getReceive()));
        }
        po.setSendingid(vo.getSend());
        if(InitialRunner.laborsmap.containsKey(vo.getSend()))
        {
            po.setSendingname(InitialRunner.laborsmap.get(vo.getSend()));
        }
        po.setChatcontent(vo.getContent());
        po.setChattypes(vo.getType());
        Date date=new Date();
        po.setChattime(date);
        po.setNumericdate(Long.valueOf(vo.getTime()));
        if(StringUtils.isNotEmpty(vo.getContentDuration()))
        {
            po.setRemark1(vo.getContentDuration());
        }
        if(vo.getType().endsWith("4") && StringUtils.isNotEmpty(vo.getContent()))
        {
            String temp=vo.getContent();
            temp=temp.substring(0,temp.length()-4)+".jpg";
            po.setRemark2(temp);
        }
        //获取IP地址
        String ipAddress = IpUtil.getIpAddr(request);

        po.setSendingip(ipAddress);
        int result=chatDetailService.insertSelective(po);

        return  result;
    }

}
