/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.gateway.goods_gatway.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.AppConstant;
import org.springblade.common.enums.GoodsConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.gateway.goods_gatway.dto.AppGoodsBillsDTO;
import org.springblade.gateway.goods_gatway.service.IAppGoodsBillsService;
import org.springblade.gateway.goods_gatway.wrapper.AppGoodsBillsWrapper;
import org.springblade.ldt.bills.dto.GoodsBillsDTO;
import org.springblade.ldt.bills.entity.GoodsBills;
import org.springblade.ldt.bills.service.IGoodsBillsService;
import org.springblade.ldt.bills.vo.GoodsBillsVO;
import org.springblade.ldt.bills.wrapper.GoodsBillsWrapper;
import org.springblade.ldt.goods.dto.GoodsDTO;
import org.springblade.ldt.shop.service.IShopService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

/**
 * 订单 控制器
 *
 * @author huangmp
 * @since 2021-09-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("goodsbills")
@Api(value = "订单", tags = "订单接口")
public class AppGoodsBillsController extends BladeController {

	private final IAppGoodsBillsService appGoodsBillsService;
	private final IShopService shopService;

	@PostMapping("/order")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "下单", notes = "传入goods")
	public R order(@RequestBody AppGoodsBillsDTO appGoodsBillsDTO) {
		return R.data(appGoodsBillsService.order(appGoodsBillsDTO));
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入goodsBills")
	public R detail(GoodsBills goodsBills) {
		GoodsBills detail = appGoodsBillsService.getOne(Condition.getQueryWrapper(goodsBills));
		return R.data(AppGoodsBillsWrapper.build(shopService).entityVO(detail));
	}
	/**
	 * 分页 订单
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入goodsBills")
	public R<IPage<GoodsBillsVO>> list(GoodsBills goodsBills, Query query) {
		String formatTime = null;
		if (!Objects.isNull(goodsBills.getCreateTime())) {
			formatTime = DateUtil.format(goodsBills.getCreateTime(), "yyyy-MM-dd");
			goodsBills.setCreateTime(null);
		}
		IPage<GoodsBills> pages = appGoodsBillsService.page(Condition.getPage(query), Condition.getQueryWrapper(goodsBills).lambda()
			.like(StrUtil.isNotEmpty(formatTime),GoodsBills::getCreateTime,formatTime).orderByDesc(GoodsBills::getCreateTime));
		return R.data(GoodsBillsWrapper.build(shopService).pageVO(pages));
	}

	/**
	 * 商户的订单中心
	 * @param goodsBills
	 * @param query
	 * @return
	 */
	@GetMapping("/listShop")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入goodsBills")
	public R<IPage<GoodsBillsVO>> listShop(GoodsBills goodsBills, Query query, String goodsStatus ,String orderDate) {
		//只查询出付款成功的订单
		IPage<GoodsBills> pages = appGoodsBillsService.page(Condition.getPage(query), Condition.getQueryWrapper(goodsBills).lambda()
			.eq(GoodsBills::getPayStatus, AppConstant.BillPayStatus.付款成功.name())
			.eq(StrUtil.isNotBlank(goodsStatus), GoodsBills::getOrderStatus, goodsStatus)
			.like(orderDate != null, GoodsBills::getCreateTime, orderDate)
			.orderByDesc(GoodsBills::getCreateTime));
		return R.data(GoodsBillsWrapper.build().pageVO(pages));
	}


}
