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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.order.MaterialOrderBuy;
import com.gree.mall.miniapp.bean.order.WorkerOrderBuy;
import com.gree.mall.miniapp.bean.order.WorkerOrderDetail;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.bean.workorder.RpMaterialOrderCount;
import com.gree.mall.miniapp.bean.workorder.RpMaterialOrderVO;
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.engin.RpMaterialLogic;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "工程维保费用记录", tags = {"工程维保费用记录"})
@RequestMapping(value = "/engin/material", produces = "application/json; charset=utf-8")
public class RpMaterialController {

    @Resource
    RpMaterialLogic rpMaterialLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;

    @PostMapping("/list/page")
    @ApiOperation(value = "工程维保费用记录列表")
    public ResponseHelper<Page<RpMaterialOrder>> page(
            @ApiParam(value = "审批状态 WAIT 待审 OK 审批  FAIL 驳回  PAID 现场支付 NO 取消 NO_PAID 未支付",required =false)
            @RequestParam(required = false) String examineStatus,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize

    ) throws RemoteServiceException {
        IPage<RpMaterialOrder> page = rpMaterialLogic.page(examineStatus, pageNum, pageSize);
        return ResponseHelper.success(page);
    }


    @PostMapping("/list/pageCount")
    @ApiOperation(value = "工程维保费用记录统计")
    public ResponseHelper<RpMaterialOrderCount> pageCount(


    ) throws RemoteServiceException {
        RpMaterialOrderCount page = rpMaterialLogic.pageCount();
        return ResponseHelper.success(page);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "工程维保费用记录详情")
    public ResponseHelper<RpMaterialOrderVO> detail(
            @ApiParam(value = "id", required = true)   @RequestParam(required = true) String id


    ) throws RemoteServiceException {
        RpMaterialOrderVO rpMaterialOrder = rpMaterialLogic.detail(id);
        return ResponseHelper.success(rpMaterialOrder);
    }


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



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

    ) throws Exception {
        rpMaterialLogic.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 = rpMaterialLogic.paid(orderId,authCode,ip,payType);
            return ResponseHelper.success(payStatus);
        } finally {
            obtain.unlock();
        }

    }

    @PostMapping("/apply")
    @ApiOperation(value = "确认申请")
    public ResponseHelper<String> cancel(
            @RequestBody MaterialOrderBuy materialOrderBuy,
            HttpServletRequest request
    ) throws Exception {
        String workerOrderDetail =rpMaterialLogic.apply(materialOrderBuy);
        return ResponseHelper.success(workerOrderDetail);
    }


    @PostMapping("/detailWorker")
    @ApiOperation(value = "工程维保费用记录详情-电子支付列表")
    public ResponseHelper<List<RpMaterialOrderVO>> detailWorker(
            @ApiParam(value = "工单号", required = true)   @RequestParam(required = true) String workerOrderId


    ) throws RemoteServiceException {
        List<RpMaterialOrderVO> rpMaterialOrder = rpMaterialLogic.detailWorker(workerOrderId);
        return ResponseHelper.success(rpMaterialOrder);
    }

    @PostMapping("/isBy")
    @ApiOperation(value = "用工程维保费用记录-是否可以审批")
    public ResponseHelper<Boolean> isBy(

            @ApiParam(value = "工单号",required = true) @RequestParam(required = true) String orderBaserId

    ) throws Exception {

        Boolean isBy = rpMaterialLogic.isBy(orderBaserId);

        return ResponseHelper.success(isBy);
    }


    @PostMapping("/examine")
    @ApiOperation(value = "工程维保费用记录-审批")
    public ResponseHelper examine(
            @ApiParam(value = "申请单号",required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "审批状态 WAIT 待审 OK 审批  FAIL 驳回  PAID 现场支付 NO 取消 NO_PAID 未支付",required = true) @RequestParam(required = true) String examineStatus,
            @ApiParam(value = "审批备注",required = false) @RequestParam(required = false) String examineRemark

    ) throws Exception {

        rpMaterialLogic.examine(id,examineRemark,examineStatus);

        return ResponseHelper.success();
    }




    @PostMapping("/cancelOrder")
    @ApiOperation(value = "工程维保费用记录-取消")
    public ResponseHelper cancelOrder(
            @ApiParam(value = "申请单号",required = true) @RequestParam(required = true) String id

    ) throws Exception {

        rpMaterialLogic.cancelOrder(id);

        return ResponseHelper.success();
    }

}
