package com.sifang.modules.smallprogram.order.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.order.request.ApplyOrderReq;
import com.sifang.modules.supply.order.service.MaterialApplyService;
import com.sifang.modules.supply.setting.entity.CrmSupplyWarehouse;
import com.sifang.modules.supply.setting.service.CrmSupplyMaterialSettingService;
import com.sifang.modules.supply.setting.service.CrmSupplyWarehouseSettingService;
import com.sifang.modules.supply.stock.utils.MaterialUtils;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 申请单
 */
@Controller
@RequestMapping("${adminPath}/main/apply")
public class MpApplyOrderController extends BaseExceptionController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    MaterialApplyService materialApplyService;

    @Autowired
    CrmSupplyMaterialSettingService materialSettingService;

    @Autowired
    CrmSupplyWarehouseSettingService warehouseSettingService;

    @Autowired
    UserDao userDao;


    /**
     * 查找物料类型列表
     */
    @ResponseBody
    @RequestMapping("/listMaterialType")
    public Object listMaterialType() {
        List<Record> typeList = materialSettingService.listMaterielType();
        return renderJson(Result.success(typeList));
    }

    /**
     * 根据物料组和物料类型查找物料和一级BOM
     */
    @ResponseBody
    @RequestMapping("/findMaterialAndBom")
    public Object findMaterialAndBom(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        logger.info("findMaterialAndBom, params: {}", params);
        LayuiPage<Record> page = new LayuiPage<>(request);
        List<Record> recordList = materialApplyService.findCanApplyMaterialsByKeyWord(params, page);
        return renderJson(Result.success(recordList));
    }

    /**
     * 根据物料组和物料类型查找物料
     */
    @ResponseBody
    @RequestMapping("/findMaterialList")
    public Object findMaterialList(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        logger.info("findMaterialList, params: {}", params);
        LayuiPage<Record> page = new LayuiPage<>(request);
        List<Record> recordList = materialApplyService.findMaterialsByKeyWord(params, page);
        return renderJson(Result.success(recordList));
    }

    /**
     * 仓库列表
     */
    @ResponseBody
    @RequestMapping("/warehouseList")
    public Object warehouseList(HttpServletRequest request){
        logger.info("mp warehouseList, params: {}", getParams(request));
        Operator operator = getOperatorBy(request);
        Ascription ascription = CrmUtils.getAscription(userDao.get(operator.getUserId()));
        List<Record> warehouses = MaterialUtils.findMaterialWarehouseByAscriptionId(ascription.getAscriptionId());
        return renderJson(Result.success(warehouses));
    }

    /**
     * 根据仓库id 获取
     */
    @ResponseBody
    @RequestMapping("findWarehouseById")
    public Object findWarehouseById(String id){
        if(StringUtil.isBlank(id)){
            return Result.fail("203","参数为空");
        }
        CrmSupplyWarehouse warehouse = warehouseSettingService.getWarehouseDetail(id);


        return Result.success(warehouse);
    }


    /**
     * 保存物料申请单
     */
    @ResponseBody
    @RequestMapping("/saveApplyOrder")
    public Result saveApplyOrder(@Valid @RequestBody ApplyOrderReq req, HttpServletRequest request) {
        logger.info("mp saveApplyOrder: req: {}", req);
        materialApplyService.saveApplyOrder(req, getOperatorBy(request));
        return Result.ok();
    }

    /**
     * 物料申请单详情
     */
    @ResponseBody
    @RequestMapping("/applyOrderDetail")
    public Object applyOrderDetail(String orderId, HttpServletRequest request) {
        logger.info("mp applyOrderDetail, params: {}", getParams(request));
        Record apply = materialApplyService.getRecordById(orderId, getOperatorBy(request));
        List<Record> gatherDetails = materialApplyService.listGatherByApplyId(orderId);
        List<Record> applyDetails = materialApplyService.listDetailsByApplyId(orderId);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("apply", apply);
        retMap.put("gatherDetails", gatherDetails);
        retMap.put("applyDetails", applyDetails);
        return renderJson(Result.success(retMap));
    }

    /**
     * 物料申请单列表数据
     */
    @ResponseBody
    @RequestMapping("/ajaxForApplyOrderList")
    public Object ajaxForApplyOrderList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        logger.info("mini app, ajaxForApplyOrderList, params: {}", map);
        Operator operator = getOperatorBy(request);
        LayuiPage<Record> page = new LayuiPage<>(request);
        materialApplyService.applyOrderListPage(page, map, operator);
        return page.renderJson();
    }

}
