package com.bonus.sgzb.base.controller;

import com.bonus.sgzb.base.api.domain.SysDic;
import com.bonus.sgzb.base.service.ISysDicService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 数据字典Controller
 *
 * @author bonus
 * @date 2023-12-11
 */
@Api(tags = "数据字典Controller")
@RestController
@RequestMapping("/base/dic")
public class SysDicController extends BaseController
{
    @Autowired
    private ISysDicService sysDicService;

    /**
     * 查询数据字典列表
     */
    @ApiOperation(value = "查询数据字典列表")
    @GetMapping("/list")
    @RequiresPermissions("system:dict:query")
    public TableDataInfo list(SysDic sysDic)
    {
        startPage();
        List<SysDic> list = sysDicService.selectSysDicList(sysDic);
        return getDataTable(list);
    }
    /**
     * 查询数据字典列表id
     */
    @ApiOperation(value = "查询数据字典列表id")
    @GetMapping("/listId")
    public TableDataInfo listid(SysDic sysDic)
    {
        startPage();
        List<SysDic> list = sysDicService.selectSysDic(sysDic);
        return getDataTable(list);
    }
    /**
     * 查询父类数据字典列表
     */
    @ApiOperation(value = "查询父类数据字典列表")
    @GetMapping("/lists")
    public TableDataInfo lists(SysDic sysDic)
    {
        List<SysDic> list = sysDicService.selectSysDicLists(sysDic);
        return getDataTable(list);
    }

    /**
     * 导出数据字典列表
     */
    @ApiOperation(value = "导出数据字典列表")
    @Log(title = "数据字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDic sysDic)
    {
        List<SysDic> list;
        list = sysDicService.selectSysDicList(sysDic);
        //根据前端列表选中选择性列表导出
        if (CollectionUtils.isNotEmpty(sysDic.getDataCondition())) {
            list = list.stream().filter(Objects::nonNull).
                    filter(u -> sysDic.getDataCondition().contains(u.getId())).collect(Collectors.toList());
        }
        ExcelUtil<SysDic> util = new ExcelUtil<SysDic>(SysDic.class);
        util.exportExcel(response, list, "数据字典数据");
    }

    /**
     * 获取数据字典详细信息
     */
    @ApiOperation(value = "获取数据字典详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysDicService.selectSysDicById(id));
    }

    /**
     * 获取费用承担方列表
     */
    @ApiOperation(value = "获取费用承担方列表")
    @GetMapping(value = "/pid")
    public AjaxResult getBear()
    {
        List<String> bear = sysDicService.getBear();
        return success(bear);
    }

    /**
     * 新增数据字典
     */
    @ApiOperation(value = "新增数据字典")
    @Log(title = "数据字典", businessType = BusinessType.INSERT)
    @PostMapping
    @RequiresPermissions("system:dict:add")
    public AjaxResult add(@RequestBody SysDic sysDic)
    {
        return sysDicService.insertSysDic(sysDic);
    }

    /**
     * 修改数据字典
     */
    @ApiOperation(value = "修改数据字典")
    @Log(title = "数据字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDic sysDic)
    {
        return sysDicService.updateSysDic(sysDic);
    }


    @ApiOperation("修改单位状态")
    @Log(title = "单位类型状态（同数据字典合并）", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult update(SysDic sysDic)
    {
        return toAjax(sysDicService.update(sysDic));
    }

    /**
     * 删除数据字典
     */
    @ApiOperation(value = "删除数据字典")
    @Log(title = "数据字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDicService.deleteSysDicByIds(ids));
    }
}
