package com.ruoyi.web.controller.warehouse.warehouseBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basicData.service.ITWarehouseService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.TWarehouse;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.reportManagement.domain.TWhgenleg;
import com.ruoyi.reportManagement.service.ITWhgenlegService;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.service.ITWarehouseBillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *  仓储小程序Controller
 *
 * @author ruoyi
 * @date 2021-03-23
 */
@RestController
@RequestMapping("/warehouseBusiness/applets")
public class AppletsController extends BaseController {
    @Autowired
    private ITWarehouseBillsService itWarehouseBillsService;

    @Autowired
    private ITWarehouseService  itWarehouseService;

    @Autowired
    private ITWhgenlegService tWhgenlegService;

    /**
     *  待办数
     */
    @GetMapping("/number")
    public AjaxResult list() {
        List<Map<String, Object>> list = itWarehouseBillsService.selectWareHouseNumber();
        return AjaxResult.success(list);
    }

    /**
     *  入库数量
     */
    @GetMapping("/inStockList")
    public TableDataInfo inList(TWarehousebillsitems tWarehousebillsitems) {
        startPage();
        tWarehousebillsitems.setfBilltype("SJRK");
        List<Map<String, Object>> list = itWarehouseBillsService.selectWarehouseBusinessInStockList(tWarehousebillsitems);
        return getDataTable(list);
    }

    /**
     *  出库数量
     */
    @GetMapping("/outStockList")
    public TableDataInfo outList( TWarehousebillsitems tWarehousebillsitems) {
        startPage();
        tWarehousebillsitems.setfBilltype("SJCK");
        List<Map<String, Object>> list = itWarehouseBillsService.selectWarehouseBusinessInStockList(tWarehousebillsitems);
        return getDataTable(list);
    }

    /**
     *   待入出库 提交
     */
    @PostMapping(value = "/ischargeCargo")
    @RepeatSubmit
    public AjaxResult ischargeCargo(@RequestBody TWarehousebillsitems tWarehousebillsitems) {
        tWarehousebillsitems.setfBillstatus(30L);
        return itWarehouseBillsService.waitWarehousingItems(tWarehousebillsitems);
    }

    /**
     *  app仓储保存业务
     */
    @PostMapping(value = "/saveWarehouse")
    @RepeatSubmit
    public AjaxResult saveWarehouse( @RequestBody String information) {
        if (StringUtils.isEmpty(information)) {
            return AjaxResult.error("未找到保存数据，请确认");
        }
        return itWarehouseBillsService.saveWarehouse(information);
    }

    /**
     *  确认入库
     */
    @PostMapping(value = "/addCredit")
    @RepeatSubmit
    public AjaxResult addCredit( @RequestBody String information) {
        if (StringUtils.isEmpty(information)) {
            return AjaxResult.error("未找到查询条件，请确认");
        }
        JSONObject jsonObject = JSONArray.parseObject(information);
        String tEnclosure = jsonObject.get("tEnclosure").toString();
        String warehousebillsitems = jsonObject.get("warehousebillsitems").toString();
        String billsType = "SJRK";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.addCreditItems(tEnclosure,warehousebillsitems,billsType,loginUser);
    }

    /**
     *  确认出库
     */
    @PostMapping(value = "/outAddCredit")
    @RepeatSubmit
    public AjaxResult outAddCredit( @RequestBody String information) {
        if (StringUtils.isEmpty(information)) {
            return AjaxResult.error("未找到查询条件，请确认");
        }
        JSONObject jsonObject = JSONArray.parseObject(information);
        String tEnclosure = jsonObject.get("tEnclosure").toString();
        String warehousebillsitems = jsonObject.get("warehousebillsitems").toString();
        String billsType = "SJCK";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.addCreditItems(tEnclosure,warehousebillsitems,billsType,loginUser);
    }

    /**
     *  仓库
     */
    @GetMapping("/warehouse")
    public TableDataInfo warehouse() {
        startPage();
        TWarehouse tWarehouse =new TWarehouse();
        tWarehouse.setParentId(100L);
        List<TWarehouse> list = itWarehouseService.selectTWarehouseList(tWarehouse);
        return getDataTable(list);
    }

    /**
     * 查询库存总账列表
     */
    @GetMapping("/tWhgenlegList")
    public TableDataInfo warehouselist(TWhgenleg tWhgenleg) {
        startPage();
        List<Map<String, Object>> list = tWhgenlegService.selectWhgenlegList(tWhgenleg);
        return getDataTable(list);
    }

}
