package com.shkj.wms.controller.third;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.shkj.common.annotation.Log;
import com.shkj.common.annotation.RepeatSubmit;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.common.enums.BusinessType;
import com.shkj.wms.bo.BaseBarcodesInThirdRepairBo;
import com.shkj.wms.bo.BaseBarcodesInThirdSyncBo;
import com.shkj.wms.bo.BaseBarcodesOutThirdPointBo;
import com.shkj.wms.bo.BaseBarcodesOutThirdSyncBo;
import com.shkj.wms.service.IBaseBarcodesInService;
import com.shkj.wms.service.IBaseBarcodesOutService;
import com.shkj.wms.third.rcs.dto.RcsCreateTaskDto;
import com.shkj.wms.third.rcs.dto.RcsThirdResult;
import com.shkj.wms.third.ykl.ThirdYklResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 *  依柯力同步条码接口
 */
@Slf4j
@RequiredArgsConstructor(onConstructor_ =@Autowired)
@RestController
@RequestMapping("/wms/third/barcodes")
public class BarcodeThirdController extends BaseController {

    private final IBaseBarcodesInService iBaseBarcodesInService;

    private  final IBaseBarcodesOutService iBaseBarcodesOutService;

    @Value("${third.QADSuccessUrl}")
    private String QADSuccessUrl;

    /**
     * 设置头部信息
     *
     * @return {@code Map<String, String>}
     */
    public Map<String, String> getHeaderMap() {
        Map<String, String> headerMap = new HashMap<>(1);
        headerMap.put("authorization", "mrbase64 mrrest:YWRtaW4mYWRtaW4=");
        headerMap.put("SOAPAction", "application/soap+xml; charset=utf-8");
        return headerMap;
    }

    public Result parseThirdRcsResult(String res) {
        try {
            res = res.trim();
            RcsThirdResult thirdResult = JSONObject.parseObject(res, RcsThirdResult.class);
            if (thirdResult == null) {
                // 添加到异常中去
                return Result.err().msg(res);
            }
            if (!RcsThirdResult.isOk(thirdResult)) {
                return Result.err().msg(res);
            }
            return Result.ok();
        } catch (Exception e) {
            log.error("parseThirdRcsResult res = {} e = {}", res, e);
            return Result.err().msg(res);
        }
    }

    /**
     * 给QAD同步正常杠
     * @author Comair
     * @date 2023/11/20 16:14
     * @param dto
     * @return com.shkj.common.core.domain.Result
     */
    @PostMapping("/rcsSyncNum")
    public Result rcsSyncNum(Map<String,Object> dto) {
        String json = JSONObject.toJSONString(dto);
        try {
            log.error("zzzzzzzzzzzzzzzzzzzzzzzzzz------给QAD同步正常杠开始");
            log.error("zzzzzzzzzzzzzzzzzzzzzzzzzz------参数："+dto.toString());
            Map<String, String> headerMap = getHeaderMap();
            String res = HttpRequest.post(QADSuccessUrl)
                    .addHeaders(headerMap)
                    .body(json)
                    .execute().body();
            Result result = parseThirdRcsResult(res);
            log.error("zzzzzzzzzzzzzzzzzzzzzzzzzz------QAD同步正常杠反馈结果："+result.toString());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("createTask 方法出错了 = {}", e.getMessage());
            return Result.err().msg(e.getMessage());
        }
    }


    @Log(title = "同步依柯力系统入库条码信息",businessType = BusinessType.UPDATE)
    @PostMapping("/syncPutinBarcodes")
    @RepeatSubmit
    public ThirdYklResult thirdSyncPutinBarcodeIn(@Valid @RequestBody List<BaseBarcodesInThirdSyncBo> bos){
        return iBaseBarcodesInService.thirdSyncBaseBarcodesIn(bos);
    }


    @Log(title = "同步依柯力系统-返修条码信息",businessType = BusinessType.UPDATE)
    @PostMapping("/syncRepairBarcodes")
    @RepeatSubmit
    public ThirdYklResult thirdSyncOutBarcodes(@Valid @RequestBody List<BaseBarcodesInThirdRepairBo> bos){
        return iBaseBarcodesInService.thirdSyncRepairBarcodes(bos);
    }

    @Log(title = "同步依柯力系统-出库接口数据",businessType = BusinessType.UPDATE)
    @PostMapping("/syncOutBarcodes")
    @RepeatSubmit
    public ThirdYklResult syncOutBarcodes(@Valid @RequestBody List<BaseBarcodesOutThirdSyncBo> bo){
        return iBaseBarcodesOutService.thirdSyncBaseBarcodesOut(bo);
    }

    @Log(title = "同步依柯力系统-过点时间",businessType = BusinessType.UPDATE)
    @PostMapping("/syncOutBarcodesPointDate")
    @RepeatSubmit
    public ThirdYklResult thirdSyncOutBarcodesPoint(@Valid @RequestBody List<BaseBarcodesOutThirdPointBo> bo){
        return iBaseBarcodesOutService.thirdSyncBaseBarcodesOutPoint(bo);
    }


}
