package com.sw.mes.yb.report.controller;

import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.dingTalk.service.DingTalkService;
import com.sw.mes.yb.report.dto.EquipmentMonitorDto;
import com.sw.mes.yb.report.service.BrokenBractBaseService;
import com.sw.mes.yb.report.service.EquipmentMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @Author 李儒
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@RestController
@RequestMapping("/equipmentMonitor")
@Api(description = "设备报表接口")
public class EquipmentMonitorController {
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private EquipmentMonitorService equipmentMonitorService;

    @Autowired
    private DingTalkService dingTalkService;

    @Autowired
    private BrokenBractBaseService brokenBractBaseService;

    @ApiOperation(value = "设备工艺监控报表")
    @GetMapping("/monitor")
    public Result monitor() {
        DecimalFormat df = new DecimalFormat("######0.00");

        //总炉台数
        Long totalNum = equipmentService.getTotalEquipmentNum("单晶炉");
        //实际开炉
        Long startUpNum = equipmentService.getTotalEquipmentStartUpNum("单晶炉");
        //差异
        Long difference = totalNum - startUpNum;
        //开机率
        String startupRate = totalNum == 0.00 ? "0.00" : (df.format(startUpNum / totalNum.doubleValue() * 100));
        //统计时间
        Date currntDate = new Date();
        List<Map<String, Object>> rptData = equipmentMonitorService.equipmentMonitor();

        EquipmentMonitorDto dto = new EquipmentMonitorDto();
        dto.setCurrntDate(currntDate);
        dto.setStartUpNum(startUpNum);
        dto.setDifference(difference);
        dto.setStartupRate(startupRate);
        dto.setTotalNum(totalNum);
        dto.setRptData(rptData);
        return ResultGenerator.success(dto);
    }

    @ApiOperation(value = "设备工艺监控看板")
    @GetMapping("/monitorKb")
    public Result monitorKb() {
        DecimalFormat df = new DecimalFormat("######0.00");

        //总炉台数
        Long totalNum = equipmentService.getTotalEquipmentNum("单晶炉");
        //实际开炉
        Long startUpNum = equipmentService.getTotalEquipmentStartUpNum("单晶炉");
        //差异
        Long difference = totalNum - startUpNum;
        //开机率
        String startupRate = totalNum == 0.00 ? "0.00" : (df.format(startUpNum / totalNum.doubleValue() * 100));
        //统计时间
        Date currntDate = new Date();
        List<Map<String, Object>> rptData = equipmentMonitorService.kbEquipmentMonitor();
        EquipmentMonitorDto dto = new EquipmentMonitorDto();
        dto.setCurrntDate(currntDate);
        dto.setStartUpNum(startUpNum);
        dto.setDifference(difference);
        dto.setStartupRate(startupRate);
        dto.setTotalNum(totalNum);
        dto.setRptData(rptData);
        return ResultGenerator.success(dto);
    }

}
