package com.iailab.module.data.channel.kio.controller.admin;

import com.iailab.framework.apilog.core.annotation.ApiAccessLog;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.pojo.PageParam;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.util.object.BeanUtils;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.framework.excel.core.util.ExcelUtils;
import com.iailab.module.data.channel.kio.entity.ChannelKioTagEntity;
import com.iailab.module.data.channel.kio.service.ChannelKioTagService;
import com.iailab.module.data.channel.kio.vo.KioTagPageReqVO;
import com.iailab.module.data.channel.kio.vo.KioTagRespVO;
import com.iailab.module.data.channel.tag.vo.TagExportExcelVO;
import com.iailab.module.data.channel.tag.vo.TagImportExcelVO;
import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.iailab.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.iailab.framework.common.pojo.CommonResult.success;

/**

 */
@RestController
@RequestMapping("/data/channel/kio/tag")
public class ChannelKioTagController {
    @Resource
    private ChannelKioTagService channelKioTagService;

    /**
     * 分页查询tag
     * */
    @PreAuthorize("@ss.hasPermission('data:channel-kio:query')")
    @GetMapping("page")
    public CommonResult<PageResult<KioTagRespVO>> page(@Valid KioTagPageReqVO reqVO){
        PageResult<ChannelKioTagEntity> page = channelKioTagService.queryPage(reqVO);
        return success(BeanUtils.toBean(page, KioTagRespVO.class));
    }

    @PreAuthorize("@ss.hasPermission('data:channel-kio:query')")
    @GetMapping("/info/{id}")
    @Operation(summary = "信息")
    public CommonResult<ChannelKioTagEntity> info(@PathVariable("id") String id) {
        ChannelKioTagEntity info = channelKioTagService.info(id);
        return success(info);
    }

    @PreAuthorize("@ss.hasPermission('data:channel-kio:create')")
    @PostMapping("/create")
    public CommonResult<Boolean> create(@RequestBody ChannelKioTagEntity channelKioTagEntity) {
        String id = UUID.randomUUID().toString();
        channelKioTagEntity.setId(id);
        channelKioTagEntity.setCreateTime(new Date());
        channelKioTagService.add(channelKioTagEntity);
        return success(true);
    }

    @PreAuthorize("@ss.hasPermission('data:channel-kio:update')")
    @PutMapping("/update")
    public CommonResult<Boolean> update(@RequestBody ChannelKioTagEntity channelKioTagEntity) {
        channelKioTagEntity.setUpdateTime(new Date());
        channelKioTagService.update(channelKioTagEntity);
        return success(true);
    }

    @PreAuthorize("@ss.hasPermission('data:channel-kio:delete')")
    @DeleteMapping("/delete")
    public CommonResult<Boolean> delete(@RequestParam("id") String id) {
        channelKioTagService.delete(id);
        return success(true);
    }

    @GetMapping("/export")
    @Operation(summary = "导出modbus tag列表")
    @PreAuthorize("@ss.hasPermission('data:channel-kio-tag:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPointList(@Validated KioTagPageReqVO reqVO, HttpServletResponse response) throws IOException {
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<ChannelKioTagEntity> page = channelKioTagService.queryPage(reqVO);
        List<TagExportExcelVO> list = ConvertUtils.sourceToTarget(page.getList(), TagExportExcelVO.class);
        ExcelUtils.write(response, "tag列表.xls", "数据", TagExportExcelVO.class, list, true);
    }
    
    @GetMapping("/get-import-template")
    @Operation(summary = "获得tag导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<TagImportExcelVO> list = Collections.singletonList(
                TagImportExcelVO.builder().tagName("Tag名称").tagDesc("Tag描述").dataType("String").enabled(1)
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "tag导入模板.xls", "tag列表", TagImportExcelVO.class, list,true);
    }

    @PostMapping("/import")
    @Operation(summary = "导入tag")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('data:channel-kio-tag:import')")
    public CommonResult<TagImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                     @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
                                                     @RequestParam("device") String device) throws Exception {
        List<TagImportExcelVO> list = ExcelUtils.read(file, TagImportExcelVO.class);
        return success(channelKioTagService.importKioTagList(list, updateSupport, device));
    }
}