/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.Netty.rest;

import com.youchain.Netty.model.Jb;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.repository.PointRepository;
import com.youchain.exception.handler.ApiResult;
import com.youchain.modules.mnt.domain.App;
import com.youchain.modules.mnt.service.AppService;
import com.youchain.modules.mnt.service.dto.AppQueryCriteria;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.BizStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
* @author zhanghouying
* @date 2019-08-24
*/
@RestController
@RequiredArgsConstructor
@Api(tags = "合肥暖通PDA")
@RequestMapping("/api/pdaHvac")
public class PdaHvacController {
    private final PointRepository pointRepository;
    @Log("解绑备货位")
    @ApiOperation("解绑备货位")
    @PostMapping(value = "/jb")
    @AnonymousAccess
    public ResponseEntity<Object> Jb(@RequestBody Jb jb){
        ApiResult apiResult=ApiResult.fail(200,"解绑成功",null);
        Point point = pointRepository.findByCode(jb.getPointCode());
        if (point!=null){
            if (!point.getStatus().equals(BaseStatus.USED)) {
                apiResult=ApiResult.fail(400,"库位未占用",null);
            }
            else if (!point.getArea().getCode().equals("出库接驳口")){
                apiResult=ApiResult.fail(400,"库位不在出库接驳点",null);
            }
            else {
                point.setStatus(BaseStatus.FREE);
                pointRepository.save(point);
            }
        }else {
            apiResult=ApiResult.fail(400,"未查询到库位",null);
        }
        return new ResponseEntity<>(apiResult.getMsg(),apiResult.getStatus());
    }
}
