package com.ruoyi.web.controller.warehouse.check;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.warehouse.check.domain.TWarehouseCheckItems;
import com.ruoyi.warehouse.check.service.ITWarehouseCheckItemsService;
import org.springframework.security.access.prepost.PreAuthorize;
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.warehouse.check.domain.TWarehouseCheckItemsDetails;
import com.ruoyi.warehouse.check.service.ITWarehouseCheckItemsDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 巡检check-items-detailsController
 * 
 * @author dongyongwei
 * @date 2021-07-21
 */
@RestController
@RequestMapping("/warehouse/checkitemsdetails")
public class TWarehouseCheckItemsDetailsController extends BaseController
{
    @Autowired
    private ITWarehouseCheckItemsDetailsService tWarehouseCheckItemsDetailsService;

    @Autowired
    private ITWarehouseCheckItemsService tWarehouseCheckItemsService;

    /**
     * 查询巡检check-items-details列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitemsdetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseCheckItemsDetails tWarehouseCheckItemsDetails)
    {
        startPage();
        List<TWarehouseCheckItemsDetails> list = tWarehouseCheckItemsDetailsService.selectTWarehouseCheckItemsDetailsList(tWarehouseCheckItemsDetails);
        return getDataTable(list);
    }

    /**
     * 导出巡检check-items-details列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitemsdetails:export')")
    @Log(title = "巡检check-items-details", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWarehouseCheckItemsDetails tWarehouseCheckItemsDetails)
    {
        List<TWarehouseCheckItemsDetails> list = tWarehouseCheckItemsDetailsService.selectTWarehouseCheckItemsDetailsList(tWarehouseCheckItemsDetails);
        ExcelUtil<TWarehouseCheckItemsDetails> util = new ExcelUtil<TWarehouseCheckItemsDetails>(TWarehouseCheckItemsDetails.class);
        return util.exportExcel(list, "checkitemsdetails");
    }

    /**
     * 获取巡检check-items-details详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitemsdetails:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return AjaxResult.success(tWarehouseCheckItemsDetailsService.selectTWarehouseCheckItemsDetailsById(fId));
    }

    /**
     * 新增巡检check-items-details
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitemsdetails:add')")
    @Log(title = "巡检check-items-details", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWarehouseCheckItemsDetails tWarehouseCheckItemsDetails)
    {
        int i = tWarehouseCheckItemsDetailsService.insertTWarehouseCheckItemsDetails(tWarehouseCheckItemsDetails);
        if(i>0){
            //新增成功的情况下
            //增加从表回写最后巡检人和最后巡检时间逻辑
            //TODO
            TWarehouseCheckItems tWarehouseCheckItems = tWarehouseCheckItemsService.selectTWarehouseCheckItemsById(tWarehouseCheckItemsDetails.getfItemFid());
            tWarehouseCheckItems.setfInspectorIdLast(SecurityUtils.getLoginUser().getUser().getUserId());
            tWarehouseCheckItems.setfInspectionDatetimeLast(new Date());
            tWarehouseCheckItemsService.updateTWarehouseCheckItems(tWarehouseCheckItems);
        }
        return toAjax(i);
    }

    /**
     * 修改巡检check-items-details
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitemsdetails:edit')")
    @Log(title = "巡检check-items-details", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWarehouseCheckItemsDetails tWarehouseCheckItemsDetails)
    {
        return toAjax(tWarehouseCheckItemsDetailsService.updateTWarehouseCheckItemsDetails(tWarehouseCheckItemsDetails));
    }

    /**
     * 删除巡检check-items-details
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitemsdetails:remove')")
    @Log(title = "巡检check-items-details", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return toAjax(tWarehouseCheckItemsDetailsService.deleteTWarehouseCheckItemsDetailsByIds(fIds));
    }
    /**
     * APP查询巡检check-items-details列表
     */
    @GetMapping("/appList")
    public TableDataInfo appList(TWarehouseCheckItemsDetails tWarehouseCheckItemsDetails)
    {
        startPage();
        List<TWarehouseCheckItemsDetails> list = tWarehouseCheckItemsDetailsService.selectTWarehouseCheckItemsDetailsList(tWarehouseCheckItemsDetails);
        return getDataTable(list);
    }
}
