package com.neusoft.smart.pos.inventory.controller;

import com.neusoft.smart.pos.dto.inventory.request.SellAndWarehouseOutRequest;
import com.neusoft.smart.pos.dto.inventory.response.InventoryGoodsConfigResponse;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.utils.LocalDateTimeUtils;
import com.neusoft.smart.pos.dto.inventory.request.InventoryConfigRequest;
import com.neusoft.smart.pos.dto.inventory.request.InventoryStoreHistoryRequest;
import com.neusoft.smart.pos.dto.inventory.request.InventoryStoreRequest;
import com.neusoft.smart.pos.dto.inventory.response.InventoryStoreHistoryResponse;
import com.neusoft.smart.pos.dto.inventory.response.InventoryStoreResponse;
import com.neusoft.smart.pos.dto.inventory.response.InventoryTotalAmountResponse;
import com.neusoft.smart.pos.inventory.service.GoodsDefaultService;
import com.neusoft.smart.pos.inventory.service.InventoryStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "库存查询使用rest接口", description = "库存查询使用rest接口")
@RequestMapping(path = "/inventory")
@Validated
public class InventoryStoreController {
    
    @Autowired
    private InventoryStoreService inventoryStoreService;
    
    @Autowired
    private GoodsDefaultService goodsDefaultService;
    
    @ApiOperation(value = "后台库存查询列表接口")
    @GetMapping("/list")
    public ResponseData<InventoryStoreResponse> getList(@Validated @ModelAttribute InventoryStoreRequest request) {
        return new ResponseData<>(inventoryStoreService.getList(request));
    }
    
    
    @ApiOperation(value = "后台库存总金额接口")
    @GetMapping("/total-amount")
    public ResponseData<InventoryTotalAmountResponse> getTotalAmount(@RequestParam(value = "shop_id") Integer shopId) {
        return new ResponseData<>(inventoryStoreService.getTotalAmount(shopId));
    }
    
    @ApiOperation(value = "导出库存列表接口")
    @GetMapping("/export-total")
    public void exportTotal(@RequestParam(value = "shop_id") Integer shopId, HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("库存列表-" + LocalDateTimeUtils.getDateTime() + ".xlsx").getBytes(), "iso-8859-1"));
        
        inventoryStoreService.exportTotal(shopId, response.getOutputStream());
    }
    
    @ApiOperation(value = "库存流水列表接口")
    @GetMapping("/history")
    public ResponseData<InventoryStoreHistoryResponse> history(@Validated @ModelAttribute InventoryStoreHistoryRequest request) {
        return new ResponseData<>(inventoryStoreService.getHistory(request));
    }
    
    @ApiOperation(value = "导出库存流水列表接口")
    @GetMapping("/export-history")
    public void exportHistory(@RequestParam(value = "shop_id") Integer shopId, HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("库存流水-" + LocalDateTimeUtils.getDateTime() + ".xlsx").getBytes(), "iso-8859-1"));
        
        inventoryStoreService.exportHistory(shopId, response.getOutputStream());
    }
    
    @ApiOperation(value = "pos或者其他端,订单状态更改后库存相应操作")
    @PutMapping("/store/order/{orderStatus}")
    public ResponseData<?> orderChangeStatus(@PathVariable Integer orderStatus, @RequestBody SellAndWarehouseOutRequest sellAndWarehouseOutRequest) {
        goodsDefaultService.orderChangeStatus(orderStatus, sellAndWarehouseOutRequest);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "创建门店或集团的进销存系统配置")
    @PostMapping("/config")
    public ResponseData<?> createConfig(@RequestBody InventoryConfigRequest InventoryConfigRequest) {
        inventoryStoreService.createInventoryConfig(InventoryConfigRequest);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "修改门店或集团的进销存系统配置")
    @PutMapping("/config")
    public ResponseData<?> updateConfig(@RequestBody InventoryConfigRequest InventoryConfigRequest) {
        inventoryStoreService.updateInventoryConfig(InventoryConfigRequest);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "获取门店或集团的进销存系统配置")
    @GetMapping("/config/{orgId}/{shopId}")
    public ResponseData<InventoryGoodsConfigResponse> getConfig(@PathVariable Integer orgId, @PathVariable Integer shopId) {
        return new ResponseData<>(inventoryStoreService.getInventoryConfig(orgId, shopId));
    }
    
}
