package com.bonus.sgzb.base.controller;

import com.bonus.sgzb.base.domain.BmUnitInfo;
import com.bonus.sgzb.base.service.BmUnitInfoService;
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.annotation.RequiresPermissions;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.system.api.domain.SysUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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;

/**
 * 基础管理  往来单位
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/bmUnitInfo")
public class BmUnitInfoController extends BaseController{

    @Autowired
    private BmUnitInfoService bmUnitInfoService;


    /**
     * 获取往来单位列表
     */
    @GetMapping("/unitInfoAll")
    public TableDataInfo getUnitInfoAll()
    {
        startPage();
        List<BmUnitInfo> list = bmUnitInfoService.getUnitInfoAll();
        return getDataTable(list);

    }

    /**
     * 根据条件进行查询往来单位
     */
    @PostMapping ("/getUnitInfo")
    public TableDataInfo getUnitInfo(@Validated @RequestBody BmUnitInfo bmUnitInfo)
    {
        startPage();
        List<BmUnitInfo> list = bmUnitInfoService.getUnitInfo(bmUnitInfo);
        return getDataTable(list);
    }


    /**
     * 新增往来单位
     */
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult unitInfoAdd(@Validated @RequestBody BmUnitInfo bmUnitInfo)
    {
        return toAjax(bmUnitInfoService.unitInfoAdd(bmUnitInfo));
    }

    /**
     * 批量删除往来单位
     */
   /* @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping ("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        bmUnitInfoService.remove(unitIds);
        return success();
    }*/
    /**
     * 批量删除往来单位
     */
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping ("/{unitId}")
    public AjaxResult deleteUnitInfoById(@PathVariable Long unitId)
    {
        bmUnitInfoService.deleteUnitInfoById(unitId);
        return success();
    }

    /**
     * 修改往来单位
     */
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BmUnitInfo bmUnitInfo)
    {

        return toAjax(bmUnitInfoService.updateBmUnitInfo(bmUnitInfo));
    }

    @Log(title = "往来单位导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:user:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmUnitInfo bmUnitInfo)
    {
        List<BmUnitInfo> list = bmUnitInfoService.getUnitInfo(bmUnitInfo);
        ExcelUtil<BmUnitInfo> util = new ExcelUtil<BmUnitInfo>(BmUnitInfo.class);
        util.exportExcel(response, list, "往来单位");
    }

    @Log(title = "往来单位导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:user:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BmUnitInfo> util = new ExcelUtil<BmUnitInfo>(BmUnitInfo.class);
        List<BmUnitInfo> bmUnitInfoList = util.importExcel(file.getInputStream());
        Long userId = SecurityUtils.getUserId();
        String message = bmUnitInfoService.importUser(bmUnitInfoList, updateSupport, userId);
        return success(message);
    }

}
