package com.malk.diwei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.malk.diwei.entity.Employee;
import com.malk.diwei.mapper.EmployeeMapper;
import com.malk.diwei.service.EmployeeService;
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-24
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private YDClient ydClient;

    @Override
    public McR syncEmplyee() {
        List<Employee> employeeList = employeeMapper.selectList(null);
        for (Employee employee : employeeList) {
            Map formData = new HashMap();

            formData.put("textField_m8msenxy",employee.getEmpName());//名称
            formData.put("textField_m8msenxz",employee.getEmpCode());//员工编号
            formData.put("numberField_m8mseny0",employee.getEmpID());//员工id
            formData.put("radioField_m8msenya",employee.getEmpSex().equals("M") ? "男" : "女");//员工性别
            formData.put("numberField_m8msenyl",employee.getDeptID());//部门id
            formData.put("selectField_m8msenyb",employee.getEmpIsSalesman() == 1 ? "是" : "否");//业务员
            formData.put("selectField_maz5ds8l",employee.getEmpIsDesigner() == 1 ? "是" : "否");//设计师
            formData.put("numberField_m8mu7y3d",employee.getJobID());//职位id
            formData.put("selectField_m8msenyj",employee.getEmpIsQCMerchandiser() != null && employee.getEmpIsQCMerchandiser() == 1 ? "是" : "否");//QC跟单
            formData.put("selectField_m8msenyk",employee.getEmpWorkStatus() == 1 ? "正常" : "停用");//状态


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

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


        }
        return McR.success();
    }
}
