package com.xhpc.activity.controller;


import com.xhpc.activity.service.XhpcClearingHistoryOrderService;
import com.xhpc.activity.vo.ClearingOrderVo;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.exception.CustomException;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.util.LogUserUtils;
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.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/clearing/order")
public class XhpcClearingHistoryOrderController extends BaseController {

    @Resource
    XhpcClearingHistoryOrderService orderService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getUnclearingPage(HttpServletRequest request,
                                           String tenantId,
                                           Long operatorId,
                                           Long chargingStationId,
                                           Long chargingPileId,
                                           Long terminalId,
                                           String startTime,
                                           String endTime,
                                           String orderNo,
                                           String userType,
                                           String internetName) {
        LoginUser loginUser = logUserUtils.getLogUser(request);

        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("orderNo", orderNo);
        params.put("userType", userType);
        params.put("internetName", internetName);
        params.put("tenantId", tenantId);
        params.put("operatorId", operatorId);
        params.put("stationId", chargingStationId);
        params.put("chargingPileId", chargingPileId);
        params.put("terminalId", terminalId);
        return getDataTable(orderService.getPage(params));
    }


    @Log(title = "清分管理-审核待清分清单", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public R updateDomainConfig(HttpServletRequest request, @RequestBody ClearingOrderVo domain) {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCheckBy(loginUser.getUserid().toString());
        return R.ok(orderService.checkDomainList(domain));
    }


    @Log(title = "清分管理-导出待清分清单", businessType = BusinessType.UPDATE)
    @PostMapping("/export")
    public void getExport(HttpServletResponse response,
                          HttpServletRequest request,
                          String startTime,
                          String endTime,
                          String orderNo,
                          String userType,
                          String internetName,
                          String operatorId,
                          String stationId,
                          String orderIds) throws IOException {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("orderNo", orderNo);
        params.put("userType", userType);
        params.put("internetName", internetName);
        params.put("operatorId", operatorId);
        params.put("stationId", stationId);
        params.put("orderIds", orderIds);
        orderService.export(response, params);
    }


    // ============================== 清分 ============================
    @GetMapping("/getOrderPage")
    public TableDataInfo getOrderPage(HttpServletRequest request, String operatorId,
                                      @RequestParam(value = "orderNo", required = false, defaultValue = "") String orderNo,
                                      @RequestParam(value = "stationId", required = false, defaultValue = "") String stationId,
                                      @RequestParam(value = "source", required = false, defaultValue = "") String source,
                                      @RequestParam(value = "startTime", required = false, defaultValue = "") String startTime,
                                      @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime,
                                      @RequestParam(value = "status", required = false, defaultValue = "") String status){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        Map<String, Object> params = new HashMap<>();

        if(StringUtils.isEmpty(operatorId)){
            operatorId = loginUser.getSysUser().getOperatorId() + "";
        }
        params.put("operatorId", operatorId);
        params.put("orderNo", orderNo);
        params.put("stationId", stationId);
        params.put("source", source);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("status", status);
        if(params.get("operatorId") == null || StringUtils.isEmpty(params.get("operatorId").toString()) || "null".equals(params.get("operatorId").toString())){
            throw new CustomException("运营商ID错误,请登陆运营商账户再试");
        }
        return getDataTable(orderService.getOrderPage(params));
    }


    @GetMapping("/getOperatorData")
    public R getOperatorData(HttpServletRequest request, String operatorId){
        LoginUser loginUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();

        if(StringUtils.isEmpty(operatorId)){
            operatorId = loginUser.getSysUser().getOperatorId() + "";
        }

        params.put("operatorId", operatorId);
        if(params.get("operatorId") == null || StringUtils.isEmpty(params.get("operatorId").toString()) || "null".equals(params.get("operatorId").toString())){
            throw new CustomException("运营商ID错误,请登陆运营商账户再试");
        }

        return R.ok(orderService.getOperatorData(params));
    }
}
