package com.malk.diwei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.malk.diwei.entity.ProcStep;
import com.malk.diwei.mapper.ProcStepMapper;
import com.malk.diwei.service.ProcStepService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WZY
 * @since 2025-03-07
 */
@Service
public class ProcStepServiceImpl extends ServiceImpl<ProcStepMapper, ProcStep> implements ProcStepService {
    private static final Map PROC_STEP_TYPE = new HashMap();

    static {
        PROC_STEP_TYPE.put(1, "厂内工序");
        PROC_STEP_TYPE.put(3, "裁剪工序");
        PROC_STEP_TYPE.put(4, "工艺委外工序");
    }

    @Autowired
    private ProcStepMapper procStepMapper;

    @Autowired
    private YDClient ydClient;

    @Override
    public McR syncProcStep() {
        LambdaQueryWrapper<ProcStep>  procStepLambdaQueryWrapper = new LambdaQueryWrapper();

        List<ProcStep> list = procStepMapper.selectList(procStepLambdaQueryWrapper);

        for (ProcStep procStep : list) {
            Map formData = new HashMap();
            formData.put("textField_m7y4xld9", procStep.getProcStepID());//工序id
            formData.put("textField_m7y4xlce", procStep.getProcStepCode());//工序编码
            formData.put("textField_m7y4xlcj",procStep.getProcStepName());//工序名称
            formData.put("textareaField_m7y4xlcx",procStep.getProcStepDescription());//备注
            formData.put("numberField_m7y4xlcv",procStep.getProcStepPrice());//工序价格
            formData.put("numberField_m7y4xlcw",procStep.getProcStepHours());//工序工时
            formData.put("textField_m7y4xlck",PROC_STEP_TYPE.get(procStep.getProcStepType()));//工序类型
            formData.put("radioField_m7y4xlcu",procStep.getProcStepIsReferStock() == 1 ? "是" : "否");//是否参照库存
            formData.put("radioField_m7y4xlcp",procStep.getProcStepIsReferOutSizeN() == 1 ? "是" : "否");//是否参照外尺寸


            //精准匹配
            List<Map> searchCondition = new ArrayList<>();
            searchCondition.add(UtilMap.map("key, value, type, operator, componentName","textField_m7y4xld9",procStep.getProcStepID(),"TEXT","eq","TextField"));
            ydClient.operateData(YDParam.builder()
                    .formUuid("FORM-E3E8CA72C68C43BBAF53A22B199AC9302SQK")
                    .searchCondition(JSONObject.toJSONString(searchCondition))
                    .formDataJson(JSONObject.toJSONString(formData))
                    .build(), YDConf.FORM_OPERATION.upsert);
        }

        return McR.success();
    }
}
