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.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.utils.poi.ExcelUtil;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.fms.domain.FmsPeriod;
import com.yunfeiyun.agmp.fms.domain.reqvo.AddPeriodListReqVo;
import com.yunfeiyun.agmp.fms.service.IFmsPeriodService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 作物周期Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-16
 */
@RestController
@RequestMapping("/fms/period")
public class FmsPeriodController extends BaseController
{
    @Autowired
    private IFmsPeriodService fmsPeriodService;
    @Autowired
    private ResManager resManager;

    /**
     * 查询作物周期列表
     */
    @PreAuthorize("@ss.hasPermi('fms:period:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmsPeriod fmsPeriod)
    {
        startPage();
        List<FmsPeriod> list = fmsPeriodService.selectFmsPeriodList(fmsPeriod);
        return getDataTable(list);
    }

    /**
     * 导出作物周期列表
     */
    @PreAuthorize("@ss.hasPermi('fms:period:export')")
    @Log(title = "作物周期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmsPeriod fmsPeriod)
    {
        List<FmsPeriod> list = fmsPeriodService.selectFmsPeriodList(fmsPeriod);
        ExcelUtil<FmsPeriod> util = new ExcelUtil<FmsPeriod>(FmsPeriod.class);
        util.exportExcel(response, list, "作物周期数据");
    }

    /**
     * 获取作物周期详细信息
     */
    @PreAuthorize("@ss.hasPermi('fms:period:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("periodId") String periodId)
    {
        return success(fmsPeriodService.selectFmsPeriodByPeriodId(periodId));
    }

    /**
     * 根据作物获取生成周期列表
     */
    @PreAuthorize("@ss.hasPermi('fms:period:crop:list')")
    @GetMapping(value = "/crop/list")
    public AjaxResult selectFmsPeriodByCropId(@RequestParam("cropId") String cropId)
    {
        return success(fmsPeriodService.selectFmsPeriodByCropId(cropId));
    }

    /**
     * 新增作物周期
     */
    @PreAuthorize("@ss.hasPermi('fms:period:add')")
    @Log(title = "作物周期", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FmsPeriod fmsPeriod)
    {
        fmsPeriod.setPeriodId(fmsPeriod.getId());
        return toAjax(fmsPeriodService.insertFmsPeriod(fmsPeriod));
    }

    /**
     * 批量新增作物周期
     */
    @PreAuthorize("@ss.hasPermi('fms:period:batch:add')")
    @Log(title = "作物周期", businessType = BusinessType.INSERT)
    @PostMapping(value = "/batch/add")
    public AjaxResult add(@Valid @RequestBody AddPeriodListReqVo reqVo)
    {
        List<FmsPeriod> list = reqVo.getList();
        for (FmsPeriod fmsPeriod : list) {
            if (StringUtils.isEmpty(fmsPeriod.getPeriodName()) || fmsPeriod.getPeriodDays() == null) {
                return AjaxResult.error(ErrorCode.FMS_CROP_PERIOD_INVALID.getCode(), ErrorCode.FMS_CROP_PERIOD_INVALID.getMessage());
            }
        }
        return toAjax(fmsPeriodService.insertFmsPeriodBatch(reqVo));
    }

    /**
     * 修改作物周期
     */
    @PreAuthorize("@ss.hasPermi('fms:period:edit')")
    @Log(title = "作物周期", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody FmsPeriod fmsPeriod)
    {
        return toAjax(fmsPeriodService.updateFmsPeriod(fmsPeriod));
    }

    /**
     * 删除作物周期
     */
    @PreAuthorize("@ss.hasPermi('fms:period:delete')")
    @Log(title = "作物周期", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam("periodIds") String[] periodIds)
    {
        return toAjax(fmsPeriodService.deleteFmsPeriodByPeriodIds(periodIds));
    }

    /**
     * 作物图片上传
     */
    @PreAuthorize("@ss.hasPermi('fms:period:preview:upload')")
    @Log(title = "作物图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/preview/upload")
    public AjaxResult uploadCropPreview(MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            SysRes sysRes  = resManager.uploadRes( ResConstants.BizType.PERIOD, ResConstants.mediaType.IMG, "", file,getLoginUser());
            return success(sysRes);
        }
        return error(ErrorCode.UPLOAD_FAIL.getCode());
    }
}
