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.supply.order.entity.CrmSupplyMaterialDeliverOrderMsg;
import com.sifang.modules.supply.order.request.InStockReq;
import com.sifang.modules.supply.order.service.DeliverOrderService;
import com.sifang.modules.supply.order.service.InStockOrderService;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
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.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/inStock")
public class MpInStockOrderController extends BaseExceptionController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    DeliverOrderService deliverOrderService;
    @Autowired
    InStockOrderService inStockOrderService;
    @Autowired
    UserDao userDao;

    /**
     * 入库单列表
     */
    @ResponseBody
    @RequestMapping("/inStockList")
    public String inStockList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        logger.info(" mp inStockList, params: {}", map);
        LayuiPage<Record> page = new LayuiPage<>(request);
        Operator operator = getOperatorBy(request);
        map.put("inAscriptionId", CrmUtils.getAscription(userDao.get(operator.getUserId())).getAscriptionId());
        map.put("inStockOnly", "1");
        deliverOrderService.deliverOrderListPage(page, map);
        return page.renderJson();
    }

    /**
     * 入库单详情
     */
    @ResponseBody
    @RequestMapping("/inStockOrderDetail")
    public Object inStockOrderDetail(String id){
        logger.info("mp inStockOrderDetail, id: {}", id);
        Record order = deliverOrderService.findOrderById(id);
        List<Record> details = deliverOrderService.findDetailsByOrderId(id);
        //过程记录
        List<CrmSupplyMaterialDeliverOrderMsg> msgList = deliverOrderService.listMsgByOrderId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("order", order);
        map.put("details", details);
        map.put("msgList", msgList);
        return renderJson(Result.success(map));
    }

    /**
     * 仓库打回
     */
    @ResponseBody
    @RequestMapping("/giveBack")
    public Result giveBack(String id, String reason, HttpServletRequest request){

        return Result.fail("不可操作打回");
      /*  logger.info("mp giveBack, params: {}", getParams(request));
        GuardUtils.notBlank(reason, "打回原因不能为空");
        inStockOrderService.giveBack(id, reason, getOperatorBy(request));
        return Result.ok();*/
    }

    /**
     * 入库
     */
    @ResponseBody
    @RequestMapping("/inStock")
    public Result inStock(@Valid @RequestBody InStockReq req, HttpServletRequest request){
        logger.info("mp inStock, req: {}", req);
        inStockOrderService.inStock(req, getOperatorBy(request));
        return Result.ok();
    }
}
