package com.prolog.eis.controller.shelves;

import com.prolog.eis.constants.station.StationTaskTypeConstants;
import com.prolog.eis.dto.shelves.*;
import com.prolog.eis.service.shelves.ShelvesService;
import com.prolog.eis.vo.GoodsInfoVO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * WEB接口(货位调整界面)
 *
 * @author zhanghan
 */
@Api(tags = "WEB接口(货位调整界面)")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/locationAdjustment")
public class LocationAdjustmentController {

    private final ShelvesService shelvesService;

    @ApiOperation(value = "验证工作站", notes = "验证工作站")
    @PostMapping("/validateStation")
    public RestMessage<Object> validateStation(@RequestBody StationNoDTO dto) {
        try {
            return shelvesService.validateStation(dto.getStationNo(), StationTaskTypeConstants.STATION_TYPE_CHANGE);
        } catch (Exception e) {
            log.error("验证工作站异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询货架分页", notes = "查询货架分页")
    @PostMapping("/findShelvesPage")
    public RestMessage<Page<GoodsInfoVO>> findShelvesPage(@RequestBody ShelvesPage page) {
        try {
            return RestMessage.querySuccess(shelvesService.findShelvesPage(page));
        } catch (IllegalArgumentException e) {
            return RestMessage.error("201", e.getMessage());
        } catch (Exception e) {
            log.error("查询货架分页错误：{}", e.getMessage(), e);
            return RestMessage.error("202", e.getMessage());
        }
    }

    @ApiOperation(value = "调出货架", notes = "调出货架")
    @PostMapping("/callOutShelves")
    public RestMessage<String> callOutShelves(@RequestBody CallOutShelvesDTO dto) {
        try {
            shelvesService.callOutShelves(dto);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            return RestMessage.error("202", e.getMessage());
        }
    }

    @ApiOperation(value = "查询原货位信息", notes = "查询原货位信息")
    @PostMapping("/querySourcePalletLocationInfo")
    public RestMessage<GoodsInfoVO> querySourcePalletLocationInfo(@RequestBody QueryLocationDto dto) {
        GoodsInfoVO goodsInfoVO = null;
        try {
            goodsInfoVO = shelvesService.querySourcePalletLocationInfo(dto);
            goodsInfoVO.setStationNo(dto.getStationNo());
        } catch (IllegalArgumentException e) {
            return RestMessage.newInstance(false, "201", e.getMessage(), goodsInfoVO);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "202", e.getMessage(), goodsInfoVO);
        }
        return RestMessage.newInstance(true, "查询成功！", goodsInfoVO);
    }

    @ApiOperation(value = "查询目的货位信息", notes = "查询目的货位信息")
    @PostMapping("/queryTargetPalletLocationInfo")
    public RestMessage<GoodsInfoVO> queryTargetPalletLocationInfo(@RequestBody QueryLocationDto dto) throws Exception {
        GoodsInfoVO goodsInfoVO = null;
        try {
            goodsInfoVO = shelvesService.queryTargetPalletLocationInfo(dto);
            goodsInfoVO.setStationNo(dto.getStationNo().trim().toUpperCase());
        } catch (IllegalArgumentException e) {
            return RestMessage.newInstance(false, "201", e.getMessage(), goodsInfoVO);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "202", e.getMessage(), goodsInfoVO);
        }
        return RestMessage.newInstance(true, "查询成功！", goodsInfoVO);
    }


    @ApiOperation(value = "确认调整", notes = "确认调整")
    @PostMapping("/confirmAdjust")
    public RestMessage<String> confirmAdjust(@RequestBody  ConfirmAdjustDto dto) throws Exception {
        try {
            shelvesService.confirmAdjust(dto);
        }catch (IllegalArgumentException e){
            return RestMessage.newInstance(false, "201", e.getMessage(), null);
        }
        return RestMessage.newInstance(true, "调整成功！", null);
    }

    @ApiOperation(value = "货架归还", notes = "货架归还")
    @PostMapping("/returnGoodsShelf")
    public RestMessage<String> returnGoodsShelf(@RequestBody ReturnGoodsShelfDto dto) throws Exception {
        try {
            synchronized (("hwtz" + dto.getStationNo() + dto.getPalletNo()).intern()) {
                RestMessage<String> restMessage = shelvesService.returnGoodsShelf(dto);
                return restMessage;
            }
        } catch (Exception e) {
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }


}
