package com.fdkankan.scene.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.user.SSOLoginHelper;
import com.fdkankan.common.user.SSOUser;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.platform.api.feign.PlatformUserClient;
import com.fdkankan.platform.api.vo.Camera;
import com.fdkankan.platform.api.vo.User;
import com.fdkankan.scene.entity.SceneCooperation;
import com.fdkankan.scene.entity.SceneProPO;
import com.fdkankan.scene.service.ISceneCooperationService;
import com.fdkankan.scene.service.ISceneService;
import com.fdkankan.scene.service.IUserSceneService;
import com.fdkankan.scene.vo.SceneParamVO;
import com.fdkankan.scene.vo.SceneVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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;

@Service
@Slf4j
public class UserSceneService implements IUserSceneService {

    @Autowired
    private SSOLoginHelper ssoLoginHelper;
    @Autowired
    private PlatformGoodsClient platformGoodsClient;
    @Autowired
    private PlatformUserClient platformUserClient;
    @Autowired
    private ISceneCooperationService sceneCooperationService;
    @Autowired
    private ISceneService sceneService;

    @Override
    public Page<SceneVO> getScenes(String token, SceneParamVO param) {
        SSOUser ssoUser = ssoLoginHelper.loginCheck(token);
        if(ssoUser == null){
            throw new BusinessException(ErrorCode.USER_NOT_LOGIN);
        }
        Long userId = ssoUser.getId();
        param.setUserId(userId);
        String cameraId = null;
        String nums = null;
        log.info("搜索条件是："+ param.getSearchKey());
        //type为12表示一键换装的请求，不查询相机数据
        if(StringUtils.isNotEmpty(param.getSearchKey()) && !"11".equals(param.getType())){
            List<Camera> cameraEntityList = platformGoodsClient.getCameraLikeSnCode(param.getSearchKey()).getData();
            cameraId = cameraEntityList.parallelStream().map(entity->"'"+entity.getId()+"'").collect(Collectors.joining(","));

            List<User> userList  = platformUserClient.getUserLikeUserName(param.getSearchKey()).getData();
            List<Long> userIds =  userList.parallelStream().map(User::getId).collect(Collectors.toList());

            if(userIds.size() > 0){
                List<SceneCooperation> cooperationList = sceneCooperationService.getListByUserId(userIds);
                nums = cooperationList.parallelStream().map(entity->"'"+entity.getNum()+"'").collect(Collectors.joining(","));
                param.setNums(nums);
            }
        }
        param.setCameraIds(cameraId);
        Page<SceneProPO> proPOPage = sceneService.findAllScene( param);

        Page<SceneVO> pageInfo = new Page<>(proPOPage.getCurrent(),proPOPage.getSize());
        pageInfo.setTotal(proPOPage.getTotal());
        pageInfo.setRecords(proPOPage.getRecords().stream().map(sceneProPO -> {
            SceneVO sceneVO = new SceneVO();
            BeanUtils.copyProperties(sceneProPO,sceneVO);
            return sceneVO;
        }).collect(Collectors.toList()));

        List<SceneVO> list = pageInfo.getRecords();
        list.parallelStream().forEach(responseScene->{
            if(responseScene.getCameraId() != null){
                Camera cameraEntity = platformGoodsClient.getCameraById(responseScene.getCameraId()).getData();
                if(cameraEntity != null){
                    responseScene.setChildName(cameraEntity.getChildName());
                    responseScene.setSnCode(cameraEntity.getSnCode());
                }
            }

            if(responseScene.getUserId()!=null){
                String username = platformUserClient.getUserByUserId(responseScene.getUserId()).getData().getUserName();
                responseScene.setUserName(username);
            }

            SceneCooperation sceneCooperationEntity = sceneCooperationService.findBySceneNum(responseScene.getNum());
            if(sceneCooperationEntity != null){
                responseScene.setCooperationUserId(String.valueOf(sceneCooperationEntity.getUserId()));
                responseScene.setCooperationUserName(sceneCooperationService.findUserName(responseScene.getNum()));
            }
        });
        return pageInfo;
    }
}
