package com.yc.ship.module.trade.controller.app.order;

import cn.hutool.json.JSONUtil;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.trade.controller.admin.order.vo.check.*;
import com.yc.ship.module.trade.enums.CheckCodeTypeEnum;
import com.yc.ship.module.trade.enums.CheckTypeEnum;
import com.yc.ship.module.trade.service.order.TradeCheckService;
import com.yc.ship.module.trade.service.pda.PdaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Base64;
import java.util.List;

import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.CHECK;


@Slf4j
@Validated
@RequestMapping("/trade")
@RestController
@Tag(name = "设备核销服务")
public class CheckController {

    @Resource
    private PdaService pdaService;

    @Resource
    private TradeCheckService tradeCheckService;


    @PostMapping("check/gate")
    @Operation(summary = "设备核销")
    @OperateLog(type = CHECK)
    @PermitAll
    public CommonResult<List<TradeCheckRespVO>> gateCheck(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        Long start = System.currentTimeMillis();
        if(checkReqVO.getType()==null) {
            checkReqVO.setType(CheckTypeEnum.Gate.getType());
        }
        CommonResult<List<TradeCheckRespVO>>  res = tradeCheckService.checkGate(checkReqVO);
        Long end = System.currentTimeMillis();
        log.error("闸机设备核销:"+ JSONUtil.toJsonStr(checkReqVO)+";res"+ JSONUtil.toJsonStr(res)+";执行时间："+(end-start));
        return res;
    }

    @PostMapping("check/gateUpdate")
    @Operation(summary = "设备核销-更新头像")
    @OperateLog(type = CHECK)
    @PermitAll
    public CommonResult<Boolean> gateUpdate(@Valid @RequestBody TradeCheckUrlVO checkReqVO) {
        return tradeCheckService.checkGateUpdate(checkReqVO);
    }




    @PostMapping("check/other")
    @Operation(summary = "不验证规则核销门票")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> checkOther(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        checkReqVO.setCodeType(CheckCodeTypeEnum.Other.getType());
        checkReqVO.setType(CheckTypeEnum.System.getType());
        return tradeCheckService.checkOther(checkReqVO);
    }

    @PostMapping("check-record/page")
    @Operation(summary = "核销记录列表")
    @OperateLog(type = CHECK)
    public CommonResult<?> page(@Valid @RequestBody TradeCheckPageReqVO pageReqVO) {
        return tradeCheckService.querySimpleCheckPage(pageReqVO, false);
    }

    @GetMapping("check-record/detail-list")
    @Operation(summary = "核销记录明细")
    @OperateLog(type = CHECK)
    public CommonResult<?> detailList(@Valid @RequestParam("id") Long id) {
        return tradeCheckService.queryCheckDetailList(id);
    }

    @PostMapping("check/query-order-info")
    @Operation(summary = "查询订单信息")
    @OperateLog(type = CHECK)
    public CommonResult<?> queryOrderInfo( @Valid @RequestBody CheckOrderPageVO reqVO) {
        return pdaService.queryOrderWithoutSiteNum(reqVO);
    }

    @PostMapping("check/query-voucher-info")
    @Operation(summary = "查询门票信息")
    @OperateLog(type = CHECK)
    public CommonResult<?> queryVoucherInfo( @Valid @RequestBody CheckOrderPageVO reqVO) {
        return pdaService.queryVoucherWithoutSiteNum(reqVO);
    }

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://tmsadmin.hbsxly.com/app-api/infra/file/uploadImg");

        String base64String = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgND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sXwMJSxb+MBqsBqsBqsBqsBqsBqsBqsBqsBqsBqsBqsBqsBqsBqv/Z"; // 示例数据
        byte[] data = Base64.getDecoder().decode(base64String);

        ByteArrayEntity reqEntity = new ByteArrayEntity(data);
        httpPost.setEntity(reqEntity);
        httpPost.setHeader("Content-Type", "application/octet-stream");

        HttpEntity responseEntity = httpClient.execute(httpPost).getEntity();
        String response = EntityUtils.toString(responseEntity);
        System.out.println(response);
        httpClient.close();
    }

}
