package com.gree.mall.manager.controller;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.gree.mall.manager.bean.Freight.FreightTemplateBean;
import com.gree.mall.manager.enums.ExchangeCodeTypeEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.ExpressLogic;
import com.gree.mall.manager.logic.common.WechatLogic;
import com.gree.mall.manager.plus.entity.GiftExchangeCode;
import com.gree.mall.manager.plus.service.GiftExchangeCodeService;
import com.gree.mall.manager.utils.RC4Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@RestController
@Api(value = "手动操作接口", tags ={"手动操作接口"} )
@RequestMapping(value = "/manuel", produces = "application/json; charset=utf-8")
public class ManuelController {
    @Autowired
    private ExpressLogic expressLogic;
    @Autowired
    private WechatLogic wechatLogic;
    @Autowired
    GiftExchangeCodeService giftExchangeCodeService;

    @PostMapping("/excode/init")
    public void initExcode(){
        int count = giftExchangeCodeService.count();
        if(count > 0){
            return;
        }
        List<GiftExchangeCode> list = new ArrayList<>();
        for(int i = 0 ;i < 3000;i++){
            String s = RC4Utils.encry_RC4_string(String.format("%04d", 1), UUID.randomUUID().toString());
            GiftExchangeCode giftExchangeCode = new GiftExchangeCode();
            giftExchangeCode.setId(s);
            giftExchangeCode.setStatus(0);
            giftExchangeCode.setCreateTime(new Date());
            if(i >= 2000) {
                giftExchangeCode.setAmount(BigDecimal.valueOf(9.9));
            }
            list.add(giftExchangeCode);
        }
        giftExchangeCodeService.saveBatch(list);
    }

    @PostMapping("/excode/init2")
    public void initExcode2(
            @ApiParam("生成的数量") @RequestParam Integer num
    ){
        int count = giftExchangeCodeService.count();
        if(count > 0){
            return;
        }
        Set<String> codes = new HashSet<>();
        List<GiftExchangeCode> list = new ArrayList<>();
        for(int i = 0 ;i < num;i++){
            String s = RC4Utils.encry_RC4_string(String.format("%04d", 1), UUID.randomUUID().toString());
            codes.add(s);
        }
        for(String s : codes) {
            GiftExchangeCode giftExchangeCode = new GiftExchangeCode();
            giftExchangeCode.setId(s);
            giftExchangeCode.setStatus(0);
            giftExchangeCode.setType(ExchangeCodeTypeEnum.SUB.toString());
            giftExchangeCode.setCreateTime(new Date());
            giftExchangeCode.setAmount(new BigDecimal(9999999));
            giftExchangeCode.setRemark("金额抵扣");
            list.add(giftExchangeCode);
        }
        giftExchangeCodeService.saveBatch(list);
    }

//
//    @PostMapping("/express/subscribe")
//    @ApiOperation(value = "快递订阅")
//    public ResponseHelper subscribe(
//            @ApiParam(value = "快递单号",required = true) @RequestParam(required = true)String logisticsNo,
//            @ApiParam(value = "快递公司代码",required = true) @RequestParam(required = true)String company
//    ){
//        expressLogic.subscribeExpressPrd(logisticsNo,company);
//        return ResponseHelper.success();
//    }
//
//
//    @PostMapping("/pay/refund")
//    @ApiOperation(value = "手动退款")
//    public ResponseHelper refund(
//            @ApiParam(value = "订单号",required = true) @RequestParam(required = true)String orderId,
//            @ApiParam(value = "退款单号",required = true) @RequestParam(required = true)String refundNo,
//            @ApiParam(value = "收款总金额",required = true) @RequestParam(required = true)Double totalFee,
//            @ApiParam(value = "退款金额",required = true) @RequestParam(required = true)Double refundFee
//    ) throws RemoteServiceException, WxPayException {
//        wechatLogic.refund(orderId,refundNo,totalFee,refundFee);
//        return ResponseHelper.success();
//    }


}
