package com.sw.mes.dingTalk.controller;

import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.yb.production.dao.AgainTaskDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 品质推送
 *
 * @Author：wanjia
 * @Date：2024/3/20 16:42
 */
@Service
@Slf4j
public class QualitySeed {

    @Autowired
    private AgainTaskDao againTaskDao;


    /**
     * 氧数据推送
     *
     * @return
     */
    public Result<String> sendOxygenInfo() {

        Calendar calendar = Calendar.getInstance(); // 获取当前日期
        calendar.add(Calendar.DATE, -1); // 将日期减去一天
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = format.format(calendar.getTime());
        String startTime = yesterday + " 08:00:00";

        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        String endTime = formattedDate + " 08:00:00";

        List<Map<String, Object>> result = againTaskDao.oxygenMonitor(startTime, endTime);
        if (CollectionUtils.isEmpty(result)) {
            return ResultGenerator.success();
        }
        StringBuilder sb = new StringBuilder();
        for (Map<String, Object> map : result) {
            //灌料次数
            int num = (int) map.get("again_num");
            BigDecimal oxygenHead = (BigDecimal) map.get("oxygen_content_head");
            String series = (String) map.get("series");
            String name = (String) map.get("name");
            sb.append("片区：" + name + " " + series);
            sb.append("\n");
            sb.append("头氧均值：" + oxygenHead.setScale(2, RoundingMode.HALF_UP));
            sb.append("\n");
            sb.append("复投次数：" + num);
            sb.append("\n");
            sb.append("\n");
        }
        log.info(sb.toString());
        return ResultGenerator.success(sb.toString());
    }

}
