package com.fs.company.controller.qw;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.domain.QwMsg;
import com.fs.qw.domain.QwUser;
import com.fs.qw.param.QwMsgSendParam;
import com.fs.qw.param.QwSessionParam;
import com.fs.qw.service.IQwMsgService;
import com.fs.qw.vo.QwContactListVO;
import com.fs.qw.vo.QwMessageListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 企微聊天记录Controller
 *
 * @author fs
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/qw/qwMsg")
@Api(tags = "企微聊天记录管理")
public class QwMsgController extends BaseController
{
    @Autowired
    private IQwMsgService qwMsgService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询企微聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwMsg qwMsg)
    {
        startPage();
        List<QwMsg> list = qwMsgService.selectQwMsgList(qwMsg);
        return getDataTable(list);
    }

    /**
     * 导出企微聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwMsg:export')")
    @Log(title = "企微聊天记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwMsg qwMsg)
    {
        List<QwMsg> list = qwMsgService.selectQwMsgList(qwMsg);
        ExcelUtil<QwMsg> util = new ExcelUtil<QwMsg>(QwMsg.class);
        return util.exportExcel(list, "企微聊天记录数据");
    }

    /**
     * 获取企微聊天记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:qwMsg:query')")
    @GetMapping(value = "/{msgId}")
    public AjaxResult getInfo(@PathVariable("msgId") Long msgId)
    {
        return AjaxResult.success(qwMsgService.selectQwMsgByMsgId(msgId));
    }

    /**
     * 新增企微聊天记录
     */
    @PreAuthorize("@ss.hasPermi('qw:qwMsg:add')")
    @Log(title = "企微聊天记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwMsg qwMsg)
    {
        return toAjax(qwMsgService.insertQwMsg(qwMsg));
    }

    /**
     * 修改企微聊天记录
     */
    @PreAuthorize("@ss.hasPermi('qw:qwMsg:edit')")
    @Log(title = "企微聊天记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwMsg qwMsg)
    {
        return toAjax(qwMsgService.updateQwMsg(qwMsg));
    }

    /**
     * 企微聊天记录会话
     */
    @PreAuthorize("@ss.hasPermi('qw:qwMsg:remove')")
    @Log(title = "企微聊天记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{msgIds}")
    public AjaxResult remove(@PathVariable Long[] msgIds)
    {
        return toAjax(qwMsgService.deleteQwMsgByMsgIds(msgIds));
    }

    //获取企微用户
    @GetMapping("/qwUserList")
    @ApiOperation("获取企微用户列表")
    public R getQwUserList(){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //员工userId
        Long userId = loginUser.getUser().getUserId();
        List<QwUser> qwUsers = qwMsgService.qwUserList(userId);
        return R.ok().put("data",qwUsers);
    }
    //获取会话
    @GetMapping("/conversationList/{userId}")
    @ApiOperation("获取会话")
    public R conversations(@PathVariable("userId")Long qwUserId){
        List<QwContactListVO> list = qwMsgService.selectQwConversationByUserId(qwUserId);
        for (QwContactListVO contract:list) {
            if(StringUtils.isEmpty(contract.getDisplayName())){
                contract.setDisplayName("群聊");
            }
        }
        return R.ok().put("data",list);
    }
    //根据会话获取消息
    @GetMapping("/getQwMessageListBySession")
    @ApiOperation("根据会话获取消息")
    public R getQwMessageListBySession(QwSessionParam param){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<QwMsg> list = qwMsgService.selectQwMsgBySession(param);
        List<QwMessageListVO> listVOS=qwMsgService.getQwMessageListBySession(param,list);
        PageInfo listPageInfo= new PageInfo(list);
        Collections.reverse(listVOS);
        listPageInfo.setList(listVOS);
        return R.ok().put("data",listPageInfo);
    }

    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody QwMsgSendParam param){
        return qwMsgService.sendMsg(param);
    }


    //获取用户单条会话
    @GetMapping("/getSession")
    public R getSession(QwSessionParam param){
        QwContactListVO data = qwMsgService.selectQwSessionBycId(param.getConversationId(),param.getUserId());
        return R.ok().put("data",data);
    }
}
