package com.ruoyi.dpc.controller;

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.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dpc.domain.DpcIntermediaryBlacklist;
import com.ruoyi.dpc.domain.dto.DpcIntermediaryBlacklistAddDTO;
import com.ruoyi.dpc.domain.dto.DpcIntermediaryBlacklistEditDTO;
import com.ruoyi.dpc.domain.dto.DpcIntermediaryBlacklistQueryDTO;
import com.ruoyi.dpc.domain.vo.DpcIntermediaryBlacklistVO;
import com.ruoyi.dpc.service.IDpcIntermediaryBlacklistService;
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-01-27
 */
@RestController
@RequestMapping("/dpc/intermediary")
public class DpcIntermediaryBlacklistController extends BaseController {

    @Resource
    private IDpcIntermediaryBlacklistService intermediaryService;

    /**
     * 查询中介黑名单列表
     */
    @PreAuthorize("@ss.hasPermi('dpc:intermediary:list')")
    @GetMapping("/list")
    public TableDataInfo list(DpcIntermediaryBlacklistQueryDTO queryDTO) {
        startPage();
        List<DpcIntermediaryBlacklistVO> list = intermediaryService.selectIntermediaryList(queryDTO);
        return getDataTable(list);
    }

    /**
     * 导出中介黑名单列表
     */
    @PreAuthorize("@ss.hasPermi('dpc:intermediary:export')")
    @Log(title = "中介黑名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DpcIntermediaryBlacklistQueryDTO queryDTO) {
        List<DpcIntermediaryBlacklist> list = intermediaryService.selectIntermediaryListForExport(queryDTO);
        ExcelUtil<DpcIntermediaryBlacklist> util = new ExcelUtil<>(DpcIntermediaryBlacklist.class);
        util.exportExcel(response, list, "中介黑名单数据");
    }

    /**
     * 获取中介黑名单详细信息
     */
    @PreAuthorize("@ss.hasPermi('dpc:intermediary:query')")
    @GetMapping(value = "/{intermediaryId}")
    public AjaxResult getInfo(@PathVariable Long intermediaryId) {
        return success(intermediaryService.selectIntermediaryById(intermediaryId));
    }

    /**
     * 新增中介黑名单
     */
    @PreAuthorize("@ss.hasPermi('dpc:intermediary:add')")
    @Log(title = "中介黑名单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DpcIntermediaryBlacklistAddDTO addDTO) {
        return toAjax(intermediaryService.insertIntermediary(addDTO));
    }

    /**
     * 修改中介黑名单
     */
    @PreAuthorize("@ss.hasPermi('dpc:intermediary:edit')")
    @Log(title = "中介黑名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DpcIntermediaryBlacklistEditDTO editDTO) {
        return toAjax(intermediaryService.updateIntermediary(editDTO));
    }

    /**
     * 删除中介黑名单
     */
    @PreAuthorize("@ss.hasPermi('dpc:intermediary:remove')")
    @Log(title = "中介黑名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{intermediaryIds}")
    public AjaxResult remove(@PathVariable Long[] intermediaryIds) {
        return toAjax(intermediaryService.deleteIntermediaryByIds(intermediaryIds));
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<DpcIntermediaryBlacklist> util = new ExcelUtil<>(DpcIntermediaryBlacklist.class);
        util.importTemplateExcel(response, "中介黑名单数据");
    }

    /**
     * 导入中介黑名单
     */
    @PreAuthorize("@ss.hasPermi('dpc:intermediary:import')")
    @Log(title = "中介黑名单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<DpcIntermediaryBlacklist> util = new ExcelUtil<>(DpcIntermediaryBlacklist.class);
        List<DpcIntermediaryBlacklist> list = util.importExcel(file.getInputStream());
        List<DpcIntermediaryBlacklistAddDTO> addDTOList = convertToAddDTOList(list);
        String message = intermediaryService.importIntermediary(addDTOList, updateSupport);
        return success(message);
    }

    /**
     * 转换为AddDTO列表
     */
    private List<DpcIntermediaryBlacklistAddDTO> convertToAddDTOList(List<DpcIntermediaryBlacklist> list) {
        return list.stream().map(entity -> {
            DpcIntermediaryBlacklistAddDTO dto = new DpcIntermediaryBlacklistAddDTO();
            dto.setName(entity.getName());
            dto.setCertificateNo(entity.getCertificateNo());
            dto.setIntermediaryType(entity.getIntermediaryType());
            dto.setStatus(entity.getStatus());
            dto.setRemark(entity.getRemark());
            return dto;
        }).toList();
    }
}
