package com.ym.mec.web.controller.education;


import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dto.PushInfoDto;
import com.ym.mec.biz.dal.entity.FinancialExpenditure;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Api(tags = "发送PUSH、IM（教务端）")
@RequestMapping("eduSendNotice")
@RestController
public class EduSendNoticeController extends BaseController {

    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private SysConfigDao sysConfigDao;

    @ApiOperation(value = "发送PUSH")
    @PostMapping(value = "/push")
    public HttpResponseResult push(@RequestBody PushInfoDto pushInfo) {
        Map<Integer, String> userMap = new HashMap<>();
        for (Integer userId : pushInfo.getUserIds()) {
            userMap.put(userId, userId.toString());
        }
//        String baseApiUrl = sysConfigDao.findConfigValue(SysConfigService.EDU_TEACHER_BASE_URL);
//        String workOrderUrl = "8?" + baseApiUrl + pushInfo.getWorkOrderUrl();
        String workOrderUrl = pushInfo.getWorkOrderUrl();
        sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, pushInfo.getMessageType(), userMap, null, 0, workOrderUrl, "SYSTEM");
        return succeed();
    }

    @ApiOperation(value = "发送IM")
    @PostMapping(value = "/im")
    public HttpResponseResult im(@RequestBody PushInfoDto pushInfo) {
        String baseApiUrl = sysConfigDao.findConfigValue(SysConfigService.EDU_TEACHER_BASE_URL);
        String workOrderUrl = baseApiUrl + pushInfo.getWorkOrderUrl();
        String msg = pushInfo.getImContent() + " " + workOrderUrl;

        for (Integer userId : pushInfo.getUserIds()) {
            sysMessageService.sendNoAuthPrivateMessage("1", userId.toString(), msg);
        }
        return succeed();
    }
}
