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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.PayDetail;
import com.gree.mall.miniapp.bean.order.WorkerOrderBuy;
import com.gree.mall.miniapp.bean.order.WorkerOrderDetail;
import com.gree.mall.miniapp.bean.workorder.IncreCountVO;
import com.gree.mall.miniapp.bean.workorder.PgIncreOrderVO;
import com.gree.mall.miniapp.bean.workorder.PgIncreVO;
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.workorder.IncreConfigLogic;
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.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "延保服务", tags = {"延保服务"})
@RequestMapping(value = "/increConfig", produces = "application/json; charset=utf-8")
public class IncreConfigController {

    @Autowired
    private IncreConfigLogic increConfigLogic;
    @Autowired
    RedisLockRegistry redisLockRegistry;



    @PostMapping("/list")
    @ApiOperation(value = "延保服务介绍")
    public ResponseHelper<IPage<PgIncre>> list(
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<PgIncre> increVOIPage = increConfigLogic.list(pageNum,pageSize);
        return ResponseHelper.success(increVOIPage);
    }

    @PostMapping("/detail")
    @ApiOperation("延保服务介绍详情")
    public ResponseHelper<PgIncreVO> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        PgIncreVO pgIncreVO = increConfigLogic.detail(id);
        return ResponseHelper.success(pgIncreVO);
    }

    @PostMapping("/getIncreItem")
    @ApiOperation(value = "获得服务内容得金额和限值")
    public ResponseHelper<List<PgIncreItem>> getIncreItem(
            @ApiParam(value = "品牌",required = false) @RequestParam(required = false) String brand,
            @ApiParam(value = "大类",required = false) @RequestParam(required = false) String mainName
    ) throws RemoteServiceException {
        List<PgIncreItem> pgIncreItem = increConfigLogic.getIncreItem(brand,mainName);
        return ResponseHelper.success(pgIncreItem);
    }


    @PostMapping("/getCategoryList")
    @ApiOperation(value = "获得服务大类")
    public ResponseHelper<List<GoodsCategory>> getCategoryList(
            @ApiParam(value = "品牌",required = false) @RequestParam(required = false) String brand
    ) throws Exception {
        List<GoodsCategory> sysDictCompanyList = increConfigLogic.getCategoryList(brand);
        return ResponseHelper.success(sysDictCompanyList);
    }


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

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

    @PostMapping("/buy")
    @ApiOperation(value = "提交延保服务,返回订单号")
    public ResponseHelper<String> buy(
            @RequestBody PgIncreOrderVO pgIncreOrderVO,
            HttpServletRequest request
    ) throws Exception {
        String ip = IpUtil.getIpAddr(request);
        String workerOrderDetail = increConfigLogic.buy(pgIncreOrderVO,ip,request);
        return ResponseHelper.success(workerOrderDetail);
    }

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

    ) throws Exception {
        PgIncreOrder pgIncreOrder = increConfigLogic.getOrder(id);
        return ResponseHelper.success(pgIncreOrder);
    }

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

    ) throws Exception {
        increConfigLogic.cancel(id);
        return ResponseHelper.success();
    }


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

    }


    @PostMapping("/pay")
    @ApiOperation(value = "代客支付")
    public ResponseHelper<PayDetail> pay(
            @ApiParam(required = true, value = "订单号") @RequestParam(required = true) String id,
            @ApiParam(required = false, value = "是否小程序支付") @RequestParam(required = false) Boolean miniPay,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String openId,
            HttpServletRequest request
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + id);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            String ip = IpUtil.getIpAddr(request);
            PayDetail payStatus = increConfigLogic.pay(id,ip,miniPay,openId);
            return ResponseHelper.success(payStatus);
        } finally {
            obtain.unlock();
        }

    }



    @PostMapping("/listIncre")
    @ApiOperation(value = "延保订单")
    public ResponseHelper<IPage<PgIncreOrder>> listIncre(
            @ApiParam(value = "支付状态CANCEL 取消 WAIT 待支付 PAID 已支付 EXPIRE 过期 ",required = false) @RequestParam(required = false) List<String>  payStatus,
            @ApiParam(value = "条码",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<PgIncreOrder> increVOIPage = increConfigLogic.listIncre(code,payStatus,pageNum,pageSize);
        return ResponseHelper.success(increVOIPage);
    }

    @PostMapping("/listIncreCount")
    @ApiOperation(value = "延保订单数量")
    public ResponseHelper<IncreCountVO> listIncreCount(

    ) throws RemoteServiceException {
        IncreCountVO increVOIPage = increConfigLogic.listIncreCount();
        return ResponseHelper.success(increVOIPage);
    }


    @PostMapping("/detailIncre")
    @ApiOperation("延保订单详情")
    public ResponseHelper<PgIncreOrder> detailIncre(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        PgIncreOrder pgIncreVO = increConfigLogic.detailIncre(id);
        return ResponseHelper.success(pgIncreVO);
    }

    @PostMapping("/listIncreCode")
    @ApiOperation(value = "延保查询")
    public ResponseHelper<IPage<PgIncreOrder>> listIncreCode(
            @ApiParam(value = "支付状态CANCEL 取消 WAIT 待支付 PAID 已支付 EXPIRE 过期 ",required = false) @RequestParam(required = false) List<String>  payStatus,
            @ApiParam(value = "条码",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<PgIncreOrder> increVOIPage = increConfigLogic.listIncreCode(code,payStatus,pageNum,pageSize);
        return ResponseHelper.success(increVOIPage);
    }


}
