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.SysExpression;
import org.jeecg.modules.flowable.apithird.business.service.ISysExpressionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 流程达式Controller
 *
 * @author ruoyi
 * @date 2022-12-12
 */
@Slf4j

@RestController
@RequestMapping("/flowable/expression")
public class SysExpressionController {

    @Autowired
    private ISysExpressionService sysExpressionService;

    @RequestMapping("/list")
    public Result<IPage<SysExpression>> list(
            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
            HttpServletRequest req,
            SysExpression sysExpression
    ) {
        QueryWrapper<SysExpression> queryWrapper = QueryGenerator.initQueryWrapper(sysExpression, req.getParameterMap());
        Page<SysExpression> page = new Page<SysExpression>(pageNo, pageSize);
        IPage<SysExpression> pageList = sysExpressionService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "sys_listener-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SysExpression sysExpression) {
        sysExpressionService.save(sysExpression);
        return Result.OK("添加成功！");
    }

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


    @DeleteMapping(value = "/delete")
    public Result delete( @RequestParam Long id) {
        sysExpressionService.removeById(id);
        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(","));
            sysExpressionService.removeBatchByIds(idsList);

        }
        return Result.OK("批量删除成功!");
    }

}
