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

import com.tmerclub.cloud.common.constant.SysTypeEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.supplier.dto.PurchaseAmountLogDTO;
import com.tmerclub.cloud.supplier.service.PurchaseAmountLogService;
import com.tmerclub.cloud.supplier.vo.ShopSupplierNameVO;
import com.tmerclub.cloud.supplier.vo.TotalPurchaseAmountVO;
import com.tmerclub.cloud.supplier.vo.mongo.MongoPurchaseAmountLogVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author gaozijie
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/m/purchase_amount_log")
@Tag(name = "采购金额日志(商家)")
public class ShopPurchaseAmountLogController {

    @Autowired
    private PurchaseAmountLogService purchaseAmountLogService;

    @GetMapping("/list_supplier")
    @Operation(summary = "获取供应商列表")
    public ServerResponseEntity<List<ShopSupplierNameVO>> listSupplier() {
        Long shopId = AuthUserContext.get().getTenantId();
        List<ShopSupplierNameVO> supplierNames = purchaseAmountLogService.listSupplier(shopId);
        return ServerResponseEntity.success(supplierNames);
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取采购金额日志")
    public ServerResponseEntity<PageVO<MongoPurchaseAmountLogVO>> pagePurchaseLog(PageDTO pageDTO, PurchaseAmountLogDTO purchaseAmountLogDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        purchaseAmountLogDTO.setShopId(shopId);
        purchaseAmountLogDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        PageVO<MongoPurchaseAmountLogVO> page = purchaseAmountLogService.pagePurchaseAmountLog(pageDTO, purchaseAmountLogDTO);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/get_total_amount")
    @Operation(summary = "获取采购总金额数据")
    public ServerResponseEntity<TotalPurchaseAmountVO> getTotalAmount(PurchaseAmountLogDTO purchaseAmountLogDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        purchaseAmountLogDTO.setShopId(shopId);
        purchaseAmountLogDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        TotalPurchaseAmountVO totalPurchaseAmount = purchaseAmountLogService.getTotalPurchaseAmount(purchaseAmountLogDTO);
        return ServerResponseEntity.success(totalPurchaseAmount);
    }

    @GetMapping("/export_excel")
    @Operation(summary = "导出excel")
    public void exportExcel(HttpServletResponse response, PurchaseAmountLogDTO purchaseAmountLogDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        purchaseAmountLogDTO.setShopId(shopId);
        purchaseAmountLogDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        purchaseAmountLogService.exportExcel(response, purchaseAmountLogDTO);
    }
}
