package org.jeecg.modules.newWarning.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.newWarning.entity.WorkingConditionCalculationLog;
import org.jeecg.modules.newWarning.service.WorkingConditionCalculationLogService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/newWarn/workLog")
public class WorkingConditionCalculationLogController {

    @Resource
    private WorkingConditionCalculationLogService service;


    @GetMapping("/page")
    public Result<?> page(@RequestParam("jh")String jh,
                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        QueryWrapper<WorkingConditionCalculationLog> wrapper =new QueryWrapper<>();
        if(jh!=null&& !Objects.equals(jh, "")){
            wrapper.like("jh",jh);
        }
        wrapper.orderByDesc("CREATE_TIME");
        Page<WorkingConditionCalculationLog> page=new Page<WorkingConditionCalculationLog>(pageNo, pageSize);
        Page<WorkingConditionCalculationLog> pagelist = service.page(page, wrapper);
        return Result.OK(pagelist);
    }

    @GetMapping("/getByJh")
    public Result<?> getByJh(@RequestParam(name="jh",required=true) String jh){
        QueryWrapper<WorkingConditionCalculationLog> wrapper =new QueryWrapper<>();
        wrapper.eq("JH",jh);
        wrapper.orderByDesc("CREATE_TIME");
        List<WorkingConditionCalculationLog> list = service.list(wrapper);
        return Result.OK(list);
    }

    @PostMapping("/save")
    public Result<?> saveJh(@RequestBody WorkingConditionCalculationLog log){

        QueryWrapper<WorkingConditionCalculationLog> wrapper =new QueryWrapper<>();
        wrapper.eq("JH",log.getJh());
        wrapper.eq("IS_CALCULATE","2");
        List<WorkingConditionCalculationLog> list = service.list(wrapper);
        if (list.size()>0){
            return Result.error("当前有任务正在计算,添加失败");
        }

        log.setCreateTime(new Date());
        log.setIsCalculate("1");
        log.setId(UUID.randomUUID().toString().replace("-", ""));
        log.setIsDelRedis("1");
        service.save(log);
        return Result.OK();
    }


    @PostMapping("/calc")
    public Result<?> calc(@RequestBody WorkingConditionCalculationLog log){

        WorkingConditionCalculationLog work = service.getById(log.getId());
        if (work == null || !work.getIsCalculate().equals("1")){
            return Result.error("计算失败!状态有误");
        }
        try {
            //String url = "http://10.68.16.209:8905/workingConditionCalculation?";
            String url = "http://localhost:8907/workingConditionCalculation?";
            url += "workingConditionCalculationLogId=" + work.getId();

            JSONObject res = RestUtil.get(url);
            if (res.getInteger("code") != 200){
                return Result.error(res.getString("message"));
            }

        }catch (Exception e){
            System.out.println(e);
            e.printStackTrace();
            return Result.error("接口错误");
        }

        work.setIsCalculate("2");
        service.updateById(work);

        return Result.OK();
    }

}
