package com.jkcredit.invoice.controller.interserver;

import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.annotation.annotationdes.AuthenticationInterceptor;
import com.jkcredit.invoice.mapper.calculateinfor.MounthAccMapper;
import com.jkcredit.invoice.mapper.customer.CustomerRecMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.model.entity.waybill.NoCarWayBill;
import com.jkcredit.invoice.model.vo.MigrateVo;
import com.jkcredit.invoice.service.lowerservice.CheckHasAuthRole;
import com.jkcredit.invoice.service.lowerservice.NoCarService;
import com.jkcredit.invoice.service.lowerservice.vo.HcInvoiceQueryVo;
import com.jkcredit.invoice.service.lowerservice.vo.MonthAccountQueryVo;
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.List;

/**
 * 无车下游接口
 * @author mumuxigua
 */
@Api(tags = "无车下游接口")
@RestController
@RequestMapping(value = {"/noCarService"})
public class NoCarServiceController {
    @Autowired
    MounthAccMapper mounthAccMapper;
    @Autowired
    private NoCarService noCarService;
    @Autowired
    private CustomerRecMapper customerRecMapper;
    @Autowired
    private CheckHasAuthRole checkHasAuthRole;

    /**
     * @param
     * @return
     */
    @PostMapping("/noCarVoiceQuery")
    @ApiOperation(value = "发票查询接口（走本地库）", notes = "发票查询接口（走本地库）")
    @LoginRequired
    public RespR noCarVoiceQuery(@RequestBody NoCarWayBill noCarWayBill, User user) {
        checkHasAuthRole.checkCustomerRole(user, noCarWayBill.getCustomerName());
        return noCarService.getInvoiceByWayBillNumReal(noCarWayBill, true);
    }

    /**
     * @param
     * @return
     */
    @PostMapping("/hCVoiceQuery")
    @ApiOperation(value = "红冲发票查询接口", notes = "红冲发票查询接口")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR hcvoicequery(@RequestBody HcInvoiceQueryVo hcInvoiceQueryVo) {
        return noCarService.hcinvoicequery(hcInvoiceQueryVo);
    }

    /**
     * @param
     * @return
     */
    @PostMapping("/hCVoiceUpdate")
    @ApiOperation(value = "红冲发票更新接口", notes = "红冲发票更新接口")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR hcvoiceupdate(@RequestBody HcInvoiceQueryVo hcInvoiceQueryVo) {
        return noCarService.hcinvoiceupdate(hcInvoiceQueryVo);
    }

    /**
     * @param
     * @return
     */
    @PostMapping("/monthAccQuery")
    @ApiOperation(value = "月账单交易查询", notes = "月账单交易查询")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR monthAccQuery(@RequestBody MonthAccountQueryVo monthAccountQueryVo) {
        return noCarService.monthAccQuery(monthAccountQueryVo);
    }

    /**
     * @param
     * @return
     */
    @PostMapping("/monthAccQueryAll")
    @ApiOperation(value = "月账单交易查询", notes = "月账单交易查询")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR monthAccQueryAll() {
        return new RespR(mounthAccMapper.selectAllMounthAcc());
    }

    /**
     * @param
     * @return
     */
    @PostMapping("/monthAccUpdate")
    @ApiOperation(value = "月账单交易查询更新", notes = "月账单交易查询更新")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR monthAccQueryUpdate(@RequestBody MonthAccountQueryVo monthAccountQueryVo) {
        if(StringUtils.isEmpty(monthAccountQueryVo.getCompanyName())){
            List<CustomerRec> companys = customerRecMapper.selectAllNoCarComapany();
            mounthAccMapper.deleteAll();
            for (int i = 0; i < companys.size(); i++) {
                monthAccountQueryVo.setCompanyName(companys.get(i).getCompanyName());
                noCarService.monthAccQueryUpdate(monthAccountQueryVo);

            }
        }else{
            mounthAccMapper.deleteByCompanyName(monthAccountQueryVo.getCompanyName());
            noCarService.monthAccQueryUpdate(monthAccountQueryVo);
        }

        return new RespR(true);
    }

    /**
     * @param customerCarRecs
     * @return
     */
    @PostMapping("/customerCarRec")
    @ApiOperation(value = "", notes = "车辆备案")
    @LoginRequired
    public RespR customerCarRec(@RequestBody List<CustomerCarRec> customerCarRecs, User user) {
        if (customerCarRecs != null && customerCarRecs.size() > 0) {
            for (CustomerCarRec customerCarRec : customerCarRecs) {
                checkHasAuthRole.checkCustomerRole(user, customerCarRec.getCustomerName());
            }

        }
        return noCarService.customerCarRec(customerCarRecs);
    }

    /**
     * @param customerCarRec
     * @return
     */
    @PostMapping("/customeRecUpperQuery")
    @ApiOperation(value = "车辆备案信息查询", notes = "用户备案信息查询")
    @LoginRequired
    public RespR customeRecUpperQuery(@RequestBody CustomerCarRec customerCarRec, User user) {
        checkHasAuthRole.checkCustomerRole(user, customerCarRec.getCustomerName());
        return noCarService.customerCarRecQueryUpper(customerCarRec);
    }


    /**
     * @param customerCarRec
     * @return
     */
    @PostMapping("/customerCarRecQuery")
    @ApiOperation(value = "", notes = "车辆备案查询")
    @LoginRequired
    public RespR customerCarRecQuery(@RequestBody CustomerCarRec customerCarRec, User user) {
        checkHasAuthRole.checkCustomerRole(user, customerCarRec.getCustomerName());
        return noCarService.customerCarRecQuery(customerCarRec);
    }

    /**
     * @param
     * @return
     */
    @PostMapping("/migrateData")
    @ApiOperation(value = "", notes = "按时间迁移数据(月)")
    @LoginRequired
    public RespR migrateData(@RequestBody MigrateVo migrateVo, User user) {
        if(!(StringUtils.isEmpty(migrateVo.getStartMounth())|| StringUtils.isEmpty(migrateVo.getEndmounth())))
        noCarService.migrateData(migrateVo);
        return  new RespR(true);
    }

}
