package com.hw.dms.controller;

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

import com.hw.dms.domain.DmsBaseDeviceLedger;
import com.hw.dms.service.IDmsBaseDeviceLedgerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.dms.domain.DmsBaseTransferInfo;
import com.hw.dms.service.IDmsBaseTransferInfoService;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.page.TableDataInfo;

/**
 * 设备转移信息Controller
 *
 * @author xins
 * @date 2024-01-02
 */
@RestController
@RequestMapping("/info")
public class DmsBaseTransferInfoController extends BaseController
{
    @Autowired
    private IDmsBaseTransferInfoService dmsBaseTransferInfoService;
    @Autowired
    private IDmsBaseDeviceLedgerService dmsBaseDeviceLedgerService;

    /**
     * 查询设备转移信息列表
     */
    @RequiresPermissions("dms:info:list")
    @GetMapping("/list")
    public TableDataInfo list(DmsBaseTransferInfo dmsBaseTransferInfo)
    {
        startPage();
        List<DmsBaseTransferInfo> list = dmsBaseTransferInfoService.selectDmsBaseTransferInfoList(dmsBaseTransferInfo);
        return getDataTable(list);
    }

    /**
     * 导出设备转移信息列表
     */
    @RequiresPermissions("dms:info:export")
    @Log(title = "设备转移信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsBaseTransferInfo dmsBaseTransferInfo)
    {
        List<DmsBaseTransferInfo> list = dmsBaseTransferInfoService.selectDmsBaseTransferInfoList(dmsBaseTransferInfo);
        ExcelUtil<DmsBaseTransferInfo> util = new ExcelUtil<DmsBaseTransferInfo>(DmsBaseTransferInfo.class);
        util.exportExcel(response, list, "设备转移信息数据");
    }

    /**
     * 获取设备转移信息详细信息
     */
    @RequiresPermissions("dms:info:query")
    @GetMapping(value = "/{transferInfoId}")
    public AjaxResult getInfo(@PathVariable("transferInfoId") Long transferInfoId)
    {
        return success(dmsBaseTransferInfoService.selectDmsBaseTransferInfoByTransferInfoId(transferInfoId));
    }

    /**
     * 新增设备转移信息
     */
    @RequiresPermissions("dms:info:add")
    @Log(title = "设备转移信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsBaseTransferInfo dmsBaseTransferInfo)
    {
        //转移时修改设备信息
        DmsBaseDeviceLedger dmsBaseDeviceLedger = new DmsBaseDeviceLedger();
        dmsBaseDeviceLedger.setDeviceId(dmsBaseTransferInfo.getDeviceId());
        dmsBaseDeviceLedger.setDeviceLocation(dmsBaseTransferInfo.getDevicePositionNew());
        dmsBaseDeviceLedgerService.updateDmsBaseDeviceLedger(dmsBaseDeviceLedger);
        return toAjax(dmsBaseTransferInfoService.insertDmsBaseTransferInfo(dmsBaseTransferInfo));
    }

    /**
     * 修改设备转移信息
     */
    @RequiresPermissions("dms:info:edit")
    @Log(title = "设备转移信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsBaseTransferInfo dmsBaseTransferInfo)
    {
        //转移时修改设备信息
        DmsBaseDeviceLedger dmsBaseDeviceLedger = new DmsBaseDeviceLedger();
        dmsBaseDeviceLedger.setDeviceId(dmsBaseTransferInfo.getDeviceId());
        dmsBaseDeviceLedger.setDeviceLocation(dmsBaseTransferInfo.getDevicePositionNew());
        dmsBaseDeviceLedgerService.updateDmsBaseDeviceLedger(dmsBaseDeviceLedger);
        return toAjax(dmsBaseTransferInfoService.updateDmsBaseTransferInfo(dmsBaseTransferInfo));
    }

    /**
     * 删除设备转移信息
     */
    @RequiresPermissions("dms:info:remove")
    @Log(title = "设备转移信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{transferInfoIds}")
    public AjaxResult remove(@PathVariable Long[] transferInfoIds)
    {
        return toAjax(dmsBaseTransferInfoService.deleteDmsBaseTransferInfoByTransferInfoIds(transferInfoIds));
    }
}
