package com.bonus.sgzb.base.controller;

import com.bonus.sgzb.base.api.domain.MaType;
import com.bonus.sgzb.base.domain.MaPropInfo;
import com.bonus.sgzb.base.domain.MaPropSet;
import com.bonus.sgzb.base.service.IMaPropInfoService;
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.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.servlet.http.HttpServletResponse;
import java.util.List;

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


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

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

    /**
     * 根据类型名称查询类型
     *
     * @return 结果
     */
    @ApiOperation(value = "根据类型名称查询类型")
    @GetMapping("/getMaTypeList")
    public TableDataInfo getMaTypeList(MaPropInfo maPropInfo) {
        startPage();
        List<MaType> maTypeList = maPropInfoService.selectMaType(maPropInfo);
        return getDataTable(maTypeList);
    }

    /**
     * 资产属性配置保存
     *
     * @param maPropSet
     * @return
     */
    @ApiOperation(value = "资产属性配置保存")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody MaPropSet maPropSet) {
//        Long fieldValue =maPropSet.getTypeId();
//        Long fieldValues =maPropSet.getPropId();
//        if (maPropSet.getPropName() != null){
//            return toAjax(maPropInfoService.updateMaPropSet(maPropSet));
//        }
//        return toAjax(maPropInfoService.insertMaPropSet(maPropSet));
        return toAjax(maPropInfoService.save(maPropSet));

    }

    /**
     * 新建资产属性
     *
     * @param maPropInfo
     * @return
     */
    @ApiOperation(value = "新建资产属性")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaPropInfo maPropInfo) {
        if (!maPropInfoService.checkPropNameUnique(maPropInfo)) {
            return error("新增资产名称'" + maPropInfo.getPropName() + "'失败，资产名称已存在");
        }
        maPropInfo.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return toAjax(maPropInfoService.insertMaPropInfo(maPropInfo));
    }

    /**
     * 删除资产管理
     *
     * @param propId
     * @return
     */
    @ApiOperation(value = "删除资产管理")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{propId}")
    public AjaxResult delete(@PathVariable("propId") Long propId) {
        return toAjax(maPropInfoService.deleteMaPropById(propId));
    }

    /**
     * 修改资产管理
     *
     * @param maPropInfo
     * @return
     */
    @ApiOperation(value = "修改资产管理")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @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 maPropInfo
     */
    @ApiOperation(value = "资产属性管理导出")
    @Log(title = "资产属性管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaPropInfo maPropInfo) {
        List<MaPropInfo> list = maPropInfoService.selectMaPropInfoList(maPropInfo);
        ExcelUtil<MaPropInfo> util = new ExcelUtil<MaPropInfo>(MaPropInfo.class);
        util.exportExcel(response, list, "资产属性管理数据");
    }

    /**
     * 资产属性配置导出
     *
     * @param response
     * @param maPropInfo
     */
    @ApiOperation(value = "资产属性配置导出")
    @Log(title = "资产属性配置导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportConfig")
    public void exportConfig(HttpServletResponse response, MaPropInfo maPropInfo) {
        List<MaType> list = maPropInfoService.selectMaType(maPropInfo);
        ExcelUtil<MaType> util = new ExcelUtil<MaType>(MaType.class);
        util.exportExcel(response, list, "资产属性配置数据");
    }

    /**
     * 资产属性管理导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "资产属性管理导入")
    @Log(title = "资产属性管理导入", businessType = BusinessType.IMPORT)
    @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);
    }
}

