package com.zcxk.controller;

import com.zcxk.entity.MapSetting;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.MapSettingService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 地图设置控制层
 *
 * @author yjy
 * @since 2020-12-4
 */
@RestController
@RequestMapping("/mapSetting")
@Api(tags = "地图设置")
public class MapSettingController {
    /**
     * 服务对象
     */
    @Autowired
    private MapSettingService mapSettingService;

    /**
     * 根据机构查询地图设置数据
     *
     * @param sceneTypeName 一级场景类型名称
     * @return 单条数据
     */
    @RequestMapping(value = "selectMap", method = RequestMethod.GET)
    @ApiOperation(value = "查询用户单条地图配置数据")
    public AjaxMessage<MapSetting> selectMap(
            @ApiParam(value = "一级场景类型名称", required = true) @RequestParam String sceneTypeName) {
       if(sceneTypeName==null) {
           return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
       }
        LoginUser currentUser = UserUtil.getCurrentUser();
        Integer userId=currentUser.getId();
        MapSetting result = mapSettingService.selectMap(sceneTypeName,userId);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 保存地图配置数据
     *
     * @param mapSetting 地图设置信息
     * @return
     */
    @RequestMapping(value = "saveMap", method = RequestMethod.POST)
    @ApiOperation(value = "保存单条地图配置数据")
    public AjaxMessage<Boolean> saveMap(
            @ApiParam(value = "地图设置信息", required = true) @RequestBody MapSetting mapSetting) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        MapSetting idSetting= mapSettingService.selectMap(mapSetting.getSceneTypeName(),currentUser.getId());
        Integer mapId=idSetting != null?idSetting.getId():null;
        Integer id = mapSetting.getId() != null?mapSetting.getId():mapId;
        mapSetting.setTenantId(currentUser.getTenantId());
        mapSetting.setUserId(currentUser.getId());
        if(id==null){//没有查询到数据则新增
            mapSetting.setCreateBy(currentUser.getUsername());
        }else {//修改
            mapSetting.setId(id);
            mapSetting.setUpdateBy(currentUser.getUsername());
        }
        mapSetting.setStatus(1);
        boolean result = mapSettingService.saveMap(mapSetting);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

}