package com.bonus.sgzb.base.controller;

import com.bonus.sgzb.base.api.domain.MaMachine;
import com.bonus.sgzb.base.mapper.MaMachineMapper;
import com.bonus.sgzb.base.service.MaMachineService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 机具设备管理
 */
@RestController
@Api(tags = "机具设备管理")
@RequestMapping("/base/machine")
public class MaMachineController extends BaseController {

    @Autowired
    private MaMachineService maMachineService;

    @Autowired
    private MaMachineMapper maMachineMapper;

    @Log(title = "根据条件查询机具设备", businessType = BusinessType.QUERY)
    @GetMapping("/getMaMachineList")
    public AjaxResult getMaMachineList(MaMachine maMachine) {
        List<MaMachine> list = maMachineMapper.getMaMachineList(maMachine);
        return success(list);
    }

    @Log(title = "根据二维码查询机具设备", businessType = BusinessType.QUERY)
    @GetMapping("/getMachineByQrCode")
    public AjaxResult getMachineByQrCode(MaMachine maMachine) {
        MaMachine ma = maMachineService.getMachineByQrCode(maMachine);
        return success(ma);
    }


    /**
     * 根据条件进行查询机具设备管理
     */
    @ApiOperation(value = "根据条件进行查询机具设备管理")
    @GetMapping("/getMachine")
    public TableDataInfo getMaMachine(MaMachine maMachine) {
        startPage();
        List<MaMachine> list = maMachineService.getMaMachine(maMachine);
        return getDataTable(list);
    }

    /**
     * 导出查询机具设备管理
     * @param response
     * @param maMachine
     */
    @ApiOperation(value = "导出查询机具设备管理")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaMachine maMachine)
    {
        List<MaMachine> list = maMachineService.getMaMachine(maMachine);
        ExcelUtil<MaMachine> util = new ExcelUtil<>(MaMachine.class);
        util.exportExcel(response, list, "机具设备管理");
    }


    /**
     * 新增机具设备管理
     */
    @Log(title = "新增机具设备管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增机具设备管理")
    @PostMapping
    public AjaxResult maMachineAdd(@Validated @RequestBody MaMachine maMachine)
    {
        return toAjax(maMachineService.maMachineAdd(maMachine));
    }

    /**
     * 删除机具设备管理
     */
    @Log(title = "删除机具设备管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除机具设备管理")
    @DeleteMapping("/{maIds}")
    public AjaxResult remove(@PathVariable Long[] maIds)
    {
        maMachineService.remove(maIds);
        return success();
    }

    /**
     * 修改机具设备管理
     */
    @Log(title = "修改机具设备管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改机具设备管理")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody MaMachine maMachine)
    {

        return toAjax(maMachineService.updateMaMachine(maMachine));
    }

    /**
     * 获取机具设备管理ma_machine详细信息
     */
    @ApiOperation(value = "获取机具设备管理详细信息")
    @GetMapping(value = "/{maId}")
    public AjaxResult getInfo(@PathVariable("maId") Long maId)
    {
        return success(maMachineService.selectMaMachineByMaId(maId));
    }

    @Log(title = "把设备推送到租赁平台", businessType = BusinessType.QUERY)
    @ApiOperation(value = "把设备推送到租赁平台")
    @PostMapping("/pushNotifications")
    public AjaxResult pushNotifications(@RequestBody List<MaMachine>  maMachineList)
    {
        logger.info("MaMachineController pushNotifications 装备推送入口====");
        return maMachineService.pushNotifications(maMachineList);
    }

}
