package com.hys.app.controller.erp.use;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.database.BaseQueryParam;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.kuaidiniao.KdConstants;
import com.hys.app.service.kuaidiniao.KuaiDiNiaoService;
import com.hys.app.service.kuaidiniao.entity.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * OAuth2.0 授权 API
 *
 * @author 张崧
 * @since 2024-02-20
 */
@Api(tags = "快递鸟")
@RestController
@RequestMapping("/admin/erp/kdn")
@Validated
@Slf4j
@AllArgsConstructor
public class KdnController {
    private KuaiDiNiaoService kuaiDiNiaoService;

    @PostMapping("/returnBack/instant")
    public resReturnBackBase kdnReturnBackInstant(ReturnBackInstant returnBackBase) {
        return kuaiDiNiaoService.kdnReturnBack(returnBackBase);
    }

    @ApiOperation(value = "快递鸟发货获取单号")
    @PostMapping("/kdShip/{orderId}")
    @Log(client = LogClient.admin, detail = "订单[${orderId}]发货")
    public String kdShip(@PathVariable(value = "orderId") Long orderId
            , @RequestBody @Valid PreOrderModelDTO preOrderModel
            , @RequestBody AddressDTO addressDTO) {
        return kuaiDiNiaoService.kdShip(preOrderModel, orderId, addressDTO);
    }


    @ApiOperation(value = "快递鸟创建门店")
    @PostMapping("/createStore")
    @Log(client = LogClient.admin, detail = "快递鸟创建店铺")
    public void createStore(@RequestBody @Valid CreateStoreModel createStoreModel) {
        kuaiDiNiaoService.createStore(createStoreModel, KdConstants.createStore);
    }

    @ApiOperation(value = "创建失败修改参数再次创建快递鸟门店")
    @PostMapping("/updateStore")
    @Log(client = LogClient.admin, detail = "快递鸟创建店铺")
    public void updateStore(@RequestBody @Valid CreateStoreModel createStoreModel) {
        kuaiDiNiaoService.createStore(createStoreModel, KdConstants.updateStore);
    }

    @ApiOperation(value = "快递鸟门店列表")
    @GetMapping("/list")
    @Log(client = LogClient.admin, detail = "获取快递鸟店铺列表")
    public Page<CreateStoreModel> getStoreList(BaseQueryParam baseQueryParam) {
        return kuaiDiNiaoService.getStoreList(baseQueryParam);
    }

    @ApiOperation(value = "selectAudit")
    @GetMapping("/selectAudit")
    @Log(client = LogClient.admin, detail = "获取快递鸟店铺列表")
    public void selectAudit(CreateStoreModel createStoreModel) {
        kuaiDiNiaoService.selectAudit(createStoreModel);
    }


    @ApiOperation(value = "queryLogistics")
    @PostMapping("/queryLogistics")
    @Log(client = LogClient.admin, detail = "订阅物流查询")
    public void queryLogistics(@RequestBody LogisyicsTopic logisyicsTopic ) {
        kuaiDiNiaoService.queryLogistics(logisyicsTopic);
    }

    @PostMapping("/returnBack/logistics")
    public ReturnBackLogisticsResponse kdnReturnBackLogistics( ReturnBackInstant returnBackInstant ) {
        ReturnBackLogistics returnBackBase = JSON.parseObject(returnBackInstant.getRequestData(), ReturnBackLogistics.class);
        ReturnBackLogisticsResponse response = new ReturnBackLogisticsResponse();
        if (returnBackBase.getCount() >0){
            List<Map> maps = JSON.parseArray(returnBackBase.getData(), Map.class);
            for (Map map : maps) {
                Boolean b = kuaiDiNiaoService.kdnReturnBackLogistics(map);
                if (!b){
                    response.setSuccess(false);
                }
            }
        }
        return response;
    }
}
