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.Employee;
import com.malk.diwei.mapper.ColorMapper;
import com.malk.diwei.service.ColorService;
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 ColorServiceImpl extends ServiceImpl<ColorMapper, Color> implements ColorService{
    @Autowired
    private ColorMapper colorMapper;

    @Autowired
    private YDClient ydClient;


    @Override
    public McR syncColor() {
        LambdaQueryWrapper<Color> colorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        colorLambdaQueryWrapper.isNotNull(Color::getColorCode)
                .ne(Color::getColorCode,"");
        List<Color> colorList = colorMapper.selectList(colorLambdaQueryWrapper);

        for (Color color : colorList) {
            Map formData = new HashMap();

            formData.put("textField_mb06w633",color.getColorName());//名称
            formData.put("textField_mb06w632",color.getColorCode());//颜色编号
            formData.put("numberField_mb06w62x",color.getColorID());//颜色id
            formData.put("selectField_mb06w634",color.getColorIsDisable() == 0 ? "正常" : "停用");//状态


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

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

        }
        return McR.success();
    }
}
