package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.RecStatus;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.user.SSOLoginHelper;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.platform.api.vo.Camera;
import com.fdkankan.scene.entity.SceneCooperation;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.mapper.ISceneCooperationMapper;
import com.fdkankan.scene.service.ISceneCooperationService;
import com.fdkankan.scene.service.ISceneProService;
import com.fdkankan.scene.service.ISceneResourceService;
import com.fdkankan.scene.vo.SceneCooperationParamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class SceneCooperationServiceImpl extends ServiceImpl<ISceneCooperationMapper, SceneCooperation> implements ISceneCooperationService {

    @Autowired
    SSOLoginHelper ssoLoginHelper;
    @Autowired
    PlatformGoodsClient platformGoodsClient;
    @Autowired
    ISceneProService sceneProService;
    @Autowired
    ISceneResourceService sceneResourceService;

    @Override
    public void updateRecStatus(String sceneNum) {
        this.update(new LambdaUpdateWrapper<SceneCooperation>()
                .eq(SceneCooperation::getNum, sceneNum)
                .set(SceneCooperation::getUpdateTime, Calendar.getInstance().getTime())
                .set(SceneCooperation::getTbStatus, TbStatus.DELETE.code()));
    }

    @Override
    public String findUserName(String sceneNum) {

        SceneCooperation sceneCooperation = this.findBySceneNum(sceneNum);
        if(Objects.isNull(sceneCooperation)) return null;

        return sceneCooperation.getUserName();
    }

    @Override
    public SceneCooperation findBySceneNum(String sceneNum) {
        List<SceneCooperation> list = this.list(new QueryWrapper<SceneCooperation>()
                .eq("tb_status", TbStatus.VALID.code())
                .eq("rec_status", RecStatus.VALID.code())
                .eq("scene_code", sceneNum)
                .orderByDesc("id"));
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public ResultData sceneResourceBySceneNum(SceneCooperationParamVO param) throws Exception{

        ResultData<Camera> cameraResult = platformGoodsClient.getCameraByChildName(param.getUserName());
        Camera camera = cameraResult.getData();
        if(camera != null){
            return ResultData.ok();
        }

        SceneCooperation sceneCooperation = this.findBySceneNum(param.getNum());
        //若数据为空表示可操作全部资源
        if(sceneCooperation == null || !sceneCooperation.getUserId().equals(param.getUserId())){
            return ResultData.ok();
        }

        ScenePro scenePro= sceneProService.findBySceneNum(param.getNum());
        if(scenePro != null && scenePro.getUserId().equals(sceneCooperation.getUserId())){
            return ResultData.ok();
        }

        return ResultData.ok(sceneResourceService.findByCooperationId(sceneCooperation.getId()));
    }

    @Override
    public List<SceneCooperation> getListByUserId(List<Long> userIds) {
        LambdaQueryWrapper<SceneCooperation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SceneCooperation::getUserId,userIds);
        return this.list(queryWrapper);
    }
}
