package com.jkcredit.invoice.controller.localBussiness;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.model.entity.Calculate.NoCarCalculateInfor;
import com.jkcredit.invoice.model.entity.Calculate.SelfCarCalculateInfor;
import com.jkcredit.invoice.model.entity.invoice.SelfCarInvoice;
import com.jkcredit.invoice.model.entity.waybill.NoCarWayBill;
import com.jkcredit.invoice.model.entity.waybill.SelfCarTrade;
import com.jkcredit.invoice.service.CalculateInfor.SelfCarCalculateInfoService;
import com.jkcredit.invoice.service.selfCar.SelfCarInvoiceService;
import com.jkcredit.invoice.service.selfCar.SelfCarService;
import com.jkcredit.invoice.service.selfCar.SelfCarTradeService;
import com.jkcredit.invoice.util.RespR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "自有车操作")
@RestController
@RequestMapping(value = {"/selfCar"})
public class SelfCarController {
    @Autowired
    SelfCarService selfCarService;
    @Autowired
    SelfCarTradeService selfCarTradeService;
    @Autowired
    SelfCarInvoiceService selfCarInvoiceService;
    @Autowired
    SelfCarCalculateInfoService selfCarCalculateInfoService;
    /**
     * 解绑etc卡
     * @param etcNum    参数集
     * @return 用户集合
     */
    @PostMapping("/selfCarUnBind")
    @ApiOperation(value="解绑etc卡", notes="解绑etc卡")
    @LoginRequired
    public RespR selfCarUnBind(String etcNum) {
        try {
            RespR respR = selfCarService.selfCarUnBind(etcNum);
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }

    /**
     * 分页查询自有车交易信息
     *
     * @param page    参数集
     * @return 交易信息
     */
    @PostMapping("/findTrades")
    @ApiOperation(value="分页查询自有车交易信息", notes="分页查询自有车交易信息")
    @LoginRequired
    public RespR findTrades(Page page, SelfCarTrade selfCarTrade) {
        try {
            setTimeDue(selfCarTrade);
            RespR respR = new RespR(selfCarTradeService.findByPageAndTrade(page, selfCarTrade));
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }
    public void setTimeDue(SelfCarTrade selfCarTrade){
        if(!StringUtils.isEmpty(selfCarTrade.getAclTimeBegin()) && !"null".equals(selfCarTrade.getAclTimeBegin())){
            String [] res = selfCarTrade.getAclTimeBegin().split(",");
            selfCarTrade.setAclTimeBegin(res[0]);
            selfCarTrade.setAclTimeEnd(res[1]);
        }else{
            selfCarTrade.setAclTimeBegin(null);
        }
        if(!StringUtils.isEmpty(selfCarTrade.getExTimeBegin()) && !"null".equals(selfCarTrade.getExTimeBegin())){
            String [] res = selfCarTrade.getExTimeBegin().split(",");
            selfCarTrade.setExTimeBegin(res[0]);
            selfCarTrade.setExTimeEnd(res[1]);
        }else{
            selfCarTrade.setExTimeBegin(null);
        }
    }
    /**
     * 分页查询所有发票信息
     *
     * @param page    参数集
     * @return 交易信息
     */
    @PostMapping("/findSelfCarInvoices")
    @ApiOperation(value="分页查询自有车发票信息", notes="分页查询自有车发票信息")
    @LoginRequired
    public RespR findSelfCarInvoices(Page page, SelfCarInvoice selfCarInvoice) {
        try {
            if(!StringUtils.isEmpty(selfCarInvoice.getInvoiceMakeTime()) && !"null".equals(selfCarInvoice.getInvoiceMakeTime())){
                String [] res =selfCarInvoice.getInvoiceMakeTime().split(",");
                selfCarInvoice.setInvoiceMakeStart(res[0]);
                selfCarInvoice.setInvoiceMakeEnd(res[1]);
            }
            RespR respR = new RespR(selfCarInvoiceService.findByPageAndInvoice(page, selfCarInvoice));
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }

    /**
     * 分页查询自有车计费信息
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findSelfcarCalculateInfo")
    @ApiOperation(value="分页查询自有车计费信息", notes="分页查询自有车计费信息")
    @LoginRequired
    public RespR findSelfcarCalculateInfo(Page page, SelfCarCalculateInfor carCalculateInfor) {
        try {
            RespR respR = new RespR(selfCarCalculateInfoService.findByPageAndCalculateInfor(page, carCalculateInfor));
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }
}
