package com.clx.performance.controller.pc.transport;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.clx.performance.component.ThirdComponent;
import com.clx.performance.dao.OrderChildDao;
import com.clx.performance.dao.OrderGoodsDao;
import com.clx.performance.dao.settle.SettlementDriverDetailDao;
import com.clx.performance.dao.settle.SettlementOwnerDetailDao;
import com.clx.performance.dto.OrderChildSyncDTO;
import com.clx.performance.dto.PayStatusNotifyDTO;
import com.clx.performance.enums.PerformanceResultEnum;
import com.clx.performance.feign.TransportFeignService;
import com.clx.performance.model.OrderChild;
import com.clx.performance.model.OrderGoods;
import com.clx.performance.model.settle.SettlementDriverDetail;
import com.clx.performance.model.settle.SettlementOwnerDetail;
import com.clx.performance.param.feign.transport.ThirdOrderChildBrokerParam;
import com.clx.performance.service.TransportSyncService;
import com.msl.common.dto.HttpDTO;
import com.msl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping(value="/pc/transport/")
@Validated
@Api(tags = "PC-网络货运相关")
@AllArgsConstructor
public class TransportSyncController {


    @Autowired
    private TransportSyncService transportSyncService;

    @Autowired
    private OrderChildDao orderChildDao;

    @Autowired
    private SettlementOwnerDetailDao settlementOwnerDetailDao;

    @Autowired
    private OrderGoodsDao orderGoodsDao;

    @Autowired
    private TransportFeignService transportFeignService;

    @Autowired
    private SettlementDriverDetailDao settlementDriverDetailDao;

    @ApiOperation(value = "payStatusNotify", notes = "<br>By:胡宇帆")
    @PostMapping("/payStatusNotify")
    public Result<Object> payStatusNotify(@RequestBody String param) {
        HttpDTO bean = JSONUtil.toBean(param, HttpDTO.class);
        log.info("payStatusNotify 原始报文：{}", bean);
        String decrypt = ThirdComponent.decrypt(bean.getData());
        log.info("payStatusNotify 解密后：{}", decrypt);
        return Result.ok(ThirdComponent.encrypt(""));
    }


    @ApiOperation(value = "syncTest", notes = "<br>By:胡宇帆")
    @GetMapping("/syncTest")
    public Result<OrderChildSyncDTO> syncTest(@RequestParam String childNo) {
        OrderChild orderChild = orderChildDao.getByChildNo(childNo).get();
        OrderGoods orderGoods = orderGoodsDao.getByOrderGoodsNo(orderChild.getOrderGoodsNo()).get();
        SettlementOwnerDetail settlementOwnerDetail = settlementOwnerDetailDao.getByChildNo(childNo).orElseThrow(PerformanceResultEnum.DATA_NOT_FIND);
        SettlementDriverDetail settlementDriverDetail = settlementDriverDetailDao.getByChildNo(childNo).orElseThrow(PerformanceResultEnum.DATA_NOT_FIND);

        ThirdOrderChildBrokerParam param = transportSyncService.generateOrderChildSync(orderChild, orderGoods, settlementOwnerDetail, settlementDriverDetail);
        log.info("=======================生成Param{}", JSONUtil.parse(param));
        HttpDTO httpDTOResult = transportFeignService.orderChildSync(param);
        String decrypt = ThirdComponent.decrypt(httpDTOResult.getData());
        OrderChildSyncDTO bean = JSONUtil.toBean(decrypt, OrderChildSyncDTO.class);
        return Result.ok(bean);
    }


}
