package com.jjt.biz.controller;

import com.jjt.biz.service.IApiYrService;
import com.jjt.biz.vo.YrCompareBackReq;
import com.jjt.biz.vo.YrCompareReq;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api(tags = "染整线数字孪生数据接口")
@RestController
@Slf4j
public class ApiYrController extends BaseController {
    @Resource
    private IApiYrService service;
    @Resource
    private RedisCache redisCache;
    @Value("${local.rzCompare}")
    private String path;

    @ApiOperation("染整线数字孪生")
    @GetMapping("/api/yr/data")
    @CrossOrigin(origins = "*")
    @ResponseBody
    public R<?> data() {
        YrTwinVO vo = redisCache.getCacheObject(CacheConstants.RZ_MOCK);
        return R.ok(vo);
    }

    @ApiOperation("染整线AI比对")
    @RequestMapping("/api/yr/compare")
    @CrossOrigin(origins = "*")
    @ResponseBody
    public R<?> compare(@RequestBody YrCompareReq req) {
        List<YrCompareBackReq> list = new ArrayList<>();
        if (req.getE1().isBefore(req.getS1()) || req.getE2().isBefore(req.getS2())) {
            return R.fail("结束时间不能早于开始时间");
        }
        YrCompareBackReq req1 = new YrCompareBackReq();
        req1.setLine(req.getLine1());
        req1.setStart(req.getS1());
        req1.setEnd(req.getE1());
        list.add(req1);
        YrCompareBackReq req2 = new YrCompareBackReq();
        req2.setLine(req.getLine2());
        req2.setStart(req.getS2());
        req2.setEnd(req.getE2());
        list.add(req2);
        return R.ok(service.compare(list, false));
    }

    @ApiOperation("后台工艺比对")
    @RequestMapping("/api/yr/compare/back")
    @CrossOrigin(origins = "*")
    @ResponseBody
    public R<?> compareBack(@RequestBody List<YrCompareBackReq> list) {
        for (YrCompareBackReq req : list) {
            if (req.getEnd().isBefore(req.getStart())) {
                return R.fail("结束时间不能早于开始时间");
            }
        }
        return R.ok(service.compare(list, true));
    }


}
