package com.yunfeiyun.agmp.fms.controller;

import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
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.exception.BizException;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.fms.domain.FmsTaskrcd;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskrcdAuditReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsTaskrcdListReqVo;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import com.yunfeiyun.agmp.fms.domain.vo.FmsTaskrcdVo;
import com.yunfeiyun.agmp.fms.service.IFmsTaskrcdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 农事任务提交记录Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-23
 */
@RestController
@RequestMapping("/fms/taskrcd")
public class FmsTaskrcdController extends BaseController
{
    @Autowired
    private IFmsTaskrcdService fmsTaskrcdService;

    @Autowired
    private ResManager resManager;

    @Resource
    private DataAuthUtil dataAuthUtil;

    /**
     * 查询农事任务提交记录列表
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmsTaskrcdListReqVo fmsTaskrcdListReqVo)
    {

        startPage();
        List<FmsTaskrcdVo> list = this.fmsTaskrcdService.selectFmsTaskrcdList(fmsTaskrcdListReqVo);
        return getDataTable(list);
    }

    /**
     * 导出农事任务提交记录列表
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:export')")
    @Log(title = "导出农事任务提交记录列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmsTaskrcd fmsTaskrcd)
    {
//        List<FmsTaskrcd> list = fmsTaskrcdService.selectFmsTaskrcdList(fmsTaskrcd);
//        ExcelUtil<FmsTaskrcd> util = new ExcelUtil<FmsTaskrcd>(FmsTaskrcd.class);
//        util.exportExcel(response, list, "农事任务提交记录数据");
    }

    /**
     * 获取农事任务提交记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:query')")
    @GetMapping(value = "/{taskrcdId}")
    public AjaxResult getInfo(@PathVariable("taskrcdId") String taskrcdId)
    {
        return success(fmsTaskrcdService.selectFmsTaskrcdByTaskrcdId(taskrcdId));
    }

    /**
     * 根据任务ID查询农事任务提交记录
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:task:info')")
    @GetMapping(value = "/task/info")
    public AjaxResult getInfoByTaskId(@RequestParam("taskId") String taskId)
    {
        return success(fmsTaskrcdService.selectFmsTaskrcdByTaskId(taskId));
    }

    /**
     * 根据农事任务记录id查询农事记录信息
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfoByTaskrcdId(@RequestParam("taskrcdId") String taskId)
    {
        return success(fmsTaskrcdService.selectFmsTaskrcdInfoByTaskrcdId(taskId));
    }

    /**
     * 新增农事任务提交记录
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:add')")
    @Log(title = "新增农事任务提交记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody FmsTaskrcdVo fmsTaskrcdVo)
    {
        return toAjax(this.fmsTaskrcdService.insertFmsTaskrcd(fmsTaskrcdVo));
    }

    /**
     * 修改农事任务提交记录
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:edit')")
    @Log(title = "修改农事任务提交记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FmsTaskrcd fmsTaskrcd)
    {
        return toAjax(fmsTaskrcdService.updateFmsTaskrcd(fmsTaskrcd));
    }

    /**
     * 删除农事任务提交记录
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:remove')")
    @Log(title = "删除农事任务提交记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskrcdIds}")
    public AjaxResult remove(@PathVariable String[] taskrcdIds)
    {
        return toAjax(fmsTaskrcdService.deleteFmsTaskrcdByTaskrcdIds(taskrcdIds));
    }

    /**
     * 审核农事任务提交记录
     * */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:audit')")
    @Log(title = "审核农事任务提交记录",businessType = BusinessType.OTHER)
    @PostMapping("/audit")
    public AjaxResult audit(@Valid @RequestBody FmsTaskrcdAuditReqVo fmsTaskrcdAuditReqVo){
        try {
            return toAjax(fmsTaskrcdService.audit(fmsTaskrcdAuditReqVo));
        } catch (BizException e){
            return AjaxResult.error(e.getCode(),e.getMessage());
        } catch (Exception e){
            return AjaxResult.error(ErrorCode.SERVICE_UNKNOWN_ERROR.getCode());
        }
    }

    /**
     * 农事任务记录图片上传
     */
    @PreAuthorize("@ss.hasPermi('fms:taskrcd:upload:preview')")
    @Log(title = "农事任务记录图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/preview")
    public AjaxResult uploadLandPreview(MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            SysRes sysRes  = resManager.uploadRes( ResConstants.BizType.TASKRCD, ResConstants.mediaType.IMG,"",file,getLoginUser());
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", sysRes.getResUrl());
            ajax.put("resId", sysRes.getResId());
            return ajax;
        }
        return error(ErrorCode.UPLOAD_FAIL.getCode());
    }
}
