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.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
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;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 基础管理  往来单位
 *
 * @author gmh
 */
@Api(tags = "往来单位")
@RestController
@RequestMapping("/base/bmUnitInfo")
@Slf4j
public class BmUnitInfoController extends BaseController{

    @Autowired
    private BmUnitInfoService bmUnitInfoService;


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

    }
    /**
     * 查询单位类型配置表bm_unit_type（同数据字典合并）列表
     */
    @ApiOperation("查询单位类型")
    @GetMapping("/typeList")
    public TableDataInfo typeList(BmUnitInfo bmUnitInfo)
    {
        startPage();
        List<BmUnitInfo> list = bmUnitInfoService.selectTypeList(bmUnitInfo);
        return getDataTable(list);
    }
    /**
     * 查询单位类型配置表bm_unit_type（同数据字典合并）列表
     */
    @ApiOperation("查询单位类型")
    @GetMapping("/userList")
    public TableDataInfo UserAll(SysUser sysUser)
    {
        startPage();
        List<SysUser> list = bmUnitInfoService.getUserAll(sysUser);
        return getDataTable(list);
    }

    /**
     * 获取往来单位下拉选
     */
    @ApiOperation(value = "获取往来单位下拉选")
    @GetMapping("/getUnitInfoSelect")
    public AjaxResult getUnitInfoSelect(BmUnitInfo bmUnitInfo)
    {
        List<BmUnitInfo> list = bmUnitInfoService.getUnitInfoAll(bmUnitInfo);
        return AjaxResult.success(list);

    }


    /**
     * 根据条件进行查询往来单位
     */
    @ApiOperation(value = "根据条件进行查询往来单位")
    @PostMapping ("/getUnitInfo")
    public TableDataInfo getUnitInfo(@Validated @RequestBody BmUnitInfo bmUnitInfo)
    {
        startPage();
        List<BmUnitInfo> list = bmUnitInfoService.getUnitInfo(bmUnitInfo);
        return getDataTable(list);
    }
    /**
     * 新增往来单位
     */
    @ApiOperation(value = "新增往来单位")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RequiresPermissions("comeAndGo:list:add")
    public AjaxResult unitInfoAdd(@Validated @RequestBody BmUnitInfo bmUnitInfo)
    {
        log.info("新增往来单位参数：{}", bmUnitInfo);
        return bmUnitInfoService.unitInfoAdd(bmUnitInfo);
    }

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

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

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

        return bmUnitInfoService.updateBmUnitInfo(bmUnitInfo);
    }
    @ApiOperation(value = "往来单位导出")
    @Log(title = "往来单位导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:user:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmUnitInfo bmUnitInfo)
    {
        List<BmUnitInfo> list;
        list = bmUnitInfoService.getUnitInfoAll(bmUnitInfo);
        //根据前端列表选中选择性列表导出
        if (CollectionUtils.isNotEmpty(bmUnitInfo.getDataCondition())) {
            list = list.stream().filter(Objects::nonNull).
                    filter(u -> bmUnitInfo.getDataCondition().contains(u.getUnitId())).collect(Collectors.toList());
        }
        ExcelUtil<BmUnitInfo> util = new ExcelUtil<BmUnitInfo>(BmUnitInfo.class);
        util.exportExcel(response, list, "往来单位");
    }
/*    @ApiOperation(value = "往来单位导入")
    @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.getLoginUser().getUserid();
        String message = bmUnitInfoService.importUser(bmUnitInfoList, updateSupport, userId);
        return success(message);
    }*/

}
