package com.bonus.material.supplier.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.supplier.domain.MaSupplier;
import com.bonus.material.supplier.service.MaSupplierService;
import com.bonus.material.ma.vo.TreeSelect;
import com.bonus.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

@Api(value = "厂家管理", tags = "厂家管理")
@RestController
@RequestMapping("/ma_supplier_info")
public class MaSupplierController extends BaseController {

    @Resource
    private MaSupplierService maSupplierService;

    @ApiOperation("分页列表")
    @GetMapping("/list")
    public AjaxResult list(MaSupplier query) {
        startPage();
        List<MaSupplier> list = maSupplierService.list(query);
        TableDataInfo dataTable = getDataTable(list);
        return AjaxResult.success(dataTable);
    }

    @ApiOperation("详情")
    @GetMapping("/{supplierId}")
    public AjaxResult detail(@PathVariable Long supplierId) {
        return AjaxResult.success(maSupplierService.getById(supplierId));
    }

    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@RequestBody MaSupplier bean) {
        return maSupplierService.add(bean);
    }

    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@RequestBody MaSupplier bean) {
        return maSupplierService.edit(bean);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{supplierId}")
    public AjaxResult del(@PathVariable Long supplierId) {
        int i = maSupplierService.remove(supplierId);
        return i > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @ApiOperation("厂家下拉框")
    @GetMapping("/select")
    public AjaxResult options(){
        List<TreeSelect> options = maSupplierService.listEnabled().stream().map(s -> {
            TreeSelect t = new TreeSelect();
            t.setId(s.getSupplierId());
            t.setLabel(s.getSupplierName());
            return t;
        }).collect(Collectors.toList());
        return AjaxResult.success(options);
    }


    @PostMapping("/export")
    @SysLog(title = "厂家管理", businessType = OperaType.EXPORT, logType = 0, module = "系统管理->厂家管理", details = "导出厂家信息")
    public void export(HttpServletResponse response, MaSupplier query) {
        try {
            List<MaSupplier> list = maSupplierService.list(query);
            ExcelUtil<MaSupplier> util = new ExcelUtil<MaSupplier>(MaSupplier.class);
            util.exportExcel(response,list,"厂家数据");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }
}


