package com.xhpc.activity.controller;

import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.activity.service.OrderLogService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * 订单报文日志
 */
@RestController
@RequestMapping("/log/order/")
public class OrderLogController extends BaseController {

    @Resource
    OrderLogService orderLogService;

    @Resource
    LogUserUtils logUserUtils;

    @GetMapping("/getPage")
    public TableDataInfo getOrderPage(
            HttpServletRequest request,
            @RequestParam(required = false) String orderId,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer number){

        LoginUser loginUser = logUserUtils.getLogUser(request);

        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("phone", phone);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("number", number);

        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();

        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_SUPER_TYPE_ONE == loginUser.getSuperType()){
                params.put("tenantId",loginUser.getTenantId());
            }
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                params.put("logOperatorId",logOperatorId);
                params.put("number",1);
                //运营商看自己的场站
            }else{
                params.put("number",2);
                params.put("logOperatorId",sysUserId);
                //查询赋值的场站
            }
        }else{
            params.put("number",0);
        }
        return getDataTable(orderLogService.getOrderPage(params));
    }


    @GetMapping("/detail")
    public TableDataInfo getOrderDetailPage(@RequestParam("orderId") String orderId){
        startPage();
        return getDataTable(orderLogService.getOrderDetailPage(orderId));
    }


    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(required = false) String orderId,
                       @RequestParam(required = false) String tenantId,
                       @RequestParam(required = false) Integer number,
                       @RequestParam(required = false) String serialNumber) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("number", number);
        params.put("serialNumber", serialNumber);
        params.put("tenantId", tenantId);
        params.put("orderId", orderId);
        orderLogService.export(response, params);
    }

}
