package com.tuoheng.admin.service.inspection.query;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.domain.*;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.user.QueryUserInfoByIdCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.entity.vo.inspection.InspectionCardVo;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ChengWang
 * @Date 2023/4/14
 */
@Service
@Slf4j
public class QueryInspectionCardListService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private DspServiceInstMapper dspServiceInstMapper;

    @Autowired
    private LiveChannelMapper liveChannelMapper;

    /**
     * 获取任务列表
     * @return
     */
    public JsonResult getInspectionCardList() {
        //根据当前登录用户获取数据权限信息
        String userId = CurrentUserUtil.getUserId();
        if(StringUtils.isEmpty(userId)){
            return JsonResult.error(QueryUserInfoByIdCodeEnum.USER_ID_IS_NULL.getCode()
                    ,QueryUserInfoByIdCodeEnum.USER_ID_IS_NULL.getMsg());
        }
        String tenantId = CurrentUserUtil.getTenantId();
        if(StringUtils.isEmpty(tenantId)){
            return JsonResult.error(QueryUserInfoByIdCodeEnum.USER_TENANT_ID_NULL.getCode()
                    ,QueryUserInfoByIdCodeEnum.USER_TENANT_ID_NULL.getMsg());
        }
        Tenant tenant = tenantMapper.selectById(tenantId);
        User user = userMapper.selectById(userId);
        if(ObjectUtil.isNull(user)){
            return JsonResult.error(QueryUserInfoByIdCodeEnum.USER_IS_NOT_EXIST.getCode(),
                    QueryUserInfoByIdCodeEnum.USER_IS_NOT_EXIST.getMsg());
        }

        //根据用户查询对应的数据权限
        List<InspectionCardVo>  iInspectionCardVoList =  this.getAllInspectionList(tenant);
        return JsonResult.success(iInspectionCardVoList);
    }

    /**
     * 获取全部任务
     * @return
     */
    private List<InspectionCardVo> getAllInspectionList(Tenant tenant) {
        String tenantId = tenant.getId();
        List<Inspection> inspectionList = inspectionMapper.selectList(Wrappers.<Inspection>lambdaQuery()
                .eq(Inspection::getTenantId, tenantId)
                .in(Inspection::getStatus, 5, 6, 10)
                .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                .ne(Inspection::getInspectionType,3)
                .orderByDesc(Inspection::getCreateTime));
        JsonResult jsonResult = this.check(inspectionList);
        if(jsonResult.getCode() != 0){
            log.info("当前租户下对应的任务状态数据为空");
            return null;
        }
        //封装InspectionCardVo
        List<InspectionCardVo> resultList = inspectionList.stream().map(x -> {
            InspectionCardVo vo = new InspectionCardVo();
            BeanUtils.copyProperties(x, vo);
            //算法实例名称
            DspServiceInst dspServiceInst = dspServiceInstMapper.selectById(x.getDspServiceInstId());
            if (ObjectUtil.isNotNull(dspServiceInst)) {
                vo.setDspServiceName(dspServiceInst.getName());
            }
            //直播视频url
            LiveChannel liveChannel = liveChannelMapper.selectOne(Wrappers.<LiveChannel>lambdaQuery()
                    .eq(LiveChannel::getInspectionId, x.getId())
                    .eq(LiveChannel::getMark, MarkEnum.VALID.getCode()));
            if (ObjectUtil.isNotNull(liveChannel)) {
                vo.setAipullUrl(liveChannel.getAipullUrl());
            }
            //回放视频
            vo.setAiVideoUrl(CommonConfig.videoURL + x.getAiVideoUrl());
            return vo;
        }).collect(Collectors.toList());

        return resultList;
    }

    //数据校验
    private JsonResult check(List<Inspection> inspectionList) {
        //查询当前租户下的任务
        if(CollectionUtils.isEmpty(inspectionList) || inspectionList.size() == 0){
            return JsonResult.error();
        }
        return JsonResult.success();
    }


}
