package com.chinaweal.aiccs.aiccs.forcedCancellation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.forcedCancellation.entity.LicenseCancellationDeclare;
import com.chinaweal.aiccs.aiccs.forcedCancellation.service.LicenseCancellationDeclareService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static com.chinaweal.youfool.framework.springboot.rest.ResultCode.BUSINESS_LOGIC_ERROR;

/**
 * <p>
 * 营业执照作废声明表 前端控制器
 * </p>
 *
 * @author lwd
 * @since 2025-10-22
 */
@RestController
@RequestMapping("/licenseCancellationDeclare")
public class LicenseCancellationDeclareController extends BaseController {

    @Autowired
    private LicenseCancellationDeclareService licenseCancellationDeclareService;

    /**
     * 分页查询营业执照作废声明
     */
    @ApiOperation("分页查询营业执照作废声明")
    @PostMapping("/page")
    public RestResult<?> page(HttpServletRequest request,
                                @RequestBody LicenseCancellationDeclare licenseCancellationDeclare,
                                @RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int rows) {
        IPage<LicenseCancellationDeclare> result = licenseCancellationDeclareService.page(new Page<>(page, rows), licenseCancellationDeclare);
        return RestResult.ok(result);
    }

    /**
     * 根据ID查询营业执照作废声明
     */
    @ApiOperation("根据ID查询营业执照作废声明")
    @GetMapping("/getById")
    public RestResult<?> getById(@RequestParam String recId) {
        AssertUtils.isNotBlank(recId);
        LicenseCancellationDeclare result = licenseCancellationDeclareService.getById(recId);
        return RestResult.ok(result);
    }

    /**
     * 新增营业执照作废声明
     */
    @ApiOperation("新增营业执照作废声明")
    @PostMapping("/save")
    public RestResult<?> save(HttpServletRequest request, @RequestBody LicenseCancellationDeclare licenseCancellationDeclare) {
        // 设置默认值
        if (licenseCancellationDeclare.getSExtDatetime() == null) {
            licenseCancellationDeclare.setSExtDatetime(LocalDateTime.now());
        }
        boolean result = licenseCancellationDeclareService.save(licenseCancellationDeclare);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"保存失败");
    }

    /**
     * 更新营业执照作废声明
     */
    @ApiOperation("更新营业执照作废声明")
    @PutMapping("/update")
    public RestResult<?> update(HttpServletRequest request, @RequestBody LicenseCancellationDeclare licenseCancellationDeclare) {
        AssertUtils.isNotBlank(licenseCancellationDeclare.getRecId());
        licenseCancellationDeclare.setSExtDatetime(LocalDateTime.now());
        boolean result = licenseCancellationDeclareService.updateById(licenseCancellationDeclare);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"更新失败");
    }

    /**
     * 删除营业执照作废声明
     */
    @ApiOperation("删除营业执照作废声明")
    @DeleteMapping("/delete")
    public RestResult<?> delete(@RequestParam String recId) {
        AssertUtils.isNotBlank(recId);
        boolean result = licenseCancellationDeclareService.removeById(recId);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"删除失败");
    }

    /**
     * 根据业务ID查询营业执照作废声明
     */
    @ApiOperation("根据业务ID查询营业执照作废声明")
    @GetMapping("/getByBizSeqId")
    public RestResult<?> getByBizSeqId(@RequestParam String bizseqId) {
        AssertUtils.isNotBlank(bizseqId);
        LicenseCancellationDeclare result = licenseCancellationDeclareService.getByBizSeqId(bizseqId);
        return RestResult.ok(result);
    }

    /**
     * 根据主体标识查询营业执照作废声明
     */
    @ApiOperation("根据主体标识查询营业执照作废声明")
    @GetMapping("/getByPripid")
    public RestResult<?> getByPripid(@RequestParam String pripid) {
        AssertUtils.isNotBlank(pripid);
        return RestResult.ok(licenseCancellationDeclareService.getByPripid(pripid));
    }

    /**
     * 根据企业名称查询营业执照作废声明
     */
    @ApiOperation("根据企业名称查询营业执照作废声明")
    @PostMapping("/getByEntName")
    public RestResult<?> getByEntName(@RequestParam String entName) {
        AssertUtils.isNotBlank(entName);
        return RestResult.ok(licenseCancellationDeclareService.getByEntName(entName));
    }

    /**
     * 根据统一社会信用代码查询营业执照作废声明
     */
    @ApiOperation("根据统一社会信用代码查询营业执照作废声明")
    @GetMapping("/getByUniscid")
    public RestResult<?> getByUniscid(@RequestParam String uniscid) {
        AssertUtils.isNotBlank(uniscid);
        return RestResult.ok(licenseCancellationDeclareService.getByUniscid(uniscid));
    }

    /**
     * 根据登记机关查询营业执照作废声明
     */
    @ApiOperation("根据登记机关查询营业执照作废声明")
    @GetMapping("/getByRegOrg")
    public RestResult<?> getByRegOrg(@RequestParam String regOrg) {
        AssertUtils.isNotBlank(regOrg);
        return RestResult.ok(licenseCancellationDeclareService.getByRegOrg(regOrg));
    }

    /**
     * 根据营业执照作废原因查询
     */
    @ApiOperation("根据营业执照作废原因查询")
    @GetMapping("/getByLicCanReason")
    public RestResult<?> getByLicCanReason(@RequestParam String licCanReason) {
        AssertUtils.isNotBlank(licCanReason);
        return RestResult.ok(licenseCancellationDeclareService.getByLicCanReason(licCanReason));
    }

    /**
     * 根据正副本标识查询
     */
    @ApiOperation("根据正副本标识查询")
    @GetMapping("/getByIsoriCop")
    public RestResult<?> getByIsoriCop(@RequestParam String isoriCop) {
        AssertUtils.isNotBlank(isoriCop);
        return RestResult.ok(licenseCancellationDeclareService.getByIsoriCop(isoriCop));
    }

    /**
     * 根据作废声明发布日期查询
     */
    @ApiOperation("根据作废声明发布日期查询")
    @PostMapping("/getByLicCanDate")
    public RestResult<?> getByLicCanDate(@RequestParam LocalDate licCanDate) {
        AssertUtils.isNotNull(licCanDate);
        return RestResult.ok(licenseCancellationDeclareService.getByLicCanDate(licCanDate));
    }

    /**
     * 根据营业执照副本编号查询
     */
    @ApiOperation("根据营业执照副本编号查询")
    @GetMapping("/getByLicCopyNo")
    public RestResult<?> getByLicCopyNo(@RequestParam String licCopyNo) {
        AssertUtils.isNotBlank(licCopyNo);
        return RestResult.ok(licenseCancellationDeclareService.getByLicCopyNo(licCopyNo));
    }

    /**
     * 根据二级部署标识查询
     */
    @ApiOperation("根据二级部署标识查询")
    @GetMapping("/getBySExtFromnode")
    public RestResult<?> getBySExtFromnode(@RequestParam String sExtFromnode) {
        AssertUtils.isNotBlank(sExtFromnode);
        return RestResult.ok(licenseCancellationDeclareService.getBySExtFromnode(sExtFromnode));
    }

    /**
     * 查询营业执照作废声明列表
     */
    @ApiOperation("查询营业执照作废声明列表")
    @PostMapping("/list")
    public RestResult<?> list(@RequestBody(required = false) LicenseCancellationDeclare licenseCancellationDeclare) {
        if (licenseCancellationDeclare == null) {
            licenseCancellationDeclare = new LicenseCancellationDeclare();
        }
        return RestResult.ok();
    }

    /**
     * 批量保存营业执照作废声明
     */
    @ApiOperation("批量保存营业执照作废声明")
    @PostMapping("/batchSave")
    public RestResult<?> batchSave(HttpServletRequest request, @RequestBody List<LicenseCancellationDeclare> declares) {
        if (declares == null || declares.isEmpty()) {
            return RestResult.error(BUSINESS_LOGIC_ERROR,"声明列表不能为空");
        }
        LocalDateTime now = LocalDateTime.now();
        for (LicenseCancellationDeclare declare : declares) {
            if (declare.getSExtDatetime() == null) {
                declare.setSExtDatetime(now);
            }
        }
        boolean result = licenseCancellationDeclareService.saveBatch(declares);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"批量保存失败");
    }

    /**
     * 批量更新作废声明发布日期
     */
    @ApiOperation("批量更新作废声明发布日期")
    @PostMapping("/batchUpdateLicCanDate")
    public RestResult<?> batchUpdateLicCanDate(HttpServletRequest request,
                                               @RequestBody Map<String, Object> params) {
        List<String> recIds = (List<String>) params.get("recIds");
        LocalDate licCanDate = params.containsKey("licCanDate") ?
            LocalDate.parse(params.get("licCanDate").toString()) : null;

        AssertUtils.isNotNull(recIds, "声明ID列表不能为空");
        AssertUtils.isNotNull(licCanDate, "作废声明发布日期不能为空");

        boolean result = licenseCancellationDeclareService.batchUpdateLicCanDate(recIds, licCanDate);
        return result ? RestResult.ok() : RestResult.error(BUSINESS_LOGIC_ERROR,"批量更新失败");
    }

    /**
     * 根据业务ID和主体标识查询最新的作废声明
     */
    @ApiOperation("根据业务ID和主体标识查询最新的作废声明")
    @GetMapping("/getLatestByBizAndPripid")
    public RestResult<?> getLatestByBizAndPripid(@RequestParam String bizseqId,
                                                    @RequestParam String pripid) {
        AssertUtils.isNotBlank(bizseqId);
        AssertUtils.isNotBlank(pripid);
        LicenseCancellationDeclare result = licenseCancellationDeclareService.getLatestByBizAndPripid(bizseqId, pripid);
        return RestResult.ok(result);
    }
}
