package com.yifu.cloud.plus.v1.msg.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.msg.constant.MesConstants;
import com.yifu.cloud.plus.v1.msg.entity.MsgInfo;
import com.yifu.cloud.plus.v1.msg.mapper.MsgMapper;
import com.yifu.cloud.plus.v1.msg.service.MsgService;
import com.yifu.cloud.plus.v1.msg.vo.EkpAlertVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author fxj
 * @Date 2023/5/30
 * @Description
 * @Version 1.0
 */
@RequiredArgsConstructor
@Service
public class MsgImpl extends ServiceImpl<MsgMapper, MsgInfo> implements MsgService {
	@Override
	public R<Boolean> createMsg(List<EkpAlertVo> list) {
		if (Common.isNotNull(list)){
			EkpAlertVo vo = list.get(CommonConstants.ZERO_INT);
			if (Common.isEmpty(vo.getItem())
					|| Common.isEmpty(vo.getType())
					|| Common.isEmpty(vo.getOrderId())
					|| Common.isEmpty(vo.getAlertUser())){
				return R.failed(CommonConstants.PARAM_INFO_ERROR);
			}
			MsgInfo save =  null;
			// 项目订单处理
			if (CommonConstants.ZERO_STRING.equals(vo.getItem())){
				// 接单提醒
				if (CommonConstants.ZERO_STRING.equals(vo.getType())){
					save = baseMapper.getMsgByOrderId(vo.getOrderId());
					if (Common.isNotNull(save)){
						save.setCreateTime(DateTime.now());
						save.setUrl(MesConstants.ORDER_URL_PATH+vo.getOrderId());
						baseMapper.insert(save);
					}
				}
				// 变更提醒
				if (CommonConstants.TWO_STRING.equals(vo.getType())){
					save = baseMapper.getMsgByOrderId(vo.getOrderId());
					if (Common.isNotNull(save)){
						save.setCreateTime(DateTime.now());
						save.setUrl(MesConstants.ORDER_URL_PATH+save.getOrderId());
						save.setAlertType(CommonConstants.TWO_STRING);
						baseMapper.insert(save);
					}
				}
			}
		}else {
			return R.failed(CommonConstants.PARAM_INFO_ERROR);
		}
		return null;
	}
}
