package com.xinda.data.controller;

import com.xinda.common.annotation.Log;
import com.xinda.common.core.controller.BaseController;
import com.xinda.common.core.domain.AjaxResult;
import com.xinda.common.core.page.TableDataInfo;
import com.xinda.common.enums.BusinessType;
import com.xinda.common.utils.poi.ExcelUtil;
import com.xinda.iot.domain.ThingsModelJsonTemplate;
import com.xinda.iot.domain.ThingsModelStandard;
import com.xinda.iot.service.IThingsModelStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 通用物模型Controller
 *
 * @author kerwincui
 * @date 2021-12-16
 */
@RestController
@RequestMapping("/iot/modelStandard")
@Api(tags = "通用物模型")
public class ThingsModelStandardController extends BaseController {
    @Autowired
    private IThingsModelStandardService thingsModelStandardService;

    /**
     * 查询通用物模型列表
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:list')")
    @GetMapping("/list")
    @ApiOperation("通用物模型分页列表")
    public TableDataInfo list(ThingsModelStandard thingsModelStandard) {
        startPage();
        return getDataTable(thingsModelStandardService.selectThingsModelStandardList(thingsModelStandard));
    }

    /**
     * 导出通用物模型列表
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:export')")
    @Log(title = "通用物模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出通用物模型")
    public void export(HttpServletResponse response, ThingsModelStandard thingsModelStandard) {
        List<ThingsModelStandard> list = thingsModelStandardService.selectThingsModelStandardList(thingsModelStandard);
        ExcelUtil<ThingsModelStandard> util = new ExcelUtil<ThingsModelStandard>(ThingsModelStandard.class);
        util.exportExcel(response, list, "通用物模型数据");
    }

    /**
     * 获取通用物模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:query')")
    @GetMapping(value = "/{templateId}")
    @ApiOperation("获取通用物模型详情")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId) {
        return AjaxResult.success(thingsModelStandardService.selectThingsModelStandardByTemplateId(templateId));
    }

    /**
     * 新增通用物模型
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:add')")
    @Log(title = "通用物模型", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加通用物模型")
    public AjaxResult add(@RequestBody ThingsModelStandard thingsModelStandard) {
        return toAjax(thingsModelStandardService.insertThingsModelStandard(thingsModelStandard));
    }

    /**
     * 修改通用物模型
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:edit')")
    @Log(title = "通用物模型", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改通用物模型")
    public AjaxResult edit(@RequestBody ThingsModelStandard thingsModelStandard) {
        return toAjax(thingsModelStandardService.updateThingsModelStandard(thingsModelStandard));
    }

    /**
     * 删除通用物模型
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:remove')")
    @Log(title = "通用物模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    @ApiOperation("批量删除通用物模型")
    public AjaxResult remove(@PathVariable Long[] templateIds) {
        return toAjax(thingsModelStandardService.deleteThingsModelStandardByTemplateIds(templateIds));
    }

    @ApiOperation(value = "物模型导入模板")
    @PostMapping("/temp")
    public void temp(HttpServletResponse response) {
        ExcelUtil<ThingsModelStandard> excelUtil = new ExcelUtil<>(ThingsModelStandard.class);
        excelUtil.importTemplateExcel(response, "采集点");
    }

    @ApiOperation(value = "物模型导入模板")
    @RequestMapping(value = "/temp-json",method = RequestMethod.POST)
    public void tempJson(HttpServletResponse response) {
        ExcelUtil<ThingsModelJsonTemplate> excelUtil = new ExcelUtil<>(ThingsModelJsonTemplate.class);
        excelUtil.importTemplateExcel(response, "JSON采集点模板");
    }


    /**
     * 导入采集点
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:add')")
    @ApiOperation(value = "采集点导入")
    @PostMapping(value = "/importData")
    public AjaxResult importData(MultipartFile file, String tempSlaveId) throws Exception {
        ExcelUtil<ThingsModelStandard> excelUtil = new ExcelUtil<>(ThingsModelStandard.class);
        List<ThingsModelStandard> list = excelUtil.importExcel(file.getInputStream());
        String result = thingsModelStandardService.importData(list, tempSlaveId);
        return AjaxResult.success(result);
    }

    @ApiOperation("导出采集点")
    @PreAuthorize("@ss.hasPermi('iot:standard:query')")
    @PostMapping("/exportJson")
    public void exportJson(HttpServletResponse response, ThingsModelStandard template)
    {
        List<ThingsModelStandard> thingsModelStandards = thingsModelStandardService.selectThingsModelStandardExport(template);
        ExcelUtil<ThingsModelStandard> util = new ExcelUtil<ThingsModelStandard>(ThingsModelStandard.class);
        util.exportExcel(response, thingsModelStandards, "采集点");
    }

}
