package com.fs.company.controller.qw;

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.utils.ServletUtils;
import com.fs.company.service.impl.CompanyDeptServiceImpl;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.param.QwGroupChatParam;
import com.fs.qw.service.IQwGroupChatService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.QwGroupChatOptionsVO;
import com.fs.qw.vo.QwGroupChatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户群详情Controller
 *
 * @author fs
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/qw/groupChat")
public class QwGroupChatController extends BaseController
{
    @Autowired
    private IQwGroupChatService qwGroupChatService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CompanyDeptServiceImpl companyDeptService;


    @Autowired
    private IQwUserService iQwUserService;

    /**
     * 查询客户群详情列表
     */
    @PreAuthorize("@ss.hasPermi('qw:groupChat:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwGroupChatParam qwGroupChat)
    {
        startPage();
        List<QwGroupChatVO> list = qwGroupChatService.selectQwGroupChatList(qwGroupChat);
        return getDataTable(list);
    }

    /**
     * 查询我的部门客户群详情列表
     */
    @PreAuthorize("@ss.hasPermi('qw:groupChat:deptList')")
    @GetMapping("/deptList")
    public TableDataInfo deptList(QwGroupChatParam qwGroupChat)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        List<Long> combinedList = new ArrayList<>();
        //本部门
        Long deptId = loginUser.getUser().getDeptId();
        if (deptId!=null){
            combinedList.add(deptId);
        }
        //本部门的下级部门
        List<Long> deptList = companyDeptService.selectCompanyDeptByParentId(deptId);
        if (!deptList.isEmpty()){
            combinedList.addAll(deptList);
        }
        qwGroupChat.setCuDeptIdList(combinedList);
        qwGroupChat.setUserType(loginUser.getUser().getUserType());


        startPage();
        List<QwGroupChatVO> list = qwGroupChatService.selectQwGroupChatList(qwGroupChat);
        return getDataTable(list);
    }


    /**
     * 我的-查询客户群详情列表
     */
    @PreAuthorize("@ss.hasPermi('qw:groupChat:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(QwGroupChatParam qwGroupChat)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwGroupChat.setCompanyId(loginUser.getCompany().getCompanyId());
        qwGroupChat.setCompanyUserId(loginUser.getUser().getUserId());
        startPage();
        List<QwGroupChatVO> list = qwGroupChatService.selectQwGroupChatList(qwGroupChat);
        return getDataTable(list);
    }


    /**
     * 获取客户群详情详细信息
     */
//    @PreAuthorize("@ss.hasPermi('qw:groupChat:query')")
    @GetMapping(value = "/{chatId}")
    public AjaxResult getInfo(@PathVariable("chatId") String chatId)
    {
        return AjaxResult.success(qwGroupChatService.selectQwGroupChatByChatId(chatId));
    }

    /**
     *  同步 插入客户群信息
     */
    @GetMapping("/cogradientGroupChat/{corpId}")
    public R cogradientGroupChat(@PathVariable("corpId") String corpId) throws Exception {

//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        Long companyId = loginUser.getCompany().getCompanyId();
        List<String> qwUserIdList=new ArrayList<>();
        return qwGroupChatService.cogradientGroupChat(corpId,qwUserIdList);
    }

    /**
     *  同步 我的客户群信息
     */
    @GetMapping("/cogradientMyGroupChat/{corpId}")
    public R cogradientMyGroupChat(@PathVariable("corpId") String corpId) throws Exception {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        List<String> qwUserIdList = iQwUserService.selectQwUserListByCompanyUserId(loginUser.getUser().getUserId(), corpId);

        return qwGroupChatService.cogradientGroupChat(corpId,qwUserIdList);
    }

    @GetMapping("/allList/{corpId}")
    public AjaxResult allList(@PathVariable("corpId") String corpId)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        Long companyId = loginUser.getCompany().getCompanyId();
        List<QwGroupChatOptionsVO> list = qwGroupChatService.selectGroupChatOptionsVOList(corpId);
        return AjaxResult.success(list);
    }
    @GetMapping("/listAll")
    public AjaxResult listAll(String qwUserIds, String corpId, String sopId){
        List<QwGroupChatOptionsVO> list = qwGroupChatService.listAllByQwUserList(qwUserIds, corpId, sopId);
        return AjaxResult.success(list);
    }
}
