package com.bonus.material.owner.controller;


import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.contract.domain.BmContract;
import com.bonus.material.device.domain.dto.DevInfoImpDto;
import com.bonus.material.owner.domain.Ownerdomin;
import com.bonus.material.owner.service.OwnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author：lizhenhua
 * @Date：2025/6/26- 9:14
 */
@RestController
@RequestMapping("/owner")
@Api(value = "自有装备管理", tags = "自有装备管理")
public class OwnerController extends BaseController {
    @Resource
    private OwnerService ownerService;

    @ApiOperation(value = "自有装备管理列表")
    @GetMapping("/list")
    public AjaxResult list(Ownerdomin ownerdomin) {
        startPage();
        List<Ownerdomin> list = ownerService.list(ownerdomin);
        return AjaxResult.success(getDataTable(list));
    }

    @ApiOperation(value = "自有装备管理新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Ownerdomin ownerdomin) throws UnsupportedEncodingException {
        if(ownerdomin.getMaNameId() != null){
            ownerdomin.setMaName(ownerdomin.getMaNameId());
        }
        Integer i = ownerService.add(ownerdomin);
        if (i > 0){
            return AjaxResult.success("新增成功");
        }else {
            return AjaxResult.error("新增失败");
        }
    }

    @ApiOperation(value = "自有装备管理修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Ownerdomin ownerdomin) throws UnsupportedEncodingException {
        Integer i = ownerService.edit(ownerdomin);
        if (i > 0){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }
    @ApiOperation(value = "自有装备管理删除")
    @PostMapping("/del")
    public AjaxResult del(@RequestBody Ownerdomin ownerdomin) {
        Integer i = ownerService.del(ownerdomin);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }
    @ApiOperation(value = "装备批量录入")
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file,
                                  String  type) throws Exception {
        // 1. 空文件判断
        if (file == null || file.isEmpty()) {
            return AjaxResult.error("上传的文件不能为空！");
        }

        // 2. 文件名及格式校验
        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 AjaxResult.error("文件后缀名必须为 xls 或 xlsx");
            } else if (fileSize > 10 * 1024 * 1024) {
                return AjaxResult.error("文件大小不能超过 10MB");
            }
        }

        // 3. 读取文件内容并处理
        ExcelUtil<Ownerdomin> util = new ExcelUtil<>(Ownerdomin.class);
        List<Ownerdomin> maPropInfoList = util.importExcel(file.getInputStream());

        if (maPropInfoList == null || maPropInfoList.isEmpty()) {
            return AjaxResult.error("导入的数据不能为空！");
        }

        Long userId = SecurityUtils.getLoginUser().getUserid();
        String message = ownerService.importMaProp(maPropInfoList, type, userId);

        return AjaxResult.success(message);
    }

}
