package com.platform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.platform.dao.SceneDao;
import com.platform.entity.Result;
import com.platform.entity.SceneEntity;
import com.platform.service.SceneService;
import com.platform.utils.DateUtils;

import com.platform.vos.ReturnDTO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-19 17:59:15
 */
@Service
public class SceneServiceImpl implements SceneService {
    private static Logger log = LoggerFactory.getLogger(SceneServiceImpl.class);

    @Autowired
    private SceneDao sceneDao;

    @Autowired
    private ZhiHouseService zhiHouseService;

    @Override
    public SceneEntity queryObject(Integer id) {
        return sceneDao.queryObject(id);
    }

    @Override
    public List<SceneEntity> queryList(Map<String, Object> map) {
        return sceneDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sceneDao.queryTotal(map);
    }

    @Override
    public int save(SceneEntity sceneEntity,boolean checkName) {
        SceneEntity SceneEntity = sceneDao.findByName(sceneEntity.getName());
        if(SceneEntity != null){
            if(ObjectUtils.isEmpty(checkName) || checkName){
                log.error("场景已经被绑定");
                return 0;
            }
            sceneEntity.setName(sceneEntity.getName().concat(DateUtils.format(new Date(),"MMddHHmmssSSS")));
        }
        return sceneDao.save(sceneEntity);
    }

    @Override
    public Result update(SceneEntity brand, boolean checkName) {
        if(checkName){
            SceneEntity SceneEntity = sceneDao.findByName(brand.getName());
            if(SceneEntity != null && SceneEntity.getId().intValue() != brand.getId().intValue()){
                return Result.failure("店铺名称重复");
            }
        }
        int update = sceneDao.update(brand);
        if(update != 1){
            return Result.failure("更新店铺失败");
        }
        return Result.success();
    }

    @Override
    public int delete(Integer id) {
        return sceneDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return sceneDao.deleteBatch(ids);
    }

    @Override
    public SceneEntity queryByScene(String sceneNum) {

        if(StringUtils.isBlank(sceneNum)){
            return null;
        }
        return sceneDao.queryBySceneNum(sceneNum);
    }

    @Override
    public List<SceneEntity> selectByDeptIdsAndExceptSceneNums(List<Long> deptIds, List<String> sceneNums) {
        return sceneDao.selectByDeptIdsAndExceptSceneNums(deptIds,sceneNums);
    }

    @Override
    public void updateSceneUser(Long fromUserId, Long toUserId,Long currentUserId) {
        sceneDao.updateSceneUser(fromUserId,toUserId,currentUserId);
    }

    @Override
    public List<SceneEntity> queryByUserId(Long userId) {
        return sceneDao.queryByUserId(userId);
    }

    @Override
    public void sync4dkkSceneData(String mobile,Long userId) {
        // 请求指房宝获取四维看看数据
        ReturnDTO returnDTO = zhiHouseService.sync4dkkSceneData(mobile);
        HashMap<String,List<HashMap<String,String>>> result = (HashMap<String, List<HashMap<String, String>>>) returnDTO.getMessage();

        // 解析数据，获取相机及场景数据，并入库
        List<HashMap<String, String>> scenes = result.get("scenes");

        if(!ObjectUtils.isEmpty(scenes)){
            scenes.parallelStream().forEach(map -> {
                // 查询本地场景是否存在
                SceneEntity sceneEntity = queryByScene(map.get("num"));
                if (!ObjectUtils.isEmpty(sceneEntity)) {
                    if(ObjectUtils.isEmpty(sceneEntity.getCreateUserId())){
                        sceneEntity.setCreateUserId(userId);
                        update(sceneEntity,false);
                    }
                    return;
                }
                SceneEntity scene = new SceneEntity();
                scene.setName(map.get("sceneName"));
                scene.setSceneUrl(map.get("webSite"));
                scene.setSceneNum(map.get("num"));
                scene.setIsShow(0);
                scene.setCreateUserId(userId);
                scene.setUpdateUserId(userId);
                scene.setIsShow(0);
                try {
                    save(scene,false);
                } catch (Exception e) {
                    log.error("场景同步失败：{}",JSONObject.toJSONString(map));
                    e.printStackTrace();
                }
            });
        }
    }
}
