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

import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.LocalDateTimeUtils;
import com.neusoft.smart.pos.inventory.constants.InventoryErrorCode;
import com.neusoft.smart.pos.dto.inventory.request.InventoryCheckStockDetailRequest;
import com.neusoft.smart.pos.dto.inventory.request.InventoryCheckStockListRequest;
import com.neusoft.smart.pos.dto.inventory.response.InventoryCheckStockDetailResponse;
import com.neusoft.smart.pos.dto.inventory.response.InventoryCheckStockListResponse;
import com.neusoft.smart.pos.dto.inventory.response.InventoryCheckStockSummaryResponse;
import com.neusoft.smart.pos.inventory.service.InventoryCheckStockService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "库存盘点使用rest接口", description = "库存盘点使用rest接口")
@RequestMapping(path = "/inventory/check-stock")
@Validated
public class InventoryCheckStockController {
    
    @Autowired
    InventoryCheckStockService checkStockService;
    
    @ApiOperation(value = "库存盘点查询列表接口")
    @GetMapping("/list")
    public ResponseData<InventoryCheckStockListResponse> getList(InventoryCheckStockListRequest request) {
        return new ResponseData<>(checkStockService.getList(request));
    }
    
    
    @ApiOperation(value = "库存盘点详情接口")
    @GetMapping("/detail")
    public ResponseData<InventoryCheckStockDetailResponse> detail(InventoryCheckStockDetailRequest request) {
        return new ResponseData<>(checkStockService.getDetail(request));
    }
    
    @ApiOperation(value = "库存盘点详情接口")
    @GetMapping("/summary")
    public ResponseData<InventoryCheckStockSummaryResponse> summary(@RequestParam(value = "check_id") Integer checkId) {
        return new ResponseData<>(checkStockService.summary(checkId));
    }
    
    @ApiOperation(value = "导出库存库存清单接口")
    @GetMapping("/export")
    public void export(@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(LocalDateTimeUtils.SIMPLE_DATETIME_FORMAT) + ".xlsx").getBytes(), "iso-8859-1"));
        
        checkStockService.export(shopId, response.getOutputStream());
    }
    
    @ApiOperation(value = "导出库存库存清单接口")
    @PostMapping("/import")
    public ResponseData<Integer> importFile(
            @RequestParam(value = "shop_id") Integer shopId,
            @RequestParam(value = "operator") String operator,
            @RequestParam("file") MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        String fileType = filename.substring(filename.lastIndexOf("."));
        if (!fileType.equals(".xlsx") && !fileType.equals(".xls")) {
            throw new BusinessCommonException(InventoryErrorCode.FILE_TYPE_ERROR);
        }
        
        return new ResponseData<>(checkStockService.importFile(shopId, operator, file));
    }
}
