package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcTradebillInternetCheckRecordDomain;
import com.xhpc.activity.service.InternetBillService;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
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.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/internet")
public class XhpcTradebillInternetBillController extends BaseController {


    @Resource
    InternetBillService internetBillService;

    @Resource
    LogUserUtils logUserUtils;

    @GetMapping("/checkList/{uploadId}")
    public AjaxResult getCheckList(@RequestParam(required = false) Integer uploadId,
                                   @RequestParam(required = false) String orderNumber,
                                   @RequestParam(required = false) String stationName,
                                   @RequestParam(required = false) String internetName,
                                   @RequestParam(required = false) Integer status) {
        Map<String, Object> params = new HashMap<>();
        params.put("uploadId", uploadId);
        params.put("orderNumber", orderNumber);
        params.put("stationName", stationName);
        params.put("internetName", internetName);
        params.put("status", status);
        return AjaxResult.success(internetBillService.getInternetCheckList(params));
    }


    @GetMapping("/checkPage")
    public TableDataInfo getCheckPage(HttpServletRequest request,
                                      @RequestParam(required = false) String tenantId,
                                      @RequestParam(required = false) Long operatorId,
                                      @RequestParam(required = false) Long chargingStationId,
                                      @RequestParam(required = false) Integer uploadId,
                                      @RequestParam(required = false) String orderNumber,
                                      @RequestParam(required = false) String station,
                                      @RequestParam(required = false) String internetName,
                                      @RequestParam(required = false) Integer status) {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        if (StringUtils.isEmpty(tenantId)) {
            tenantId = loginUser.getTenantId();
        }

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", tenantId);
        params.put("operatorId", operatorId);
        params.put("chargingStationId", chargingStationId);
        params.put("uploadId", uploadId);
        params.put("orderNumber", orderNumber);
        params.put("stationName", station);
        params.put("internetName", internetName);
        params.put("status", status);
        startPage();
        return getDataTable(internetBillService.getInternetCheckList(params));
    }


    @GetMapping("/statusTotal")
    public AjaxResult getStatusTotal(HttpServletRequest request,
                                     @RequestParam(required = false) String tenantId,
                                     @RequestParam(required = false) Long operatorId,
                                     @RequestParam(required = false) Long chargingStationId,
                                     @RequestParam(required = false) Integer uploadId,
                                     @RequestParam(required = false) String orderNumber,
                                     @RequestParam(required = false) String stationName,
                                     @RequestParam(required = false) String internetName,
                                     @RequestParam(required = false) Integer status) {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        if (StringUtils.isEmpty(tenantId)) {
            tenantId = loginUser.getTenantId();
        }

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", tenantId);
        params.put("operatorId", operatorId);
        params.put("chargingStationId", chargingStationId);
        params.put("uploadId", uploadId);
        params.put("orderNumber", orderNumber);
        params.put("stationName", stationName);
        params.put("internetName", internetName);
        params.put("status", status);
        return AjaxResult.success(internetBillService.getStatusTotal(params));
    }


    @GetMapping("/checkInfo/{id}")
    public AjaxResult getCheckInfo(@PathVariable Integer id) {

        return AjaxResult.success(internetBillService.getInfoByPk(id));
    }


    @Log(title = "人工确认流量方对账数据", businessType = BusinessType.UPDATE)
    @PostMapping("/checkInfo")
    public AjaxResult updateCheckInfo(@RequestBody XhpcTradebillInternetCheckRecordDomain domain) {

        return AjaxResult.success(internetBillService.updateInfoByDomain(domain));
    }


    @Log(title = "导出流量方对账数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(required = false) Integer uploadId,
                       @RequestParam(required = false) String orderNumber,
                       @RequestParam(required = false) String stationName,
                       @RequestParam(required = false) String internetName,
                       @RequestParam(required = false) Integer status) throws Exception {

        Map<String, Object> params = new HashMap<>();
        params.put("uploadId", uploadId);
        params.put("orderNumber", orderNumber);
        params.put("stationName", stationName);
        params.put("internetName", internetName);
        params.put("status", status);

        internetBillService.export(response, params);
    }


    @GetMapping("/getXhpcTradebillUploadRecordcheckPage")
    public TableDataInfo getXhpcTradebillUploadRecordcheckPage(HttpServletRequest request, HttpServletResponse response) {
        LoginUser loginUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("userId", loginUser.getUserid());
        params.put("type", 0);
        if (loginUser.getUserid() != 0) {
            params.put("type", 1);
        }
        startPage();
        return getDataTable(internetBillService.getXhpcTradebillUploadRecordcheckPage(params));
    }


}
