package com.gree.mall.miniapp.controller.pay;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.order.OrderCount;
import com.gree.mall.miniapp.bean.order.WorkerOrderBuy;
import com.gree.mall.miniapp.bean.order.WorkerOrderDetail;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.order.PayOrderLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.utils.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "辅材配件销售", tags = {"辅材配件销售"})
@RequestMapping(value = "/pay", produces = "application/json; charset=utf-8")
public class PayOrderController {

    @Resource
    PayOrderLogic payOrderLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;


    @PostMapping("/chargeList")
    @ApiOperation(value = "辅材产品列表")
    public ResponseHelper<Page<WebsitNormCharge>> pageCharge(
            @ApiParam(required = false, value = "商品名称") @RequestParam(required = false) String name,
            @ApiParam(required = false, value = "分类id") @RequestParam(required = false) String categoryId,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        Page<WebsitNormCharge> websitNormChargePage = payOrderLogic.pageCharge(name, categoryId, orderId, pageNum, pageSize);
        return ResponseHelper.success(websitNormChargePage);
    }

    @PostMapping("/orderPage")
    @ApiOperation(value = "订单列表")
    public ResponseHelper<Page<WorkerOrder>> orderPage(
            @ApiParam(required = false, value = "商品类型 M=辅材 P=配件") @RequestParam(required = false) String goodsType,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId,
            @ApiParam(required = false, value = "销售类型 OWN自有 OUT 外购") @RequestParam(required = false) String settlementType,
            @ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "年月") @RequestParam(required = false) String yearMonth,
            @ApiParam(required = false, value = "支付状态CANEL 取消 WAIT 待支付 PAID 已支付") @RequestParam(required = false) String payStatus,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        Page<WorkerOrder> workerOrderVOPage = payOrderLogic.orderPage(yearMonth, settlementType, goodsType, orderId, startTime, endTime, payStatus, pageNum, pageSize);
        return ResponseHelper.success(workerOrderVOPage);
    }


    @PostMapping("/orderCount")
    @ApiOperation(value = "订单统计")
    public ResponseHelper<OrderCount> orderCount(
            @ApiParam(required = false, value = "商品类型 M=辅材 P=配件") @RequestParam(required = false) String goodsType,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId,
            @ApiParam(required = false, value = "销售类型 OWN自有 OUT 外购") @RequestParam(required = false) String settlementType,
            @ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String startTime,
            @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime,
            @ApiParam(required = false, value = "支付状态CANEL 取消 WAIT 待支付 PAID 已支付") @RequestParam(required = false) String payStatus

    ) throws Exception {
        OrderCount workerOrderVOPage = payOrderLogic.orderCount(settlementType, goodsType, orderId, startTime, endTime, payStatus);
        return ResponseHelper.success(workerOrderVOPage);
    }


    @PostMapping("/buy")
    @ApiOperation(value = "提交订单,返回订单号")
    public ResponseHelper<String> buy(
            @RequestBody WorkerOrderBuy workerOrderBuy,
            HttpServletRequest request
    ) throws Exception {
        String ip = IpUtil.getIpAddr(request);
        String workerOrderDetail = payOrderLogic.buy(workerOrderBuy, ip, request);
        return ResponseHelper.success(workerOrderDetail);
    }

    @PostMapping("/getOrder")
    @ApiOperation(value = "获得订单")
    public ResponseHelper<WorkerOrderDetail> getOrder(
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId

    ) throws Exception {
        WorkerOrderDetail workerOrderDetail = payOrderLogic.getOrder(orderId);
        return ResponseHelper.success(workerOrderDetail);
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "取消订单")
    public ResponseHelper cancel(
            @ApiParam(required = true, value = "订单号") @RequestParam(required = true) String orderId

    ) throws Exception {
        payOrderLogic.cancel(orderId);
        return ResponseHelper.success();
    }


    @PostMapping("/paid")
    @ApiOperation(value = "扫码支付获得结果")
    public ResponseHelper<Boolean> paid(
            @ApiParam(required = true, value = "订单号") @RequestParam(required = true) String orderId,
            @ApiParam(required = false, value = "扫付款码得到得code") @RequestParam(required = false) String authCode,
            @ApiParam(required = true, value = "WECHAT 微信支付  LINE 线下支付") @RequestParam(required = true) String payType,
            HttpServletRequest request
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + orderId);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            String ip = IpUtil.getIpAddr(request);
            Boolean payStatus = payOrderLogic.paid(orderId, authCode, ip, payType);
            return ResponseHelper.success(payStatus);
        } finally {
            obtain.unlock();
        }

    }

    @PostMapping("/websitGoodsList")
    @ApiOperation(value = "配件列表")
    public ResponseHelper<Page<WebsitGoods>> websitGoodsList(
            @ApiParam(required = false, value = "商品名称") @RequestParam(required = false) String name,
            @ApiParam(required = false, value = "服务大类") @RequestParam(required = false) String categoryName,
            @ApiParam(required = false, value = "品牌") @RequestParam(required = false) String brand,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId,
            @ApiParam(required = false, value = "收费类型 M=物料收费 S=服务收费") @RequestParam(required = false) String normType,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        Page<WebsitGoods> websitGoodsList = payOrderLogic.websitGoodsList(normType, brand, name, orderId, categoryName, pageNum, pageSize);
        return ResponseHelper.success(websitGoodsList);
    }


    @PostMapping("/websitCategoryList")
    @ApiOperation(value = "分类")
    public ResponseHelper<Page<WebsitGoodsCategory>> websitCategoryList(

            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {
        Page<WebsitGoodsCategory> websitGoodsList = payOrderLogic.websitCategoryList(pageNum, pageSize);
        return ResponseHelper.success(websitGoodsList);
    }


    @PostMapping("/sysDict")
    @ApiOperation(value = "获得字典值")
    public ResponseHelper<List<SysDictCompany>> sysDict(
            @ApiParam(required = true, value = "BRAND 品牌 ASSIST_UNIT辅材单位") @RequestParam(required = true) String dictType

    ) throws Exception {
        List<SysDictCompany> sysDictCompanyList = payOrderLogic.sysDict(dictType);
        return ResponseHelper.success(sysDictCompanyList);
    }

    @PostMapping("/getBrand")
    @ApiOperation(value = "获得品牌")
    public ResponseHelper<List<Brand>> getBrand(

    ) throws Exception {
        List<Brand> brand = payOrderLogic.getBrand();
        return ResponseHelper.success(brand);
    }

    @PostMapping("/getCategoryList")
    @ApiOperation(value = "获得服务大类")
    public ResponseHelper<List<GoodsCategory>> getCategoryList(

    ) throws Exception {
        List<GoodsCategory> sysDictCompanyList = payOrderLogic.getCategoryList();
        return ResponseHelper.success(sysDictCompanyList);
    }


}
