package com.yunfeiyun.agmp.fms.controller;

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.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.fms.domain.reqvo.AddAgmTaskPickOutReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.AddAgmTaskPickReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.AgmTaskPickListReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.MppMaterialReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.AgmTaskPickListResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.MppMaterialResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.WmsFinishgoodsListResVo;
import com.yunfeiyun.agmp.fms.service.IAgmTaskpickService;
import com.yunfeiyun.agmp.fms.service.IMppMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 溯源信息Controller
 * 
 * @author 杨晓辉
 * @date 2023-06-25
 */
@RestController
@RequestMapping("/agm/taskpick")
public class AgmTaskPickController extends BaseController
{
    @Autowired
    private IAgmTaskpickService agmTaskpickService;
    @Autowired
    private IMppMaterialService mppMaterialService;
    @Autowired
    private ResManager resManager;

    /**
     * 查询农事任务采收列表
     */
    @PreAuthorize("@ss.hasPermi('agm:taskpick:notin:list')")
    @GetMapping("/notIn/list")
    public TableDataInfo list(AgmTaskPickListReqVo reqVo)
    {
        startPage();
        List<AgmTaskPickListResVo> list = agmTaskpickService.selectFmsTaskpickList(reqVo);
        return getDataTable(list);
    }

    /**
     * 查询农事任务采收列表
     */
    @PreAuthorize("@ss.hasPermi('agm:taskpick:in:list')")
    @GetMapping("/in/list")
    public TableDataInfo inList(AgmTaskPickListReqVo reqVo)
    {
        startPage();
        List<WmsFinishgoodsListResVo> list = agmTaskpickService.selectWmsFinishgoodsList(reqVo);
        for (WmsFinishgoodsListResVo wmsFinishgoodsListResVo : list) {
            //查询溯源图片
            List<SysRes> res = resManager.queryRes(wmsFinishgoodsListResVo.getSourceinfoId(), ResConstants.BizType.SOURCE_INFO_AVATAR.getKey());
            if (res.size() > 0) {
                wmsFinishgoodsListResVo.setSourceinfoAvatar(res.get(0).getResUrl());
            }

            //查询加工前产品编号
            if ("1".equals(wmsFinishgoodsListResVo.getFinishgoodsType())) {
                MppMaterialReqVo reqVo1 = new MppMaterialReqVo();
                reqVo1.setFinishgoodsId(wmsFinishgoodsListResVo.getFinishgoodsId());
                List<MppMaterialResVo> mppMaterialResVos = mppMaterialService.selectMppMaterialList(reqVo1);
                String oldFinishgoodNos = "";
                for (MppMaterialResVo mppMaterialResVo : mppMaterialResVos) {
                    oldFinishgoodNos += mppMaterialResVo.getFinishgoodNo();
                }
                wmsFinishgoodsListResVo.setOldFinishgoodNos(oldFinishgoodNos);
            }
        }
        return getDataTable(list);
    }



    /**
     * 采收入库
     */
    @PreAuthorize("@ss.hasPermi('agm:taskpick:add')")
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody AddAgmTaskPickReqVo reqVo)
    {
        return agmTaskpickService.add(reqVo);
    }
    /**
     * 外采购入库
     */
    @PreAuthorize("@ss.hasPermi('agm:taskpick:out:add')")
    @PostMapping("/out/add")
    public AjaxResult outAdd(@Valid @RequestBody AddAgmTaskPickOutReqVo reqVo)
    {
        return agmTaskpickService.add(reqVo);
    }

}
