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.dao.MaterialOutStockOrderDao;
import com.sifang.modules.supply.order.dao.MaterialOutStockOrderDetailDao;
import com.sifang.modules.supply.order.request.OutStockReq;
import com.sifang.modules.supply.order.service.OutStockOrderService;
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.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.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/main/outStock")
public class MpOutStockController extends BaseExceptionController {

    private Logger logger = LoggerFactory.getLogger(MpOutStockController.class);

    @Autowired
    private UserDao userDao;
    @Autowired
    private MaterialOutStockOrderDao materialOutStockOrderDao;
    @Autowired
    private MaterialOutStockOrderDetailDao materialOutStockOrderDetailDao;
    @Autowired
    private OutStockOrderService outStockOrderService;

    /**
     * 小程序出库单列表
     */
    @ResponseBody
    @RequestMapping(value = "outStockList")
    public Object outStockList(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        Operator operator = getOperatorBy(request);
        Ascription ascription = CrmUtils.getAscription(userDao.getById(operator.getUserId()));
        map.put("ascriptionId", ascription.getAscriptionId());
        logger.info(" into mp outStockList, params: {}", map);
        outStockOrderService.getOutStockOrderPage(page, map);
        return page.renderJson();
    }

    /**
     * 小程序出库单详情
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "outStockOrderDetail")
    public Object inventoryOrderDetail(String id) {
        GuardUtils.notBlank(id, "出库单id不能为空");
        Record order = materialOutStockOrderDao.findById(id);
        List<Record> orderDetails = materialOutStockOrderDetailDao.listDetailByMainId(id);
        order.set("details", CrmUtils.returnListMap(orderDetails));
        return renderJson(Result.success(order.getColumns()));
    }

    /**
     * 小程序 保存出库单
     */
    @ResponseBody
    @RequestMapping(value = "saveOutStockOrder")
    public Result saveOutStockOrder(@RequestBody @Valid OutStockReq req) {
        logger.info(" into mp saveOutStockOrder, req={}", req);
        GuardUtils.notBlank(req.getUserId(), "userId不能为空");
        outStockOrderService.saveOrder(req);
        return Result.ok();
    }
}
