package com.kingdee.eas.custom.sendmessage.osf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.logs.IMessageLogs;
import com.kingdee.eas.custom.logs.MessageLogsCollection;
import com.kingdee.eas.custom.logs.MessageLogsFactory;
import com.kingdee.eas.custom.logs.MessageLogsInfo;
import com.kingdee.eas.custom.logs.app.SendStatusEnum;
import com.kingdee.eas.framework.CoreBaseInfo;

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

/**
 * description: CallBackToOSFService <br>
 * date: 2025/3/3 18:27 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class CallBackToOSFService implements IHRMsfService {
    @Override
    public Object process(Context context, Map<String, Object> map) throws EASBizException, BOSException {
        String data = (String) map.get("data");
        JSONObject jsonObject = JSON.parseObject(data);
        List<Object> list = null;
        String type = (String) jsonObject.get("type");
        String requestId = BOSUuid.create("1C714FA2").toString();
        if ("sms_report".equals(type)) {
            list = jsonObject.getJSONArray("sendSmsRecordDtoList");

        } else if ("sms_task_report".equals(type)) {
            list = jsonObject.getJSONArray("smsDeliverDtoList");

        } else if ("sms_deliver".equals(type)) {
            list = jsonObject.getJSONArray("sendSmsTaskDto");

        }
        IMessageLogs iMessageLogs = MessageLogsFactory.getLocalInstance(context);
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("sourceBillId");
        sic.add("sourceFunction");
        sic.add("id");
        sic.add("status");
        sic.add("callBackResult");

        for(Object entry : list){
            JSONObject object = (JSONObject) entry;
            int sendRecordId = object.getInteger("sendRecordId");
            int sendStatus = object.getInteger("sendStatus");
            String reportDate = object.getString("reportDate");
            String remark = object.getString("remark");
            FilterInfo filterInfo = new FilterInfo();
            filterInfo.getFilterItems().add(new FilterItemInfo("sourceBillId",sendRecordId, CompareType.EQUALS));
            EntityViewInfo view = EntityViewInfo.getInstance(filterInfo, sic, null);
            MessageLogsCollection mlc = iMessageLogs.getMessageLogsCollection(view);

            for(int i = 0; i < mlc.size(); i++) {
                MessageLogsInfo ml = mlc.get(i);
                ml.setString("callBackResult",data);
                ml.setStatus(SendStatusEnum.succeed);
                switch (sendStatus){
                    case 0:ml.setSourceFunction(sendStatus+"-待发送");break;
                    case 1:ml.setSourceFunction(sendStatus+"-已发送");break;
                    case 2:ml.setSourceFunction(sendStatus+"-发送中");break;
                    case -1:ml.setSourceFunction(sendStatus+"-发送失败");break;
                    default:ml.setSourceFunction(sendStatus+"其他");break;
                }
                iMessageLogs.updatePartial(ml, sic);
            }
        }
        Map<String,String> result = new HashMap<String,String>();
        result.put("status","success");
        result.put("data",data);
        return result;
    }
}
