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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.warehouse.dao.InventoryOrderDao;
import com.sifang.modules.supply.warehouse.dao.InventoryOrderDetailDao;
import com.sifang.modules.supply.warehouse.service.InventoryOrderService;
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.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}/main/smInventory")
public class SmInventoryController extends BaseExceptionController {

    Logger logger = LoggerFactory.getLogger(SmInventoryController.class);

    @Autowired
    private UserDao userDao;
    @Autowired
    private InventoryOrderDao inventoryOrderDao;
    @Autowired
    private InventoryOrderDetailDao inventoryOrderDetailDao;
    @Autowired
    private InventoryOrderService inventoryOrderService;

    /**
     * 移动端 只有代理商可以登录
     */
    @ResponseBody
    @RequestMapping(value = "listInventoryOrder")
    public Object listInventoryOrder(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("inventoryObjectId", ascription.getAscriptionId());
        logger.info(" into listInventoryOrder, map={}", map);

        inventoryOrderService.getInventoryOrderPage(page, map);
        return page.renderJson();
    }

    @ResponseBody
    @RequestMapping(value = "inventoryOrderDetail")
    public Object inventoryOrderDetail(String id) {
        GuardUtils.notBlank(id, "盘点单id不能为空");
        Record order = inventoryOrderDao.findById(id);
        List<Record> orderDetails = inventoryOrderDetailDao.listDetailByMainId(id);
        order.set("details", CrmUtils.returnListMap(orderDetails));
        return renderJson(Result.success(order.getColumns()));
    }

}
