package com.xinelu.quartz.controller;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.xinelu.manage.domain.shortmessagesendrecord.ShortMessageSendRecord;
import com.xinelu.manage.domain.textmessage.SmsReport;
import com.xinelu.manage.mapper.shortmessagesendrecord.ShortMessageSendRecordMapper;
import com.xinelu.manage.mapper.signpatientmanageroute.SignPatientManageRouteMapper;
import com.xinelu.manage.vo.signpatientmanageroute.SignPatientManageRouteVO;
import com.xinelu.quartz.service.SendTextMessageService;
import com.xinelu.quartz.task.SendTextMessageTask;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description 短信相关定时任务
 * @Author zh
 * @Date 2024-07-22
 */
@RestController
@RequestMapping("/monitor/sendTextMessage")
public class SendTextMessageController {

    @Resource
    private SendTextMessageTask textMessageTask;
    @Resource
    private SendTextMessageService sendTextMessageService;
    @Resource
    private ShortMessageSendRecordMapper shortMessageSendRecordMapper;
    @Resource
    private SendTextMessageTask sendTextMessageTask;
    @Resource
    private SignPatientManageRouteMapper signPatientManageRouteMapper;

    /**
     * 手动执行短信发送定时任务
     */
    @GetMapping("/textMessageSend")
    public void textMessageSend() throws ClientException {
        textMessageTask.automaticSendTextMessageTask();
    }

    @PostMapping("/textMessageSendBask")
    public JSONObject textMessageSendBask(SmsReport data) {
        JSONObject retObj = new JSONObject();
        retObj.fluentPut("code", 0).fluentPut("msg", "成功");
        if (Objects.isNull(data) || StringUtils.isBlank(data.getPhone_number())) {
            return retObj;
        }
        List<Long> longs = new ArrayList<>();
        longs.add(Long.valueOf(data.getOut_id()));
        List<SignPatientManageRouteVO> signPatientManageRoutes = signPatientManageRouteMapper.selectRouteByBatchSendTaskRecordId(longs);
        if (CollectionUtils.isEmpty(signPatientManageRoutes)){
            return retObj;
        }
        SignPatientManageRouteVO signPatientManageRoute = signPatientManageRoutes.stream().filter(Objects::nonNull).filter(item -> data.getPhone_number().equals(item.getPhone())).findFirst().orElse(new SignPatientManageRouteVO());
        if (Objects.isNull(signPatientManageRoute.getId())){
            return retObj;
        }
        List<ShortMessageSendRecord> shortMessageSendRecords = new ArrayList<>();
        JSONObject jsonObject = sendTextMessageService.taskTextMessageSendBack(data, shortMessageSendRecords, signPatientManageRoute);
        if (CollectionUtils.isNotEmpty(shortMessageSendRecords)) {
            shortMessageSendRecordMapper.insertShortMessageSendRecords(shortMessageSendRecords);
        }
        return jsonObject;
    }

    @PostMapping("/aa")
    public void aa() {
        sendTextMessageTask.smsReportBack();
    }
}
