package com.jkcredit.invoice.controller.interserver;

import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.CardTradeModel;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.annotation.annotationdes.AuthenticationInterceptor;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.service.lowerservice.CheckHasAuthRole;
import com.jkcredit.invoice.service.lowerservice.SelfCarCustService;
import com.jkcredit.invoice.service.lowerservice.vo.*;
import com.jkcredit.invoice.util.RespR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 自有车下游接口
 * @author mumuxigua
 */
@Api(tags = "自有车下游接口")
@RestController
@RequestMapping(value = {"/selfCarService"})
public class SelfCarServiceController {
    @Autowired
    SelfCarCustService selfCarService;
    @Autowired
    private CheckHasAuthRole checkHasAuthRole;

    /**
     * @param tradeRequestVo
     * @return
     */
    @PostMapping("/getTradeList")
    @ApiOperation(value = "获取交易Id列表", notes = "获取交易Id列表")
    @LoginRequired
    public RespR getTradeList(@RequestBody TradeRequestVo tradeRequestVo, User user) {
        checkHasAuthRole.checkCustomerRole(user, tradeRequestVo.getCustomerName());
        if (tradeRequestVo.getTradeStatus() == null) {
            tradeRequestVo.setTradeStatus(1);
            RespR<List<CardTradeModel>> respRbefore = selfCarService.getTradeList(tradeRequestVo);
            tradeRequestVo.setTradeStatus(2);
            RespR<List<CardTradeModel>> tradeList = selfCarService.getTradeList(tradeRequestVo);
            tradeRequestVo.setTradeStatus(3);
            RespR<List<CardTradeModel>> listRespR = selfCarService.getTradeList(tradeRequestVo);
            //合并展示
            List<CardTradeModel> cardTradeModels = new ArrayList<>();
            if (respRbefore.getCode() == 0) {
                cardTradeModels.addAll(respRbefore.getData());
            }
            if (tradeList.getCode() == 0) {
                cardTradeModels.addAll(tradeList.getData());
            }
            if (listRespR.getCode() == 0) {
                cardTradeModels.addAll(listRespR.getData());
            }
            return new RespR(cardTradeModels);
        }
        //合并展示

        return selfCarService.getTradeList(tradeRequestVo);
    }

    /**
     * @param invoiceApplVo
     * @return
     */
    @PostMapping("/applInvoice")
    @ApiOperation(value = "开票申请", notes = "开票申请")
    @LoginRequired
    public RespR applInvoice(@RequestBody InvoiceApplVo invoiceApplVo, User user) {
        checkHasAuthRole.checkCompanyRole(user, invoiceApplVo.getCompanyName());
        return selfCarService.applInvoice(invoiceApplVo);
    }

    /**
     * @param selfCarDueQueryVo
     * @return
     */
    @PostMapping("/getSelfCarInvoicesByTime")
    @ApiOperation(value = "已开发票查询", notes = "已开发票查询")
    @LoginRequired
    public RespR getSelfCarInvoicesByTime(@RequestBody SelfCarDueQueryVo selfCarDueQueryVo, User user) {
        checkHasAuthRole.checkCompanyRole(user, selfCarDueQueryVo.getCompanyName());
        return selfCarService.getSelfCarInvoicesByTime(selfCarDueQueryVo, null);
    }

    /**
     * @param invoicePackageVo
     * @return
     */
    @PostMapping("/getSelfCarInvoicePackage")
    @ApiOperation(value = "发票打包下载", notes = "发票打包下载")
    @LoginRequired
    public RespR getSelfCarInvoicePackage(@RequestBody InvoicePackageVo invoicePackageVo, User user) {
        checkHasAuthRole.checkCustomerRole(user, invoicePackageVo.getCustomerName());
        return selfCarService.getInvoicePackge(invoicePackageVo);
    }

    /**
     * @param applQueryInvVo
     * @return
     */
    @PostMapping("/getSelfCarInvoicesByAppl")
    @ApiOperation(value = "按申请id查询发票", notes = "按申请id查询发票")
    @LoginRequired
    public RespR getSelfCarInvoicesByAppl(ApplQueryInvVo applQueryInvVo) {
        if (StringUtils.isEmpty(applQueryInvVo.getApplId()) || StringUtils.isEmpty(applQueryInvVo.getCompanyNum()) || StringUtils.isEmpty(applQueryInvVo.getEtcNum())) {
            return new RespR(false, "参数不能为空");
        }
        return selfCarService.getSelfCarInvoicesByApplyIds(applQueryInvVo);
    }

    /**
     * @param etcQueryVo
     * @return
     */
    @PostMapping("/customerETCQuery")
    @ApiOperation(value = "", notes = "ETC绑定列表查询")
    @LoginRequired
    public RespR customeretcquery(@RequestBody EtcQueryVo etcQueryVo, User user) {
        checkHasAuthRole.checkCustomerRole(user, etcQueryVo.getCustomerName());
        return selfCarService.getEtcInfo(etcQueryVo);
    }


    /**
     * @param etcBindVo
     * @return
     */
    @PostMapping("/queryEtcInfo")
    @ApiOperation(value = "", notes = "备案ETC查询")
    @LoginRequired
    public RespR customerQueryEtcInfo(@RequestBody EtcBindVo etcBindVo, User user) {
        checkHasAuthRole.checkCustomerRole(user, etcBindVo.getCustomerName());
        return selfCarService.queryEtcInfo(etcBindVo);
    }

    /**
     * @param etcBindVo
     * @return
     */
    @PostMapping("/customerETCRec")
    @ApiOperation(value = "", notes = "ETC备案")
    @LoginRequired
    public RespR customerEtcRec(@RequestBody EtcBindVo etcBindVo, User user) {
        checkHasAuthRole.checkCompanyRole(user, etcBindVo.getCompanyName());
        return selfCarService.customerEtcRec(etcBindVo);
    }


    /**
     * @param etcValidVo
     * @return
     */
    @PostMapping("/customerETCRecValid")
    @ApiOperation(value = "", notes = "ETC备案验证")
    @LoginRequired
    public RespR customerEtcRecValid(@RequestBody EtcValidVo etcValidVo) {
        return selfCarService.customerEtcRecValid(etcValidVo);
    }

    /**
     * @param customerCarRecs
     * @return
     */
    @PostMapping("/customerCarUnRec")
    @ApiOperation(value = "", notes = "etc解绑")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR customerCarUnRec(@RequestBody List<CustomerCarRec> customerCarRecs) {
        return selfCarService.customerCarUnRec(customerCarRecs);
    }


    /**
     * @param tradeApplyQueryInvVo
     * @return
     */
    @PostMapping("/getApplyQueryByTradeIds")
    @ApiOperation(value = "按交易查申请", notes = "按交易查申请")
    @LoginRequired
    public RespR getApplyQueryByTradeId(TradeApplyQueryInvVo tradeApplyQueryInvVo) {
        try {
            if (StringUtils.isEmpty(tradeApplyQueryInvVo.getCompanyNum()) || StringUtils.isEmpty(tradeApplyQueryInvVo.getTradeId()) || StringUtils.isEmpty(tradeApplyQueryInvVo.getCardId())) {
                return new RespR(false, "参数不能为空");
            }

            List<TradeApplyQueryInvVo> tradeApplyQueryInvVos = new ArrayList<>();

            if (!org.springframework.util.StringUtils.isEmpty(tradeApplyQueryInvVo.getCompanyNum()) &&
                    !org.springframework.util.StringUtils.isEmpty(tradeApplyQueryInvVo.getCardId()) &&
                    !org.springframework.util.StringUtils.isEmpty(tradeApplyQueryInvVo.getTradeId())) {
                String[] companyNumArr = tradeApplyQueryInvVo.getCompanyNum().split(",");
                String[] cardIdArr = tradeApplyQueryInvVo.getCardId().split(",");
                String[] tradeIdArr = tradeApplyQueryInvVo.getTradeId().split(",");


                for (int i = 0; i < cardIdArr.length; i++) {
                    TradeApplyQueryInvVo tradeApplyQueryInvVoTmp = new TradeApplyQueryInvVo();
                    tradeApplyQueryInvVoTmp.setCardId(cardIdArr[i]);
                    if (tradeIdArr != null && tradeIdArr.length >= i + 1) {
                        tradeApplyQueryInvVoTmp.setTradeId(tradeIdArr[i]);
                    } else {
                        tradeApplyQueryInvVoTmp.setTradeId("");
                    }
                    if (companyNumArr != null && companyNumArr.length >= i + 1) {
                        tradeApplyQueryInvVoTmp.setCompanyNum(companyNumArr[i]);
                    } else {
                        tradeApplyQueryInvVoTmp.setCompanyNum("");
                    }
                    tradeApplyQueryInvVos.add(tradeApplyQueryInvVoTmp);
                }

            }
            return selfCarService.getApplyQueryByTradeIds(tradeApplyQueryInvVos);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespR(false, "查询数据有误，请排查");
        }
    }
}
