package com.sifang.modules.supply.warehouse.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.TraderConstants;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.stock.dao.MaterialStockDao;
import com.sifang.modules.supply.stock.dao.SupplyMaterialDao;
import com.sifang.modules.supply.stock.entity.MaterialWarehouseStocks;
import com.sifang.modules.supply.stock.entity.SupplyMaterial;
import com.sifang.modules.supply.stock.utils.MaterialUtils;
import com.sifang.modules.supply.warehouse.service.SupplyMaterialBatchService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping(value = "${adminPath}/materialBatch")
public class SupplyMaterialBatchController extends BaseExceptionController {

    @Autowired
    private SupplyMaterialBatchService supplyMaterialBatchService;

    @Autowired
    private MaterialStockDao materialStockDao;

    @Autowired
    private SupplyMaterialDao supplyMaterialDao;

    @Autowired
    private TraderService traderService;

    @Autowired
    private AddressService addressService;

    //物料批次列表
    @RequestMapping("materialBathList")
    public String materialBathList(HttpServletRequest request ,Model model){
        //获取从库存管理页面跳转过来的参数
        String id = request.getParameter("id");
        String inStockTime = request.getParameter("inStockTime");
        model.addAttribute("inStockTime",inStockTime);
        if (StringUtil.isNotBlank(id)){
            MaterialWarehouseStocks materialWarehouseStocks = materialStockDao.get(id);
            String materialId = materialWarehouseStocks.getMaterialId();
            SupplyMaterial supplyMaterial = supplyMaterialDao.get(materialId);
            String materialCode = supplyMaterial.getCode();
            String warehouseId = materialWarehouseStocks.getWarehouseId();
            model.addAttribute("materialCode",materialCode);
            model.addAttribute("warehouseId",warehouseId);
        }
        //获取单位/物料类型/物料组/仓库
        User user= UserUtils.getUser();
        MaterialUtils.returnModelMaterial(model,"",user.getTraderId());
        if(UserConstants.TYPE_TRADER.equals(user.getUserType()) || UserConstants.TYPE_PLAT_EMP.equals(user.getUserType())){
            //代理商
            List<Record> warehouseList;
            if (Objects.equals(TraderConstants.TYPE_TWO, user.getTraderType())) {
                warehouseList = MaterialUtils.findWarehouseBySecondTrader(user.getTraderId());
                model.addAttribute("wares", warehouseList);
            }
        }
        model.addAttribute("userType",user.getUserType());
        //代理商列表
        List<Record> traderList = traderService.getAllTopTrader();
        model.addAttribute("traderList", traderList);
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("province", pros);
        return "modules/supply/warehouse/materialBath";
    }

    @ResponseBody
    @RequestMapping(value="materialBathListData")
    public Object materialBathListData(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        if(UserConstants.TYPE_TRADER.equals(user.getUserType()) || UserConstants.TYPE_PLAT_EMP.equals(user.getUserType())){
            map.put("traderId",user.getTopTraderId());
        }
        supplyMaterialBatchService.materialBathListData(page,map);
        return page.renderJson();
    }
}
