
package com.tmerclub.cloud.product.controller.multishop;

import com.alibaba.excel.EasyExcel;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.common.util.PriceUtil;
import com.tmerclub.cloud.product.dto.StockBillLogDTO;
import com.tmerclub.cloud.product.listener.StockBillLogItemExcelListener;
import com.tmerclub.cloud.product.service.StockBillLogItemService;
import com.tmerclub.cloud.product.vo.StockBillLogItemExcelVO;
import com.tmerclub.cloud.product.vo.StockBillLogItemImportExcelVO;
import com.tmerclub.cloud.product.vo.StockBillLogItemImportVO;
import com.tmerclub.cloud.product.vo.mongo.MongoStockBillLogItemVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 出入库商品项
 *
 * @author lhd
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopStockBillLogItemController")
@RequestMapping("/m/stock_bill_log_item")
@Tag(name = "商家出入库商品项")
public class StockBillLogItemController {

    @Autowired
    private StockBillLogItemService stockBillLogItemService;

    @GetMapping("/page")
    @Operation(summary = "库存流水", description = "库存流水")
    public ServerResponseEntity<PageVO<MongoStockBillLogItemVO>> page(@Valid PageDTO pageDTO, StockBillLogDTO stockBillLogDTO) {
        stockBillLogDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<MongoStockBillLogItemVO> stockBillLogItemPage = stockBillLogItemService.page(pageDTO, stockBillLogDTO);
        return ServerResponseEntity.success(stockBillLogItemPage);
    }

    @GetMapping("/download_model")
    @Operation(summary = "下载导入出入库商品项模板", description = "下载导入出入库商品项模板")
    public ServerResponseEntity<Void> downloadModel(HttpServletResponse response, Integer type) {
        stockBillLogItemService.downloadModel(response, type);
        return ServerResponseEntity.success();
    }

    @GetMapping("/export_flow")
    @Operation(summary = "导出库存流水", description = "导出库存流水")
    public void exportFlow(StockBillLogDTO stockBillLogDTO, HttpServletResponse response) {
        stockBillLogDTO.setShopId(AuthUserContext.get().getTenantId());
        List<StockBillLogItemExcelVO> stockBillLogItemVOList = stockBillLogItemService.exportFlow(stockBillLogDTO, response);
        for (StockBillLogItemExcelVO stockBillLogItemExcelVO : stockBillLogItemVOList) {
            // 价格单位转换
            stockBillLogItemExcelVO.setUnitPrice(PriceUtil.divideByBankerRounding(stockBillLogItemExcelVO.getUnitPrice(), 100));
        }
        ExcelUtil.soleExcel(response, stockBillLogItemVOList, StockBillLogItemExcelVO.EXCEL_NAME, StockBillLogItemExcelVO.MERGE_ROW_INDEX, StockBillLogItemExcelVO.MERGE_COLUMN_INDEX, StockBillLogItemExcelVO.class);
    }

    @Operation(summary = "批量导入商品项", description = "批量导入商品项")
    @PostMapping("/importExcel")
    public ServerResponseEntity<StockBillLogItemImportVO> importExcel(@RequestParam("excelFile") MultipartFile file, @RequestParam("type") Integer type) {
        if (file == null) {
            throw new LuckException("网络繁忙，请稍后重试");
        }
        StockBillLogItemImportVO stockBillLogItemImportVO = new StockBillLogItemImportVO();
        try {
            // 错误信息集合
            List<String> errorList = new ArrayList<>(Constant.INITIAL_CAPACITY);
            EasyExcel.read(file.getInputStream(), StockBillLogItemImportExcelVO.class, new StockBillLogItemExcelListener(stockBillLogItemService, errorList, type, stockBillLogItemImportVO)).sheet().doRead();
        } catch (IOException e) {
            throw new LuckException(e.getMessage());
        }
        return ServerResponseEntity.success(stockBillLogItemImportVO);
    }
}
