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

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.order.service.DeliverOrderService;
import com.sifang.modules.supply.order.service.MaterialApplyService;
import com.sifang.modules.sys.common.UserUtils;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("${adminPath}/main/allOrder")
public class MpAllOrderController extends BaseExceptionController {

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

    @Autowired
    private DeliverOrderService deliverOrderService;
    @Autowired
    private MaterialApplyService materialApplyService;

    @Autowired
    private UserDao userDao;

    /**
     * 小程序全部订单列表
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Object allOrderList(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("applyTraderId", ascription.getAscriptionId());
        logger.info(" into mp allOrderList, params: {}", map);
        deliverOrderService.allOrderListPage(page, map);
        return page.renderJson();
    }

    /**
     * 详情
     */
    @ResponseBody
    @RequestMapping(value = "orderDetail")
    public Object orderDetail(String id) {
        logger.info(" into mp orderDetail, id: {}", id);
        Record order = deliverOrderService.getOrderDetail(id);
        //物料明细
        List<Record> materialDetails;
        List<Record> gatherDetails = new ArrayList<>();
        if (StrKit.notBlank(order.getStr("deliverOrderId"))) {
            //从发货单取物料明细
            materialDetails = deliverOrderService.findDetailsByOrderId(order.getStr("deliverOrderId"));
        } else {
            //从申请单取物料明细
            materialDetails = materialApplyService.listDetailsByApplyId(id);
            gatherDetails = materialApplyService.listGatherByApplyId(id);
        }
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("order", order);
        retMap.put("materialDetails", materialDetails);
        retMap.put("gatherDetails", gatherDetails);
        return JsonKit.toJson(Result.success(retMap));
    }

    /**
     * 删除申请单
     */
    @ResponseBody
    @RequestMapping("/deleteOrder")
    public Result deleteOrder(String ids, HttpServletRequest request){
        Operator operator = getOperatorBy(request);
        materialApplyService.deleteApplyOrder(ids, operator);
        return Result.ok();
    }

}
