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.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.domain.QwGroupMsg;
import com.fs.qw.param.QwGroupMsgDetailsParam;
import com.fs.qw.param.QwGroupMsgParam;
import com.fs.qw.service.IQwGroupMsgService;
import com.fs.qw.vo.QwGroupMsgDetailsVO;
import com.fs.qw.vo.QwGroupMsgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户群发记录主Controller
 *
 * @author fs
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/qw/groupMsg")
public class QwGroupMsgController extends BaseController
{
    @Autowired
    private IQwGroupMsgService qwGroupMsgService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询客户群发记录主列表
     */
    @PreAuthorize("@ss.hasPermi('qw:groupMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwGroupMsg qwGroupMsg)
    {
        startPage();
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwGroupMsg.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwGroupMsgVO> list = qwGroupMsgService.selectQwGroupMsgListVO(qwGroupMsg);
        return getDataTable(list);
    }

    /**
     * 查询客户群发记录主列表
     */
    @PreAuthorize("@ss.hasPermi('qw:groupMsg:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(QwGroupMsg qwGroupMsg)
    {
        startPage();
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwGroupMsg.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwGroupMsgVO> list = qwGroupMsgService.selectQwGroupMsgListMyVO(qwGroupMsg);
        return getDataTable(list);
    }


    /**
     * 导出客户群发记录主列表
     */
    @PreAuthorize("@ss.hasPermi('qw:groupMsg:export')")
    @Log(title = "客户群发记录主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwGroupMsg qwGroupMsg)
    {
        List<QwGroupMsgVO> list = qwGroupMsgService.selectQwGroupMsgListVO(qwGroupMsg);
        ExcelUtil<QwGroupMsgVO> util = new ExcelUtil<QwGroupMsgVO>(QwGroupMsgVO.class);
        return util.exportExcel(list, "客户群发记录主数据");
    }

    /**
     * 获取客户群发记录主详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:groupMsg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwGroupMsgService.selectQwGroupMsgByIdVO(id));
    }

    /**
     * 新增客户群发/客户群群发记录
     */
    @PreAuthorize("@ss.hasPermi('qw:groupMsg:add')")
    @Log(title = "客户群发记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody QwGroupMsgParam qwGroupMsgParam) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        Long companyId = loginUser.getCompany().getCompanyId();
        qwGroupMsgParam.setCreateName(loginUser.getUser().getNickName());
        return qwGroupMsgService.insertQwGroupMsg(qwGroupMsgParam);
    }

    /** 统计数据详情，已发送，未发送，已接收，未接收等
     *  groupMsgId 消息主表的主键
     * */
    @GetMapping("/getCountGroupMsgUser/{groupMsgId}")
    public R getCountGroupMsgUser(@PathVariable("groupMsgId") Long groupMsgId){

        return qwGroupMsgService.getCountGroupMsgUser(groupMsgId);
    }

    /** 统计数据详情，已发送群主，送达群聊，未发送群主，未送达群聊等 */
    @GetMapping("/getCountGroupMsgBaseUser/{groupMsgId}")
    public R getCountGroupMsgBaseUser(@PathVariable("groupMsgId") Long groupMsgId){

        return qwGroupMsgService.getCountGroupMsgBaseUser(groupMsgId);
    }

    /**
     * 客户群发 发送/接收的数据详情
     */
    @GetMapping("/getCountGroupMsgUserDetails")
    public TableDataInfo getCountGroupMsgUserDetails(QwGroupMsgDetailsParam qwGroupMsgDetailsParam){

        startPage();
        List<QwGroupMsgDetailsVO> list = qwGroupMsgService.getCountGroupMsgUserDetails(qwGroupMsgDetailsParam);
        return getDataTable(list);
    }

    /** 提醒成员群发 */
    @PreAuthorize("@ss.hasPermi('qw:groupMsg:remindGroupMsg')")
    @GetMapping("/remindGroupMsg")
    public R remindGroupMsg(QwGroupMsgDetailsParam qwGroupMsgDetailsParam){


        return  qwGroupMsgService.remindGroupMsg(qwGroupMsgDetailsParam);
    }

//    /**
//     * 修改客户群发记录主
//     */
//    @PreAuthorize("@ss.hasPermi('qw:groupMsg:edit')")
//    @Log(title = "客户群发记录主", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody QwGroupMsg qwGroupMsg)
//    {
//        return toAjax(qwGroupMsgService.updateQwGroupMsg(qwGroupMsg));
//    }

    /**
     * 删除客户群发记录主
     */
    @PreAuthorize("@ss.hasPermi('qw:groupMsg:remove')")
    @Log(title = "客户群发记录主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwGroupMsgService.deleteQwGroupMsgByIds(ids));
    }
}
