package cn.pluss.platform.controller.bill;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.bill.BillSendRecordService;
import cn.pluss.platform.bill.MemberTransStatisticsService;
import cn.pluss.platform.bill.MerchantBillStatisticsService;
import cn.pluss.platform.entity.BillSendRecord;
import cn.pluss.platform.entity.MemberTransStatistics;
import cn.pluss.platform.entity.MerchantBillStatistics;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 商户账单统计
 * @author DJH
 */
@RestController
@RequestMapping("/bill")
public class BillStatisticsController {

    @Setter(onMethod_ = {@Autowired})
    private UserAppService userAppService;

    @Setter(onMethod_ = {@Autowired})
    private MerchantBillStatisticsService billStatisticsService;

    @Setter(onMethod_ = {@Autowired})
    private MemberTransStatisticsService memberTransStatisticsService;

    @Setter(onMethod_ = {@Autowired})
    private BillSendRecordService billSendRecordService;

    @GetMapping("/page")
    public Result<Page<MerchantBillStatistics>> historyDayBill(Page<MerchantBillStatistics> page, MerchantBillStatistics condition, OrderItem orderItem) {
        UserApp tokenUa = userAppService.queryUserAppByToken();
        if ("staff".equals(tokenUa.getUserType()) && !Objects.equals(tokenUa.getPermissionBill(), "1")) {
            return ResultGenerator.genSuccessResult(new Page<>());
        }
        page.getOrders().clear();
        page.getOrders().add(orderItem);
        Page<MerchantBillStatistics> pageData = billStatisticsService.page(page, new QueryWrapper<>(condition));
        return ResultGenerator.genSuccessResult(pageData);
    }

    /**
     * 查询历史会员账单流水
     * @param page          分页数据
     * @param condition     查询条件
     * @param orderItem     排序条件
     * @return
     */
    @GetMapping("/member/history/page")
    public Result<Page<MemberTransStatistics>> historyMemberBill(Page<MemberTransStatistics> page, MemberTransStatistics condition, OrderItem orderItem) {
        UserApp userApp = userAppService.queryUserAppByToken();
        if ("staff".equals(userApp.getUserType()) && !Objects.equals(userApp.getPermissionBill(), "1")) {
            return ResultGenerator.genSuccessResult(new Page<>());
        }

        page.getOrders().clear();
        page.getOrders().add(orderItem);
        condition.setUserId(userApp.getUserId().intValue());
        Page<MemberTransStatistics> pageData = memberTransStatisticsService.page(page, new QueryWrapper<>(condition));
        return ResultGenerator.genSuccessResult(pageData);
    }

    @GetMapping("/member/current")
    public Result<MemberTransStatistics> currentDuringMemberBill(MemberTransStatistics condition) {
        UserApp userApp = userAppService.queryUserAppByToken();
        condition.setUserId(userApp.getUserId().intValue());

        MemberTransStatistics result = memberTransStatisticsService.currentDuringBill(condition);
        if (result == null) {
            result = new MemberTransStatistics();
            result.setUserId(userApp.getUserId().intValue());
            result.setConsumeAmount(BigDecimal.ZERO);
            result.setGiveAmount(BigDecimal.ZERO);
            result.setRechargeAmount(BigDecimal.ZERO);
        }

        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("/reqSendBill")
    public Result<Object> reqSendBill(@RequestBody BillSendRecord entity) {
        billSendRecordService.reqBill(entity);
        return ResultGenerator.genSuccessResult("操作成功，报表会在5分钟内发送到该邮箱", null);
    }

    @GetMapping("/sendBillRecord/page")
    public Result<Page<BillSendRecord>> page(Page<BillSendRecord> page, BillSendRecord condition, OrderItem orderItem) {
        page.getOrders().clear();
        page.getOrders().add(orderItem);
        Page<BillSendRecord> pageData = billSendRecordService.page(page, new QueryWrapper<>(condition));
        return ResultGenerator.genSuccessResult(pageData);
    }
}
