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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.admin.conver.InspectionConverMapper;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.entity.domain.User;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.entity.request.inspection.QueryInspectionPageListRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.entity.vo.inspection.InspectionVo;
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.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 查询巡检任务分页列表业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-11-22
 */
@Slf4j
@Service
public class QueryInspectionPageListService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private InspectionMapper inspectionMapper;


    public JsonResult getPageList(QueryInspectionPageListRequest request) {
//        log.info("进入查询巡检任务分页列表业务");

        String userId = CurrentUserUtil.getUserId();
        String tenantId = CurrentUserUtil.getTenantId();
        request.setTenantId(tenantId);

        JsonResult result = this.check(tenantId, request);
        if (0 != result.getCode()) {
            log.info("进入查询巡检任务分页列表业务：校验失败：{}", result.getMsg());
            return result;
        }

        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getTenantId, tenantId)
                .eq(User::getId, userId)
                .eq(User::getMark, 1));

        // 设置分页参数
        IPage<Inspection> page = new Page<>(request.getPage(), request.getLimit());
        // 查询结果
        IPage<Inspection> pageData = inspectionMapper.selectPageList(page, request);

        if (null == pageData || pageData.getTotal() == 0) {
            log.info("获取任务分页列表为空");
            return JsonResult.success(pageData);
        }

        // 构造返回结果对象
        List<InspectionVo> inspectionVoList = this.buildIspectionVoList(user, pageData.getRecords());

        // 重写返回结果对象
        IPage<InspectionVo> inspectionVoPageData = new Page<>();
        inspectionVoPageData.setPages(pageData.getPages());
        inspectionVoPageData.setCurrent(pageData.getCurrent());
        inspectionVoPageData.setSize(pageData.getSize());
        inspectionVoPageData.setTotal(pageData.getTotal());
        inspectionVoPageData.setRecords(inspectionVoList);

        return JsonResult.success(inspectionVoPageData);
    }

    /**
     * 检查参数
     *
     * @param tenantId
     * @param queryInspectionRequest
     * @return
     */
    private JsonResult check(String tenantId, QueryInspectionPageListRequest queryInspectionRequest) {
        return JsonResult.success();
    }

    /**
     * 超级管理员可查全部的任务
     *
     * @param request
     * @return
     */
    private IPage<Inspection> getAllList(QueryInspectionPageListRequest request) {
        // 设置分页参数
        IPage<Inspection> page = new Page<>(request.getPage(), request.getLimit());
        // 查询结果
        IPage<Inspection> pageData = inspectionMapper.selectPageList(page, request);
        return pageData;
    }

    /**
     * 1）、查找已确认问题数、发现问题数字段
     * 2）、判断是否有操作权限：立即执行、直播、回放、问题详情、问题核实、重新提交
     *
     * @param user
     * @param inspectionList
     * @return
     */
    private List<InspectionVo> buildIspectionVoList(User user, List<Inspection> inspectionList) {
        List<InspectionVo> inspectionVoList = InspectionConverMapper.INSTANCE.fromInspectionListToInspectionVoList(inspectionList);
        for (InspectionVo inspectionVo : inspectionVoList) {
            if (StringUtils.isNotEmpty(inspectionVo.getAiVideoUrl())) {
                inspectionVo.setAiVideoUrl(CommonConfig.videoURL + inspectionVo.getAiVideoUrl());
            }
        }
        return inspectionVoList;
    }

}
