package com.ruoyi.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.*;
import com.ruoyi.business.service.ITBaseHcService;
import com.ruoyi.business.service.ITOngoodsshelfService;
import com.ruoyi.business.utils.OrderCodeFactory;
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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/business/emptyCtl")
public class EmptyCtlController extends BaseController {
    private String prefix = "business/emptyCtl";

    @Autowired
    private ITBaseHcService tBaseHcService;

    @Autowired
    private ITOngoodsshelfService tOngoodsshelfService;

    /**
     * 空托调度
     * @param mmap
     * @return
     */
    @RequiresPermissions("business:emptyCtl:view")
    @GetMapping()
    public String ebsConfirm(ModelMap mmap) {
        mmap.put("hc", new TBaseHC());
        return prefix + "/emptyCtlControl";
    }

    /**
     * 查询缓存位列表
     */
    @RequiresPermissions("business:emptyCtl:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TBaseHC tBaseHC)
    {
        startPage();
        List<TBaseHC> list = tBaseHcService.list(new LambdaQueryWrapper<TBaseHC>()
                .eq(StringUtils.isNotEmpty(tBaseHC.getAreaCode()), TBaseHC::getAreaCode, tBaseHC.getAreaCode()));
        return getDataTable(list);
    }

    /**
     * 选择移库库位
     */
    @RequiresPermissions("business:emptyCtl:edit")
    @GetMapping("/emptyCtlInputModal/{areaCode}")
    public String selMoveLocation(@PathVariable("areaCode") String areaCode, ModelMap mmap)
    {
        mmap.put("areaCode", areaCode);
        return prefix + "/emptyCtlInputModal";
    }

    /**
     * 生成空托->缓存位任务
     */
    @RequiresPermissions("business:emptyCtl:edit")
    @Log(title = "生成空托->缓存位任务", businessType = BusinessType.UPDATE)
    @PostMapping("/inEmptyCtl")
    @ResponseBody
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public AjaxResult inEmptyCtl(TBaseHC tBaseHC) {
        if (tBaseHC == null || StringUtils.isEmpty(tBaseHC.getAreaCode())
        || StringUtils.isEmpty(tBaseHC.getLastCtl())) {
            return error("参数缺少缓存位或托盘号。");
        }
        // 查询当前缓存位是否已经有任务
        TOngoodsshelf query = new TOngoodsshelf();
        query.setTasktype(String.valueOf(11));
        query.setAgvbgn("D00591");
        query.setAgvend(tBaseHC.getAreaCode());
        List<TOngoodsshelf> oldTaskList = tOngoodsshelfService.selectTOngoodsshelfList(query);
        if (oldTaskList != null && oldTaskList.size() > 0) {
            return error("当前缓存位已有任务，请勿重复下发。");
        }
        // 生成任务
        TOngoodsshelf newTask = new TOngoodsshelf();
        newTask.setLotId(OrderCodeFactory.getOrderCode("INEMPTY", ""));
        newTask.setTasktype(String.valueOf(11));
        newTask.setInstatus("0");
        newTask.setAgvbgn("D00591");
        newTask.setAgvend(tBaseHC.getAreaCode());
        int insertResult = tOngoodsshelfService.insertTOngoodsshelf(newTask);
        if (insertResult > 0) {
            return success("生成任务成功。");
        } else {
            return error("生成任务失败。");
        }
    }

    /**
     * 生成缓存位->月台任务
     */
    @RequiresPermissions("business:emptyCtl:edit")
    @Log(title = "生成缓存位->月台任务", businessType = BusinessType.UPDATE)
    @PostMapping("/outEmptyCtl")
    @ResponseBody
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public AjaxResult outEmptyCtl(TBaseHC tBaseHC) {
        if (tBaseHC == null || StringUtils.isEmpty(tBaseHC.getAreaCode())) {
            return error("参数缺少缓存位。");
        }
        TOngoodsshelf query = new TOngoodsshelf();
        query.setTasktype(String.valueOf(12));
        query.setAgvbgn(tBaseHC.getAreaCode());
        query.setAgvend("PPS02");
        List<TOngoodsshelf> oldTaskList = tOngoodsshelfService.selectTOngoodsshelfList(query);
        if (oldTaskList != null && oldTaskList.size() > 0) {
            return error("当前缓存位已有任务，请勿重复下发。");
        }
        // 生成任务
        TOngoodsshelf newTask = new TOngoodsshelf();
        newTask.setLotId(OrderCodeFactory.getOrderCode("OUTEMPTY", ""));
        newTask.setTasktype(String.valueOf(12));
        newTask.setInstatus("0");
        newTask.setAgvbgn(tBaseHC.getAreaCode());
        newTask.setAgvend("PPS02");
        int insertResult = tOngoodsshelfService.insertTOngoodsshelf(newTask);
        if (insertResult > 0) {
            return success("生成任务成功。");
        } else {
            return error("生成任务失败。");
        }
    }
}
