package com.zcxk.charge.web.controller;

import com.zcxk.charge.api.enums.ChargeErrorEnum;
import com.zcxk.charge.core.entity.MapSetting;
import com.zcxk.charge.web.service.MapSettingService;
import com.zcxk.core.common.enums.StatusEnum;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.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;

    @RequestMapping(value = "selectMap", method = RequestMethod.GET)
    @ApiOperation(value = "查询用户单条地图配置数据")
    public AjaxMessage selectMap(@ApiParam(value = "一级场景类型名称", required = true) @RequestParam String sceneTypeName) {
       if (sceneTypeName == null) {
           return AjaxMessage.success(ChargeErrorEnum.PARAM_ERROR);
       }
       return AjaxMessage.success(mapSettingService.selectMap(sceneTypeName,UserUtil.getCurrentUser().getId()));
    }

    @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(StatusEnum.OK.getCode());
        return AjaxMessage.success(mapSettingService.saveMap(mapSetting));
    }

}