package com.bonus.material.codeCollection.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.utils.SecurityUtils;
import com.bonus.material.codeCollection.domain.WsMaInfo;
import com.bonus.material.codeCollection.service.WsMaInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 机具信息控制器
 * 提供机具信息的 RESTful API 接口，实现增删改查功能
 */
@RestController
@RequestMapping("/wsMaInfo")
public class WsMaInfoController extends BaseController {

    @Resource
    private WsMaInfoService service;

    @ApiOperation(value = "根据ID获取机具信息")
    @SysLog(title = "机具信息查询", businessType = OperaType.QUERY, logType = 1, module = "机具管理->查询")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        return service.getById(id);
    }

    @ApiOperation(value = "查询所有机具信息列表")
    @SysLog(title = "机具信息列表查询", businessType = OperaType.QUERY, logType = 1, module = "机具管理->查询")
    @GetMapping("/list")
    public AjaxResult getAll(WsMaInfo info) {
        List<WsMaInfo> list = service.getAll(info);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "新增机具信息")
    @SysLog(title = "机具信息新增", businessType = OperaType.INSERT, logType = 1, module = "机具管理->新增")
    @PostMapping("/addWsMaInfo")
    public AjaxResult save(@RequestBody WsMaInfo info) {
        info.setOptUser(SecurityUtils.getLoginUser().getSysUser().getNickName());

        return service.save(info);
    }

    @ApiOperation(value = "更新机具信息")
    @SysLog(title = "机具信息更新", businessType = OperaType.UPDATE, logType = 1, module = "机具管理->更新")
    @PostMapping("/updateWsMaInfo")
    public AjaxResult update(@RequestBody WsMaInfo info) {
        return service.update(info);
    }

    @ApiOperation(value = "删除机具信息")
    @SysLog(title = "机具信息删除", businessType = OperaType.DELETE, logType = 1, module = "机具管理->删除")
    @PostMapping("/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        return service.delete(id);
    }

    @ApiOperation(value = "获取机具类型")
    @SysLog(title = "获取机具类型", businessType = OperaType.DELETE, logType = 1, module = "获取机具类型")
    @PostMapping("/getMaTypeData")
    public AjaxResult getMaTypeData() {
        return service.getMaTypeData();
    }


    @ApiOperation(value = "获取机具规格")
    @SysLog(title = "获取机具规格", businessType = OperaType.DELETE, logType = 1, module = "获取机具规格")
    @PostMapping("/getMaModeData")
    public AjaxResult getMaModeData(@RequestBody WsMaInfo info) {
        return service.getMaModeData(info.getParentId());
    }

    @ApiOperation(value = "获取出厂厂家")
    @SysLog(title = "获取出厂厂家", businessType = OperaType.DELETE, logType = 1, module = "获取出厂厂家")
    @PostMapping("/getSupplier")
    public AjaxResult getSupplier() {
        return service.getSupplier();
    }


    @ApiOperation(value = "获取出厂厂家")
    @SysLog(title = "获取出厂厂家", businessType = OperaType.DELETE, logType = 1, module = "获取出厂厂家")
    @PostMapping("/updateCheckTime")
    public AjaxResult updateCheckTime(@RequestBody WsMaInfo info) {
        return service.updateCheckTime(info);
    }




    @ApiOperation(value = "查询小工具编码信息列表")
    @GetMapping("/getGadgetList")
    public TableDataInfo getGadgetList(WsMaInfo info) {
        startPage();
        List<WsMaInfo> list = service.getGadgetList(info);
        return getDataTable(list);
    }


    /**
     * 导出小工具编码信息列表
     */
    @ApiOperation(value = "导出小工具编码信息列表")
    @SysLog(title = "小工具编码信息管理", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出小工具编码信息列表")
    @PostMapping("/exportGadgetList")
    public void exportGadgetList(HttpServletResponse response, WsMaInfo info) {
        List<WsMaInfo> list = service.getGadgetList(info);
        ExcelUtil<WsMaInfo> util = new ExcelUtil<>(WsMaInfo.class);
        util.exportExcel(response, list, "领料出库数据");
    }

    @ApiOperation(value = "新增小工具编码信息")
    @SysLog(title = "新增小工具编码信息", businessType = OperaType.INSERT, logType = 1, module = "小工具编码信息管理->新增")
    @PostMapping("/addWsMaInfoData")
    public AjaxResult addWsMaInfoData(@RequestBody WsMaInfo info) {
        return service.addWsMaInfoData(info);
    }

    @ApiOperation(value = "删除小工具编码信息")
    @SysLog(title = "删除小工具编码信息", businessType = OperaType.DELETE, logType = 1, module = "小工具编码信息管理->删除")
    @PostMapping("/delGadget")
    public AjaxResult delGadget(@RequestBody WsMaInfo info) {
        return service.delGadget(info);
    }

    @ApiOperation(value = "根据ID获取小工具信息")
    @SysLog(title = "机具信息查询", businessType = OperaType.QUERY, logType = 1, module = "小工具编码信息管理->查询")
    @GetMapping("/getGadgetInfo/{id}")
    public AjaxResult getGadgetInfo(@PathVariable Integer id) {
        return service.getGadgetInfo(id);
    }

    @ApiOperation(value = "更新小工具信息")
    @SysLog(title = "更新小工具信息", businessType = OperaType.UPDATE, logType = 1, module = "小工具编码信息管理->更新")
    @PostMapping("/updateGadgetInfo")
    public AjaxResult updateGadgetInfo(@RequestBody WsMaInfo info) {
        return service.updateGadgetInfo(info);
    }

    /**
     * 导入小工具信息模板
     */
    @ApiOperation(value = "小工具导入模版下载")
    @PostMapping("/downLoad")
    public void downLoadExcelFile(){
        HttpServletResponse resp = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
        service.downLoadTemplate(resp);
    }

    /**
     * 小工具信息导入
     * @param file
     * @return
     */
    @ApiOperation(value = "小工具信息导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        return service.importTbData(file);
    }
}
