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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.order.constant.PendingOrderConstants;
import com.sifang.modules.supply.order.dao.CheckPendingOrderDao;
import com.sifang.modules.supply.order.dao.MaterialDeliverDetailDao;
import com.sifang.modules.supply.order.entity.CrmSupplyCheckPendingOrder;
import com.sifang.modules.supply.order.service.CheckPendingOrderService;
import com.sifang.modules.supply.warehouse.service.CrmSupplyAllocationOrderService;
import com.sifang.modules.supply.warehouse.service.InventoryOrderService;
import com.sifang.modules.sys.util.GuardUtils;
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.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;

@Controller
@RequestMapping(value = "${adminPath}/checkPendingOrder")
public class CheckPendingOrderController extends BaseExceptionController {
    Logger logger = LoggerFactory.getLogger(CheckPendingOrderController.class);

    @Autowired
    private CheckPendingOrderService checkPendingOrderService;
    @Autowired
    private CheckPendingOrderDao checkPendingOrderDao;

    @Autowired
    private InventoryOrderService inventoryOrderService;

    @Autowired
    private CrmSupplyAllocationOrderService allocationOrderService;

    @Autowired
    MaterialDeliverDetailDao deliverDetailDao;

    /**
     * 待审核订单页面
     */
    @RequestMapping(value = "toCheckPendingOrderPage")
    public String toCheckPendingOrderPage() {
        return "modules/supply/order/checkPendingOrderPage";
    }

    /**
     * 待审核订单 列表数据
     */
    @ResponseBody
    @RequestMapping(value = "ajaxForCheckPendingOrderData")
    public Object ajaxForCheckPendingOrderData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        logger.info(" into ajaxForCheckPendingOrderData, map={}", map);
        checkPendingOrderService.getCheckPendingOrderPage(page, map);
        return page.renderJson();
    }

    /**
     * 审核页面
     */
    @RequestMapping(value = "toAuditForm")
    public String toAuditForm(String id, Model model) {

        CrmSupplyCheckPendingOrder order = checkPendingOrderDao.get(id);
        List<Record> list = null;
        if(PendingOrderConstants.PD.equals(order.getOrderType())){
            //1.盘点单 2.调拨单 3.入库单
            list = inventoryOrderService.getDetails(order.getOrderId());

        }else if(PendingOrderConstants.DB.equals(order.getOrderType())){
            list = allocationOrderService.getMaterialList(order.getOrderId());
        }else if(PendingOrderConstants.RK.equals(order.getOrderType())){
            //发货单
            list = deliverDetailDao.findDetailsByOrderId(order.getOrderId());
        }

        model.addAttribute("list", list);

        model.addAttribute("order", order);
        model.addAttribute("img", checkPendingOrderService.getVoucherImg(order));
        return "modules/supply/order/checkPendingOrderAuditForm";
    }

    /**
     * 审批通过
     */
    @ResponseBody
    @RequestMapping(value = "auditOrder")
    public Result auditOrder(String orderId, String reason) {
        GuardUtils.notBlank(orderId, "orderId不能为空");
        logger.info(" into auditOrder, orderId={}, reason={}", orderId, reason);
        checkPendingOrderService.auditOrder(orderId, reason);
        return Result.ok();
    }

    /**
     * 审批 驳回
     */
    @ResponseBody
    @RequestMapping(value = "rejectOrder")
    public Result rejectOrder(String orderId, String reason) {
        GuardUtils.notBlank(orderId, "orderId不能为空");
        GuardUtils.notBlank(reason, "reason不能为空");
        logger.info(" into rejectOrder, orderId={}, reason={}", orderId, reason);
        checkPendingOrderService.rejectOrder(orderId, reason);
        return Result.ok();
    }

}
