package org.jeecg.modules.flowable.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.flowable.apithird.business.entity.FlowForm;
import org.jeecg.modules.flowable.apithird.business.entity.SysDeployForm;

import org.jeecg.modules.flowable.apithird.business.service.IFlowFormService;
import org.jeecg.modules.flowable.apithird.business.service.ISysDeployFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j

@RestController
@RequestMapping("/flowable/form")
public class FlowFormController {

    @Autowired
    private IFlowFormService flowFormService;
    @Autowired
    private ISysDeployFormService sysDeployFormService;

    @RequestMapping("/list")
    public Result<IPage<FlowForm>> getFormList(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req,
            FlowForm flowForm
    ) {
        QueryWrapper<FlowForm> queryWrapper = QueryGenerator.initQueryWrapper(flowForm, req.getParameterMap());
        Page<FlowForm> page = new Page<FlowForm>(pageNo, pageSize);
        IPage<FlowForm> pageList = flowFormService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    @RequestMapping("/listAllForm")
    public Result<List<FlowForm>> getAllFormList(HttpServletRequest req, FlowForm flowForm) {
        QueryWrapper<FlowForm> queryWrapper = QueryGenerator.initQueryWrapper(flowForm, req.getParameterMap());

        List<FlowForm> list = flowFormService.list(queryWrapper);
        return Result.OK(list);
    }

    @DeleteMapping(value = "/delete")
    public Result delete( @RequestParam Long formId) {
        flowFormService.removeById(formId);
        return Result.OK();
    }

    @DeleteMapping(value = "/batchDelete")
    public Result<String> batchDelete(@RequestParam(name="ids",required=true) String ids) {
        if(!ids.isEmpty()) {
            List<String> idsList = Arrays.asList(ids.split(","));
            flowFormService.removeBatchByIds(idsList);
        }
        return Result.OK("批量删除成功!");
    }

    @AutoLog(value = "表单添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody FlowForm flowForm) {
        flowFormService.save(flowForm);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody FlowForm flowForm) {
        flowFormService.updateById(flowForm);
        return Result.OK("编辑成功!");
    }

    @GetMapping(value = "/queryById")
    public Result<FlowForm> queryById(@RequestParam(name="id",required=true) String id) {
        FlowForm flowForm = flowFormService.getById(id);
        if(flowForm==null) {
            return Result.error("未找到");
        }
        return Result.OK(flowForm);
    }

    @GetMapping(value = "/findFlowFormByDeployId")
    public Result<FlowForm> findFlowFormByDeployId(@RequestParam(name="id",required=true) String deployId) {
        List<SysDeployForm> listSysDeployForm = sysDeployFormService.lambdaQuery()
            .eq(SysDeployForm::getDeployId, deployId)
            .list();
        if(listSysDeployForm.size()>0) {
            SysDeployForm sysDeployForm = listSysDeployForm.get(0);
            FlowForm flowForm = flowFormService.getById(sysDeployForm.getFormId());
            if(flowForm==null) {
                return Result.error("未找到");
            }
            return Result.OK(flowForm);
        }
        return Result.OK(null);
    }

}
