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.FmsCrop;
import com.yunfeiyun.agmp.fms.domain.FmsTaskpick;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskPickListReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskPickStockDetailReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsTaskPickListResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsTaskPickStockDetailResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsTaskPickStockListResVo;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import com.yunfeiyun.agmp.fms.service.IFmsTaskpickService;
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 javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 农事任务采收Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-23
 */
@RestController
@RequestMapping("/fms/taskpick")
public class FmsTaskpickController extends BaseController
{
    @Autowired
    private IFmsTaskpickService fmsTaskpickService;
    @Resource
    private DataAuthUtil dataAuthUtil;

    /**
     * 查询农事任务采收列表
     */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmsTaskPickListReqVo fmsTaskPickListReqVo)
    {
        fmsTaskPickListReqVo.setBlockIds(dataAuthUtil.blockList());
        fmsTaskPickListReqVo.setDataFilter(dataAuthUtil.isValidate());
        if(fmsTaskPickListReqVo.getDataFilter()&&fmsTaskPickListReqVo.getBlockIds().isEmpty()){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<FmsTaskPickListResVo> list = fmsTaskpickService.selectFmsTaskpickList(fmsTaskPickListReqVo);
        return getDataTable(list);
    }

    /**
     * 查询采收农产品列表
     */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:stock:list')")
    @GetMapping("/stock/list")
    public TableDataInfo stockList(FmsCrop fmsCrop)
    {
        fmsCrop.setBlockIds(dataAuthUtil.blockList());
        fmsCrop.setDataFilter(dataAuthUtil.isValidate());
        startPage();
        List<FmsTaskPickStockListResVo> list = fmsTaskpickService.selectFmsTaskPickStockList(fmsCrop);
        return getDataTable(list);
    }
    /**
     * 查询采收农产品列表详情
     * */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:stock:detail')")
    @GetMapping("/stock/detail")
    public TableDataInfo stockDetail(@Valid FmsTaskPickStockDetailReqVo fmsTaskPickStockDetailReqVo)
    {
        startPage();
        List<FmsTaskPickStockDetailResVo> list = fmsTaskpickService.selectFmsTaskPickStockDetailList(fmsTaskPickStockDetailReqVo);
        return getDataTable(list);
    }

    /**
     * 导出农事任务采收列表
     */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:export')")
    @Log(title = "农事任务采收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmsTaskPickListReqVo fmsTaskPickListReqVo)
    {
        List<FmsTaskPickListResVo> list = fmsTaskpickService.selectFmsTaskpickList(fmsTaskPickListReqVo);
        ExcelUtil<FmsTaskPickListResVo> util = new ExcelUtil<FmsTaskPickListResVo>(FmsTaskPickListResVo.class);
        util.exportExcel(response, list, "农事任务采收数据");
    }

    /**
     * 获取农事任务采收详细信息
     */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:query')")
    @GetMapping(value = "/{taskpickId}")
    public AjaxResult getInfo(@PathVariable("taskpickId") String taskpickId)
    {
        return success(fmsTaskpickService.selectFmsTaskpickByTaskpickId(taskpickId));
    }

    /**
     * 新增农事任务采收
     */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:add')")
    @Log(title = "农事任务采收", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FmsTaskpick fmsTaskpick)
    {
        return toAjax(fmsTaskpickService.insertFmsTaskpick(fmsTaskpick));
    }

    /**
     * 修改农事任务采收
     */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:edit')")
    @Log(title = "农事任务采收", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FmsTaskpick fmsTaskpick)
    {
        return toAjax(fmsTaskpickService.updateFmsTaskpick(fmsTaskpick));
    }

    /**
     * 删除农事任务采收
     */
    @PreAuthorize("@ss.hasPermi('fms:taskpick:remove')")
    @Log(title = "农事任务采收", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskpickIds}")
    public AjaxResult remove(@PathVariable String[] taskpickIds)
    {
        return toAjax(fmsTaskpickService.deleteFmsTaskpickByTaskpickIds(taskpickIds));
    }
}
