package com.chinaweal.aiccs.aiccs.abnormal.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.entity.*;
import com.chinaweal.aiccs.aiccs.abnormal.entity.dto.DissentAppDto;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSAbnListService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSBizDissentService;
import com.chinaweal.aiccs.aiccs.system.entity.TRBaseCode;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.common.util.ConstantsUtil;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 经营异常异议移动端
 *
 * @author ymc
 * @since 2024年09月13日 9:50
 */
@Slf4j
@RestController
@RequestMapping("/wx/bizDissent")
public class BizDissentAppController {

    @Resource
    private TSBizDissentService tsBizDissentService;
    @Resource
    private EBaseinfoService eBaseinfoService;
    @Resource
    private TRBaseCodeService baseCodeService;
    @Resource
    private TSAbnListService tsAbnListService;

    /**
     * 启动待办
     *
     * @param dissentAppDto
     * @return
     * @author ymc
     * @since 2024年9月13日 10:06
     */
    @PostMapping("/startAppDissent")
    public RestResult<?> startAppDissent(@RequestBody DissentAppDto dissentAppDto) {
        tsBizDissentService.startAppDissent(dissentAppDto);
        return RestResult.ok();
    }

   
    /**
     * 办理页面
     * @author ymc
     * @since 2024年9月13日 15:40
     * @param dissentAppDto
     * @param request
     * @return
     */
    @PostMapping(value = "/info")
    public Map<String, Object> info(@RequestBody DissentAppDto dissentAppDto, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        String uniscid = dissentAppDto.getUniscid();
        String abnlistid = dissentAppDto.getAbnlistid();

        // 封装参数
        LambdaQueryWrapper<EBaseinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EBaseinfo::getUniscid, uniscid);
        wrapper.eq(EBaseinfo::getRegstate, "1");
        wrapper.select(EBaseinfo::getPripid, EBaseinfo::getCerno,
                EBaseinfo::getUniscid, EBaseinfo::getRegno,
                EBaseinfo::getEntname, EBaseinfo::getDom,
                EBaseinfo::getName, EBaseinfo::getPhone);

        // 查询基本信息
        EBaseinfo eBaseinfo = eBaseinfoService.getOne(wrapper);

        // 列异信息
        TSAbnList tsAbnList = tsAbnListService.selectTsAbnById(abnlistid);
        tsAbnList.setCerno(null); // 证件号码不显示 敏感信息

        Map<String, TRBaseCode> codeMap = baseCodeService.getBaseCodeMapFormCache("CD71");
        TRBaseCode trBaseCode = codeMap.get(tsAbnList.getSpecause());
        if (trBaseCode != null) {
            tsAbnList.setSpecauseCn(trBaseCode.getName());
        }
        result.put("eBaseinfo", eBaseinfo); //企业基本信息
        result.put("entertype", ConstantsUtil.getEntTypeAlias(eBaseinfo.getEnttype()));
        result.put("bizseq", IdUtil.randomUUID());
        result.put("tsAbnList", tsAbnList); //经营异常违法失信企业信息
        return result;
    }

    /**
     * 检验该笔件已经在申请中了
     * @author ymc
     * @since 2024年9月20日 10:55
     * @param abnlistid
     * @return
     */
    @GetMapping(value = "/checkTaskList")
    public RestResult<?> checkTaskList(@RequestParam String abnlistid) {
        TSBizDissent tsBizDissents = tsBizDissentService.lambdaQuery().eq(TSBizDissent::getAbnlistid, abnlistid).notIn(TSBizDissent::getStatus, "3", "4").one();
        if (tsBizDissents != null) {
            return RestResult.ok(tsBizDissents.getStatus());
        } else {
            return RestResult.ok(false);
        }
    }
}
