package com.prolog.eis.controller.shelves;

import com.prolog.eis.dto.shelves.*;
import com.prolog.eis.service.shelves.ShelvesService;
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;
import org.springframework.web.multipart.MultipartFile;

/**
 * WEB接口(货架资料配置界面)
 *
 * @author zhanghan
 */
@Api(tags = "WEB接口(货架资料配置界面)")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/shelvesInfoConfig")
public class ShelvesInfoConfigController {

    private final ShelvesService shelvesService;

    @ApiOperation(value = "分页查询货架信息", notes = "分页查询货架信息")
    @PostMapping("/findShelvesInfoPage")
    public RestMessage<Page<ShelvesInfoDTO>> findShelvesInfoPage(@RequestBody ShelvesInfoPage page) {
        try {
            return RestMessage.querySuccess(shelvesService.findShelvesInfoPage(page));
        } catch (Exception e) {
            log.error("查询货架信息异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

    @ApiOperation(value = "新增货架", notes = "新增货架")
    @PostMapping(value = "/addShelves")
    public RestMessage<String> addShelves(@RequestBody ShelvesConfigureDTO dto) {
        try {
            shelvesService.addShelves(dto);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            log.error("新增货架异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

    @PostMapping(value = "/findCargoInformationDetail")
    @ApiOperation(value = "查询货格详情", notes = "查询货格详情")
    public RestMessage<PalletLocationDTO> findCargoInformationDetail(@RequestBody PalletNoDTO dto) {
        try {
            PalletLocationDTO palletLocationDTO = shelvesService.findCargoInformationDetail(dto.getPalletNo());
            return RestMessage.querySuccess(palletLocationDTO);
        } catch (Exception e) {
            log.error("查询货格详情异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

    @PostMapping(value = "/updateShelves")
    @ApiOperation(value = "修改货架", notes = "修改货架")
    public RestMessage<String> updateShelves(@RequestBody PalletLocationDTO dto) {
        try {
            shelvesService.updateShelves(dto);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            log.error("修改货架异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

    @PostMapping(value = "/deleteShelves")
    @ApiOperation(value = "删除货架", notes = "删除货架")
    public RestMessage<String> deleteShelves(@RequestBody PalletNoDTO dto) {
        try {
            shelvesService.deleteShelves(dto.getPalletNo());
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            log.error("删除货架异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

    @PostMapping(value = "/setTemplateShelves")
    @ApiOperation(value = "设置模板货架", notes = "设置模板货架")
    public RestMessage<String> setTemplateShelves(@RequestBody TemplateShelvesDTO dto) {
        try {
            shelvesService.setTemplateShelves(dto);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            log.error("设置模板货架异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

    @PostMapping(value = "/batchImport")
    @ApiOperation(value = "批量导入", notes = "批量导入")
    public RestMessage<String> batchImport(@RequestBody MultipartFile file) {
        try {
            shelvesService.batchImport(file);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            log.error("批量导入异常:{}", e.getMessage(), e);
            return RestMessage.error(e.getMessage());
        }
    }

}
