package com.fs.app.controller;

import com.fs.app.params.SopLogsEditParam;
import com.fs.common.BeanCopyUtils;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.ResponseResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.StringUtils;
import com.fs.course.vo.FsCourseWatchLogStatisticsListVO;
import com.fs.fastGpt.param.FastGptChatSessionParam;
import com.fs.fastGpt.service.IFastGptChatSessionService;
import com.fs.qw.domain.QwTagGroup;
import com.fs.qw.param.*;
import com.fs.qw.param.sidebar.ExternalContactInfoParam;
import com.fs.qw.param.sidebar.TagGroupListParam;
import com.fs.qw.param.sidebar.TagGroupUpdateParam;
import com.fs.qw.result.QwExternalContactByQwResult;
import com.fs.qw.service.IExternalContactRedPacketService;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.qw.service.IQwTagGroupService;
import com.fs.qw.param.SidebarSendRedPacketParam;
import com.fs.qw.vo.QwTagGroupListVO;
import com.fs.qw.vo.sidebar.ExternalContactInfoVO;
import com.fs.qw.vo.sidebar.ExternalContactTagVO;
import com.fs.sop.domain.QwSopLogs;
import com.fs.sop.params.GetQwSopLogsByJsApiParam;
import com.fs.sop.params.SendSopParamDetailsC;
import com.fs.sop.service.IQwSopLogsService;
import com.fs.sop.service.IQwSopService;
import com.fs.store.vo.h5.ExternalUserStatsVO;
import com.fs.store.vo.h5.FsUserStatisticsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/apis/app/qwSop")
public class ApisQwSopController  extends BaseController {

    @Autowired
    RedisCache redisCache;
    @Autowired
    IFastGptChatSessionService fastGptChatSessionService;
    @Autowired
    private IQwSopLogsService qwSopLogsService;

    @Autowired
    private IQwExternalContactService qwExternalContactService;

    @Autowired
    private IQwTagGroupService qwTagGroupService;

    @Autowired
    private IQwSopService qwSopService;

    @Autowired
    private IExternalContactRedPacketService externalContactRedPacketService;
    /**
     * 更新AI发送状态
     */
    @PostMapping("/updateQwSopLogs")
    public R updateCourseSopLogs(@RequestBody SopLogsEditParam param){

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            QwSopLogs qwSopLogs=new QwSopLogs();
            qwSopLogs.setId(param.getId());
            qwSopLogs.setReceivingStatus(param.getReceivingStatus());
            qwSopLogs.setSendStatus(param.getSendStatus());
            qwSopLogs.setRealSendTime(sdf.format(new Date()));
            qwSopLogs.setRemark(param.getRemark());
            qwSopLogsService.updateQwSopLogsSendType(qwSopLogs);
                return  R.ok();
        }catch (Exception e){
                return R.error("更新失败");
            }

    }

    //主动获取发送信息
    @PostMapping("/getQwSopLogsByJsApi")
    public R getQwSopLogsByJsApi(@RequestBody GetQwSopLogsByJsApiParam param) {

        SendSopParamDetailsC qwSopLogsByJsApi = qwSopLogsService.getQwSopLogsByJsApi(param);

        return R.ok().put("data",qwSopLogsByJsApi);
    }

    //获取销售的某个联系人
    @GetMapping("/getExternalContactByAppKey/{appKey}")
    public R getExternalContactByAppKey(@PathVariable("appKey") String appKey) {

        QwExternalContactByQwResult result=qwSopLogsService.getExternalContactByAppKey(appKey);

        return R.ok().put("data",result);
    }

    //清除不是当前员工的 外部联系以及营期
    @PostMapping("/deleteQwSopLogsByJsApi")
    public R deleteQwSopLogsByJsApi(@RequestBody GetQwSopLogsByJsApiParam param) {

        return qwSopLogsService.deleteQwSopLogsByJsApi(param);

    }
    @PostMapping("/artificialInfo")
    public R artificialInfo(@RequestBody FastGptChatSessionParam sessionParam) {
        return R.ok().put("type",fastGptChatSessionService.selectFastGptChatSessionArtificialType(sessionParam));
    }

    @GetMapping("/getQwSopLogs")
    public R getQwSopLogs(SopMsgParam param) throws Exception {
        //获取记录
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<QwSopLogs> list = qwSopLogsService.selectQwSopLogsListVO(param);
        PageInfo<QwSopLogs> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @GetMapping("/externalContact")
    @ApiOperation("获取侧边栏外部联系人信息")
    public R getExternalContactInfo(@RequestParam(value = "qwExternalContactId") Long qwExternalContactId) {
        ExternalContactInfoVO externalContactInfo = qwExternalContactService.getExternalContactInfo(qwExternalContactId);
        return R.ok().put("data", externalContactInfo);
    }


    @GetMapping("/externalContact/tag")
    @ApiOperation("获取侧边栏外部联系人标签")
    public R getExternalContactTag(@RequestParam(value = "qwExternalContactId") Long qwExternalContactId) {
        List<ExternalContactTagVO> tagList = qwExternalContactService.getExternalContactTag(qwExternalContactId);
        return R.ok().put("data", tagList);
    }

    @PutMapping("/externalContact")
    @ApiOperation("编辑外部联系人信息")
    public R updateExternalContactInfo(@RequestBody ExternalContactInfoParam param) {
        return qwExternalContactService.updateExternalContactInfo(param);
    }

    @GetMapping("/tagGroupList")
    @ApiOperation("获取所有标签组和其下标签")
    public R getTagGroupList(TagGroupListParam param) {
        QwTagGroup qwTagGroup = new QwTagGroup();
        BeanCopyUtils.copy(param, qwTagGroup);
        qwTagGroup.setName(param.getTagName());

        PageHelper.startPage(qwTagGroup.getPageNum(), qwTagGroup.getPageSize());
        List<QwTagGroupListVO> list = qwTagGroupService.selectQwGroupTagList(qwTagGroup);

        PageInfo<QwTagGroupListVO> result = new PageInfo<>(list);
        return R.ok().put("data", result);
    }

//    @GetMapping("/searchTags")
//    @ApiOperation("搜索标签-跟管理端保持一致")
//    public R searchTagList(QwTagParam param) {
//        List<QwTagGroupListVO> list = qwTagService.searchTags(param);
//        return R.ok().put("data", list);
//    }

    @PutMapping("/externalContact/tag")
    @ApiOperation("编辑标签")
    public R updateExternalContactTag(@RequestBody TagGroupUpdateParam param) {
        int i = qwExternalContactService.updateExternalContactTag(param);
        if (i > 0) {
            return R.ok();
        }
        return R.error();
    }

    //员工看板 课程/答题/红包统计--侧边栏（查得很慢，设计点进去了才查）
    @PostMapping("/boardCourseQuizRedEnvelopeStats")
    @ApiOperation("员工看板 课程/答题/红包统计")
    public ResponseResult<FsUserStatisticsVO> boardCourseQuizRedEnvelopeStats(@RequestBody CourseQuizRedEnvelopeStatsParam qwParam) {
        if (StringUtils.isBlank(qwParam.getStartTime()) || StringUtils.isBlank(qwParam.getEndTime())) {
            return ResponseResult.fail(500,"时间不能为空");
        }
        FsUserStatisticsVO resultVo = qwSopService.boardCourseQuizRedEnvelopeStats(qwParam);
        return ResponseResult.ok(resultVo);
    }

    //外部联系人答题/红包/看课统计--侧边栏（查得很慢，设计点进去了才查）
    @PostMapping("/externalStatsList")
    @ApiOperation("外部联系人答题/红包/看课统计")
    public ResponseResult<ExternalUserStatsVO> externalStatsList(@RequestBody QwSidebarStatsParam qwParam) {
        ExternalUserStatsVO vo = qwSopService.externalStatsList(qwParam);
        return ResponseResult.ok(vo);
    }

    //外部联系人看课轨迹--侧边栏（查得很慢，设计点进去了才查）
    @PostMapping("/externalWatchRecordStatsList")
    @ApiOperation("用户看课轨迹")
    public TableDataInfo externalWatchRecordStatsList(@RequestBody QwSidebarStatsParam qwParam) {
        if (qwParam.getStartTime() == null || qwParam.getEndTime() == null) {
            return getDataTable(Collections.emptyList());
        }
        List<FsCourseWatchLogStatisticsListVO> list = qwSopService.externalWatchRecordStatsList(qwParam);
        return getDataTable(list);
    }

    /**
     * 侧边栏发送红包：校验今日完课/已发/已领，返回确认收款链接
     */
    @PostMapping("/prepareSidebarRedPacket")
    @ApiOperation("侧边栏准备完课红包领取链接")
    public R prepareSidebarRedPacket(@RequestBody SidebarSendRedPacketParam param) {
        return externalContactRedPacketService.prepareSidebarRedPacketLink(param);
    }

}
