package com.jkcredit.invoice.controller.localBussiness;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.mapper.waybill.NoCarWaybillMapper;
import com.jkcredit.invoice.model.entity.Calculate.NoCarCalculateInfor;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
import com.jkcredit.invoice.model.entity.invoice.BillInvoice;
import com.jkcredit.invoice.model.entity.waybill.NoCarWayBill;
import com.jkcredit.invoice.service.CalculateInfor.NoCarCalculateInfoService;
import com.jkcredit.invoice.service.lowerService.NoCarService;
import com.jkcredit.invoice.service.nocar.NoCarBillWayService;
import com.jkcredit.invoice.service.nocar.NoCarRecService;
import com.jkcredit.invoice.service.nocar.NocarInvoiceService;
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;

import java.util.List;

@Api(tags = "无车操作")
@RestController
@RequestMapping(value = {"/noCar"})
public class NoCarController {
    @Autowired
    NoCarRecService noCarRecService;

    @Autowired
    NoCarBillWayService noCarBillWayService;

    @Autowired
    NocarInvoiceService nocarInvoiceService;

    @Autowired
    NoCarCalculateInfoService noCarCalculateInfoService;

    @Autowired
    NoCarWaybillMapper noCarWaybillMapper;

    @Autowired
    NoCarService noCarService;
    /**
     * 分页查询无车备案信息
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findCarRec")
    @ApiOperation(value="分页查询无车备案信息", notes="分页查询无车备案信息")
    @LoginRequired
    public RespR getCustomersByPage(Page page, CustomerCarRec customerCarRec) {
        try {
            RespR respR = new RespR(noCarRecService.findByPageAndCarRec(page, customerCarRec));
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }
    public void setTimeDue(NoCarWayBill noCarWayBill){
        if(!StringUtils.isEmpty(noCarWayBill.getStartBegin()) && !"null".equals(noCarWayBill.getStartBegin())){
            String [] res = noCarWayBill.getStartBegin().split(",");
            noCarWayBill.setStartBegin(res[0]);
            noCarWayBill.setStartEnd(res[1]);
        }else{
            noCarWayBill.setStartBegin(null);
        }
        if(!StringUtils.isEmpty(noCarWayBill.getEndBegin()) && !"null".equals(noCarWayBill.getEndBegin())){
            String [] res = noCarWayBill.getEndBegin().split(",");
            noCarWayBill.setEndBegin(res[0]);
            noCarWayBill.setEndEnd(res[1]);
        }else{
            noCarWayBill.setEndBegin(null);
        }
    }
    /**
     * 分页查询运单信息
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findBillWay")
    @ApiOperation(value="分页查询无车运单信息", notes="分页查询无车运单信息")
    @LoginRequired
    public RespR findBillWay(Page page, NoCarWayBill noCarWayBill) {
        try {
            setTimeDue(noCarWayBill);
            RespR respR = new RespR(noCarBillWayService.findByPageAndWayBill(page, noCarWayBill));
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }

    /**
     * 重新获取发票并更新状态
     *
     * @return 用户集合
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value="重新获取发票并更新状态", notes="重新获取发票并更新状态")
    @LoginRequired
    public RespR updateStatus() {
        try {
            delGetInvoce(0);
            delGetInvoce(1);
            return new RespR("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }
    public void delGetInvoce(Integer historyFlag){
        NoCarWayBill carWayBill = new NoCarWayBill();
        carWayBill.setHisFlag(historyFlag);
        carWayBill.setBillwayStatus(2);
        List<NoCarWayBill> noCarWayBillsMdEnd = noCarWaybillMapper.selectByHisFlagAndBillStatus(carWayBill);
        if(noCarWayBillsMdEnd!=null && noCarWayBillsMdEnd.size()>0){
            noCarWayBillsMdEnd.stream().forEach(noCarWayBill -> {
                noCarService.getInvoiceByWayBillNumReal(noCarWayBill,true);
            });
        }
    }
    /**
     * 分页查询异常运单信息
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findBillWayException")
    @ApiOperation(value="分页查询异常运单信息", notes="分页查询异常运单信息")
    @LoginRequired
    public RespR findBillWayException(Page page, NoCarWayBill noCarWayBill) {
        try {
            setTimeDue(noCarWayBill);
            RespR respR = new RespR(noCarBillWayService.findByPageAndWayBillException(page, noCarWayBill));
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }
    /**
     * 分页查询无车运单信息
     *
     * @param page    参数集
     * @return 用户集合
     */
    @PostMapping("/findNocarInvoices")
    @ApiOperation(value="分页查询无车发票信息", notes="分页查询无车发票信息")
    @LoginRequired
    public RespR findNocarInvoices(Page page, BillInvoice billInvoice) {
        try {
            if(!StringUtils.isEmpty(billInvoice.getInvoiceMakeTime()) && !"null".equals(billInvoice.getInvoiceMakeTime())){
                String [] res =billInvoice.getInvoiceMakeTime().split(",");
                billInvoice.setInvoiceMakeStart(res[0]);
                billInvoice.setInvoiceMakeEnd(res[1]);
            }
            RespR respR = new RespR(nocarInvoiceService.findByPageAndInvoice(page, billInvoice));
            return respR;
        }catch (Exception e){
            e.printStackTrace();
            return new RespR(false,e.getMessage());
        }
    }

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

    public void setTimeDue1(NoCarCalculateInfor carCalculateInfor){
        if(!StringUtils.isEmpty(carCalculateInfor.getInvoiceMakeTime()) && !"null".equals(carCalculateInfor.getInvoiceMakeTime())){
            String [] res = carCalculateInfor.getInvoiceMakeTime().split(",");
            carCalculateInfor.setInvoiceMakeStart(res[0]);
            carCalculateInfor.setInvoiceMakeEnd(res[1]);
        }else{
            carCalculateInfor.setInvoiceMakeTime(null);
        }
        if(!StringUtils.isEmpty(carCalculateInfor.getCalculateTime()) && !"null".equals(carCalculateInfor.getCalculateTime())){
            String [] res = carCalculateInfor.getCalculateTime().split(",");
            carCalculateInfor.setCalculateStart(res[0]);
            carCalculateInfor.setCalculateEnd(res[1]);
        }else{
            carCalculateInfor.setCalculateTime(null);
        }
    }
}
