package com.bonus.sgzb.machine.controller;

import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.machine.damain.MaPropInfo;
import com.bonus.sgzb.machine.damain.MaType;
import com.bonus.sgzb.machine.service.IMaLabelBindService;
import com.bonus.sgzb.machine.vo.MaLabelBindVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 机具设备标签ma_label_bind(MaLabelBind)表控制层
 *
 * @author makejava
 * @since 2023-11-28 16:37:43
 */
@RestController
@RequestMapping("maLabelBind")
public class MaLabelBindController  extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private IMaLabelBindService maLabelBindService;

    /**
     * 查询标签绑定管理列表
     * @param typeName
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(String typeName)
    {
        startPage();
        List<MaLabelBindVO> list = maLabelBindService.selectMaLabelList(typeName);
        return getDataTable(list);
    }

    /**
     * 标签绑定管理列表数据导出
     * @param response
     * @param typeName
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String typeName)
    {
        List<MaLabelBindVO> list = maLabelBindService.selectMaLabelList(typeName);
        ExcelUtil<MaLabelBindVO> util = new ExcelUtil<MaLabelBindVO>(MaLabelBindVO.class);
        util.exportExcel(response, list, "标签绑定管理列表数据");
    }

    /**
     * 标签批量绑定
     * @param labelIds
     * @return
     */
    @PutMapping("/{labelIds}")
    public AjaxResult batchBind(@PathVariable Long[] labelIds)
    {
        return toAjax(maLabelBindService.batchBindByIds(labelIds));
    }

    /**
     * 标签解绑
     * @param labelId
     * @return
     */
    @PutMapping("/{labelId}")
    public AjaxResult batchNoBind(@PathVariable Long labelId)
    {
        return toAjax(maLabelBindService.batchNoBindByIds(labelId));
    }

    /**
     * 导入标签管理数据
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        String fileName = file.getOriginalFilename();
        if (fileName != null) {
            String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
            long fileSize = file.getSize();
            if (!fileExtension.equalsIgnoreCase("xls") && !fileExtension.equalsIgnoreCase("xlsx")) {
                // 文件后缀名不符合要求
                return error("文件后缀名不符合要求");
            } else if (fileSize > 10 * 1024 * 1024) {
                // 文件大小超过10M
                return error("文件大小超过10M");
            }
        }
        ExcelUtil<MaLabelBindVO> util = new ExcelUtil<MaLabelBindVO>(MaLabelBindVO.class);
        List<MaLabelBindVO> maLabelBindList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = maLabelBindService.importMaLabel(maLabelBindList, updateSupport, operName);
        return success(message);
    }

    /**
     * 修改标签管理数据
     * @param maLabelBindVO
     * @return
     */
    @PutMapping
    public AjaxResult update(@Validated @RequestBody MaLabelBindVO maLabelBindVO){
        maLabelBindService.updatetMaLabel(maLabelBindVO);
        return success();
    }
}

