package com.ruoyi.web.controller.business;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.TCkPickingwavegoods;
import com.ruoyi.business.domain.TMiStock;
import com.ruoyi.business.service.ITCkPickingwavegoodsService;
import com.ruoyi.business.service.ITMiStockService;
import com.ruoyi.business.utils.OrderCodeFactory;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.TCkOrderdetail;
import com.ruoyi.business.service.ITCkOrderdetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库物料明细Controller
 * 
 * @author wms
 * @date 2022-06-11
 */
@Controller
@RequestMapping("/business/orderdetail")
public class TCkOrderdetailController extends BaseController
{
    private String prefix = "business/orderdetail";

    @Autowired
    private ITCkOrderdetailService tCkOrderdetailService;

    @Autowired
    private ITMiStockService tMiStockService;

    @Autowired
    private ITCkPickingwavegoodsService tCkPickingwavegoodsService;

    @RequiresPermissions("business:orderdetail:view")
    @GetMapping()
    public String orderdetail()
    {
        return prefix + "/orderdetail";
    }

    /**
     * 查询出库物料明细列表
     */
    @RequiresPermissions("business:orderdetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TCkOrderdetail tCkOrderdetail)
    {
        startPage();
        List<TCkOrderdetail> list = tCkOrderdetailService.selectTCkOrderdetailList(tCkOrderdetail);
        return getDataTable(list);
    }

    /**
     * 导出出库物料明细列表
     */
    @RequiresPermissions("business:orderdetail:export")
    @Log(title = "导出出库物料明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TCkOrderdetail tCkOrderdetail)
    {
        List<TCkOrderdetail> list = tCkOrderdetailService.selectTCkOrderdetailList(tCkOrderdetail);
        ExcelUtil<TCkOrderdetail> util = new ExcelUtil<TCkOrderdetail>(TCkOrderdetail.class);
        return util.exportExcel(list, "出库物料明细数据");
    }

    /**
     * 新增出库物料明细
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存出库物料明细
     */
    @RequiresPermissions("business:orderdetail:add")
    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TCkOrderdetail tCkOrderdetail)
    {
        // start 检查当前库存是否充足(只查合格品)
        TMiStock tMiStock = new TMiStock();
        tMiStock.setGoodsId(tCkOrderdetail.getGoodId());
        tMiStock.setProduclotid(tCkOrderdetail.getPutinId());
        tMiStock.setSts("0");
        List<TMiStock> list = tMiStockService.selectTMiStockOutList(tMiStock);
        if (list.size() > 0) {
            if (list.get(0).getShelvesNum().compareTo(tCkOrderdetail.getPickNum()) >= 0) {// 库存充足
                tCkOrderdetail.setCkId(OrderCodeFactory.getOrderCode("CK", ""));
                tCkOrderdetail.setStatus("0");
                tCkOrderdetail.setAddtime(new Date());
                // 获取用户名
                SysUser sysUser = ShiroUtils.getCurrentUser();
                tCkOrderdetail.setUserName(sysUser.getUserName());
                return toAjax(tCkOrderdetailService.insertTCkOrderdetail(tCkOrderdetail));
            } else {// 库存不足
                return success("0");
            }
        }
        // end 检查当前库存是否充足
        return success("-1");
    }

    /**
     * 修改出库物料明细
     */
    @RequiresPermissions("business:orderdetail:edit")
    @GetMapping("/edit/{ckId}")
    public String edit(@PathVariable("ckId") String ckId, ModelMap mmap)
    {
        TCkOrderdetail tCkOrderdetail = tCkOrderdetailService.selectTCkOrderdetailByCkId(ckId);
        mmap.put("tCkOrderdetail", tCkOrderdetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存出库物料明细
     */
    @RequiresPermissions("business:orderdetail:edit")
    @Log(title = "出库物料明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TCkOrderdetail tCkOrderdetail)
    {
        return toAjax(tCkOrderdetailService.updateTCkOrderdetail(tCkOrderdetail));
    }

    /**
     * 删除出库物料明细
     */
    @RequiresPermissions("business:orderdetail:remove")
    @Log(title = "出库物料明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tCkOrderdetailService.deleteTCkOrderdetailByCkIds(ids));
    }
}
