package com.uas.platform.b2b.task;

import com.uas.platform.b2b.dao.CommunalLogDao;
import com.uas.platform.b2b.model.CommunalLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 每天定时更新发货提醒异常的数据
 *
 * Created by hejq on 2018-06-21.
 */
@Component
public class SendNoticeTask {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private CommunalLogDao communalLogDao;

    /**
     * 时间格式
     */
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    /**
     * 默认UU号
     */
    final Long DEFAULT_ENUU = 0L;

    /**
     * 每天定时更新发货提醒状态
     * <pre>
     *     早上6点和晚上8点之间每两小时更新一次
     * </pre>
     */
    @Scheduled(cron = "0 0 6,8,10,12,14,16,18,20 * * ? ")
    public void updateSendNoticeStatus() {
        String sql = "update purc$notice set pn_status = 200 where pn_endqty < pn_qty and pn_status = 201 and coalesce(pn_end, 0) <> 1";
        jdbcTemplate.update(sql);
        communalLogDao.save(new CommunalLog("更新发货提醒状态", "定时任务自动更新异常状态数据", "时间： " + sdf.format(new Date()), DEFAULT_ENUU));
    }

    /**
     * 每天6点和12点定时更新下发货提醒备料状态
     */
    @Scheduled(cron = "0 0 6,12 * * ? ")
    public void updateWaitingStatus() {
        // 更新备料状态
        String sql = "update purc$notice,products,purc$orderitems set is_waiting = PURC_NOTICE_WAIT_TO_SEND(pn_delivery, coalesce(pr_ltinstock, 0 )) " +
                "where pr_id = pd_prid and pn_pdid = pd_id and is_waiting = 1 and coalesce(pn_end, 0) <> 1 and pn_status = 200";
        jdbcTemplate.update(sql);
        communalLogDao.save(new CommunalLog("更新发货提醒备料状态", "定时任务自动更新备料数据", "时间： " + sdf.format(new Date()), DEFAULT_ENUU));
    }
}
