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.XrForceDeregisterRes;
import com.chinaweal.aiccs.aiccs.force.entity.XrLiccanRecTask;
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.IXrForceDeregisterResService;
import com.chinaweal.aiccs.aiccs.force.service.IXrLiccanRecTaskService;
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/10/10
 */
@Api(tags = "营业执照作废声明流程表")
@RestController
@RequestMapping("/XrLiccanRecTask")
@Slf4j
public class XrLiccanRecTaskController extends BaseController {

    @Resource
    private IXrLiccanRecTaskService xrLiccanRecTaskService;
    @Resource
    private IXrForceDeregisterResService xrForceDeregisterResService;
    @Resource
    private IXrActivityService iXrActivityService;


    @ApiOperation(value = "1.分页查询营业执照作废声明业务数据", position = 1)
    @PostMapping("/list")
    public RestResult<IPage<XrLiccanRecTask>> listXrLiccanRecTask(@RequestBody BaseQuery<ForceDeregisterSpeQueryDto> query, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        if (user == null) {
            throw new BusinessException(ResultCode.USER_NOT_LOGGED_IN);
        }
        IPage<XrLiccanRecTask> data = xrLiccanRecTaskService.listXrLiccanRecTask(query, user);
        return RestResult.ok(data);
    }

    @ApiOperation(value = "2.根据登记机关分页显示拟强制注销详细数据", position = 2)
    @PostMapping("/pageForceResByRegOrg")
    public RestResult<?> pageForceResByRegOrg(@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<XrForceDeregisterRes> data = xrForceDeregisterResService.pageForceResByRegOrg(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.getXrForceDeregisterRes() == null) {
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
        XrTaskListVo xrTaskListVo = xrLiccanRecTaskService.saveIvdAcceptList(queryDto, user);
        return RestResult.ok(xrTaskListVo);
    }

    @ApiOperation(value = "4.业务号查询营业执照作废声明业务", position = 4)
    @GetMapping("/selectByBizSeqNo")
    public RestResult<XrLiccanRecTask> selectByBizSeqNo(@RequestParam @ApiParam("id") String bizSeqNo) {
        LambdaQueryWrapper<XrLiccanRecTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XrLiccanRecTask::getBizSeqNo, bizSeqNo);
        XrLiccanRecTask xrLiccanRecTask = xrLiccanRecTaskService.getOne(queryWrapper);
        return RestResult.ok(xrLiccanRecTask);
    }

    @ApiOperation(value = "5.生成营业执照作废声明内容", position = 5)
    @PostMapping("/generateLiccanSta")
    public RestResult<?> generateLiccanSta(@RequestBody XrLiccanRecTask xrLiccanRecTask, HttpServletRequest request) {
        AICUser user = getLoginUser(request);
        String noticeContent = xrLiccanRecTaskService.generateLiccanSta(user,xrLiccanRecTask);
        if(noticeContent != null){
            return RestResult.ok(noticeContent);
        } else {
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
    }

    @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, "无权操作");
//        }
        xrLiccanRecTaskService.processControl(forceSaveDto, user);
        return RestResult.ok();
    }

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

}
