package com.xzzn.web.controller.ems;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xzzn.common.annotation.Log;
import com.xzzn.common.core.controller.BaseController;
import com.xzzn.common.core.domain.AjaxResult;
import com.xzzn.common.enums.BusinessType;
import com.xzzn.ems.domain.EmsPointMatch;
import com.xzzn.ems.domain.vo.DevicePointMatchExportVo;
import com.xzzn.ems.domain.vo.DevicePointMatchVo;
import com.xzzn.ems.domain.vo.ImportPointDataRequest;
import com.xzzn.ems.domain.vo.ImportPointTemplateRequest;
import com.xzzn.ems.service.IEmsPointMatchService;
import com.xzzn.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 点位匹配Controller
 *
 * @author xzzn
 * @date 2025-11-04
 */
@RestController
@RequestMapping("/ems/pointMatch")
public class EmsPointMatchController extends BaseController
{
    @Autowired
    private IEmsPointMatchService emsPointMatchService;

    /**
     * 导出点位匹配列表
     */
    @PreAuthorize("@ss.hasPermi('system:match:export')")
    @Log(title = "点位匹配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsPointMatch emsPointMatch)
    {
        List<DevicePointMatchExportVo> list = emsPointMatchService.selectEmsPointMatchList(emsPointMatch);
        ExcelUtil<DevicePointMatchExportVo> util = new ExcelUtil<>(DevicePointMatchExportVo.class);
        util.exportExcel(response, list, "点位匹配数据");
    }

    /**
     * 查询点位配置列表
     */
    @GetMapping("/list")
    public com.xzzn.common.core.page.TableDataInfo list(EmsPointMatch emsPointMatch)
    {
        startPage();
        List<EmsPointMatch> list = emsPointMatchService.selectPointMatchConfigList(emsPointMatch);
        return getDataTable(list);
    }

    /**
     * 查询点位配置详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(emsPointMatchService.selectPointMatchById(id));
    }

    /**
     * 新增点位配置
     */
    @Log(title = "点位配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsPointMatch emsPointMatch)
    {
        emsPointMatch.setCreateBy(getUsername());
        return toAjax(emsPointMatchService.insertPointMatch(emsPointMatch));
    }

    /**
     * 修改点位配置
     */
    @Log(title = "点位配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsPointMatch emsPointMatch)
    {
        emsPointMatch.setUpdateBy(getUsername());
        return toAjax(emsPointMatchService.updatePointMatch(emsPointMatch));
    }

    /**
     * 删除点位配置
     */
    @Log(title = "点位配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emsPointMatchService.deletePointMatchByIds(ids));
    }

    /**
     * 上传点位清单
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @Log(title = "点位匹配", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EmsPointMatch> util = new ExcelUtil<EmsPointMatch>(EmsPointMatch.class);
        List<EmsPointMatch> pointMatcheList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = emsPointMatchService.importPoint(pointMatcheList, updateSupport, operName);
        return success(message);
    }

    /**
     * 上传设备的点位清单
     * @param request
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @Log(title = "点位匹配", businessType = BusinessType.IMPORT)
    @PostMapping("/importDataByDevice")
    public void importDataByDevice(@Valid ImportPointDataRequest request, HttpServletResponse response) {
        List<DevicePointMatchVo> list = emsPointMatchService.importDataByDevice(request, getUsername());
        if (CollectionUtils.isNotEmpty(list)) {
            ExcelUtil<DevicePointMatchVo> util = new ExcelUtil<>(DevicePointMatchVo.class);
            util.exportExcel(response, list, "点位匹配数据");
        }
    }

    /**
     * 根据站点导入模板点位配置
     * @param request 请求参数
     * @return 导入结果
     */
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @Log(title = "点位配置", businessType = BusinessType.IMPORT)
    @PostMapping("/importTemplateBySite")
    public AjaxResult importTemplateBySite(@Valid @RequestBody ImportPointTemplateRequest request)
    {
        String message = emsPointMatchService.importTemplateBySite(request, getUsername());
        return success(message);
    }

}
