package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.ucenter.common.PageInfo;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.*;
import com.fdkankan.ucenter.service.*;
import com.fdkankan.ucenter.vo.request.SceneParam;
import com.fdkankan.ucenter.vo.response.ScenePlusVo;
import com.fdkankan.ucenter.vo.response.SceneProEntityVo;
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.*;
import java.util.stream.Collectors;

@Service
public class FusionService implements IFusionService {

    @Autowired
    IScenePlusService scenePlusService;
    @Autowired
    IScenePlusExtService scenePlusExtService;
    @Autowired
    IUserService userService;
    @Autowired
    ICameraService cameraService;
    @Autowired
    IScenePlusVoidService scenePlusVoidService;


    @Override
    public PageInfo scenePageOnlyV4List(SceneParam param, String token) {
        PageInfo pageInfo = this.scenePageList(param, token);
        List<ScenePlusVo> list = (List<ScenePlusVo>) pageInfo.getList();
        List<SceneProEntityVo> proEntityVos = new ArrayList<>();
        for (ScenePlusVo scenePlusVo : list) {
            SceneProEntityVo vo = new SceneProEntityVo();
            vo.setSceneName(scenePlusVo.getTitle());
            vo.setStatus(scenePlusVo.getSceneStatus());
            BeanUtils.copyProperties(scenePlusVo.getPlusExt(),vo);
            BeanUtils.copyProperties(scenePlusVo,vo);
            proEntityVos.add(vo);
        }
        Page<SceneProEntityVo> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
        pageVo.setRecords(proEntityVos);
        pageVo.setTotal(pageInfo.getTotal());

        return PageInfo.PageInfo(pageVo);
    }

    @Override
    public PageInfo scenePageList(SceneParam param, String token) {
        LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
        if(param.getSnCodes() != null && param.getSnCodes().size() >0){
            List<Camera> cameras = cameraService.getBySnCodes(param.getSnCodes());
            if(cameras.size() >0){
                List<Long> cameraIds = cameras.stream().map(Camera::getId).collect(Collectors.toList());
                wrapper.in(ScenePlus::getCameraId,cameraIds);
            }else {
                return PageInfo.PageInfo(new Page<>(param.getPageNum(),param.getPageSize()));
            }
        }
        if(StringUtils.isNotBlank(param.getSnCode())){
            List<Camera> cameraLikeSnCode = cameraService.getCameraLikeSnCode(param.getSnCode());
            if(cameraLikeSnCode.isEmpty()){
                return PageInfo.PageInfo(new Page<>(param.getPageNum(),param.getPageSize()));
            }
            List<Long> cameraIds = cameraLikeSnCode.stream().map(Camera::getId).collect(Collectors.toList());
            wrapper.in(ScenePlus::getCameraId,cameraIds);
        }
        if(StringUtils.isNotBlank(token)){
            User user = userService.getByToken(token);
            if(user == null){
                throw new BusinessException(LoginConstant.FAILURE_CODE_3015,LoginConstant.FAILURE_MSG_3015);
            }
            wrapper.eq(ScenePlus::getUserId,user.getId());
        }
        List<Integer> resourceList = new ArrayList<>();
        if(param.getType()!= null && param.getType() == 0){             //看看
            resourceList = Arrays.asList(1,2,12,13,14);
        }else if(param.getType()!= null && param.getType() == 1){       //看见
            resourceList = Collections.singletonList(3);
        }else if(param.getType()!=null && param.getType() == 4){
            resourceList = Collections.singletonList(4);
        }else if(param.getType() !=null && param.getType() == 6){
            resourceList = Collections.singletonList(5);
        }
        if(param.getType() !=null &&  (param.getType() == 4 || param.getType() == 6 )){
            List<ScenePlusExt> plusExtList = scenePlusExtService.getSsObj();
            List<Long> plusIds = plusExtList.stream().map(ScenePlusExt::getPlusId).collect(Collectors.toList());
            if(plusIds.size() >0){
                wrapper.in(ScenePlus::getId,plusIds);
            }
        }
        if(param.getStatus() !=null && param.getStatus() == 2){
            wrapper.in(ScenePlus::getSceneStatus,1,-2);           //计算成功
            wrapper.eq(ScenePlus::getPayStatus,1);
        }
        if(resourceList.size() >0){
            wrapper.in(ScenePlus::getSceneSource,resourceList);
        }
        if(StringUtils.isNotBlank(param.getSceneName())){
            wrapper.like(ScenePlus::getTitle,param.getSceneName());
        }
        if(param.getHaveVoid() !=null && param.getHaveVoid() == 0 && param.getNumList()!= null && param.getNumList().size() >0){
            wrapper.in(ScenePlus::getNum, param.getNumList());
        }
        if(param.getHaveVoid() !=null && param.getHaveVoid() == 1 && param.getNumList()!= null && param.getNumList().size() >0){
            wrapper.notIn(ScenePlus::getNum, param.getNumList());
        }
        wrapper.orderByDesc(ScenePlus::getCreateTime);
        Page<ScenePlus> page = scenePlusService.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
        if(page.getTotal() <=0){
            return PageInfo.PageInfo(page);
        }
        List<Long> plusIds = page.getRecords().parallelStream().map(ScenePlus::getId).collect(Collectors.toList());
        List<Long> cameraIds = page.getRecords().parallelStream().map(ScenePlus::getCameraId).collect(Collectors.toList());
        List<ScenePlusVo> list = new ArrayList<>();
        HashMap<Long, ScenePlusExt> extMap = scenePlusExtService.getByPlusIds(plusIds);
        HashMap<Long, ScenePlusVoid> voidMap = scenePlusVoidService.getByPlusIds(plusIds);
        HashMap<Long, Camera> cameraMap = cameraService.getByIds(cameraIds);
        HashMap<Long, User> userMap = userService.getByIds(page.getRecords().stream().map(ScenePlus::getUserId).collect(Collectors.toList()));
        for (ScenePlus record : page.getRecords()) {
            ScenePlusVo scenePlusVo = new ScenePlusVo();
            BeanUtils.copyProperties(record,scenePlusVo);
            String snCode = cameraMap.get(scenePlusVo.getCameraId()) == null ? "" :cameraMap.get(scenePlusVo.getCameraId()).getSnCode();
            scenePlusVo.setPlusExt(extMap.get(record.getId()));
            scenePlusVo.setSnCode(snCode);
            scenePlusVo.setSceneType(param.getType());
            scenePlusVo.setPlusVoid(voidMap.get(record.getId()));
            scenePlusVo.setPhone(userMap.get(record.getUserId()) == null ?null : userMap.get(record.getUserId()).getUserName());
            list.add(scenePlusVo);
        }
        Page<ScenePlusVo> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
        pageVo.setRecords(list);
        pageVo.setTotal(page.getTotal());
        return PageInfo.PageInfo(pageVo);
    }
}
