package com.flossom.system.controller;

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

import com.flossom.common.core.domain.entity.SysUser;
import com.flossom.common.core.domain.entity.WxInstrumentSerial;
import com.flossom.common.core.domain.entity.WxUserInstrument;
import com.flossom.common.core.domain.req.WxInstrumentSerialExportVm;
import com.flossom.common.core.domain.req.WxInstrumentSerialImportVm;
import com.flossom.common.core.domain.req.WxUserInstrumentExportVm;
import com.flossom.common.core.exception.ServiceException;
import com.flossom.common.core.exception.UtilException;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import com.flossom.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.flossom.system.service.IWxInstrumentSerialService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 仪器序列号关联Controller
 *
 * @author flossom
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/instrumentSerial")
public class WxInstrumentSerialController extends BaseController
{
    @Autowired
    private IWxInstrumentSerialService wxInstrumentSerialService;

    /**
     * 查询仪器序列号关联列表
     */
    @RequiresPermissions("system:instrumentSerial:list")
    @GetMapping("/list")
    public TableDataInfo list(WxInstrumentSerial wxInstrumentSerial)
    {
        startPage();
        List<WxInstrumentSerial> list = wxInstrumentSerialService.selectWxInstrumentSerialList(wxInstrumentSerial);
        return getDataTable(list);
    }

    /**
     * 导出仪器序列号关联列表
     */
    @RequiresPermissions("system:instrumentSerial:export")
    @Log(title = "仪器序列号关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxInstrumentSerial wxInstrumentSerial)
    {
        List<WxInstrumentSerial> list = wxInstrumentSerialService.selectWxInstrumentSerialList(wxInstrumentSerial);
        ExcelUtil<WxInstrumentSerial> util = new ExcelUtil<WxInstrumentSerial>(WxInstrumentSerial.class);
        util.exportExcel(response, list, "仪器序列号关联数据");
    }

    /**
     * 获取仪器序列号关联详细信息
     */
    @RequiresPermissions("system:instrumentSerial:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wxInstrumentSerialService.selectWxInstrumentSerialById(id));
    }

    /**
     * 新增仪器序列号关联
     */
    @RequiresPermissions("system:instrumentSerial:add")
    @Log(title = "仪器序列号关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxInstrumentSerial wxInstrumentSerial)
    {
        return toAjax(wxInstrumentSerialService.insertWxInstrumentSerial(wxInstrumentSerial));
    }

    /**
     * 修改仪器序列号关联
     */
    @RequiresPermissions("system:instrumentSerial:edit")
    @Log(title = "仪器序列号关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxInstrumentSerial wxInstrumentSerial)
    {
        return toAjax(wxInstrumentSerialService.updateWxInstrumentSerial(wxInstrumentSerial));
    }


    /**
     * 修改仪器序列号关联
     */
    @RequiresPermissions("system:instrumentSerial:edit")
    @Log(title = "仪器序列号关联", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody WxInstrumentSerial wxInstrumentSerial)
    {
        return toAjax(wxInstrumentSerialService.updateRecordStatus(wxInstrumentSerial));
    }


    /**
     * 删除仪器序列号关联
     */
    @RequiresPermissions("system:instrumentSerial:remove")
    @Log(title = "仪器序列号关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wxInstrumentSerialService.deleteWxInstrumentSerialByIds(ids));
    }

    /**
     * 删除仪器序列号关联
     */
    @RequiresPermissions("system:instrumentSerial:remove")
    @Log(title = "仪器序列号关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeAll")
    public AjaxResult removeAll()
    {
        return toAjax(wxInstrumentSerialService.deleteAll());
    }


    /**
     * 批量导出列表
     */
    @Log(title = "批量导出", businessType = BusinessType.EXPORT)
    @PostMapping("/batchExport")
    public void batchExport(HttpServletResponse response, WxInstrumentSerial wxInstrumentSerial) {
        List<WxInstrumentSerialExportVm> wxInstrumentSerialExportVms = wxInstrumentSerialService.handleExportData(wxInstrumentSerial);
        ExcelUtil<WxInstrumentSerialExportVm> util = new ExcelUtil<WxInstrumentSerialExportVm>(WxInstrumentSerialExportVm.class);
        util.exportExcel(response, wxInstrumentSerialExportVms, "仪器序列号", wxInstrumentSerial.getExportFields());
    }
    /**
     * 仪器导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<WxInstrumentSerialImportVm> util = new ExcelUtil<WxInstrumentSerialImportVm>(WxInstrumentSerialImportVm.class);
        util.importTemplateExcel(response, "仪器序列号导入模板");
    }

    @Log(title = "仪器序列号", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        if (!(StringUtils.equals(suffix, ".xls") || StringUtils.equals(suffix, ".xlsx"))) {
            throw new ServiceException("请使用模板文件导入数据");
        }
        ExcelUtil<WxInstrumentSerialImportVm> util = new ExcelUtil<WxInstrumentSerialImportVm>(WxInstrumentSerialImportVm.class);
        List<WxInstrumentSerialImportVm> WxInstrumentSerialImportList = util.importExcel(file.getInputStream());
        if(WxInstrumentSerialImportList == null ||WxInstrumentSerialImportList.isEmpty() || WxInstrumentSerialImportList.get(0) == null) {
            throw new ServiceException("数据格式不正确，请检查模板数据");
        }

        String result = wxInstrumentSerialService.handleImportData(WxInstrumentSerialImportList);
        return success(result);
    }

}
