package com.malk.zhiwei.controller;

import com.alibaba.fastjson.JSONObject;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilServlet;
import com.malk.zhiwei.service.ZwService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：
 * 作者：hanxue
 * 日期：2025/8/4 14:47
 */
@Slf4j
@RestController
@RequestMapping
public class ZwErpController {

    @Autowired
    private YDClient ydClient;
    @Autowired
    private ZwService zwService;


    @PostMapping("/deliver/save")
    public McR savedeliver(@RequestBody Map param){
        log.info("出库单数据保存:{}",param);
        McException.assertParamException_Null(param,"saleCode","noticeCode","code","prod","qty","isdelete");
        try {
            Map formData = new HashMap();
            formData.put("textField_mdwr7jnh",param.get("saleCode"));
            formData.put("textField_mdwpe4m7",param.get("noticeCode"));
            formData.put("textField_mdwr7jni",param.get("code"));
            formData.put("textField_mdqsatd4",param.get("prod"));
            formData.put("numberField_mdwr7jnf",param.get("qty"));
            formData.put("textField_mdwr7jng",param.get("isdelete"));
            Map serarchParam= new HashMap();
            serarchParam.put("textField_mdwr7jnh",param.get("saleCode"));
            serarchParam.put("textField_mdwpe4m7",param.get("noticeCode"));
            serarchParam.put("textField_mdwr7jni",param.get("code"));
            serarchParam.put("textField_mdqsatd4",param.get("prod"));
            ydClient.operateData(YDParam.builder().formUuid("FORM-54A845D217A64108BC2B288F6F0BE622BR5C")
                    .searchCondition(JSONObject.toJSONString(serarchParam))
                    .formDataJson(JSONObject.toJSONString(formData)).build(), YDConf.FORM_OPERATION.upsert);
            return McR.success();
        }catch (Exception e){
            e.printStackTrace();
            return McR.error("201","服务异常");
        }

    }

    @SneakyThrows
    @PostMapping("/customer/save")
    McR savecustomer(HttpServletRequest request){
        Map<String, ?> data = UtilServlet.getParamMap(request);
        log.info("客户信息保存, {}", data);
        McException.assertParamException_Null(data, "instanceId");
        return McR.success(zwService.savecustomer(UtilMap.getString(data,"instanceId")));
    }



}
