package cn.iocoder.yudao.module.llm.service.application;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.llm.controller.admin.application.vo.ApplicationPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.application.vo.ApplicationRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.application.vo.ApplicationSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.application.ApplicationDO;
import cn.iocoder.yudao.module.llm.dal.mysql.application.ApplicationMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.APPLICATION_NOT_EXISTS;

/**
 * 大模型应用 Service 实现类
 *
 * @author 华大大模型
 */
@Service
@Validated
public class ApplicationServiceImpl implements ApplicationService {

    @Resource
    private ApplicationMapper applicationMapper;

    @Resource
    private AdminUserApi adminUserApi;


    @Override
    public Long createApplication(ApplicationSaveReqVO createReqVO) {
        // 插入
        ApplicationDO application = BeanUtils.toBean(createReqVO, ApplicationDO.class);
        applicationMapper.insert(application);
        // 返回
        return application.getId();
    }

    @Override
    public void updateApplication(ApplicationSaveReqVO updateReqVO) {
        // 校验存在
        validateApplicationExists(updateReqVO.getId());
        // 更新
        ApplicationDO updateObj = BeanUtils.toBean(updateReqVO, ApplicationDO.class);
        applicationMapper.updateById(updateObj);
    }

    @Override
    public void deleteApplication(Long id) {
        // 校验存在
        validateApplicationExists(id);
        // 删除
        LambdaUpdateWrapper<ApplicationDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ApplicationDO::getId, id)
                .set(ApplicationDO::getDeleted,true);
        applicationMapper.update(null, wrapper);
    }

    private void validateApplicationExists(Long id) {
        if (applicationMapper.selectById(id) == null) {
            throw exception(APPLICATION_NOT_EXISTS);
        }
    }

    @Override
    public ApplicationDO getApplication(Long id) {
        return applicationMapper.selectById(id);
    }

    @Override
    public PageResult<ApplicationRespVO> getApplicationPage(ApplicationPageReqVO pageReqVO) {
        PageResult<ApplicationDO> applicationDOPageResult = applicationMapper.selectPage(pageReqVO);
        PageResult<ApplicationRespVO> result = BeanUtils.toBean(applicationDOPageResult, ApplicationRespVO.class);
        List<Long> ids = applicationDOPageResult.getList().stream().map(applicationDO -> Long.parseLong(applicationDO.getCreator())).collect(Collectors.toList());
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(ids);
        for (ApplicationRespVO applicationRespVO : result.getList()) {
            applicationRespVO.setCreatorName(userMap.get(Long.parseLong(applicationRespVO.getCreator())).getNickname());
        }
        return result;
    }

    @Override
    public List<ApplicationRespVO> getApplicationList(ApplicationPageReqVO pageReqVO) {
        LambdaQueryWrapper<ApplicationDO> queryWrapper = new LambdaQueryWrapper<>();
        if(pageReqVO.getAppName() != null && !pageReqVO.getAppName().equals("")){
            queryWrapper.like(ApplicationDO::getAppName,pageReqVO.getAppName());
        }
        if(pageReqVO.getAppCategory() != null && !pageReqVO.getAppCategory().equals("")){
            queryWrapper.eq(ApplicationDO::getAppCategory,pageReqVO.getAppCategory());
        }
        if(pageReqVO.getCreator() != null && !pageReqVO.getCreator().equals("")){
            queryWrapper.eq(ApplicationDO::getCreator,pageReqVO.getCreator());
        }
        if(pageReqVO.getExclude() != null && !pageReqVO.getExclude().equals("")){
            queryWrapper.ne(ApplicationDO::getCreator,pageReqVO.getCreator());
        }
        queryWrapper.eq(ApplicationDO::getDeleted,false);
        queryWrapper.orderByDesc(ApplicationDO::getId);
        List<ApplicationDO> applicationDOPageResult = applicationMapper.selectList(queryWrapper);
        List<ApplicationRespVO> result = BeanUtils.toBean(applicationDOPageResult, ApplicationRespVO.class);
        List<Long> ids = applicationDOPageResult.stream().map(applicationDO -> Long.parseLong(applicationDO.getCreator())).collect(Collectors.toList());
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(ids);
        for (ApplicationRespVO applicationRespVO : result) {
            applicationRespVO.setCreatorName(userMap.get(Long.parseLong(applicationRespVO.getCreator())).getNickname());
        }
        return result;
    }

}
