package com.ruoyi.ccdi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ccdi.domain.dto.CcdiIntermediaryEntityAddDTO;
import com.ruoyi.ccdi.domain.dto.CcdiIntermediaryEntityEditDTO;
import com.ruoyi.ccdi.domain.dto.CcdiIntermediaryPersonAddDTO;
import com.ruoyi.ccdi.domain.dto.CcdiIntermediaryPersonEditDTO;
import com.ruoyi.ccdi.domain.dto.CcdiIntermediaryQueryDTO;
import com.ruoyi.ccdi.domain.excel.CcdiIntermediaryEntityExcel;
import com.ruoyi.ccdi.domain.excel.CcdiIntermediaryPersonExcel;
import com.ruoyi.ccdi.domain.vo.CcdiIntermediaryEntityDetailVO;
import com.ruoyi.ccdi.domain.vo.CcdiIntermediaryPersonDetailVO;
import com.ruoyi.ccdi.domain.vo.CcdiIntermediaryVO;
import com.ruoyi.ccdi.service.ICcdiIntermediaryService;
import com.ruoyi.ccdi.utils.EasyExcelUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 中介信息Controller
 *
 * @author ruoyi
 * @date 2026-02-04
 */
@Tag(name = "中介信息管理")
@RestController
@RequestMapping("/ccdi/intermediary")
public class CcdiIntermediaryController extends BaseController {

    @Resource
    private ICcdiIntermediaryService intermediaryService;

    /**
     * 查询中介列表
     */
    @Operation(summary = "查询中介列表")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:list')")
    @GetMapping("/list")
    public TableDataInfo list(CcdiIntermediaryQueryDTO queryDTO) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<CcdiIntermediaryVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<CcdiIntermediaryVO> result = intermediaryService.selectIntermediaryPage(page, queryDTO);
        return getDataTable(result.getRecords(), result.getTotal());
    }

    /**
     * 查询个人中介详情
     */
    @Operation(summary = "查询个人中介详情")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:query')")
    @GetMapping("/person/{bizId}")
    public AjaxResult getPersonInfo(@PathVariable String bizId) {
        CcdiIntermediaryPersonDetailVO vo = intermediaryService.selectIntermediaryPersonDetail(bizId);
        return success(vo);
    }

    /**
     * 查询实体中介详情
     */
    @Operation(summary = "查询实体中介详情")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:query')")
    @GetMapping("/entity/{socialCreditCode}")
    public AjaxResult getEntityInfo(@PathVariable String socialCreditCode) {
        CcdiIntermediaryEntityDetailVO vo = intermediaryService.selectIntermediaryEntityDetail(socialCreditCode);
        return success(vo);
    }

    /**
     * 新增个人中介
     */
    @Operation(summary = "新增个人中介")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:add')")
    @Log(title = "个人中介", businessType = BusinessType.INSERT)
    @PostMapping("/person")
    public AjaxResult addPerson(@Validated @RequestBody CcdiIntermediaryPersonAddDTO addDTO) {
        return toAjax(intermediaryService.insertIntermediaryPerson(addDTO));
    }

    /**
     * 修改个人中介
     */
    @Operation(summary = "修改个人中介")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:edit')")
    @Log(title = "个人中介", businessType = BusinessType.UPDATE)
    @PutMapping("/person")
    public AjaxResult editPerson(@Validated @RequestBody CcdiIntermediaryPersonEditDTO editDTO) {
        return toAjax(intermediaryService.updateIntermediaryPerson(editDTO));
    }

    /**
     * 新增实体中介
     */
    @Operation(summary = "新增实体中介")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:add')")
    @Log(title = "实体中介", businessType = BusinessType.INSERT)
    @PostMapping("/entity")
    public AjaxResult addEntity(@Validated @RequestBody CcdiIntermediaryEntityAddDTO addDTO) {
        return toAjax(intermediaryService.insertIntermediaryEntity(addDTO));
    }

    /**
     * 修改实体中介
     */
    @Operation(summary = "修改实体中介")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:edit')")
    @Log(title = "实体中介", businessType = BusinessType.UPDATE)
    @PutMapping("/entity")
    public AjaxResult editEntity(@Validated @RequestBody CcdiIntermediaryEntityEditDTO editDTO) {
        return toAjax(intermediaryService.updateIntermediaryEntity(editDTO));
    }

    /**
     * 删除中介
     */
    @Operation(summary = "删除中介")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:remove')")
    @Log(title = "中介信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(intermediaryService.deleteIntermediaryByIds(ids));
    }

    /**
     * 校验人员ID唯一性
     */
    @Operation(summary = "校验人员ID唯一性")
    @GetMapping("/checkPersonIdUnique")
    public AjaxResult checkPersonIdUnique(@RequestParam String personId, @RequestParam(required = false) String bizId) {
        boolean unique = intermediaryService.checkPersonIdUnique(personId, bizId);
        return success(unique);
    }

    /**
     * 校验统一社会信用代码唯一性
     */
    @Operation(summary = "校验统一社会信用代码唯一性")
    @GetMapping("/checkSocialCreditCodeUnique")
    public AjaxResult checkSocialCreditCodeUnique(@RequestParam String socialCreditCode, @RequestParam(required = false) String excludeId) {
        boolean unique = intermediaryService.checkSocialCreditCodeUnique(socialCreditCode, excludeId);
        return success(unique);
    }

    /**
     * 下载个人中介导入模板
     */
    @Operation(summary = "下载个人中介导入模板")
    @PostMapping("/importPersonTemplate")
    public void importPersonTemplate(HttpServletResponse response) {
        EasyExcelUtil.importTemplateWithDictDropdown(response, CcdiIntermediaryPersonExcel.class, "个人中介信息");
    }

    /**
     * 下载实体中介导入模板
     */
    @Operation(summary = "下载实体中介导入模板")
    @PostMapping("/importEntityTemplate")
    public void importEntityTemplate(HttpServletResponse response) {
        EasyExcelUtil.importTemplateWithDictDropdown(response, CcdiIntermediaryEntityExcel.class, "实体中介信息");
    }

    /**
     * 导入个人中介数据
     */
    @Operation(summary = "导入个人中介数据")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:import')")
    @Log(title = "个人中介", businessType = BusinessType.IMPORT)
    @PostMapping("/importPersonData")
    public AjaxResult importPersonData(MultipartFile file, @RequestParam(defaultValue = "false") boolean updateSupport) throws Exception {
        List<CcdiIntermediaryPersonExcel> list = EasyExcelUtil.importExcel(file.getInputStream(), CcdiIntermediaryPersonExcel.class);
        String message = intermediaryService.importIntermediaryPerson(list, updateSupport);
        return success(message);
    }

    /**
     * 导入实体中介数据
     */
    @Operation(summary = "导入实体中介数据")
    @PreAuthorize("@ss.hasPermi('ccdi:intermediary:import')")
    @Log(title = "实体中介", businessType = BusinessType.IMPORT)
    @PostMapping("/importEntityData")
    public AjaxResult importEntityData(MultipartFile file, @RequestParam(defaultValue = "false") boolean updateSupport) throws Exception {
        List<CcdiIntermediaryEntityExcel> list = EasyExcelUtil.importExcel(file.getInputStream(), CcdiIntermediaryEntityExcel.class);
        String message = intermediaryService.importIntermediaryEntity(list, updateSupport);
        return success(message);
    }
}
