package org.dromara.app.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzj.soopin.order.business.IOrderBusiness;
import com.wzj.soopin.order.domain.bo.OrderBo;
import com.wzj.soopin.order.domain.entity.Order;
import com.wzj.soopin.order.domain.vo.OrderVO;
import com.wzj.soopin.order.service.IMerchantOrderService;
import com.wzj.soopin.order.service.VerificationCodeService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;

/**
 * 商户端,订单接口
 *
 * @author Chopper
 * @since 2020-12-18 16:59
 */
@Slf4j
@Api(tags = "商户端,订单接口")
@RequestMapping("/app/merchant/order")
@RestController
@RequiredArgsConstructor
public class AppMerchantOrderController {

    private final VerificationCodeService verificationCodeService;

    private final IMerchantOrderService merchantOrderService;

    private final IOrderBusiness orderBusiness;
    /**
     * 扫码核销接口
     * @return 核销结果
     */
    @GetMapping("/verify")
    @Tag(name = "核销接口")
    public R verifyCode(@RequestParam("code") String code) {

        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.fail("用户未登录");
        }
        if(loginUser.getTenantId() == null){
            return R.fail("用户不是商户");
        }
         verificationCodeService.verifyCode(code, loginUser.getTenantId());

        return R.ok("核销成功");
    }
    /**
     * 扫码核销接口
     * @return 核销结果
     */
    @GetMapping("/scan")
    @Tag(name = "扫码接口")
    public R scan(@RequestParam("code") String code) {

        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            return R.fail("用户未登录");
        }
        if(loginUser.getTenantId() == null){
            return R.fail("用户不是商户");
        }
        Order order = verificationCodeService.scan(code);
        return R.ok(orderBusiness.info(order.getId()));
    }

    @Tag(name ="查询订单列表")
    @PostMapping("/page")
    public R<IPage<OrderVO>> page(@RequestBody OrderBo bo){
        LoginUser loginUser= LoginHelper.getLoginUser();
        bo.setTenantId(loginUser.getTenantId());
        return R.ok(merchantOrderService.page(bo));
    }
}
