package com.fdkanfang.web.backend;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.fdkanfang.common.util.Result;
import com.fdkanfang.domain.backend.HouseEntity;
import com.fdkanfang.domain.backend.SceneProEditEntity;
import com.fdkanfang.domain.backend.SceneProEntity;
import com.fdkanfang.domain.response.ResponseScene;
import com.fdkanfang.service.backend.HouseService2;
import com.fdkanfang.service.backend.ISceneProEditService;
import com.fdkanfang.service.backend.ISceneProService;

import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by owen on 2020/4/26 0026 14:05
 *
 * 不拦截token
 *
 */
@Api(tags = "场景管理")
@RestController
@RequestMapping("api")
@Log4j2
public class SceneController {


    @Autowired
    private ISceneProService sceneProService;

    @Autowired
    private ISceneProEditService sceneProEditService;

    @Autowired
    private HouseService2 houseService2;

    /**
     * 获取场景详情
     */
    @ApiImplicitParam(name = "num", value = "场景码", required = true)
    @ApiOperation("场景详情")
    @GetMapping(value = "scene/getInfo")
    public Result getInfo(String num) throws Exception {
//        String sceneNum = request.getParameter("num");
//        if(StringUtils.isEmpty(sceneNum)){
//            throw new BaseRuntimeException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
//        }
        SceneProEntity sceneProEntity = sceneProService.findBySceneNum(num);
//        if(sceneProEntity == null){
//            throw new BaseRuntimeException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
//        }
//        if(sceneProEntity.getPayStatus() != 1){
//            throw new BaseRuntimeException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
//        }
        ResponseScene responseScene = new ResponseScene();
        SceneProEditEntity editEntity = sceneProEditService.findByProId(sceneProEntity.getId());
        BeanUtils.copyProperties(editEntity, responseScene);
        BeanUtils.copyProperties(sceneProEntity, responseScene);
        if(StringUtil.isNotEmpty(editEntity.getEntry())){
            responseScene.setEntry(JSONObject.parseObject(editEntity.getEntry()).toJSONString());
        }

        if(StringUtil.isNotEmpty(sceneProEntity.getGps())){
            responseScene.setGps(JSONObject.parseObject(sceneProEntity.getGps()).toJSONString());
        }

        responseScene.setCreateTime(new DateTime(sceneProEntity.getCreateTime()).toString("yyyy-MM-dd"));
        responseScene.setCreateDate(sceneProEntity.getCreateTime().getTime());
        if(StringUtil.isEmpty(editEntity.getSceneKey())){
            responseScene.setIsPublic(0);
        }else {
            responseScene.setIsPublic(1);
        }

        //更新访问数量
        return Result.success( responseScene);
    }


    /**
     * 给算法查询服务器文件位置
     * m 是场景码
     */
    @ApiImplicitParam(name = "m", value = "场景码（d_xxx）", required = true)
    @ApiOperation("查询服务器文件夹")
    @GetMapping(value = "getFolder")
    public Result getFolder(String m) {
        HouseEntity houseEntity = houseService2.findBySceneCode(m);
        assert houseEntity != null;
        log.info("filePath: {}", houseEntity.getFilePath());
        return Result.success(houseEntity.getFilePath());
    }

}
