package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.TCkPickingwavegoods;
import com.ruoyi.business.domain.TConveyorStatus;
import com.ruoyi.business.service.ITCkPickingwavegoodsService;
import com.ruoyi.business.service.ITConveyorStatusService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报废管理Controller
 * 
 * @author hw
 * @date 2022-06-25
 */
@Controller
@RequestMapping("/business/goodsScrap")
public class GoodsScrapController extends BaseController
{
    private String prefix = "business/goodsScrap";

    @Autowired
    private ITCkPickingwavegoodsService tCkPickingwavegoodsService;

    @Autowired
    private ITConveyorStatusService itConveyorStatusService;

    @RequiresPermissions("business:goodsScrap:view")
    @GetMapping()
    public String goodsScrapInit(ModelMap mmap)
    {
        // 获取当前用户
        SysUser sysUser = ShiroUtils.getCurrentUser();

        TConveyorStatus tConveyorStatus = new TConveyorStatus();
        // 用户登录IP
        tConveyorStatus.setPcip(ShiroUtils.getIp());
        List<TConveyorStatus> tConveyorStatusList = itConveyorStatusService.selectTConveyorStatusList(tConveyorStatus);
        if (tConveyorStatusList != null && tConveyorStatusList.size() > 0) {
            tConveyorStatus = tConveyorStatusList.get(0);
            // 获取用户登录的站台号
            sysUser.setOutStand(tConveyorStatus.getConveryorId());
        }
        mmap.put("sysUser", sysUser);
        return prefix + "/goodsScrap";
    }

    /**
     * 查询出库物料明细列表
     */
    @RequiresPermissions("business:goodsScrap:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TCkPickingwavegoods tCkPickingwavegoods)
    {
        startPage();
        List<TCkPickingwavegoods> list = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
        return getDataTable(list);
    }

    /**
     * 修改出库任务
     */
    @RequiresPermissions("business:pickingwavegoods:edit")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @PostMapping("/updateResult")
    @ResponseBody
    public AjaxResult updateResult(TCkPickingwavegoods tCkPickingwavegoods)
    {
        return toAjax(tCkPickingwavegoodsService.updateGoodsOutResult(tCkPickingwavegoods));
    }

    /**
     * 判断站台上的空托盘是否为空，如果不为空，根据托盘号获取出库任务信息
     */
    @RequiresPermissions("business:pickingwavegoods:edit")
    @GetMapping("/queryByCtl")
    @ResponseBody
    public AjaxResult queryByCtl()
    {
        TConveyorStatus tConveyorStatus = new TConveyorStatus();
        // 用户登录IP
        SysUser sysUser = ShiroUtils.getCurrentUser();
        tConveyorStatus.setPcip(ShiroUtils.getIp());

        List<TConveyorStatus> tConveyorStatusList = itConveyorStatusService.selectTConveyorStatusList(tConveyorStatus);
        if (tConveyorStatusList != null && tConveyorStatusList.size() > 0) {
            tConveyorStatus = tConveyorStatusList.get(0);

            // 判断当前站台上的托盘是否为空，不为空时，根据托盘获取出库任务数据
            if (StringUtils.isNotEmpty(tConveyorStatus.getBarcode())) {
                TCkPickingwavegoods tCkPickingwavegoods = new TCkPickingwavegoods();
                // 托盘号
                tCkPickingwavegoods.setCtl(tConveyorStatus.getBarcode());
                // 状态
                tCkPickingwavegoods.setOutstatus("0");
                // 站台号
                tCkPickingwavegoods.setOutstand(tConveyorStatus.getConveryorId());
                // 获取任务列表
                List<TCkPickingwavegoods> tCkPickingwavegoodsList = tCkPickingwavegoodsService.selectTCkPickingwavegoodsList(tCkPickingwavegoods);
                if (tCkPickingwavegoodsList != null && tCkPickingwavegoodsList.size() > 0) {
                    return success(tCkPickingwavegoodsList.get(0));
                }
            }
        }
        return null;
    }
}
