package com.bonus.sgzb.material.service.impl;

import com.bonus.sgzb.common.core.enums.TaskStatusEnum;
import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.material.domain.AgreementInfo;
import com.bonus.sgzb.material.domain.BmNoticeInfo;
import com.bonus.sgzb.material.domain.TmTask;
import com.bonus.sgzb.material.domain.ToDoBean;
import com.bonus.sgzb.material.exception.ExceptionDict;
import com.bonus.sgzb.material.mapper.AgreementInfoMapper;
import com.bonus.sgzb.material.mapper.PurchaseCheckDetailsMapper;
import com.bonus.sgzb.material.mapper.PurchaseCheckInfoMapper;
import com.bonus.sgzb.material.mapper.ToDoMapper;
import com.bonus.sgzb.material.service.AgreementInfoService;
import com.bonus.sgzb.material.service.ToDoService;
import com.bonus.sgzb.material.vo.GlobalContants;
import com.bonus.sgzb.material.vo.NoticeInfoVO;
import com.bonus.sgzb.system.api.RemoteUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import com.bonus.sgzb.common.core.exception.ServiceException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 阮世耀
 * @Create 2023/12/13 15:45
 * @Version 1.0
 */
@Service
@Slf4j
public class ToDoServiceImpl implements ToDoService {

    @Resource
    private ToDoMapper toDoMapper;

    @Resource
    private PurchaseCheckInfoMapper purchaseCheckInfoMapper;

    @Resource
    private RemoteUserService remoteUserService;

    @Override
    public List<ToDoBean> getToDoList(ToDoBean bean) {
        return toDoMapper.getToDoList(bean);
    }

    @Override
    public AjaxResult getTaskType(ToDoBean bean) {
        List<ToDoBean> list =new ArrayList<>();
        try {
            list = toDoMapper.getTaskType(bean);
        } catch (Exception e) {
            log.error("代办事件-查询失败", e);
        }
        return AjaxResult.success(list);
    }

    /**
     * 发送催办短信
     * @param noticeInfoVO
     * @return
     */
    @Override
    public Boolean urgentProcessing(NoticeInfoVO noticeInfoVO){
        log.info("ToDoServiceImpl  urgentProcessing begin {} ",noticeInfoVO.getTaskId());
        Long taskId = noticeInfoVO.getTaskId();
        //判断任务是否为空
        if (taskId == null) {
            throw new ServiceException(ExceptionDict.TASK_ID_IS_EMPTY_MSG,ExceptionDict.TASK_ID_IS_EMPTY);
        }
        String message = noticeInfoVO.getMessage();
        List<BmNoticeInfo> bmNoticeInfoList = noticeInfoVO.getBmNoticeInfoList();
        //判断短信通知为是否为空
        if (bmNoticeInfoList.size() <= 0) {
            throw new ServiceException(ExceptionDict.BINDING_USER_IS_EMPTY_MSG,ExceptionDict.BINDING_USER_IS_EMPTY);
        }
        Boolean send = true;
        for (BmNoticeInfo bmNoticeInfo : bmNoticeInfoList) {
            bmNoticeInfo.setContent(message);
            bmNoticeInfo.setTaskId(taskId);
            bmNoticeInfo.setModelName(bmNoticeInfo.getModelName());
            bmNoticeInfo.setCreateTime(new Date());
            String phone = bmNoticeInfo.getPhone();
            try {
                // 短信通知
                send = remoteUserService.send(phone, message);
                purchaseCheckInfoMapper.insertBmNoticeInfo(bmNoticeInfo);
            }catch (Exception e){
                log.error("发送手机号：{} 失败！",phone);
            }
        }
        log.info("ToDoServiceImpl  urgentProcessing end {} ",noticeInfoVO.getTaskId());
        return send;
    }


}
