package com.fs.qw.controller;

import com.fs.common.annotation.Log;
import com.fs.common.constant.HttpStatus;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.PageDomain;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.core.page.TableSupport;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.service.ICompanyUserService;
//import com.fs.framework.security.LoginUser;
//import com.fs.framework.service.TokenService;
import com.fs.qw.domain.QwUserVoiceLog;
import com.fs.qw.param.QwTagSearchParam;
import com.fs.qw.service.IQwTagService;
import com.fs.qw.service.IQwUserVoiceLogService;
import com.fs.qw.vo.QwUserVoiceLogTotalVo;
import com.fs.qw.vo.QwUserVoiceLogVo;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 企微用户通话记录Controller
 *
 * @author fs
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/qw/qwUserVoiceLog")
public class QwUserVoiceLogController extends BaseController
{
    @Autowired
    private IQwUserVoiceLogService qwUserVoiceLogService;

//    @Autowired
//    private TokenService tokenService;

    @Autowired
    private IQwTagService iQwTagService;

    @Autowired
    private ICompanyUserService userService;

//    /**
//     * 查询企微用户通话记录列表
//     */
//    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(QwUserVoiceLogVo qwUserVoiceLog)
//    {
//        startPage();
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
//        List<QwUserVoiceLogVo> list = qwUserVoiceLogService.selectQwUserVoiceLogList(qwUserVoiceLog);
//        return getDataTable(list);
//    }


//    /**
//     * 查询企微用户通话记录列表
//     */
//    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:list')")
//    @PostMapping("/newList")
//    public TableDataInfo newList(@RequestBody QwUserVoiceLogVo qwUserVoiceLog)
//    {
//        PageHelper.startPage(qwUserVoiceLog.getPageNum(), qwUserVoiceLog.getPageSize());
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
//        List<QwUserVoiceLogVo> list = qwUserVoiceLogService.selectQwUserVoiceLogList(qwUserVoiceLog);
//        list.forEach(item->{
//
//            if (!Objects.equals(item.getQwExternalContact().getTagIds(), "[]") && item.getQwExternalContact().getTagIds()!=null) {
//                QwTagSearchParam param = new QwTagSearchParam();
//                Gson gson = new Gson();
//                List<String> tagIds = gson.fromJson(
//                        item.getQwExternalContact().getTagIds(),
//                        new TypeToken<List<String>>() {
//                        }.getType()
//                );
//                param.setTagIds(tagIds);
//                item.setTagIdsName(iQwTagService.selectQwTagListByTagIds(param));
//            }
//        });
//        return getDataTable(list);
//    }

//    /**
//     * 查询我的通话记录列表
//     * @param qwUserVoiceLog
//     * @return
//     */
//    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:myList')")
//    @GetMapping("/myList")
//    public TableDataInfo myList(QwUserVoiceLogVo qwUserVoiceLog)
//    {
//        startPage();
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyUserId(loginUser.getUser().getUserId());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
//        List<QwUserVoiceLogVo> list = qwUserVoiceLogService.selectQwUserVoiceLogList(qwUserVoiceLog);
//        return getDataTable(list);
//    }

//    /**
//     * 导出我的通话记录
//     */
//
//    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:myExport')")
//    @Log(title = "我的通话记录", businessType = BusinessType.EXPORT)
//    @GetMapping("/myExport")
//    public AjaxResult myExport(QwUserVoiceLogVo qwUserVoiceLog)
//    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyUserId(loginUser.getUser().getUserId());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
//        List<QwUserVoiceLogVo> list = qwUserVoiceLogService.selectQwUserVoiceLogList(qwUserVoiceLog);
//        list.forEach(m-> {
//            m.setCompanyUserName(m.getCompanyUser().getUserName());
//            m.setCompanyName(m.getCompany().getCompanyName());
//            m.setExtName(m.getQwExternalContact().getName());
//            m.setQwUserName(m.getQwUser().getQwUserName());
//        });
//        ExcelUtil<QwUserVoiceLogVo> util = new ExcelUtil<QwUserVoiceLogVo>(QwUserVoiceLogVo.class);
//        return util.exportExcel(list, "企微用户通话记录数据");
//    }


    /**
     * 统计查询企微用户通话记录
     * @param qwUserVoiceLog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:totalList')")
    @GetMapping("/totalList")
    public TableDataInfo totalList(QwUserVoiceLogTotalVo qwUserVoiceLog)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
        qwUserVoiceLog.setQwUserId(1L);
        List<QwUserVoiceLogTotalVo> list = qwUserVoiceLogService.selectQwUserVoiceLogTotalList(qwUserVoiceLog);
        list.forEach(item->{
            if(item.getQwExternalContact() != null){
                if (item.getQwExternalContact().getTagIds() != null && !Objects.equals(item.getQwExternalContact().getTagIds(), "[]")) {
                    QwTagSearchParam param = new QwTagSearchParam();
                    Gson gson = new Gson();
                    List<String> tagIds = gson.fromJson(
                            item.getQwExternalContact().getTagIds(),
                            new TypeToken<List<String>>() {
                            }.getType()
                    );
                    param.setTagIds(tagIds);
                    item.setTagIdsName(iQwTagService.selectQwTagListByTagIds(param));
                }
            }
        });

        // 获取分页参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        int total = list.size();
        // 在内存中进行分页处理
        if (pageNum != null && pageSize != null) {
            int fromIndex = (pageNum - 1) * pageSize;
            int toIndex = Math.min(fromIndex + pageSize, total);

            // 确保索引不越界
            if (fromIndex < total) {
                list = list.subList(fromIndex, toIndex);
            } else {
                list = new ArrayList<>(); // 返回空列表
            }
        }

        // 构造返回结果
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }


    @GetMapping("/sellTotalList")
    public TableDataInfo sellTotalList(QwUserVoiceLogTotalVo qwUserVoiceLog)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwUserVoiceLogTotalVo> list = qwUserVoiceLogService.selectQwUserVoiceLogTotalList(qwUserVoiceLog);

        // 获取分页参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        int total = list.size();
        // 在内存中进行分页处理
        if (pageNum != null && pageSize != null) {
            int fromIndex = (pageNum - 1) * pageSize;
            int toIndex = Math.min(fromIndex + pageSize, total);

            // 确保索引不越界
            if (fromIndex < total) {
                list = list.subList(fromIndex, toIndex);
            } else {
                list = new ArrayList<>(); // 返回空列表
            }
        }

        // 构造返回结果
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }

    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:sellTotalExport')")
    @Log(title = "企微用户通话记录统计", businessType = BusinessType.EXPORT)
    @GetMapping("/sellTotalExport")
    public AjaxResult sellTotalExport(QwUserVoiceLogTotalVo qwUserVoiceLog)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwUserVoiceLogTotalVo> list = qwUserVoiceLogService.selectQwUserVoiceLogTotalList(qwUserVoiceLog);
        list.forEach(m-> {
            m.setQwUserName(m.getQwUser().getQwUserName());
        });
        ExcelUtil<QwUserVoiceLogTotalVo> util = new ExcelUtil<QwUserVoiceLogTotalVo>(QwUserVoiceLogTotalVo.class);
        return util.exportExcel(list, "企微用户通话记录数据");
    }

//    /**
//     * 导出统计企微用户通话记录
//     */
//    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:totalExport')")
//    @Log(title = "企微用户通话记录统计", businessType = BusinessType.EXPORT)
//    @GetMapping("/totalExport")
//    public AjaxResult totalExport(QwUserVoiceLogTotalVo qwUserVoiceLog)
//    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        qwUserVoiceLog.setCompanyId(loginUser.getCompany().getCompanyId());
//        qwUserVoiceLog.setQwUserId(1L);
//        List<QwUserVoiceLogTotalVo> list = qwUserVoiceLogService.selectQwUserVoiceLogTotalList(qwUserVoiceLog);
//        list.forEach(m-> {
//            m.setQwUserName(m.getQwUser().getQwUserName());
//        });
//        ExcelUtil<QwUserVoiceLogTotalVo> util = new ExcelUtil<QwUserVoiceLogTotalVo>(QwUserVoiceLogTotalVo.class);
//        return util.exportExcel(list, "企微用户通话记录数据");
//    }


    /**
     * 导出企微用户通话记录列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:export')")
    @Log(title = "企微用户通话记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwUserVoiceLogVo qwUserVoiceLog)
    {
        List<QwUserVoiceLogVo> list = qwUserVoiceLogService.selectQwUserVoiceLogList(qwUserVoiceLog);
        list.forEach(m-> {
            m.setCompanyUserName(m.getCompanyUser().getUserName());
            m.setCompanyName(m.getCompany().getCompanyName());
            m.setExtName(m.getQwExternalContact().getName());
            m.setQwUserName(m.getQwUser().getQwUserName());
        });
        ExcelUtil<QwUserVoiceLogVo> util = new ExcelUtil<QwUserVoiceLogVo>(QwUserVoiceLogVo.class);
        return util.exportExcel(list, "企微用户通话记录数据");
    }

    /**
     * 获取企微用户通话记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwUserVoiceLogService.selectQwUserVoiceLogById(id));
    }

    /**
     * 新增企微用户通话记录
     */
    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:add')")
    @Log(title = "企微用户通话记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwUserVoiceLog qwUserVoiceLog)
    {
        return toAjax(qwUserVoiceLogService.insertQwUserVoiceLog(qwUserVoiceLog));
    }

    /**
     * 修改企微用户通话记录
     */
    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:edit')")
    @Log(title = "企微用户通话记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwUserVoiceLog qwUserVoiceLog)
    {
        return toAjax(qwUserVoiceLogService.updateQwUserVoiceLog(qwUserVoiceLog));
    }

    /**
     * 删除企微用户通话记录
     */
    @PreAuthorize("@ss.hasPermi('qw:qwUserVoiceLog:remove')")
    @Log(title = "企微用户通话记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwUserVoiceLogService.deleteQwUserVoiceLogByIds(ids));
    }
}
