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.MaHouse;
import com.bonus.sgzb.machine.damain.MaPropInfo;
import com.bonus.sgzb.machine.damain.MaType;
import com.bonus.sgzb.machine.service.IMaPropInfoService;
import com.bonus.sgzb.system.api.domain.SysUser;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资产管理ma_prop_info(MaPropInfo)表控制层
 *
 * @author makejava
 * @since 2023-11-28 10:18:05
 */
@RestController
@RequestMapping("maPropInfo")
public class MaPropInfoController  extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private IMaPropInfoService maPropInfoService;

    /**
     * 查询资产属性列表
     * @param propName
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(String propName)
    {
        startPage();
        List<MaPropInfo> list = maPropInfoService.selectMaPropInfoList(propName);
        return getDataTable(list);
    }

    /**
     * 新建资产属性
     * @param maPropInfo
     * @return
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaPropInfo maPropInfo){
        if (!maPropInfoService.checkPropNameUnique(maPropInfo)) {
            return error("新增资产名称'" + maPropInfo.getPropName() + "'失败，资产名称已存在");
        }
        maPropInfo.setCreateBy(SecurityUtils.getUsername());
        return toAjax(maPropInfoService.insertMaPropInfo(maPropInfo));
    }

    /**
     * 删除资产管理
     * @param propId
     * @return
     */
    @DeleteMapping("/{propId}")
    public AjaxResult delete(@PathVariable("propId") Long propId){
        return toAjax(maPropInfoService.deleteMaPropById(propId));
    }

    /**
     * 修改资产管理
     * @param maPropInfo
     * @return
     */
    @PutMapping
    public AjaxResult update(@Validated @RequestBody MaPropInfo maPropInfo){
        if (!maPropInfoService.checkPropNameUnique(maPropInfo)) {
            return error("新增资产名称'" + maPropInfo.getPropName() + "'失败，资产名称已存在");
        }
        maPropInfo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(maPropInfoService.updatetMaProp(maPropInfo));
    }

    /**
     * 资产属性导出
     * @param response
     * @param propName
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String propName)
    {
        List<MaPropInfo> list = maPropInfoService.selectMaPropInfoList(propName);
        ExcelUtil<MaPropInfo> util = new ExcelUtil<MaPropInfo>(MaPropInfo.class);
        util.exportExcel(response, list, "资产属性类型数据");
    }

    /**
     * 资产属性管理导入
     * @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<MaPropInfo> util = new ExcelUtil<MaPropInfo>(MaPropInfo.class);
        List<MaPropInfo> maPropInfoList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = maPropInfoService.importMaProp(maPropInfoList, updateSupport, operName);
        return success(message);
    }
}

