package com.chinaweal.aiccs.aiccs.force.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.entity.XrForceDeregisterRem;
import com.chinaweal.aiccs.aiccs.force.entity.XrForceDeregisterSpe;
import com.chinaweal.aiccs.aiccs.force.entity.querydto.ForceDeregisterSpeQueryDto;
import com.chinaweal.aiccs.aiccs.force.entity.querydto.ForceDeregisterSpeSaveDto;
import com.chinaweal.aiccs.aiccs.force.service.IXrForceDeregisterRemService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.SessionConstants;
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.HashMap;
import java.util.Map;

/**
 * 终止强制注销业务表 控制器
 *
 * @author zhouxy
 * @since 2025/9/29
 */
@Api(tags = "终止强制注销业务表")
@RestController
@RequestMapping("/XrForceDeregisterRem")
@Slf4j
public class XrForceDeregisterRemController extends BaseController {

    @Resource
    private IXrForceDeregisterRemService iXrForceDeregisterRemService;
    @Resource
    private IXrActivityService iXrActivityService;


    @ApiOperation(value = "1.分页查询终止强制注销业务数据", position = 1)
    @PostMapping("/list")
    public RestResult<IPage<XrForceDeregisterRem>> listXrForceDeregisterRem(@RequestBody BaseQuery<ForceDeregisterSpeQueryDto> query , HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_LOGGED_IN);
        }
        IPage<XrForceDeregisterRem> data = iXrForceDeregisterRemService.listXrForceDeregisterRem(query, user);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "2.根据登记机关分页显示拟强制注销业务数据", position = 2)
    @PostMapping("/forceSpePageByRegOrg")
    public RestResult<?> forceSpePageByRegOrg(@RequestBody BaseQuery<ForceDeregisterSpeQueryDto> query, HttpServletRequest request) {
        Map<String,Object> rs = new HashMap<>();
        AICUser user = getLoginUser(request);
        if (user == null) {
            return RestResult.error(ResultCode.USER_NOT_LOGGED_IN);
        }
        IPage<XrForceDeregisterSpe> data = iXrForceDeregisterRemService.forceSpePageByRegOrg(query, user);
        rs.put("dataPage",data);
        return RestResult.ok(rs);
    }

    @ApiOperation(value = "3.启动待办", position = 3)
    @PostMapping("/saveIvdAcceptList")
    public RestResult<XrTaskListVo> saveIvdAcceptList(@RequestBody ForceDeregisterSpeQueryDto queryDto, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            return RestResult.error(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (queryDto.getXrForceDeregisterSpe() == null) {
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
        XrTaskListVo xrTaskListVo = iXrForceDeregisterRemService.saveIvdAcceptList(queryDto, user);
        return RestResult.ok(xrTaskListVo);
    }

    @ApiOperation(value = "4.业务号查询终止强制注销业务", position = 4)
    @GetMapping("/selectByBizSeqNo")
    public RestResult<XrForceDeregisterRem> selectByBizSeqNo(@RequestParam @ApiParam("id") String bizSeqNo) {
        LambdaQueryWrapper<XrForceDeregisterRem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XrForceDeregisterRem::getBizSeqNo, bizSeqNo);
        XrForceDeregisterRem xrForceRem = iXrForceDeregisterRemService.getOne(queryWrapper);
        return RestResult.ok(xrForceRem);
    }

    @ApiOperation(value = "5.流程控制", position = 5)
    @PostMapping("/processControl")
    public RestResult<?> processControl(@RequestBody @Validated ForceDeregisterSpeSaveDto forceSaveDto,
                                        HttpServletRequest request) {
//        SaSession session = StpUtil.getSession();
//        Map<String, Object> dataMap = session.getDataMap();
//        AICUser user = (AICUser) dataMap.get(SessionConstants.AICUSER);
        AICUser user = getLoginUser(request);
        if (user == null) {
            return RestResult.error(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (StringUtils.isEmpty(forceSaveDto.getNextNodeID()) || StringUtils.isEmpty(forceSaveDto.getBizSeqNo())) {
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
        Map<String, AICPermission> aicPermissions = user.getAicPermissions();
        //根据当前流程节点判断权限
//        if (!iXrActivityService.checkPermissionsAndNodeID(aicPermissions, forceSaveDto.getCurrentNodeID(), forceSaveDto.getNextNodeID(),"seriousillegal")) {
//            return RestResult.error(ResultCode.INTERFACE_FORBID_VISIT, "无权操作");
//        }

        String forceRemType = forceSaveDto.getXrForceDeregisterRem().getForceRemType();
        if(forceRemType.equals("1")){   //终止拟强制注销
            iXrForceDeregisterRemService.processControlRem(forceSaveDto, user);
        } else if(forceRemType.equals("0")){    //恢复登记申请
            iXrForceDeregisterRemService.processControlRecovery(forceSaveDto, user);
        }
        return RestResult.ok();
    }

    @ApiOperation(value = "6.予以恢复/不予恢复决定书内容", position = 6)
    @PostMapping("/generateDecisionContent")
    public RestResult<?> generateDecisionContent(@RequestBody XrForceDeregisterRem xrForceDeregisterRem, HttpServletRequest request) {
         AICUser user = getLoginUser(request);
        String noticeContent = iXrForceDeregisterRemService.generateDecisionContent(user,xrForceDeregisterRem);
        if(noticeContent != null){
            return RestResult.ok(noticeContent);
        } else {
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
    }
    @ApiOperation(value = "7.业务号获取审批表", position = 7)
    @GetMapping("/getApprovalFormByBizSeqNo")
    public RestResult<ApprovalFormDto> getApprovalFormByBizSeqNo(@RequestParam @ApiParam("id") String bizSeqNo, String forceRemId) {
        ApprovalFormDto approvalDto = iXrForceDeregisterRemService.getApprovalFormByBizSeqNo(bizSeqNo, forceRemId);
        return RestResult.ok(approvalDto);
    }

}
