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

import java.util.List;

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

/**
 * 巡检check-itemController
 * 
 * @author dongyongwei
 * @date 2021-07-21
 */
@RestController
@RequestMapping("/warehouse/checkitems")
public class TWarehouseCheckItemsController extends BaseController
{
    @Autowired
    private ITWarehouseCheckItemsService tWarehouseCheckItemsService;

    @Autowired
    private ITWarehouseCheckItemsDetailsService tWarehouseCheckItemsDetailsService;

    /**
     * 查询巡检check-item列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitems:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseCheckItems tWarehouseCheckItems)
    {
        startPage();
        List<TWarehouseCheckItems> list = tWarehouseCheckItemsService.selectTWarehouseCheckItemsList(tWarehouseCheckItems);
        return getDataTable(list);
    }

    /**
     * 查询巡检check-item列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitems:list')")
    @GetMapping("/listByUser")
    public TableDataInfo listByUser(TWarehouseCheckItems tWarehouseCheckItems)
    {
        if (!SecurityUtils.getUsername().equals("admin")){
            tWarehouseCheckItems.setfInspectorId(SecurityUtils.getLoginUser().getUser().getUserId());
        }
        startPage();
        List<TWarehouseCheckItems> list = tWarehouseCheckItemsService.selectTWarehouseCheckItemsList(tWarehouseCheckItems);
        return getDataTable(list);
    }


    /**
     * 导出巡检check-item列表
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitems:export')")
    @Log(title = "巡检check-item", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWarehouseCheckItems tWarehouseCheckItems)
    {
        List<TWarehouseCheckItems> list = tWarehouseCheckItemsService.selectTWarehouseCheckItemsList(tWarehouseCheckItems);
        ExcelUtil<TWarehouseCheckItems> util = new ExcelUtil<TWarehouseCheckItems>(TWarehouseCheckItems.class);
        return util.exportExcel(list, "checkitems");
    }

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

    /**
     * 新增巡检check-item
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitems:add')")
    @Log(title = "巡检check-item", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWarehouseCheckItems tWarehouseCheckItems)
    {
        return toAjax(tWarehouseCheckItemsService.insertTWarehouseCheckItems(tWarehouseCheckItems));
    }

    /**
     * 修改巡检check-item
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitems:edit')")
    @Log(title = "巡检check-item", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWarehouseCheckItems tWarehouseCheckItems)
    {
        return toAjax(tWarehouseCheckItemsService.updateTWarehouseCheckItems(tWarehouseCheckItems));
    }

    /**
     * 删除巡检check-item
     */
    @PreAuthorize("@ss.hasPermi('warehouse:checkitems:remove')")
    @Log(title = "巡检check-item", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        for (int f = 0; f < fIds.length; f++) {
            Long itemId = fIds[f];
            TWarehouseCheckItemsDetails tWarehouseCheckItemsDetailsQuery = new TWarehouseCheckItemsDetails();
            tWarehouseCheckItemsDetailsQuery.setfItemFid(itemId);
            List<TWarehouseCheckItemsDetails> tWarehouseCheckItemsDetailsList =tWarehouseCheckItemsDetailsService.selectTWarehouseCheckItemsDetailsList(tWarehouseCheckItemsDetailsQuery);
            if(tWarehouseCheckItemsDetailsList.size()>0){
                return AjaxResult.error("任务编号:"+itemId.intValue()+" 已存在巡检记录,无法删除");
            }
        }
        return toAjax(tWarehouseCheckItemsService.deleteTWarehouseCheckItemsByIds(fIds));
    }
    /**
     * 查询巡检check-item列表
     */
    @GetMapping("/appListByUser")
    public TableDataInfo appListByUser(TWarehouseCheckItems tWarehouseCheckItems)
    {
        startPage();
        List<TWarehouseCheckItems> list = tWarehouseCheckItemsService.appListByUser(tWarehouseCheckItems);
        return getDataTable(list);
    }
}
