package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcTradebillPaymentCheckRecordDomain;
import com.xhpc.activity.service.PaymentBillService;
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("/payment")
public class XhpcTradebillPaymentBillController extends BaseController {

    @Resource
    PaymentBillService paymentBillService;

    @Resource
    LogUserUtils logUserUtils;


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


    @GetMapping("/checkPage")
    public TableDataInfo getCheckPage(HttpServletRequest request, @RequestParam(required = false) Integer uploadId,
                                      @RequestParam(required = false) String orderNumber,
                                      @RequestParam(required = false) String userId,
                                      @RequestParam(required = false) Integer status){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("uploadId", uploadId);
        params.put("orderNumber",orderNumber);
        params.put("userId", userId);
        params.put("status", status);
        params.put("tenantId", loginUser.getTenantId());
        return getDataTable(paymentBillService.getPaymentCheckList(params));
    }


    @GetMapping("/statusTotal")
    public AjaxResult getStatusTotal(HttpServletRequest request, @RequestParam(required = false) Integer uploadId,
                                     @RequestParam(required = false) String orderNumber,
                                     @RequestParam(required = false) String userId,
                                     @RequestParam(required = false) Integer status){

        LoginUser loginUser = logUserUtils.getLogUser(request);
        Map<String, Object> params = new HashMap<>();
        params.put("uploadId", uploadId);
        params.put("orderNumber",orderNumber);
        params.put("userId", userId);
        params.put("status", status);
        params.put("tenantId", loginUser.getTenantId());
        return AjaxResult.success(paymentBillService.getStatusTotal(params));
    }


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

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


    @Log(title = "人工确认支付平台对账数据", businessType = BusinessType.UPDATE)
    @PostMapping("/checkInfo")
    public AjaxResult updateCheckInfo(@RequestBody XhpcTradebillPaymentCheckRecordDomain domain){
        return AjaxResult.success(paymentBillService.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 userId,
                       @RequestParam(required = false) Integer status) throws Exception {

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

        paymentBillService.export(response, params);
    }

}
