package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcClearingCheckoutDomain;
import com.xhpc.activity.service.XhpcClearingCheckoutService;
import com.xhpc.activity.vo.ClearingReceiptVo;
import com.xhpc.common.core.annotation.NoRepeatSubmit;
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.List;
import java.util.Map;

@RestController
@RequestMapping("/clearing/checkout")
public class XhpcClearingCheckoutController extends BaseController {

    @Resource
    XhpcClearingCheckoutService checkoutService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request,
                                 String status,
                                 Long operatorId,
                                 String startTime,
                                 String endTime,
                                 String tenantId) {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        if(StringUtils.isEmpty(tenantId)){
            tenantId = loginUser.getTenantId();
        }
        if("-1".equals(tenantId)){
            tenantId = "";
        }

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", tenantId);
        params.put("status", status);
        params.put("operatorId", operatorId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return getDataTable(checkoutService.getPage(params));
    }


    @GetMapping("/getCheckoutPage")
    public TableDataInfo getCheckoutPage(Long operatorId,
                       String tenantId,String startTime,String endTime) {

        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", tenantId);
        params.put("operatorId", operatorId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return getDataTable(checkoutService.getCheckoutPage(params));
    }


    @GetMapping("/detail")
    public R getDetail(@RequestParam("checkoutId") Long checkoutId) {
        return R.ok(checkoutService.getDomainByPk(checkoutId));
    }


    @GetMapping("/temp")

    public R getTmpCheckoutData(HttpServletRequest request) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        Long operatorId = loginUser.getSysUser().getOperatorId();
        if(operatorId == null){
            throw new CustomException("运营商ID错误,请登陆运营商账户再试");
        }

        return R.ok(checkoutService.getTmpCheckoutData(operatorId));
    }


    @Log(title = "清分管理-新增提现申请", businessType = BusinessType.INSERT)
    @PostMapping("/")
    @NoRepeatSubmit(lockTime = 5)
    public R insertDomain(HttpServletRequest request, @RequestBody XhpcClearingCheckoutDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCreateBy(loginUser.getUserid().toString());
        domain.setUpdateBy(loginUser.getUserid().toString());
        domain.setTenantId(loginUser.getTenantId());

        if (domain.getOperatorId() == null || domain.getOperatorId() == 0){
            domain.setOperatorId(loginUser.getSysUser().getOperatorId());
        }
        if(domain.getOperatorId() == null){
            throw new CustomException("运营商ID错误,请登陆运营商账户再试");
        }
        return R.ok(checkoutService.insertDomain(domain));
    }


    /**
     * 导出银行转账单
     *
     * @param response
     * @throws IOException
     */
    @Log(title = "清分管理-导出待提现订单", businessType = BusinessType.INSERT)
    @PostMapping("/bankExport")
    public void getBankExport(HttpServletResponse response, String checkoutIds) throws IOException {

        checkoutService.bankExport(response, checkoutIds);
    }


    /**
     * 导出提现单订单记录
     *
     * @param response
     * @throws IOException
     */
    @Log(title = "清分管理-导出提现记录", businessType = BusinessType.INSERT)
    @PostMapping("/orderExport")
    public void getOrderExport(HttpServletResponse response, String checkoutIds) throws IOException {

        checkoutService.orderExport(response, checkoutIds);
    }


    @Log(title = "清分管理-导入银行回执", businessType = BusinessType.INSERT)
    @PostMapping("/importReceipt")
    public R importReceipt(HttpServletRequest request, @RequestBody ClearingReceiptVo receiptVo) throws Exception {

        if(StringUtils.isEmpty(receiptVo.getFile()) || receiptVo.getReceiptList().size() < 1){
            throw new CustomException("Excel文件和回执图片必传");
        }

        LoginUser loginUser = logUserUtils.getLogUser(request);
        receiptVo.setCreateBy(loginUser.getUserid().toString());
        return R.ok(checkoutService.insertReceipt(receiptVo));
    }


    @GetMapping("/getListPage")
    public TableDataInfo getListPage(HttpServletRequest request, String phone, String transactionNumber, @RequestParam("status") Integer status, String chargingStationName, Long operatorId, Integer source, String beginStartTime, String beginEndTime, @RequestParam("userId") Long userId, Integer type,
                                     String affiliationOrganization, String evcsOrderNo, String plateNum, Integer internetId, String internetSerialNumber, String terminalName, String vinCode, String overStartTime, String overEndTime, Long personnelId, Integer confirmResult, Integer checkoutId) {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        if(operatorId == null || operatorId == 0){
            operatorId = loginUser.getSysUser().getOperatorId();
        }

        List<Map<String, Object>> listPage = checkoutService.getListPage(phone, transactionNumber, status, chargingStationName, operatorId, source, beginStartTime, beginEndTime, userId, type, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, checkoutId);
        return getDataTable(listPage);
    }


    @PostMapping("/getListPageExport")
    public void getListPageExport(HttpServletRequest request, HttpServletResponse response,String phone, String transactionNumber, @RequestParam("status") Integer status, String chargingStationName, Long operatorId, Integer source, String beginStartTime, String beginEndTime, @RequestParam("userId") Long userId, Integer type,
                                     String affiliationOrganization, String evcsOrderNo, String plateNum, Integer internetId, String internetSerialNumber, String terminalName, String vinCode, String overStartTime, String overEndTime, Long personnelId, Integer confirmResult, Integer checkoutId)throws IOException {
        LoginUser loginUser = logUserUtils.getLogUser(request);
        if(operatorId == null || operatorId == 0){
            operatorId = loginUser.getSysUser().getOperatorId();
        }

        checkoutService.getListPageExport(response,phone, transactionNumber, status, chargingStationName, operatorId, source, beginStartTime, beginEndTime, userId, type, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, checkoutId);

    }
}
