package com.malk.diwei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.malk.diwei.entity.Color;
import com.malk.diwei.entity.Size;
import com.malk.diwei.mapper.SizeMapper;
import com.malk.diwei.service.SizeService;
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;

@Service
public class SizeServiceImpl extends ServiceImpl<SizeMapper, Size> implements SizeService {
    @Autowired
    private SizeMapper sizeMapper;

    @Autowired
    private YDClient ydClient;

    @Override
    public McR syncSize() {
        List<Size> sizeList = sizeMapper.selectList(null);

        for (Size size : sizeList) {
            Map formData = new HashMap();

            formData.put("textField_mb08mev5",size.getSizeName());//名称
            formData.put("textField_mb08mev4",size.getSizeCode());//尺码编号
            formData.put("numberField_mb08mev3",size.getSizeID());//尺码id
            formData.put("selectField_mb08mev6",size.getSizeIsDisable() == 0 ? "正常" : "停用");//状态


            //精准匹配
            List<Map> jsonString = new ArrayList<>();
            jsonString.add(UtilMap.map("key, value, type, operator, componentName", "textField_mb08mev4",size.getSizeCode(),"TEXT","eq","TextField"));

            ydClient.operateData(YDParam.builder()
                    .formUuid("FORM-63F2679C18914FCB8D5FCF120FB331785WBN")
                    .searchCondition(JSONObject.toJSONString(jsonString))
                    .formDataJson(JSONObject.toJSONString(formData))
                    .build(), YDConf.FORM_OPERATION.upsert);

        }
        return McR.success();
    }
}
