package com.malk.shangfeng.service.impl;

import com.alibaba.fastjson.JSON;
import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.DDFormComponentDto;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Workflow;
import com.malk.shangfeng.repository.dao.Fitemss97Dao;
import com.malk.shangfeng.repository.entity.Fitemss97Po;
import com.malk.shangfeng.service.SFService;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SFImplService implements SFService {

    @Autowired
    private DDClient ddClient;

    @Autowired
    private DDConf ddConf;

    @Autowired
    private DDClient_Workflow workflow;

    @Autowired
    private Fitemss97Dao dao;

    /**
     * 同步u8项目档案
     */
    @Override
    public void syncU8Project() {

        for (Fitemss97Po po : dao.queryProject()) {
            Map data = (Map) JSON.parse(JSON.toJSONString(po));
            // 组件数据格式化
            Map ruleForm = UtilMap.map("citemcode, citemname", "项目编码, 项目名称");
            // 推送钉钉审批单
            List<Map> formValues = DDFormComponentDto.formatComponentValues(data, ruleForm, null);
            Map extInfo = UtilMap.map("dept_id", DDConf.TOP_DEPARTMENT);
            workflow.doProcessInstances(ddClient.getAccessToken(), ddConf.getOperator(), Fitemss97Po.PROCESS_CODE, formValues, extInfo);
            dao.updateProject(po.getCitemcode());
            log.info("同步项目信息, {}", po);
        }

    }
}
