package com.qmth.distributed.print.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.distributed.print.business.bean.flow.CustomFlowCommonDto;
import com.qmth.distributed.print.business.bean.flow.CustomFlowEditDto;
import com.qmth.distributed.print.business.bean.flow.CustomFlowSaveDto;
import com.qmth.distributed.print.business.bean.params.CustomFlowParam;
import com.qmth.distributed.print.business.bean.params.CustomFlowRenameParam;
import com.qmth.distributed.print.business.bean.params.FlowTaskApproveParam;
import com.qmth.distributed.print.business.bean.result.*;
import com.qmth.distributed.print.business.entity.TFCustomFlow;
import com.qmth.distributed.print.business.entity.TFCustomFlowEntity;
import com.qmth.distributed.print.business.entity.TFFlowApprove;
import com.qmth.distributed.print.business.service.ActivitiService;
import com.qmth.distributed.print.business.service.TFCustomFlowEntityService;
import com.qmth.distributed.print.business.service.TFCustomFlowService;
import com.qmth.distributed.print.business.service.TFFlowApproveService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicSchool;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.FieldUniqueEnum;
import com.qmth.teachcloud.common.enums.FlowStatusEnum;
import com.qmth.teachcloud.common.enums.TFCustomTypeEnum;
import com.qmth.teachcloud.common.service.CommonCacheService;
import com.qmth.teachcloud.common.util.*;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义流程表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2022-01-21
 */
@Api(tags = "自定义流程Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.customFlow}")
//@Aac(auth = BOOL.FALSE, strict = BOOL.FALSE)
@Validated
public class TFCustomFlowController {
    private final static Logger log = LoggerFactory.getLogger(TFCustomFlowController.class);

    @Resource
    TFCustomFlowService tfCustomFlowService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    ActivitiService activitiService;

    @Resource
    CommonCacheService commonCacheService;

    @Resource
    TFFlowApproveService tfFlowApproveService;

    @Resource
    TFCustomFlowEntityService tfCustomFlowEntityService;

    @ApiOperation(value = "保存和发布流程")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional
    public Result save(@Valid @RequestBody CustomFlowSaveDto customFlowSaveDto, BindingResult bindingResult) throws NoSuchAlgorithmException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        Long schoolId = Objects.isNull(sysUser.getSchoolId()) ? SystemConstant.getHeadOrUserSchoolId() : sysUser.getSchoolId();
        customFlowSaveDto.setSchoolAndOrgInfo(schoolId, sysUser.getOrgId());
        String flowBpmnId = MD5Util.encoder(customFlowSaveDto.toString());
        BasicSchool basicSchool = commonCacheService.schoolCache(customFlowSaveDto.getSchoolId());
        flowBpmnId = basicSchool.getCode() + "_" + flowBpmnId;
        boolean lock = redisUtil.lock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId, SystemConstant.REDIS_LOCK_CUSTOM_FLOW_TIME_OUT);
        if (!lock) {
            throw ExceptionResultEnum.ERROR.exception("正在发布中，请稍候再试！");
        }
        try {
            TFCustomFlow dbTfCustomFlow = tfCustomFlowService.findMaxVersion(customFlowSaveDto.getSchoolId(), null, customFlowSaveDto.getType());
            TFCustomFlow tfCustomFlow = null;
            if (Objects.isNull(customFlowSaveDto.getCustomFlowId())) {
                tfCustomFlow = new TFCustomFlow(customFlowSaveDto.getSchoolId(), customFlowSaveDto.getOrgId(), customFlowSaveDto.getName(), customFlowSaveDto.getType(), customFlowSaveDto.getPublish(), JacksonUtil.parseJson(customFlowSaveDto.getCustomFlowLists()), sysUser.getId(), flowBpmnId, customFlowSaveDto.getModelType());
            } else {
                tfCustomFlow = tfCustomFlowService.getById(customFlowSaveDto.getCustomFlowId());
                Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
                if (!tfCustomFlow.getEnable()) {
                    throw ExceptionResultEnum.ERROR.exception("自定义流程数据已删除");
                }
                tfCustomFlow.setInfo(customFlowSaveDto);
                tfCustomFlow.updateInfo(sysUser.getId());
            }
            AtomicInteger atomicInteger = null;
            if (Objects.isNull(dbTfCustomFlow)) {//新增
                atomicInteger = new AtomicInteger(1);
                tfCustomFlow.setVersion(atomicInteger.get());
            } else {//版本号递增
                atomicInteger = new AtomicInteger(dbTfCustomFlow.getVersion());
                tfCustomFlow.setVersion(atomicInteger.incrementAndGet());
            }
            //自定义流程处理开始
            Map<String, Object> map = activitiService.dynamicBuildBpmn(customFlowSaveDto, flowBpmnId, tfCustomFlow.getVersion());
            String actFlowId = tfCustomFlowService.findActIdByFlowKey(flowBpmnId);
            tfCustomFlow.setActFlowId(actFlowId);
            tfCustomFlow.setFlowProcessVar(JacksonUtil.parseJson(map));
            tfCustomFlowService.saveOrUpdate(tfCustomFlow);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            if (e instanceof DuplicateKeyException) {
                String errorColumn = e.getCause().toString();
                String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length()).replaceAll("'", "");
                throw ExceptionResultEnum.SQL_ERROR.exception("[" + FieldUniqueEnum.convertToTitle(columnStr) + "]数据不允许重复插入");
            } else if (e instanceof ApiException) {
                ResultUtil.error((ApiException) e, e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
        } finally {
            redisUtil.releaseLock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId);
        }
        return ResultUtil.ok(true);
    }

//    @ApiOperation(value = "测试启动流程")
//    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
//    @RequestMapping(value = "/testStart", method = RequestMethod.POST)
//    @Transactional
//    public Result testStart(@ApiParam(value = "命题任务id", required = true) @RequestParam String taskId,
//                            @ApiParam(value = "命题老师id", required = true) @RequestParam String approveId) {
//        Map<String, Object> map = new HashMap<>();
//        map.computeIfAbsent(SystemConstant.OBJECT_ID, v -> SystemConstant.convertIdToLong(taskId));
//        map.computeIfAbsent(SystemConstant.APPROVE_ID, v -> SystemConstant.convertIdToLong(approveId));
//        map.computeIfAbsent(SystemConstant.FLOW_TYPE, v -> TFCustomTypeEnum.ELECTRON_FLOW);
//        activitiService.customFlowStart(map);
//        return ResultUtil.ok(true);
//    }

    @ApiOperation(value = "审批流程")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/task/approve", method = RequestMethod.POST)
    public Result taskApprove(@Valid @RequestBody FlowTaskApproveParam flowTaskApproveParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Map<String, Object> map = new HashMap<>();
        map.computeIfAbsent(SystemConstant.FLOW_TASK_ID, v -> flowTaskApproveParam.getTaskId());
        map.computeIfAbsent(SystemConstant.APPROVE_OPERATION, v -> flowTaskApproveParam.getApprovePass());
        map.computeIfAbsent(SystemConstant.APPROVE_REMARK, v -> flowTaskApproveParam.getRemark());
        map.computeIfAbsent(SystemConstant.APPROVE_SETUP, v -> flowTaskApproveParam.getSetup());
        map.computeIfAbsent(SystemConstant.APPROVE_USER_IDS, v -> flowTaskApproveParam.getApproveUserIds());
        activitiService.taskApprove(map);
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "流程审批记录列表")
    @ApiResponses({@ApiResponse(code = 200, message = "流程审批记录信息", response = FlowApproveListResult.class)})
    @RequestMapping(value = "/approve/list", method = RequestMethod.POST)
    public Result taskApproveList(@ApiParam(value = "状态", required = false) @RequestParam(required = false) FlowStatusEnum status,
                                  @ApiParam(value = "提交人名称", required = false) @RequestParam(required = false) String teacherUserName,
                                  @ApiParam(value = "教研室", required = false) @RequestParam(required = false) String teachingRoomId,
                                  @ApiParam(value = "提交开始时间", required = false) @RequestParam(required = false) Long startTime,
                                  @ApiParam(value = "提交结束时间", required = false) @RequestParam(required = false) Long endTime,
                                  @ApiParam(value = "提交人名称", required = false) @RequestParam(required = false) String pendApproveUserName,
                                  @ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                  @ApiParam(value = "数量", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(tfFlowApproveService.findApproveList(new Page<>(pageNumber, pageSize), status, teacherUserName, SystemConstant.convertIdToLong(teachingRoomId), startTime, endTime, pendApproveUserName, SystemConstant.getHeadOrUserSchoolId(), null));
    }

    @ApiOperation(value = "流程列表")
    @ApiResponses({@ApiResponse(code = 200, message = "流程信息", response = TFCustomFlow.class)})
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@ApiParam(value = "流程名称", required = false) @RequestParam(required = false) String name,
                       @ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @ApiParam(value = "数量", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(tfCustomFlowService.list(new Page<>(pageNumber, pageSize), name, SystemConstant.getHeadOrUserSchoolId(), null));
    }

    @ApiOperation(value = "流程编辑")
    @ApiResponses({@ApiResponse(code = 200, message = "流程信息", response = ResultUtil.class)})
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result edit(@ApiParam(value = "自定义流程id", required = true) @RequestParam String id) {
        TFCustomFlow tfCustomFlow = tfCustomFlowService.getById(SystemConstant.convertIdToLong(id));
        Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
        Optional.ofNullable(tfCustomFlow.getObjectData()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程绘图数据为空"));
        Gson gson = new Gson();
        List<CustomFlowCommonDto> customFlowLists = gson.fromJson(tfCustomFlow.getObjectData(), new TypeToken<List<CustomFlowCommonDto>>() {
        }.getType());
        return ResultUtil.ok(new CustomFlowEditDto(tfCustomFlow, customFlowLists), null);
    }

    @ApiOperation(value = "流程逻辑删除")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enable(@Valid @RequestBody CustomFlowParam customFlowParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        TFCustomFlow tfCustomFlow = tfCustomFlowService.getById(customFlowParam.getId());
        Optional.ofNullable(tfCustomFlow).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("自定义流程数据为空"));
        if (!customFlowParam.getEnable()) {
            List<TFCustomFlowEntity> tfCustomFlowEntityList = tfCustomFlowEntityService.findListByCustomFlowId(customFlowParam.getId());
            if (Objects.nonNull(tfCustomFlowEntityList) && tfCustomFlowEntityList.size() > 0) {
                List<Long> flowIds = tfCustomFlowEntityList.stream().map(e -> e.getFlowId()).collect(Collectors.toList());
                int min = 0;
                int max = SystemConstant.IN_SIZE_MAX, size = flowIds.size();
                if (max >= size) {
                    max = size;
                }
                while (max <= size) {
                    QueryWrapper<TFFlowApprove> tfFlowApproveQueryWrapper = new QueryWrapper<>();
                    tfFlowApproveQueryWrapper.lambda().ne(TFFlowApprove::getStatus, FlowStatusEnum.FINISH)
                            .ne(TFFlowApprove::getStatus, FlowStatusEnum.END)
                            .in(TFFlowApprove::getFlowId, flowIds.subList(min, max));
                    int count = tfFlowApproveService.count(tfFlowApproveQueryWrapper);
                    if (count > 0) {
                        throw ExceptionResultEnum.ERROR.exception("已存在流程数据，不能删除");
                    }
                    if (max == size) {
                        break;
                    }
                    min = max;
                    max += SystemConstant.IN_SIZE_MAX;
                    if (max >= size) {
                        max = size;
                    }
                }
            }
        }
        tfCustomFlow.setEnable(!customFlowParam.getEnable() ? null : customFlowParam.getEnable());
        return ResultUtil.ok(tfCustomFlowService.updateById(tfCustomFlow));
    }

    @ApiOperation(value = "查看流程信息")
    @ApiResponses({@ApiResponse(code = 200, message = "审批流程信息", response = FlowViewResult.class)})
    @RequestMapping(value = "/view", method = RequestMethod.POST)
    public Result view(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
        return ResultUtil.ok(activitiService.getFlowView(SystemConstant.convertIdToLong(flowId)));
    }

    @ApiOperation(value = "流程审批记录逻辑删除")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/approve/enable", method = RequestMethod.POST)
    public Result approveEnable(@Valid @RequestBody CustomFlowParam customFlowParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        TFFlowApprove tfFlowApprove = tfFlowApproveService.getById(customFlowParam.getId());
        Optional.ofNullable(tfFlowApprove).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("流程审批记录数据为空"));
        tfFlowApprove.setEnable(customFlowParam.getEnable());
        return ResultUtil.ok(tfFlowApproveService.updateById(tfFlowApprove));
    }

    @ApiOperation(value = "流程终止")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/end", method = RequestMethod.POST)
    public Result end(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
        activitiService.flowEnd(flowId);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "获取所有流程节点")
    @ApiResponses({@ApiResponse(code = 200, message = "流程节点", response = FlowTaskResult.class)})
    @RequestMapping(value = "/task/all", method = RequestMethod.POST)
    public Result taskAll(@ApiParam(value = "流程id", required = true) @RequestParam String flowId) {
        return ResultUtil.ok(activitiService.getTaskAll(flowId));
    }

    @ApiOperation(value = "流程节点转他人审批")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/task/approver/exchange", method = RequestMethod.POST)
    public Result taskApproverExchange(@ApiParam(value = "审批人id", required = true) @RequestParam String userId,
                                       @ApiParam(value = "流程节点id", required = true) @RequestParam String taskId) {
        return ResultUtil.ok(activitiService.taskApproverExchange(userId, taskId));
    }

    @ApiOperation(value = "获取当前流程节点信息")
    @ApiResponses({@ApiResponse(code = 200, message = "当前流程节点信息", response = TaskInfoResult.class)})
    @RequestMapping(value = "/task/info", method = RequestMethod.POST)
    public Result taskInfo(@ApiParam(value = "流程节点id", required = true) @RequestParam String taskId) {
        return ResultUtil.ok(activitiService.getTaskInfo(SystemConstant.convertIdToLong(taskId)));
    }

    @ApiOperation(value = "重命名自定义流程名称")
    @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)})
    @RequestMapping(value = "/rename", method = RequestMethod.POST)
    public Result rename(@Valid @RequestBody CustomFlowRenameParam customFlowRenameParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        UpdateWrapper<TFCustomFlow> tfCustomFlowUpdateWrapper = new UpdateWrapper<>();
        tfCustomFlowUpdateWrapper.lambda().eq(TFCustomFlow::getId, customFlowRenameParam.getId())
                .set(TFCustomFlow::getName, customFlowRenameParam.getName())
                .set(TFCustomFlow::getUpdateId, sysUser.getId())
                .set(TFCustomFlow::getUpdateTime, System.currentTimeMillis());
        return ResultUtil.ok(tfCustomFlowService.update(tfCustomFlowUpdateWrapper));
    }

    @ApiOperation(value = "根据流程类型获取流程节点")
    @ApiResponses({@ApiResponse(code = 200, message = "当前流程节点信息", response = FlowInfoResult.class)})
    @RequestMapping(value = "/get_flow_info_by_type", method = RequestMethod.POST)
    public Result getFlowInfoByType(@ApiParam(value = "流程类型", required = false) @RequestParam(required = false) TFCustomTypeEnum type,
                                    @ApiParam(value = "流程id", required = false) @RequestParam(required = false) String flowId) {
        if (Objects.isNull(type) && Objects.isNull(flowId)) {
            throw ExceptionResultEnum.ERROR.exception("流程类型或流程id必须输入一项");
        }
        return ResultUtil.ok(activitiService.getFlowInfoByType(type, Objects.nonNull(flowId) ? SystemConstant.convertIdToLong(flowId) : null));
    }
}
