package com.lightinit.hsdataplatform.impl.task;

import com.lightinit.hsdataplatform.dictionary.DicNotificationState;
import com.lightinit.hsdataplatform.entity.Notification;
import com.lightinit.hsdataplatform.mapper.NotificationMapper;
import com.lightinit.hsdataplatform.mapper.OrderMapper;
import com.lightinit.hsdataplatform.service.task.IOrdersAndContractExpireTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: sunxiang
 * @Date: 2018/10/12 11:14
 * @Description:
 **/
@Service
public class OrdersAndContractExpireTaskServiceImpl implements IOrdersAndContractExpireTaskService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private NotificationMapper notificationMapper;
    @Override
    @Transactional
    public void getExpiringOrder(int periodDay) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+periodDay);
        Date expireTime=calendar.getTime();
        List<Map<String, Object>> expiringOrderList = orderMapper.getExpiringOrder(expireTime);
        expiringOrderList.stream().forEach(e ->{
            Notification notification = new Notification();
            notification.setCreatedAt(new Date());
            notification.setPublishAt(new Date());
            notification.setNotiState(DicNotificationState.sended);
            notification.setNotiTitle("订单到期提醒");
            notification.setNotiContent("订单【"+e.get("order_number")+"】还要3天即将到期");
            notification.setUserId((long)e.get("user_id"));
            int i=notificationMapper.insert(notification);
            if(i<0){
                throw new RuntimeException("插入失败");
            }
        });
    }


}
