package com.fs.api.controller;

import com.fs.api.param.ExpressParam;
import com.fs.api.param.OrderListParam;
import com.fs.api.vo.ExpressVO;
import com.fs.api.vo.OrderListVO;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.company.domain.Company;
import com.fs.company.service.ICompanyService;
import com.fs.store.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 订单Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/api")
public class CompanyAPIController extends BaseController
{
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IFsStoreOrderService storeOrderService;
    @Autowired
    private IFsExpressService expressService;

    private Company checkCompany(HttpServletRequest request){
        String appId=request.getHeader("appId");
        String appKey=request.getHeader("appKey");
        if(appId==null){
            throw new CustomException("appId不能为空");
        }
        if(appKey==null){
            throw new CustomException("appKey不能为空");
        }
        Company company=companyService.selectCompanyByAppId(appId);
        if(company==null){
            throw new CustomException("appId不存在");
        }
        return company;
    }

    @GetMapping("/getOrderList")
    public R getOrderList(OrderListParam param, HttpServletRequest request)
    {
        Company company=checkCompany(request);
        param.setCompanyId(company.getCompanyId());
        List<OrderListVO> list = storeOrderService.selectCompanyStoreOrderListAPI(param);
        return R.ok().put("data",list);
    }


    @GetMapping("/getExpressInfo")
    @Deprecated
    public R getExpressInfo(ExpressParam param, HttpServletRequest request) {
        Company company=checkCompany(request);
        List<ExpressVO> list = expressService.getExpressInfoAPI(param);
        return R.ok().put("data",list);
    }



}
