package com.fdkankan.ucenter.controller.decorate;

import com.fdkankan.common.constant.SceneConstant;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.*;
import com.fdkankan.ucenter.service.*;
import com.fdkankan.ucenter.vo.request.RequestScene;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Log4j2
/**随心装接口*/
@RequestMapping("/ucenter/decorate/scene/")
@RestController
public class DecorateSceneController extends BaseController {

    @Autowired
    private ISceneService sceneService;

    @Autowired
    private ISceneProService sceneProService;

    @Autowired
    IScenePlusService scenePlusService;

    @Autowired
    ISceneEditInfoService sceneEditInfoService;

    @Autowired
    private ISceneProEditService sceneProEditService;

    @Autowired
    IUserService userService;

    /**
     * 获取用户场景记录
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result getScenes(@RequestBody RequestScene param) throws Exception {
        User user = userService.getByToken(getToken());
        if(user == null){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
        }
        param.setUserId(user.getId());
        return Result.success(sceneService.getDecorateScenes( param));

    }


    /**
     *
     * 随心装场景码同步
     */
    @RequestMapping(value = "/synsencecode", method = RequestMethod.POST)
    public Result synsencecode(@RequestBody Map<String,String> params) throws Exception{
        if(StringUtils.isEmpty(getToken())){
            throw new BusinessException(3004, "无token参数");
        }
        User user = userService.getByToken(getToken());
        if(user == null){
            throw new BusinessException(3004, "token参数不正确");
        }

        String sceneNum = params.get("sceneNum");
        if(StringUtils.isEmpty(sceneNum)){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }

        ScenePro sceneProEntity = sceneProService.getByNum(sceneNum);
        if(sceneProEntity == null){
            throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
        }

        SceneProEdit editEntity = sceneProEditService.getByProId(sceneProEntity.getId());

        if(editEntity == null){
            throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
        }
        editEntity.setVrNum(params.get("vrSceneNum"));
        editEntity.setVrThumb(params.get("vrThumb"));
        sceneProEditService.updateById(editEntity);
        return Result.success();
    }
}
