package com.chinaweal.aiccs.aiccs.temp.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSAbnListService;
import com.chinaweal.aiccs.aiccs.temp.entity.TemporarySave;
import com.chinaweal.aiccs.aiccs.temp.entity.Temprepairinfo;
import com.chinaweal.aiccs.aiccs.temp.service.TemporarySaveService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.chinaweal.aiccs.common.base.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 业务暂存表 前端控制器
 * </p>
 *
 * @author sh
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/temporarySave")
public class TemporarySaveController extends BaseController {
    @Autowired
    private TemporarySaveService temporarySaveService;

    @PostMapping("getInfo")
    public RestResult<?> getInfo(String bizseq,String module, String flow) {
        AssertUtils.isNotBlank(bizseq, module, flow);
        TemporarySave temporarySave = temporarySaveService.getOne(new LambdaQueryWrapper<TemporarySave>()
                .eq(TemporarySave::getBizseq, bizseq)
                .eq(TemporarySave::getModuleTag, module)
                .eq(TemporarySave::getState, "1")
                .eq(TemporarySave::getFlowTag, flow));
        return RestResult.ok(temporarySave);
    }

    @PostMapping("saveInfo")
    public RestResult<?> saveInfo(@RequestBody Map<String,Object> info) {
        if(StringUtils.isNotBlank(info.get("id").toString())){
            TemporarySave atom = temporarySaveService.getById(info.get("id").toString());
            atom.setDataJson(JSON.toJSONString(info.get("data"))).setState("1").setSaveTime(LocalDateTime.now());
            temporarySaveService.updateById(atom);
            return RestResult.ok(atom.getId());
        }else{
            TemporarySave atom = new TemporarySave()
                    .setId(UUID.randomUUID().toString())
                    .setBizseq(info.get("bizseq").toString())
                    .setModuleTag(info.get("module").toString())
                    .setFlowTag(info.get("flow").toString())
                    .setDataJson(JSON.toJSONString(info.get("data")))
                    .setState("1")
                    .setCreator(info.get("creator").toString())
                    .setCreatorId(info.get("creator_id").toString())
                    .setSaveTime(LocalDateTime.now())
                    .setCreateTime(LocalDateTime.now());
            temporarySaveService.save(atom);
            return RestResult.ok(atom.getId());
        }
    }
}

