package com.fs.company.controller.fastGpt;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.course.domain.FsUserCourseVideo;
import com.fs.fastGpt.domain.FastGptRole;
import com.fs.fastGpt.service.IFastGptRoleService;
import com.fs.fastGpt.vo.FastGptRoleVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.his.vo.OptionsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用Controller
 *
 * @author fs
 * @date 2024-09-30
 */
@RestController
@RequestMapping("/fastGpt/fastGptRole")
public class FastGptRoleController extends BaseController
{
    @Autowired
    private IFastGptRoleService fastGptRoleService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询应用列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:list')")
    @GetMapping("/list")
    public TableDataInfo list(FastGptRole fastGptRole)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fastGptRole.setCompanyId(loginUser.getCompany().getCompanyId());
        //List<FastGptRoleVO> list = fastGptRoleService.selectFastGptRoleListVO(fastGptRole);
        List<FastGptRoleVO> list = fastGptRoleService.selectFastGptRoleListVONew(fastGptRole);
        for (FastGptRoleVO fastGptRoleVO : list) {
            String reminderWords = fastGptRoleVO.getReminderWords();
            if (reminderWords!=null && reminderWords.length()>110) {
                fastGptRoleVO.setReminderWords(reminderWords.substring(0,110)+"...");
            }
        }
        return getDataTable(list);
    }


    @GetMapping("/getAllRoleType")
    public R list()
    {
        List<OptionsVO> list = fastGptRoleService.selectFastGptRoleType();
        return R.ok().put("data",list);
    }
    /**
     * 导出应用列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:export')")
    @Log(title = "应用", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FastGptRole fastGptRole)
    {
        List<FastGptRoleVO> list = fastGptRoleService.selectFastGptRoleListVO(fastGptRole);
        ExcelUtil<FastGptRoleVO> util = new ExcelUtil<FastGptRoleVO>(FastGptRoleVO.class);
        return util.exportExcel(list, "应用数据");
    }

    @GetMapping("/getAllCourseList")
    public R getAllCourseList()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<FsUserCourseVideo> list = fastGptRoleService.selectAllCourseList(loginUser.getCompany().getCompanyId());
        return R.ok().put("data",list);
    }

    /**
     * 获取应用详细信息
     */
    @GetMapping(value = "/{roleId}")
    public R getInfo(@PathVariable("roleId") Long roleId)
    {
        ;
        return fastGptRoleService.selectFastGptRoleByRoleIdTagListVO(roleId);
    }

    /**
     * 新增应用
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:add')")
    @Log(title = "应用", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FastGptRole fastGptRole)
    {
//        WeixinKfAddEditAccountDTO dto=new WeixinKfAddEditAccountDTO();
//        dto.setName(fastGptRole.getRoleName());
//        dto.setMedia_id(fastGptRole.getKfMediaId());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fastGptRole.setCompanyId(loginUser.getCompany().getCompanyId());
        if(fastGptRoleService.insertFastGptRole(fastGptRole)>0){

            return R.ok();
        }
        else{
            return R.error();
        }
//        CompanyConfig config=companyConfigService.selectCompanyConfigByKey(loginUser.getCompany().getCompanyId(),"qw:config");
//        QwConfig qwConfig= JSONUtil.toBean(config.getConfigValue(), QwConfig.class);
//        WeixinKfAddEditAccountVO vo=weixinKfService.addAccount(qwConfig.getCorpId(),qwConfig.getSecret(),dto);
//        if(vo.getErrcode().equals(0)){
//            WeixinKuGetAccountUrlDTO urlDTO=new WeixinKuGetAccountUrlDTO();
//            urlDTO.setOpen_kfid(vo.getOpen_kfid());
//            WeixinKfGetAccountUrlVO urlVO=weixinKfService.getAccountUrl(qwConfig.getCorpId(),qwConfig.getSecret(),urlDTO);

//            FastGptRole role=new FastGptRole();
//            BeanUtils.copyProperties(fastGptRole,role);
//            role.setCompanyId(loginUser.getCompany().getCompanyId());
//            role.setKfId(vo.getOpen_kfid());
//            role.setKfUrl(urlVO.getUrl());
//
//            role.setAvatar(fastGptRole.getAvatar());
//
//            if(fastGptRoleService.insertFastGptRole(role)>0){
//
//                return R.ok();
//            }
//            else{
//                return R.error();
//            }
//        }
//        else{
//            return R.error(vo.getErrmsg());
//        }

    }

    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:add')")
    @Log(title = "应用", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public R copy(@RequestBody FastGptRole fastGptRole)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fastGptRole.setCompanyId(loginUser.getCompany().getCompanyId());

        FastGptRole role = fastGptRoleService.selectFastGptRoleByRoleId(fastGptRole.getRoleId());

        role.setRoleId(null);
        role.setAvatar(fastGptRole.getAvatar());
        role.setRoleName(fastGptRole.getRoleName());
        role.setBindCorpId(null);
        if(fastGptRoleService.insertFastGptRole(role)>0){
            return R.ok();
        }
        else{
            return R.error();
        }


    }

    /**
     * 修改应用
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:edit')")
    @Log(title = "应用", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody FastGptRole fastGptRole)
    {
        if(fastGptRoleService.updateFastGptRole(fastGptRole)>0){
            return R.ok();
        }
        else{
            return R.error();
        }
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        CompanyConfig config=companyConfigService.selectCompanyConfigByKey(loginUser.getCompany().getCompanyId(),"qw:config");
//        QwConfig qwConfig= JSONUtil.toBean(config.getConfigValue(), QwConfig.class);
//        FastGptRole role = fastGptRoleService.selectFastGptRoleByRoleId(fastGptRole.getRoleId());
//
//        WeixinKfAddEditAccountDTO dto=new WeixinKfAddEditAccountDTO();
//        dto.setOpen_kfid(role.getKfId());
//        dto.setName(fastGptRole.getRoleName());
//        dto.setMedia_id(fastGptRole.getKfMediaId());
//        WeixinKfAddEditAccountVO vo=weixinKfService.editAccount(qwConfig.getCorpId(),qwConfig.getSecret(),dto);
//        if(vo.getErrcode().equals(0)){
//
//            BeanUtils.copyProperties(fastGptRole,role);
//
//            if(fastGptRoleService.updateFastGptRole(role)>0){
//                return R.ok();
//            }
//            else{
//                return R.error();
//            }
//        }
//        else{
//            return R.error(vo.getErrmsg());
//        }
    }

    /**
     * 解除应用绑定
     */
//    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:relieve')")
    @Log(title = "解除应用", businessType = BusinessType.UPDATE)
    @GetMapping("/relieveFastGptRole/{roleId}")
    public R relieveFastGptRole(@PathVariable("roleId") Long roleId)
    {
        return fastGptRoleService.relieveFastGptRole(roleId);
    }

    /**
     * 删除应用
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptRole:remove')")
    @Log(title = "应用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{roleIds}")
    public AjaxResult remove(@PathVariable Long[] roleIds)
    {
        return toAjax(fastGptRoleService.deleteFastGptRoleByRoleIds(roleIds));
    }

    @GetMapping("/getAllRoleList")
    public R getAllRoleList()
    {
        FastGptRole role=new FastGptRole();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        role.setCompanyId(loginUser.getCompany().getCompanyId());

        List<FastGptRole> list = fastGptRoleService.selectFastGptRoleList(role);
        return R.ok().put("data",list);
    }
}
