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

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.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.multishop.dto.ShopWalletLogSearchDTO;
import com.tmerclub.cloud.multishop.mongo.MongoShopWalletLogBO;
import com.tmerclub.cloud.multishop.service.ShopWalletLogService;
import com.tmerclub.cloud.multishop.service.ShopWalletService;
import com.tmerclub.cloud.multishop.vo.ShopWalletLogExcelVO;
import com.tmerclub.cloud.multishop.vo.ShopWalletVO;
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;
import java.util.Objects;

/**
 * 店铺钱包日志
 * @author Pineapple
 * @date 2021/6/8 19:46
 */
@RestController("adminShopWalletController")
@RequestMapping("/mp/shopWallet")
@Tag(name = "admin-店铺钱包")
public class ShopWalletController {

    @Autowired
    private ShopWalletLogService shopWalletLogService;

    @Autowired
    private ShopWalletService shopWalletService;

    @GetMapping("/page")
    @Operation(summary = "分页查询钱包记录信息", description = "分页查询")
    public ServerResponseEntity<PageVO<MongoShopWalletLogBO>> getShopWalletLogPage(PageDTO pageDTO, ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            shopWalletLogSearchDTO.setShopId(shopId);
        }
        PageVO<MongoShopWalletLogBO> page = shopWalletLogService.pageByParam(pageDTO, shopWalletLogSearchDTO);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/getAllShopWalletVoByShopId")
    @Operation(summary = "查看所有店铺钱包总信息")
    public ServerResponseEntity<ShopWalletVO> getAllShopWalletVoByShopId(ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        ShopWalletVO shopWallet = shopWalletService.getAllShop(shopWalletLogSearchDTO);
        return ServerResponseEntity.success(shopWallet);
    }

    @GetMapping("/pageShopWalletByTime")
    @Operation(summary = "分页查看店铺钱包总信息")
    public ServerResponseEntity<PageVO<ShopWalletVO>> pageShopWalletByTime(PageDTO pageDTO, ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        PageVO<ShopWalletVO> pageShopWalletByTime = shopWalletService.pageShopWalletByTime(pageDTO, shopWalletLogSearchDTO);
        return ServerResponseEntity.success(pageShopWalletByTime);
    }

    @GetMapping("/getAllShopWalletLogPage")
    @Operation(summary = "查看所有店铺的日志")
    public ServerResponseEntity<PageVO<MongoShopWalletLogBO>> getAllShopWalletLogPage(PageDTO pageDTO, ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        PageVO<MongoShopWalletLogBO> page = shopWalletLogService.pageAllShop(pageDTO, shopWalletLogSearchDTO);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/getShopWalletLogForm")
    @Operation(summary = "导出店铺结算报表", description = "导出店铺结算报表")
    public ServerResponseEntity<Void> getShopWalletLogForm(HttpServletResponse response, ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        List<ShopWalletLogExcelVO> list = shopWalletLogService.listShopWalletLogExcel(shopWalletLogSearchDTO);
        ExcelUtil.soleExcel(response, list, ShopWalletLogExcelVO.EXCEL_NAME, ShopWalletLogExcelVO.MERGE_ROW_INDEX, ShopWalletLogExcelVO.MERGE_COLUMN_INDEX, ShopWalletLogExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getShopWalletVoByShopId")
    @Operation(summary = "查看店铺钱包信息", description = "根据店铺id查看店铺钱包信息")
    public ServerResponseEntity<ShopWalletVO> getShopWalletVoByShopId() {
        Long shopId = AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            return ServerResponseEntity.success(shopWalletLogService.getPlatformWallet());
        }
        ShopWalletVO shopWallet = shopWalletService.getByShopId(shopId);
        return ServerResponseEntity.success(shopWallet);
    }
}
