package com.fs.fastGpt.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.BeanCopyUtils;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.fastGpt.domain.FastGptRoleTag;
import com.fs.fastGpt.mapper.FastGptRoleTagMapper;
import com.fs.fastGpt.vo.FastGptRoleDataVO;
import com.fs.fastGpt.vo.FastGptRoleVO;
import com.fs.fastGpt.vo.FastgptEventLogTotalVo;
import com.fs.his.vo.OptionsVO;
import com.fs.qw.mapper.QwUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.fastGpt.mapper.FastGptRoleMapper;
import com.fs.fastGpt.domain.FastGptRole;
import com.fs.fastGpt.service.IFastGptRoleService;

/**
 * 应用Service业务层处理
 *
 * @author fs
 * @date 2024-09-30
 */
@Service
public class FastGptRoleServiceImpl implements IFastGptRoleService
{
    @Autowired
    private FastGptRoleMapper fastGptRoleMapper;

    @Autowired
    private FastGptRoleTagMapper fastGptRoleTagMapper;

    @Autowired
    private QwUserMapper qwUserMapper;

    /**
     * 查询应用
     *
     * @param roleId 应用主键
     * @return 应用
     */
    @Override
    public FastGptRole selectFastGptRoleByRoleId(Long roleId)
    {
        return fastGptRoleMapper.selectFastGptRoleByRoleId(roleId);
    }

    @Override
    public R selectFastGptRoleByRoleIdTagListVO(Long roleId) {

        FastGptRole role = fastGptRoleMapper.selectFastGptRoleByRoleId(roleId);

        FastGptRoleTag fastGptRoleTag=new FastGptRoleTag();
        fastGptRoleTag.setRoleId(roleId);

        List<FastGptRoleTag> fastGptRoleTags = fastGptRoleTagMapper.selectFastGptRoleTagList(fastGptRoleTag);
        if (fastGptRoleTags == null) {
            fastGptRoleTags = Collections.emptyList();
        }

        return R.ok().put("role", role).put("tags", fastGptRoleTags);
    }

    /**
     * 查询应用列表
     *
     * @param fastGptRole 应用
     * @return 应用
     */
    @Override
    public List<FastGptRole> selectFastGptRoleList(FastGptRole fastGptRole)
    {
        return fastGptRoleMapper.selectFastGptRoleList(fastGptRole);
    }

    /**
     * 查询应用列表
     *
     * @param fastGptRole 应用
     * @return 应用
     */
    @Override
    public List<FastGptRoleVO> selectFastGptRoleListVO(FastGptRole fastGptRole)
    {
        return fastGptRoleMapper.selectFastGptRoleListVO(fastGptRole);
    }


    /**
     * 新增应用
     *
     * @param fastGptRole 应用
     * @return 结果
     */
    @Override
    public int insertFastGptRole(FastGptRole fastGptRole)
    {
        fastGptRole.setCreateTime(DateUtils.getNowDate());
        return fastGptRoleMapper.insertFastGptRole(fastGptRole);
    }

    /**
     * 修改应用
     *
     * @param fastGptRole 应用
     * @return 结果
     */
    @Override
    public int updateFastGptRole(FastGptRole fastGptRole)
    {
        fastGptRole.setUpdateTime(DateUtils.getNowDate());
        return fastGptRoleMapper.updateFastGptRole(fastGptRole);
    }

    /**
     * 批量删除应用
     *
     * @param roleIds 需要删除的应用主键
     * @return 结果
     */
    @Override
    public int deleteFastGptRoleByRoleIds(Long[] roleIds)
    {
        return fastGptRoleMapper.deleteFastGptRoleByRoleIds(roleIds);
    }

    /**
     * 删除应用信息
     *
     * @param roleId 应用主键
     * @return 结果
     */
    @Override
    public int deleteFastGptRoleByRoleId(Long roleId)
    {
        return fastGptRoleMapper.deleteFastGptRoleByRoleId(roleId);
    }

    /**
    * 解除AI客服的全部绑定
    */
    @Override
    public R relieveFastGptRole(Long roleId) {
        int i = fastGptRoleMapper.relieveFastGptRole(roleId);
        int i1 = qwUserMapper.relieveFastGptRoleId(roleId);
        if (i > 0 && i1 > 0) {
            return R.ok();
        }else{
            return R.error();
        }

    }

    @Override
    public List<FastGptRole> selectAiDoctorList(FastGptRole fastgptRole) {
        fastgptRole.setRoleType(3);
        List<FastGptRole> fastGptRoles = fastGptRoleMapper.selectFastGptRoleList(fastgptRole);
        return fastGptRoles;
    }

    @Override
    public List<OptionsVO> selectFastGptRoleType() {
        return fastGptRoleMapper.selectFastGptRoleType();
    }

    @Override
    public FastGptRole selectFastGptRoleTypeByRoleId(Long roleId) {
        return fastGptRoleMapper.selectFastGptRoleTypeByRoleId(roleId);
    }

    @Override
    public List<String> selectFastGptRoleRoleIdsByAppKey(String appKey) {
        return fastGptRoleMapper.selectFastGptRoleRoleIdsByAppKey(appKey);
    }

    @Override
    public List<FastGptRole> selectFastGptRoleByRoleIds(List<Long> roleIds) {
        return fastGptRoleMapper.selectFastGptRoleByRoleIds(roleIds);
    }

    @Override
    public List<FastGptRoleDataVO> selectFastGptRoleAppKeyList() {
        List<FastgptEventLogTotalVo> eventLogTotalVos = fastGptRoleMapper.selectFastGptRoleAppKeyList();
        Map<String, List<FastgptEventLogTotalVo>> appKeyLitMap = eventLogTotalVos.stream()
                .filter(m -> {
                    if (m.getAppKey() != null && !m.getAppKey().isEmpty()) {
                        try {
                            String appKey = m.getAppKey();
                            if (appKey.startsWith("\"") && appKey.endsWith("\"")) {
                                appKey = appKey.substring(1, appKey.length() - 1)
                                        .replace("\\", "")
                                        .replace(" ","");
                            } else {
                                appKey = appKey.replace("\\", "");
                            }
                            JSONObject jsonObject = JSONObject.parseObject(appKey);
                            String key = jsonObject.getString("APPKey");
                            if (key != null && !key.isEmpty() && !"null".equals(key)) {
                                m.setAppKey(key);
                            } else {
                                return false;
                            }
                        } catch (Exception e) {
                            return false;
                        }
                        return true;
                    } else {
                        return false;
                    }
                }).collect(Collectors.groupingBy(FastgptEventLogTotalVo::getAppKey));

        List<FastGptRoleDataVO> roleDataList = new ArrayList<>();
        for (String appKey : appKeyLitMap.keySet()) {
            List<FastgptEventLogTotalVo> eventLogTotalVos1 = appKeyLitMap.get(appKey);
            FastGptRoleDataVO roleDataVO = new FastGptRoleDataVO();
            List<FastGptRoleDataVO> roleInfoList = new ArrayList<>();
            roleDataVO.setRoleName(appKey);
            roleDataVO.setRoleId((long)roleDataList.size() + 1);
            for (FastgptEventLogTotalVo eventLogTotalVo : eventLogTotalVos1) {
                String roleName = eventLogTotalVo.getRoleName();
                Long roleId = eventLogTotalVo.getRoleId();
                if(roleDataVO.getRoleList() == null){
                    FastGptRoleDataVO roleInfo = new FastGptRoleDataVO();
                    roleInfo.setRoleName(roleName);
                    roleInfo.setRoleId(roleId);
                    roleInfoList.add(roleInfo);
                    roleDataVO.setRoleList(roleInfoList);
                }else{
                    List<FastGptRoleDataVO> roleInfoList1 = roleDataVO.getRoleList();
                    FastGptRoleDataVO roleInfo = new FastGptRoleDataVO();
                    roleInfo.setRoleName(roleName);
                    roleInfo.setRoleId(roleId);
                    roleInfoList1.add(roleInfo);
                    roleDataVO.setRoleList(roleInfoList1);
                }
            }
            roleDataList.add(roleDataVO);
        }

        return roleDataList;
    }


}
