package com.ruoyi.web.controller.oa;

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

import com.ruoyi.oa.domain.CmcCar;
import com.ruoyi.oa.domain.CmcDevice;
import com.ruoyi.oa.service.ICmcCarService;
import com.ruoyi.oa.service.ICmcDeviceService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.oa.domain.CmcDeviceScrap;
import com.ruoyi.oa.service.ICmcDeviceScrapService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc设备报废Controller
 * 
 * @author cmc
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/oa/deviceScrap")
public class CmcDeviceScrapController extends BaseController
{
    @Autowired
    private ICmcDeviceScrapService cmcDeviceScrapService;

    @Autowired
    private ICmcDeviceService cmcDeviceService;

    @Autowired
    private ICmcCarService cmcCarService;

    /**
     * 查询cmc设备报废列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcDeviceScrap cmcDeviceScrap)
    {
        startPage();
        List<CmcDeviceScrap> list = cmcDeviceScrapService.selectCmcDeviceScrapList(cmcDeviceScrap);
        return getDataTable(list);
    }

    /**
     * 导出cmc设备报废列表
     */
    @Log(title = "cmc设备报废", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcDeviceScrap cmcDeviceScrap)
    {
        List<CmcDeviceScrap> list = cmcDeviceScrapService.selectCmcDeviceScrapList(cmcDeviceScrap);
        ExcelUtil<CmcDeviceScrap> util = new ExcelUtil<CmcDeviceScrap>(CmcDeviceScrap.class);
        util.exportExcel(response, list, "cmc设备报废数据");
    }

    /**
     * 获取cmc设备报废详细信息
     */
    @GetMapping(value = "/{deviceScrapId}")
    public AjaxResult getInfo(@PathVariable("deviceScrapId") Integer deviceScrapId)
    {
        return success(cmcDeviceScrapService.selectCmcDeviceScrapByDeviceScrapId(deviceScrapId));
    }

    /**
     * 新增cmc设备报废
     */
    @Log(title = "cmc设备报废", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcDeviceScrap cmcDeviceScrap)
    {
        if (cmcDeviceScrap.getDeviceId() != null && cmcDeviceScrap.getCarId() == null) {
            cmcDeviceScrap.setCarId(0);
            CmcDevice cmcDevice = cmcDeviceService.selectCmcDeviceByDeviceId(cmcDeviceScrap.getDeviceId());
            cmcDevice.setStatus("4");
            cmcDeviceService.updateCmcDevice(cmcDevice);
        }
        if (cmcDeviceScrap.getCarId() != null && cmcDeviceScrap.getDeviceId() == null) {
            cmcDeviceScrap.setDeviceId(0);
            CmcCar cmcCar = cmcCarService.selectCmcCarByCarId(cmcDeviceScrap.getCarId());
            cmcCar.setStatus("4");
            cmcCarService.updateCmcCar(cmcCar);
        }
        return toAjax(cmcDeviceScrapService.insertCmcDeviceScrap(cmcDeviceScrap));
    }

    /**
     * 修改cmc设备报废
     */
    @Log(title = "cmc设备报废", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcDeviceScrap cmcDeviceScrap)
    {
        return toAjax(cmcDeviceScrapService.updateCmcDeviceScrap(cmcDeviceScrap));
    }

    /**
     * 删除cmc设备报废
     */
    @Log(title = "cmc设备报废", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceScrapIds}")
    public AjaxResult remove(@PathVariable Integer[] deviceScrapIds)
    {
        return success(cmcDeviceScrapService.deleteCmcDeviceScrapByDeviceScrapIds(deviceScrapIds));
    }
}
