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.poi.ExcelUtil;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.fms.domain.WmsFinishgoods;
import com.yunfeiyun.agmp.fms.domain.reqvo.MppMaterialReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.WmsFinishgoodsReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.MppMaterialResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.WmsFinishGoodsStockSumInfoResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.WmsFinishgoodsInfoResVo;
import com.yunfeiyun.agmp.fms.service.IMppMaterialService;
import com.yunfeiyun.agmp.fms.service.IWmsFinishgoodsService;
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 java.util.List;

/**
 * 农产品货物Controller
 * 
 * @author 杨晓辉
 * @date 2023-06-30
 */
@RestController
@RequestMapping("/wms/finishgoods")
public class WmsFinishgoodsController extends BaseController
{
    @Autowired
    private IWmsFinishgoodsService wmsFinishgoodsService;
    @Autowired
    private ResManager resManager;
    @Autowired
    private IMppMaterialService mppMaterialService;

    /**
     * 查询农产品货物列表
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsFinishgoods wmsFinishgoods)
    {
        startPage();
        List<WmsFinishgoods> list = wmsFinishgoodsService.selectWmsFinishgoodsList(wmsFinishgoods);
        return getDataTable(list);
    }

    /**
     * 查询农产品货物列表
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:stock:sum')")
    @GetMapping("/stock/sum")
    public TableDataInfo stockSum(WmsFinishgoodsReqVo reqVo)
    {
        startPage();
        List<WmsFinishGoodsStockSumInfoResVo> list = wmsFinishgoodsService.selectWmsFinishGoodsStockSumInfo(reqVo);
        return getDataTable(list);
    }

    /**
     * 导出农产品货物列表
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:export')")
    @Log(title = "农产品货物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsFinishgoods wmsFinishgoods)
    {
        List<WmsFinishgoods> list = wmsFinishgoodsService.selectWmsFinishgoodsList(wmsFinishgoods);
        ExcelUtil<WmsFinishgoods> util = new ExcelUtil<WmsFinishgoods>(WmsFinishgoods.class);
        util.exportExcel(response, list, "农产品货物数据");
    }

    /**
     * 获取农产品货物详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("finishgoodsId") String finishgoodsId)
    {
        WmsFinishgoodsInfoResVo resVo = wmsFinishgoodsService.selectWmsFinishgoodsByFinishgoodsId(finishgoodsId);
        //查询图片
        List<SysRes> res = resManager.queryRes(finishgoodsId, ResConstants.BizType.SOURCE_INFO_AVATAR, ResConstants.mediaType.IMG);
        String resIds = "";
        for (SysRes re : res) {
            resIds += re.getResId() + ",";
        }
        if (resIds.length() > 0) {
            resIds = resIds.substring(0, resIds.length() - 1);
        }
        resVo.setProcessResIds(resIds);
        //查询原材料列表
        MppMaterialReqVo reqVo = new MppMaterialReqVo();
        reqVo.setFinishgoodsId(finishgoodsId);
        List<MppMaterialResVo> mppMaterialResVos = mppMaterialService.selectMppMaterialList(reqVo);
        resVo.setMppMaterials(mppMaterialResVos);

        return success(resVo);
    }

    /**
     * 新增农产品货物
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:add')")
    @Log(title = "农产品货物", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WmsFinishgoods wmsFinishgoods)
    {
        return toAjax(wmsFinishgoodsService.insertWmsFinishgoods(wmsFinishgoods));
    }

    /**
     * 修改农产品货物
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:edit')")
    @Log(title = "农产品货物", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody WmsFinishgoods wmsFinishgoods)
    {
        return toAjax(wmsFinishgoodsService.updateWmsFinishgoods(wmsFinishgoods));
    }

    /**
     * 删除农产品货物
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:delete')")
    @Log(title = "农产品货物", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam("finishgoodsIds") String[] finishgoodsIds)
    {
        return toAjax(wmsFinishgoodsService.deleteWmsFinishgoodsByFinishgoodsIds(finishgoodsIds));
    }

    /**
     * 生长过程图片上传
     */
    @PreAuthorize("@ss.hasPermi('wms:finishgoods:period:upload')")
    @Log(title = "生长过程图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/period/upload")
    public AjaxResult periodUpload(MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            SysRes sysRes  = resManager.uploadRes( ResConstants.BizType.FINISH_GOODS_PERIOD, ResConstants.mediaType.IMG, "", file,getLoginUser());
            return success(sysRes);
        }
        return error(ErrorCode.UPLOAD_FAIL.getCode());
    }
}
