package com.fs.app.controller;

import com.fs.app.params.SopLogsEditParam;
import com.fs.common.BeanCopyUtils;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.fastGpt.service.IFastGptChatSessionService;
import com.fs.qw.domain.QwTagGroup;
import com.fs.qw.param.SopMsgParam;
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.IQwExternalContactService;
import com.fs.qw.service.IQwTagGroupService;
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.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 javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/app/qwSop")
public class QwSopController {

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

    @Autowired
    private IQwExternalContactService qwExternalContactService;

    @Autowired
    private IQwTagGroupService qwTagGroupService;

    /**
     * 更新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(@Valid @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(@Valid @RequestBody GetQwSopLogsByJsApiParam param) {
         return  qwSopLogsService.deleteQwSopLogsByJsApi(param);
    }

    //获取侧边栏显示记录
    @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);
    }

}
