package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.fms.domain.FmsTaskasset;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskAssetUsageReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsTaskAssetUsageResVo;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import com.yunfeiyun.agmp.fms.service.IFmsTaskassetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 农事任务农资Controller
 *
 * @author 杨晓辉
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/fms/taskasset")
public class FmsTaskassetController extends BaseController
{
    @Autowired
    private IFmsTaskassetService fmsTaskassetService;
    @Resource
    private DataAuthUtil dataAuthUtil;

    /**
     * 查询农事任务农资列表
     */
    @PreAuthorize("@ss.hasPermi('fms:taskasset:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmsTaskasset fmsTaskasset)
    {
        startPage();
        List<FmsTaskasset> list = fmsTaskassetService.selectFmsTaskassetList(fmsTaskasset);
        return getDataTable(list);
    }

    /**
     * 查询农资使用记录
     *
     * @param fmsTaskAssetUsageReqVo 查询农资使用记录条件实体
     * @return 农资使用记录集合
     */
    @PreAuthorize("@ss.hasPermi('fms:taskasset:usage:list')")
    @GetMapping("/usage/list")
    public TableDataInfo usageList(FmsTaskAssetUsageReqVo fmsTaskAssetUsageReqVo)
    {
        fmsTaskAssetUsageReqVo.setDataFilter(dataAuthUtil.isValidate());
        fmsTaskAssetUsageReqVo.setBlockIds(dataAuthUtil.blockList());
        startPage();
        List<FmsTaskAssetUsageResVo> list = fmsTaskassetService.selectFmsTaskAssetUsageResVoList(fmsTaskAssetUsageReqVo);
        return getDataTable(list);
    }


    /**
     * 导出农事任务农资列表
     */
    @PreAuthorize("@ss.hasPermi('fms:taskasset:export')")
    @Log(title = "农事任务农资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmsTaskasset fmsTaskasset)
    {
        List<FmsTaskasset> list = fmsTaskassetService.selectFmsTaskassetList(fmsTaskasset);
        ExcelUtil<FmsTaskasset> util = new ExcelUtil<FmsTaskasset>(FmsTaskasset.class);
        util.exportExcel(response, list, "农事任务农资数据");
    }

    /**
     * 获取农事任务农资详细信息
     */
    @PreAuthorize("@ss.hasPermi('fms:taskasset:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("taskassetId") String taskassetId)
    {
        return success(fmsTaskassetService.selectFmsTaskassetByTaskassetId(taskassetId));
    }

    /**
     * 新增农事任务农资
     */
    @PreAuthorize("@ss.hasPermi('fms:taskasset:add')")
    @Log(title = "农事任务农资", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FmsTaskasset fmsTaskasset)
    {
        return toAjax(fmsTaskassetService.insertFmsTaskasset(fmsTaskasset));
    }

    /**
     * 修改农事任务农资
     */
    @PreAuthorize("@ss.hasPermi('fms:taskasset:edit')")
    @Log(title = "农事任务农资", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FmsTaskasset fmsTaskasset)
    {
        return toAjax(fmsTaskassetService.updateFmsTaskasset(fmsTaskasset));
    }

    /**
     * 删除农事任务农资
     */
    @PreAuthorize("@ss.hasPermi('fms:taskasset:delete')")
    @Log(title = "农事任务农资", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam("taskassetIds") String[] taskassetIds)
    {
        return toAjax(fmsTaskassetService.deleteFmsTaskassetByTaskassetIds(taskassetIds));
    }
}
