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.DataScope;
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.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.reportManagement.domain.TWhgenleg;
import com.ruoyi.reportManagement.service.ITWhgenlegService;
import com.ruoyi.system.domain.SysCheckCode;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
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("/appCheckCode")
    public AjaxResult appCheckCode(SysCheckCode sysCheckCode) {
        if (StringUtils.isNull(sysCheckCode.getfId())) {
            return AjaxResult.success(true);
        }
        return AjaxResult.success(itWarehouseBillsService.checkCode(sysCheckCode));
    }

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

    /**
     *  查询入库列表数据
     */
    @GetMapping("/inStockList")
    @DataScope(deptAlias = "d", userAlias = "u")
    public TableDataInfo inList(TWarehouseBills warehouseBills) {
        startPage();
        warehouseBills.setfBilltype("SJRK");
        List<Map<String, Object>> list = itWarehouseBillsService.selectAppStockList(warehouseBills);
        return getDataTable(list);
    }

    /**
     *  查询出库列表数据
     */
    @GetMapping("/outStockList")
    @DataScope(deptAlias = "d", userAlias = "u")
    public TableDataInfo outList( TWarehouseBills warehouseBills) {
        startPage();
        warehouseBills.setfBilltype("SJCK");
        List<Map<String, Object>> list = itWarehouseBillsService.selectAppStockList(warehouseBills);
        return getDataTable(list);
    }

    /**
     *  查询调拨列表数据
     */
    @GetMapping("/stockTransferList")
    @DataScope(deptAlias = "d", userAlias = "u")
    public TableDataInfo stockTransfer( TWarehouseBills warehouseBills) {
        startPage();
        warehouseBills.setfBilltype("CKDB");
        List<Map<String, Object>> list = itWarehouseBillsService.selectAppStockList(warehouseBills);
        return getDataTable(list);
    }

    /**
     *  根据id查询对应明细列表数据
     */
    @GetMapping("/warehouseItemById")
    public TableDataInfo warehouseItemById(TWarehouseBills warehouseBills) {
        if (warehouseBills.getfId() == null)
        {
            return null;
        }
        startPage();
        List<Map<String, Object>> mapList = itWarehouseBillsService.warehouseItemById(warehouseBills);
        return getDataTable(mapList);
    }

    /**
     *  查询app 添加新行 必要的基础资料
     * @return
     */
    @GetMapping("/appBasicInformation")
    public AjaxResult getAppBasicInformation(TWarehouseBills warehouseBills) {
        if (StringUtils.isNull(warehouseBills.getfId())) {
            return AjaxResult.error("未找到主表id，请确认");
        }
        return itWarehouseBillsService.getAppBasicInformation(warehouseBills.getfId());
    }

    /**
     *  根据明细id查询对应明细表数据
     */
    @GetMapping(value = "/appWarehouseItemDataById")
    public AjaxResult getInfo(TWarehouseBills warehouseBills)
    {
        if (StringUtils.isNull(warehouseBills.getfId()))
        {
            return AjaxResult.error("未找到查询条件，请确认");
        }
        return itWarehouseBillsService.selectWarehouseItemDataById(warehouseBills.getfId());
    }

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

    /**
     *  app仓储新行保存业务
     */
    @PostMapping(value = "/appSubmitWarehouse")
    @RepeatSubmit
    public AjaxResult submitWarehouse( @RequestBody String information) {
        if (StringUtils.isEmpty(information)) {
            return AjaxResult.error("未找到提交数据，请确认");
        }
        JSONObject jsonObject = JSONArray.parseObject(information);
        String item = jsonObject.get("item").toString();
        String attachs = jsonObject.get("attachs").toString();
        if (StringUtils.isEmpty(item) || "{}".equals(item)) {
            return AjaxResult.error("未找到明细信息，请确认");
        }
        return itWarehouseBillsService.submitWarehouse(item, attachs);
    }

    /**
     *  app仓储新行提交业务
     */
    @PostMapping(value = "/saveWarehouse")
    @RepeatSubmit
    public AjaxResult saveWarehouse( @RequestBody String information) {
        if (StringUtils.isEmpty(information)) {
            return AjaxResult.error("未找到提交数据，请确认");
        }
        JSONObject jsonObject = JSONArray.parseObject(information);
        String item = jsonObject.get("item").toString();
        String attachs = jsonObject.get("attachs").toString();
        if (StringUtils.isEmpty(item) || "{}".equals(item)) {
            return AjaxResult.error("未找到明细信息，请确认");
        }
        return itWarehouseBillsService.saveWarehouse(item, attachs);
    }

    /**
     *  app叫车进场
     */
    @PostMapping(value = "/appWarehouseOperation")
    @RepeatSubmit
    public AjaxResult warehouseOperation( @RequestBody String information) {
        if (StringUtils.isEmpty(information)) {
            return AjaxResult.error("未接收到数据，请确认");
        }
        JSONObject jsonObject = JSONArray.parseObject(information);
        String item = jsonObject.get("item").toString();
        String attachs = jsonObject.get("attachs").toString();
        if (StringUtils.isEmpty(item) || "{}".equals(item)) {
            return AjaxResult.error("未找到明细信息，请确认");
        }
        return itWarehouseBillsService.addCreditItems(item, attachs);
    }

    /**
     *  app确认入库、出库、货转
     */
    @PostMapping(value = "/appOperationConfirmation")
    @RepeatSubmit
    public AjaxResult warehouseOperationConfirmation( @RequestBody String information) {
        if (StringUtils.isEmpty(information)) {
            return AjaxResult.error("未找到查询条件，请确认");
        }
        JSONObject jsonObject = JSONArray.parseObject(information);
        String item = jsonObject.get("item").toString();
        String attachs = jsonObject.get("attachs").toString();
        if (StringUtils.isEmpty(item) || "{}".equals(item)) {
            return AjaxResult.error("未找到明细信息，请确认");
        }
        return itWarehouseBillsService.warehouseOperationConfirmation(item, attachs);
//        return null;
    }

    /**
     *  仓库
     */
    @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);
    }

}
