package com.fdkankan.ucenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.ucenter.common.PageInfo;
import com.fdkankan.ucenter.entity.Camera;
import com.fdkankan.ucenter.entity.Scene;
import com.fdkankan.ucenter.entity.ScenePlus;
import com.fdkankan.ucenter.entity.ScenePro;
import com.fdkankan.ucenter.mapper.ISceneMapper;
import com.fdkankan.ucenter.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.util.DateUserUtil;
import com.fdkankan.ucenter.vo.ResponseScene;
import com.fdkankan.ucenter.vo.request.RequestScene;
import com.fdkankan.ucenter.vo.request.SceneParam;
import com.fdkankan.ucenter.vo.response.SceneVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 场景表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-04
 */
@Service
public class SceneServiceImpl extends ServiceImpl<ISceneMapper, Scene> implements ISceneService {

    @Autowired
    IUserService userService;
    @Autowired
    ICameraService cameraService;
    @Autowired
    ISceneProService sceneProService;
    @Autowired
    IScenePlusService scenePlusService;

    @Override
    public Integer getCountByUserId(Long userId) {
        LambdaQueryWrapper<Scene> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Scene::getUserId,userId);
        return Math.toIntExact(this.count(wrapper));
    }


    @Override
    public void bindOrUnCamera(List<Long> cameraIds, Long userId) {
        if(cameraIds.size() >0){
            LambdaUpdateWrapper<Scene> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(Scene::getUserId,userId)
                    .in(Scene::getCameraId,cameraIds);
            this.update(wrapper);
        }

    }

    @Override
    public List<Scene> getListByNums(List<String> numList) {
        LambdaQueryWrapper<Scene> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Scene::getNum,numList);
        return this.list(wrapper);
    }

    @Override
    public JSONObject pageList(SceneParam param) {
        LambdaQueryWrapper<Scene> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Scene::getUserId,param.getUserId());
        if(StringUtils.isNotBlank(param.getSceneName())){
            wrapper.like(Scene::getSceneName,param.getSceneName());
        }
        Page<Scene> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
        Set<Long> cameraIds = page.getRecords().stream().map(Scene::getCameraId).collect(Collectors.toSet());
        HashMap<Long, Camera> cameraHashMap = cameraService.getByIds(new ArrayList<>(cameraIds));
        List<SceneVo> voList = new ArrayList<>();
        for (Scene record : page.getRecords()) {
            SceneVo vo = new SceneVo();
            BeanUtils.copyProperties(record,vo);
            vo.setChildName(cameraHashMap.get(record.getCameraId()).getChildName());
            vo.setSnCode(cameraHashMap.get(record.getCameraId()).getSnCode());
            voList.add(vo);
        }
        Page<SceneVo> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
        pageVo.setRecords(voList);
        pageVo.setTotal(page.getTotal());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageInfo", PageInfo.PageInfo(pageVo));
        jsonObject.put("sceneNum",page.getTotal() );
        return jsonObject;
    }

    @Override
    public Scene getByNum(String sceneNum) {
        LambdaQueryWrapper<Scene> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Scene::getNum,sceneNum);
        List<Scene> list = this.list(wrapper);
        if(list != null && list.size() >0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Page<SceneVo> getSceneList(RequestScene param) {
        return this.getBaseMapper().getSceneList(new Page<>(param.getPageNum(),param.getPageSize()),param);
    }

    @Override
    public PageInfo getDecorateScenes( RequestScene param) {
        Page<SceneVo> page = this.getBaseMapper().getDecorateScenes(new Page<>(param.getPageNum(),param.getPageSize()),param);
        for (SceneVo record : page.getRecords()) {
            if(StringUtils.isNotBlank(record.getCreateTime())){
                record.setCreateTime(DateUserUtil.getDayTime(record.getCreateTime()));
            }
        }
        return PageInfo.PageInfo(page);
    }
}
