package org.jeecg.modules.flowpath.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flowpath.convert.FlowPathConvert;
import org.jeecg.modules.flowpath.dto.ItdmCreateRunFlowPath;
import org.jeecg.modules.flowpath.entity.ItdmFlowPathStep;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPath;
import org.jeecg.modules.flowpath.entity.ItdmRunFlowPathStep;
import org.jeecg.modules.flowpath.service.IItdmFlowPathService;
import org.jeecg.modules.flowpath.service.IItdmFlowPathStepService;
import org.jeecg.modules.flowpath.service.IItdmRunFlowPathService;
import org.jeecg.modules.flowpath.service.IItdmRunFlowPathStepService;
import org.jeecg.modules.flowpath.vo.ItdmRunFlowPathStepVO;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 运行流程表
 * @Author: jeecg-boot
 * @Date: 2023-07-20
 * @Version: V1.0
 */
@Api(tags = "运行流程表")
@RestController
@RequestMapping("/flowpath/itdmRunFlowPath")
@Slf4j
public class ItdmRunFlowPathController extends JeecgController<ItdmRunFlowPath, IItdmRunFlowPathService> {
    @Autowired
    private IItdmRunFlowPathService itdmRunFlowPathService;
    @Autowired
    private IItdmFlowPathService itdmFlowPathService;

    @Autowired
    private IItdmRunFlowPathStepService itdmRunFlowPathStepService;


    @Autowired
    private IItdmFlowPathStepService iItdmFlowPathStepService;


    @Autowired
    private SysBaseApiImpl sysBaseApi;


    /**
     * 分页列表查询
     *
     * @param itdmRunFlowPath
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "运行流程表-分页列表查询")
    @ApiOperation(value = "运行流程表-分页列表查询", notes = "运行流程表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ItdmRunFlowPath>> queryPageList(ItdmRunFlowPath itdmRunFlowPath,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<ItdmRunFlowPath> queryWrapper = QueryGenerator.initQueryWrapper(itdmRunFlowPath, req.getParameterMap());
        Page<ItdmRunFlowPath> page = new Page<ItdmRunFlowPath>(pageNo, pageSize);
        IPage<ItdmRunFlowPath> pageList = itdmRunFlowPathService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param itdmRunFlowPath
     * @return
     */
    @AutoLog(value = "运行流程表-添加")
    @ApiOperation(value = "运行流程表-添加", notes = "运行流程表-添加")
    //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ItdmCreateRunFlowPath itdmRunFlowPath) {
        itdmRunFlowPathService.createRunFlowPath(itdmRunFlowPath);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param itdmRunFlowPath
     * @return
     */
    @AutoLog(value = "运行流程表-编辑")
    @ApiOperation(value = "运行流程表-编辑", notes = "运行流程表-编辑")
    //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ItdmRunFlowPath itdmRunFlowPath) {
        itdmRunFlowPathService.updateById(itdmRunFlowPath);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "运行流程表-通过id删除")
    @ApiOperation(value = "运行流程表-通过id删除", notes = "运行流程表-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        itdmRunFlowPathService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "运行流程表-批量删除")
    @ApiOperation(value = "运行流程表-批量删除", notes = "运行流程表-批量删除")
    //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.itdmRunFlowPathService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "运行流程表-通过id查询")
    @ApiOperation(value = "运行流程表-通过id查询", notes = "运行流程表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ItdmRunFlowPath> queryById(@RequestParam(name = "id", required = true) String id) {
        ItdmRunFlowPath itdmRunFlowPath = itdmRunFlowPathService.getById(id);
        if (itdmRunFlowPath == null) {
            return Result.error("未找到对应数据");
        }

        List<ItdmRunFlowPathStep> list = itdmRunFlowPathStepService.
                list(Wrappers.lambdaQuery(ItdmRunFlowPathStep.class).eq(ItdmRunFlowPathStep::getFlowPath, id));


        List<ItdmRunFlowPathStepVO> vos = new ArrayList<>();
        for (ItdmRunFlowPathStep i : list) {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            List<String> strings = StringUtils.isNotBlank(i.getUserIds()) ?
                    Arrays.stream(i.getUserIds().split(",")).collect(Collectors.toList()) :
                    new ArrayList<>();
            List<String> roleIds = sysBaseApi.getRoleIdsByUsername(user.getUsername());
            if (strings.contains(user.getUsername()) || roleIds.contains(i.getRoleId())) {
                vos.add(FlowPathConvert.INSTANCE.to(i, true));
            } else {
                vos.add(FlowPathConvert.INSTANCE.to(i, false));
            }

        }


        return Result.OK(itdmRunFlowPath);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param itdmRunFlowPath
     */
    //@RequiresPermissions("org.jeecg.modules:itdm_run_flow_path:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ItdmRunFlowPath itdmRunFlowPath) {
        return super.exportXls(request, itdmRunFlowPath, ItdmRunFlowPath.class, "运行流程表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("itdm_run_flow_path:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ItdmRunFlowPath.class);
    }




}
