package org.springblade.gateway.web_gateway.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springblade.common.enums.OrderType;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.web_gateway.entity.dto.BillDataCensusDto;
import org.springblade.ldt.agent.entity.AgentLeagueRecord;
import org.springblade.ldt.agent.service.IAgentLeagueRecordService;
import org.springblade.ldt.agent.vo.AgentLeagueRecordVO;
import org.springblade.ldt.agent.wrapper.AgentLeagueRecordWrapper;
import org.springblade.ldt.bills.entity.*;
import org.springblade.ldt.bills.service.*;
import org.springblade.ldt.bills.vo.*;
import org.springblade.ldt.bills.wrapper.*;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账单追溯
 */
@RestController
@RequestMapping("/web/billTrace")
@Api(tags = "账单追溯")
@AllArgsConstructor
public class BillTraceController {

	private ILoginUserService loginUserService;
	private IShopService shopService;
	private IMallService mallService;
	private IBillsService billsService;
	private IPointBillsService pointBillsService;
	private IBalanceBillsService balanceBillsService;
	private IFrozenRecService frozenRecService;
	private IPlatformBillsService platformBillsService;
	private IAgentLeagueRecordService agentLeagueRecordService;
	private IGoodsBillsService goodsBillsService;

	@GetMapping()
	@ApiOperation(value = "账单追溯")
	public R getBillTrace(@ApiParam(required = true) Long billId) {
		BillDataCensusDto billDataCensusDto = new BillDataCensusDto();
		BillsVO bills = BillsWrapper.build(loginUserService, shopService, mallService).entityVO(billsService.getById(billId));
		if (ObjectUtils.isNotEmpty(bills)) {
			PointBills pointBill = pointBillsService.getOne(Wrappers.<PointBills>lambdaQuery().eq(PointBills::getTradeNo, bills.getId()));
			if (ObjectUtils.isNotEmpty(pointBill)) {
				PointBillsVO pointBills = PointBillsWrapper.build(loginUserService, shopService, mallService).entityVO(pointBill);
				billDataCensusDto.setPointBills(pointBills);
			}

			BalanceBills balanceBill = balanceBillsService.getOne(Wrappers.<BalanceBills>lambdaQuery()
				.eq(BalanceBills::getTradeNo, bills.getId()).eq(BalanceBills::getType, OrderType.PAY_CONSUMER));
			if (ObjectUtils.isNotEmpty(balanceBill)) {
				BalanceBillsVO balanceBills = BalanceBillsWrapper.build(loginUserService, shopService, mallService).entityVO(balanceBill);
				billDataCensusDto.setBalanceBills(balanceBills);
			}

			PlatformBills platformBill = platformBillsService.getOne(Wrappers.<PlatformBills>lambdaQuery().eq(PlatformBills::getPayment, bills.getId()));
			if (ObjectUtils.isNotEmpty(platformBill)) {
				PlatformBillsVO platformBills = PlatformBillsWrapper.build(loginUserService, shopService, mallService).entityVO(platformBill);
				billDataCensusDto.setPlatformBills(platformBills);
			}

			AgentLeagueRecord agentLeagueRecord = agentLeagueRecordService.getOne(Wrappers.<AgentLeagueRecord>lambdaQuery().eq(AgentLeagueRecord::getTradeNo, bills.getId()));
			if (ObjectUtils.isNotEmpty(agentLeagueRecord)) {
				AgentLeagueRecordVO agentLeague = AgentLeagueRecordWrapper.build().entityVO(agentLeagueRecord);
				billDataCensusDto.setAgentLeagueRecord(agentLeague);
			}

			GoodsBills goodsBill = goodsBillsService.getOne(Wrappers.<GoodsBills>lambdaQuery().eq(GoodsBills::getBillsId, bills.getId()));
			if (ObjectUtils.isNotEmpty(goodsBill)) {
				GoodsBillsVO goodsBills = GoodsBillsWrapper.build().entityVO(goodsBill);
				billDataCensusDto.setGoodsBills(goodsBills);
			}

			FrozenRec frozenRec = frozenRecService.getOne(Wrappers.<FrozenRec>lambdaQuery().eq(FrozenRec::getTradeNo, bills.getId()));
			billDataCensusDto.setBills(bills);
			billDataCensusDto.setFrozenRec(frozenRec);
		}
		return R.data(billDataCensusDto);
	}

	@GetMapping("/getBills")
	@ApiOperation(value = "账单列表分页", notes = "传入bills")
	public R<IPage<BillsVO>> list(Bills bills, Query query) {
		IPage<Bills> iPage = billsService.page(Condition.getPage(query), Condition.getQueryWrapper(bills).lambda().orderByDesc(Bills::getCreateTime));
		return R.data(BillsWrapper.build(loginUserService, shopService, mallService).pageVO(iPage));
	}

}
