package com.chinaweal.aiccs.aiccs.seriousillegal.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.business.entity.dto.ApprovalFormDto;
import com.chinaweal.aiccs.aiccs.business.entity.vo.XrTaskListVo;
import com.chinaweal.aiccs.aiccs.business.service.IXrActivityService;
import com.chinaweal.aiccs.aiccs.force.service.IXrLiccanRecTaskService;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.RpeiExeInfo;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.RpeiExeInfoTask;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.TSSerIllegalList;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.dto.RpeiExeInfoTaskSaveDto;
import com.chinaweal.aiccs.aiccs.seriousillegal.service.IRpeiExeInfoService;
import com.chinaweal.aiccs.aiccs.seriousillegal.service.IRpeiExeInfoTaskService;
import com.chinaweal.aiccs.aiccs.seriousillegal.service.TSSerIllegalListService;
import com.chinaweal.aiccs.aiccs.seriousillegalPerson.entity.dto.SeriousIllegalSpeQueryDto;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.aicorg.pojo.AICPermission;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 重整计划或者和解协议执行流程表 控制器
 *
 * @author zhouxy
 * @since 2025/12/23
 */
@Api(tags = "营业执照作废声明流程表")
@RestController
@RequestMapping("/RpeiExeInfo")
@Slf4j
public class RpeiExeInfoController extends BaseController {

    @Resource
    private IXrLiccanRecTaskService xrLiccanRecTaskService;
    @Resource
    private IXrActivityService iXrActivityService;
    @Resource
    private IRpeiExeInfoTaskService rpeiExeInfoTaskService;
    @Resource
    private TSSerIllegalListService tsSerIllegalListService;
    @Resource
    private IRpeiExeInfoService rpeiExeInfoService;


    @ApiOperation(value = "1.分页查询处于严重违法的主体", position = 1)
    @PostMapping("/listXrSeriousIllegalListEnt")
    public RestResult<IPage<TSSerIllegalList>> listXrSeriousIllegalListEnt(@RequestBody BaseQuery<EBaseinfo> query, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_LOGGED_IN);
        }
        //查出有未移出的严重违法的企业数据，并且显示主体的任意一条严重违法信息
        IPage<TSSerIllegalList> data = tsSerIllegalListService.listXrSeriousIllegalListEnt(query, user);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "2.分页查询重整计划或者和解协议执行流程数据", position = 2)
    @PostMapping("/listRpeiExeInfoTask")
    public RestResult<IPage<RpeiExeInfoTask>> listRpeiExeInfoTask(@RequestBody BaseQuery<SeriousIllegalSpeQueryDto> query, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_LOGGED_IN);
        }
        IPage<RpeiExeInfoTask> data = rpeiExeInfoTaskService.listRpeiExeInfoTask(query, user);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "3.分页查询重整计划或者和解协议执行流程数据", position = 3)
    @PostMapping("/listRpeiExeInfo")
    public RestResult<IPage<RpeiExeInfo>> listRpeiExeInfo(@RequestBody BaseQuery<SeriousIllegalSpeQueryDto> query, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_LOGGED_IN);
        }

        SeriousIllegalSpeQueryDto entity = query.getEntity(SeriousIllegalSpeQueryDto.class);
        LambdaQueryWrapper<RpeiExeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RpeiExeInfo::getRegAuth, user.getRegionID().substring(0, 6))
                .eq(RpeiExeInfo::getExeState, "1");  //只查执行期间的
        if (StringUtils.isNotEmpty(entity.getSearchUscc())) {
            queryWrapper.eq(RpeiExeInfo::getUniscid, entity.getSearchUscc())
                    .or().eq(RpeiExeInfo::getRegno, entity.getSearchUscc());
        }
        if (StringUtils.isNotEmpty(entity.getEntName())) {
            queryWrapper.like(RpeiExeInfo::getEntname, entity.getEntName());
        }
        Page<RpeiExeInfo> page = new Page<>(query.getCurrent(), query.getSize());
        IPage<RpeiExeInfo> data = rpeiExeInfoService.page(page, queryWrapper);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "4.启动待办", position = 4)
    @PostMapping("/saveIvdAcceptList")
    public RestResult<XrTaskListVo> saveIvdAcceptList(@RequestBody SeriousIllegalSpeQueryDto queryDto, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            return RestResult.error(ResultCode.USER_NOT_LOGGED_IN);
        }
        RpeiExeInfo rpeiExeInfo = queryDto.getRpeiExeInfo();
        String operType = queryDto.getOperType();
        if (StringUtils.isEmpty(operType) || rpeiExeInfo == null) {
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }

        //判断该企业还有没有没走完的流程
        List<RpeiExeInfoTask> taskList = rpeiExeInfoTaskService.list(new LambdaQueryWrapper<RpeiExeInfoTask>()
                .eq(RpeiExeInfoTask::getPripId, rpeiExeInfo.getPripid())
                .in(RpeiExeInfoTask::getStatus, "0", "1", "2"));//还没走完的流程
        if (!taskList.isEmpty()) {
            return RestResult.error(ResultCode.BUSINESS_LOGIC_ERROR, "该企业还有未完成的流程");
        }

        //若是要新增执行流程，需要判断该企业的重整计划或者和解协议是否在执行
        if ("add".equals(operType)) {
            List<RpeiExeInfo> infoList = rpeiExeInfoService.list(new LambdaQueryWrapper<RpeiExeInfo>()
                    .eq(RpeiExeInfo::getPripid, rpeiExeInfo.getPripid())
                    .eq(RpeiExeInfo::getExeState, "1"));//执行状态是1代表还在执行中
            if (!infoList.isEmpty()) {
                return RestResult.error(ResultCode.BUSINESS_LOGIC_ERROR, "该企业的重整计划或者和解协议尚在执行中");
            }
        }
        XrTaskListVo xrTaskListVo = rpeiExeInfoTaskService.saveIvdAcceptList(rpeiExeInfo, operType, user);
        return RestResult.ok(xrTaskListVo);
    }

    @ApiOperation(value = "5.业务号查询重整计划或者和解协议执行流程", position = 5)
    @GetMapping("/selectByBizSeqNo")
    public RestResult<RpeiExeInfoTask> selectByBizSeqNo(@RequestParam @ApiParam("id") String bizSeqNo) {
        LambdaQueryWrapper<RpeiExeInfoTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RpeiExeInfoTask::getBizSeqNo, bizSeqNo);
        RpeiExeInfoTask rpeiExeInfoTask = rpeiExeInfoTaskService.getOne(queryWrapper);
        return RestResult.ok(rpeiExeInfoTask);
    }

    @ApiOperation(value = "6.流程控制", position = 6)
    @PostMapping("/processControl")
    public RestResult<?> processControl(@RequestBody @Validated RpeiExeInfoTaskSaveDto rpeiExeInfoTaskSaveDto, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            return RestResult.error(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (StringUtils.isEmpty(rpeiExeInfoTaskSaveDto.getNextNodeID()) || StringUtils.isEmpty(rpeiExeInfoTaskSaveDto.getBizSeqNo())) {
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
        Map<String, AICPermission> aicPermissions = user.getAicPermissions();
        //根据当前流程节点判断权限
        if (!iXrActivityService.checkPermissionsAndNodeID(aicPermissions, rpeiExeInfoTaskSaveDto.getCurrentNodeID(), rpeiExeInfoTaskSaveDto.getNextNodeID(), "seriousillegal")) {
            return RestResult.error(ResultCode.INTERFACE_FORBID_VISIT, "无权操作");
        }
        rpeiExeInfoTaskService.processControl(rpeiExeInfoTaskSaveDto, user);
        return RestResult.ok();
    }

    @ApiOperation(value = "8.业务号查看公告", position = 8)
    @GetMapping("/getApprovalFormByBizSeqNo")
    public RestResult<ApprovalFormDto> getApprovalFormByBizSeqNo(@RequestParam @ApiParam("id") String bizSeqNo, String recId) {
        ApprovalFormDto approvalDto = xrLiccanRecTaskService.getApprovalFormByBizSeqNo(bizSeqNo, recId);
        return RestResult.ok(approvalDto);
    }

}
