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

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.sys.common.entity.User;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping(value = "${adminPath}/main/traderOrder")
public class MpTraderOrderController extends BaseController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DeliverOrderService deliverOrderService;

    /**
     * 我的订单
     */
    @RequestMapping(value = "deliverOrderList")
    public Result<List<Map<String, Object>>> deliverOrderList(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        String userId = params.get("userId");
        User user = userDao.getById(userId);
        String traderId = "";
        if (User.USER_TYPE_ADVISOR.equals(user.getUserType())) {
            traderId = CrmUtils.getTraderId(user.getId());
        } else if (User.USER_TYPE_KJ.equals(user.getUserType())) {
            traderId = CrmUtils.getTraderIdByUserId(user.getId());
        }
        params.put("traderId", traderId);
        LayuiPage<Record> page = new LayuiPage<>(request);
        deliverOrderService.deliverOrderListPage(page, params);
        List<Map<String, Object>> retList = CrmUtils.turnRecordListToMapList(page.getData());
        return Result.success(retList);
    }

    /**
     * 订单详情
     */
    @RequestMapping(value = "deliverOrderDetail")
    public Result deliverOrderDetail(String id) {
        Map<String, Object> map = new HashMap<>();
        Record order = deliverOrderService.findOrderById(id);
        map.put("order", order);
        List<Record> details = deliverOrderService.findDetails(id);
        map.put("details", details);
        return Result.success(map);
    }

}
