package com.jjt.biz.controller;

import com.jjt.biz.service.IApiAllService;
import com.jjt.biz.vo.CurrYieldVO;
import com.jjt.biz.vo.IndexData;
import com.jjt.biz.vo.TwinAllVO;
import com.jjt.biz.vo.YrTwinVO;
import com.jjt.common.constant.CacheConstants;
import com.jjt.common.core.controller.BaseController;
import com.jjt.common.core.domain.R;
import com.jjt.common.core.redis.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api(tags = "全厂孪生数据接口")
@RestController
@Slf4j
public class ApiAllController extends BaseController {
    @Resource
    private IApiAllService service;
    @Resource
    private RedisCache redisCache;

    @ApiOperation("全厂数字孪生")
    @GetMapping("/api/all/data")
    @CrossOrigin(origins = "*")
    @ResponseBody
    public R<?> data() {
        TwinAllVO vo = redisCache.getCacheObject(CacheConstants.ALL_MOCK);
        //处理经编当前数据  --start
        CurrYieldVO yieldVO = redisCache.getCacheObject(CacheConstants.CURR_YIELD);
        IndexData todayData = redisCache.getCacheObject(CacheConstants.INDEX_CALC);
        IndexData currData = redisCache.getCacheObject(CacheConstants.INDEX_ALARM);
        yieldVO.getJb().setYield(BigDecimal.valueOf(todayData.getEfficiency().getTotalLength()).setScale(0, RoundingMode.HALF_UP));
        yieldVO.getJb().setYieldMax(BigDecimal.valueOf(50000));
        BigDecimal jdl = BigDecimal.valueOf(todayData.getEfficiency().getAEfficiency() + todayData.getEfficiency().getBEfficiency()).divide(BigDecimal.valueOf(2), 0, RoundingMode.HALF_UP);
        if (todayData.getEfficiency().getBEfficiency() == 0f) {
            jdl = BigDecimal.valueOf(todayData.getEfficiency().getAEfficiency()).setScale(0, RoundingMode.HALF_UP);
        }
        yieldVO.getJb().setJdl(jdl);
        yieldVO.getJb().setOpenNum(currData.getDevice().getOnline());
        yieldVO.getJb().setTotalNum(currData.getDevice().getTotal());

        yieldVO.getJb().setOpenRatio(BigDecimal.valueOf(currData.getDevice().getRunningRatio()).setScale(0, RoundingMode.HALF_UP));
        //处理经编当前数据  --end

        //处理染整当前数据  --start
        YrTwinVO rzVO = redisCache.getCacheObject(CacheConstants.RZ_MOCK);
        CurrYieldVO.RZ rz = yieldVO.getRz();
        rz.setYield(rzVO.getStatus().getLength());
        rz.setOpenLine(rzVO.getStatus().getOpenProd());
        rz.setYieldMax(BigDecimal.valueOf(200000));
        rz.setOpenNum(rzVO.getStatus().getOpen());
        rz.setTotalNum(rzVO.getStatus().getTotal());
        rz.setOpenRatio(rzVO.getStatus().getRatio());
        vo.setCurrYield(yieldVO);
        //处理染整当前数据  --end
        return R.ok(vo);
    }

}
