package com.fs.course.service.impl;

import com.alibaba.fastjson.JSON;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsCourseSop;
import com.fs.course.domain.FsCourseSopLogs;
import com.fs.course.mapper.FsCourseSopLogsMapper;
import com.fs.course.mapper.FsCourseSopMapper;
import com.fs.course.param.FsCourseSopLogsAiBParam;
import com.fs.course.param.FsCourseSopParam;
import com.fs.course.service.IFsCourseSopService;
import com.fs.course.vo.FsCourseSopAiListVO;
import com.fs.course.vo.FsCourseSopListVO;
import com.fs.course.vo.FsCourseSopLogsListVO;
import com.fs.course.vo.FsCourseSopLogsVO;
import com.fs.qw.domain.QwUser;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.sop.params.QwSopSettingParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 看课sopService业务层处理
 *
 * @author fs
 * @date 2024-09-04
 */
@Service
public class FsCourseSopServiceImpl implements IFsCourseSopService
{
    @Autowired
    private FsCourseSopMapper fsSopMapper;

    @Autowired
    private QwUserMapper qwUserMapper;

    @Autowired
    private FsCourseSopLogsMapper fsSopLogsMapper;


    /**
     * 查询看课sop
     *
     * @param id 看课sop主键
     * @return 看课sop
     */
    @Override
    public FsCourseSop selectFsCourseSopById(Long id)
    {
        return fsSopMapper.selectFsCourseSopById(id);
    }

    /**
     * 查询看课sop列表
     *
     * @param fsSop 看课sop
     * @return 看课sop
     */
    @Override
    public List<FsCourseSop> selectFsCourseSopList(FsCourseSop fsSop)
    {
        return fsSopMapper.selectFsCourseSopList(fsSop);
    }

    @Override
    public List<FsCourseSopListVO> selectFsCourseSopVOList(FsCourseSop fsSop) {
        return fsSopMapper.selectFsCourseSopVOList(fsSop);
    }

    /**
     * 新增看课sop
     *
     * @param fsSop 看课sop
     * @return 结果
     */
    @Override
    public int insertFsCourseSop(FsCourseSop fsSop)
    {
        fsSop.setCreateTime(DateUtils.getNowDate());
        return fsSopMapper.insertFsCourseSop(fsSop);
    }

    /**
     * 修改看课sop
     *
     * @param fsSop 看课sop
     * @return 结果
     */
    @Override
    public int updateFsCourseSop(FsCourseSop fsSop)
    {
        return fsSopMapper.updateFsCourseSop(fsSop);
    }

    /**
     * 批量删除看课sop
     *
     * @param ids 需要删除的看课sop主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseSopByIds(Long[] ids)
    {
        return fsSopMapper.deleteFsCourseSopByIds(ids);
    }

    /**
     * 删除看课sop信息
     *
     * @param id 看课sop主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseSopById(Long id)
    {
        return fsSopMapper.deleteFsCourseSopById(id);
    }

    /**
     * AI-chat课程 获取员工SOP规则列表
     */

    @Override
    public List<FsCourseSopAiListVO> selectSopListVO(FsCourseSopParam param) {

        QwUser qwUser = qwUserMapper.selectQwUserByCompanyUserId(param.getUserId(), param.getCorpId());

        param.setQwQwUserID(qwUser.getQwUserId());

        return fsSopMapper.selectSopListVO(param);
    }

    /**
     * AI-chat课程 获取员工sop发送列表历史
     */
    @Override
    public List<FsCourseSopLogsListVO> selectSopSendCustomerLogsListVO(FsCourseSopParam param) {
        List<FsCourseSopLogsListVO> fsCourseSopLogsListVOS = fsSopLogsMapper.selectSopSendCustomerLogsListVO(param);
        fsCourseSopLogsListVOS.forEach(fsCourseSopLogsListVO -> {

            List<QwSopSettingParams.Content> contentList = JSON.parseArray(fsCourseSopLogsListVO.getContentJson(), QwSopSettingParams.Content.class);
            contentList.stream()
                    .filter(content -> "1".equals(content.getIsBindUrl()))
                    .forEach(content -> content.setValue(content.getValue() + "\n" + content.getUrl()));
            fsCourseSopLogsListVO.setContentJson(JSON.toJSONString(contentList));
        });
        return  fsCourseSopLogsListVOS;
    }

    /**
     * AI-chat课程 获取员工sop发送历史 详情
     */
    @Override
    public FsCourseSopLogsVO getFsCourseSopLogsDetailVO(FsCourseSopLogsAiBParam param) {

        FsCourseSopLogsVO fsCourseSopLogsDetailVO = fsSopLogsMapper.getFsCourseSopLogsDetailVO(param);
        List<QwSopSettingParams.Content> contentList = JSON.parseArray(fsCourseSopLogsDetailVO.getContentJson(), QwSopSettingParams.Content.class);
        contentList.stream()
                .filter(content -> "1".equals(content.getIsBindUrl()))
                .forEach(content -> content.setValue(content.getValue() + "\n" + content.getUrl()));
        fsCourseSopLogsDetailVO.setContentJson(JSON.toJSONString(contentList));
        return  fsCourseSopLogsDetailVO;
    }

    /**
     * 更新AI发送状态
     */
    @Override
    public int updateFsCourseSopLogsSendType(FsCourseSopLogs fsSopLogs) {
        return  fsSopLogsMapper.updateFsCourseSopLogs(fsSopLogs);
    }
}
