package com.jkcredit.invoice.service.customer.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkcredit.invoice.mapper.customer.CustomerOperMapper;
import com.jkcredit.invoice.mapper.user.UserMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerOper;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.service.customer.CustomerOperService;
import com.jkcredit.invoice.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 无车运单导入接口实现类
 * @author mumuxigua
 */
@Slf4j
@Service("customerOperService")
public class CustomerOperServiceImpl extends ServiceImpl<CustomerOperMapper, CustomerOper> implements CustomerOperService {


    @Autowired
    CustomerOperMapper customerOperMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 查找所有备案信息
     *
     * @param page
     * @param customerOper
     * @return
     */
    @Override
    public IPage findAllCustomerOper(Page page, CustomerOper customerOper) {
        return customerOperMapper.selectAllByPage(page, customerOper);
    }

    /**
     * 平台上传指令批次信息保存
     *
     * @param customerName
     * @param operType
     * @return
     */
    @Override
    public String saveCustomerOper(String customerName, int operType) {
        log.info("开始保存批次信息CustomerOperServiceImpl.saveCustomerOper:(" + customerName + "," + operType + ")");
        //保存批次信息
        User user = userMapper.getUserByUserName(customerName);
        String batchNumber = "YD" + System.currentTimeMillis();
        CustomerOper customerOper = new CustomerOper();
        customerOper.setOperType(operType);
        customerOper.setCompany(user.getCompany());
        customerOper.setCreateTime(DateUtil.getCurrentDateStr());
        customerOper.setBatchNumber(batchNumber);
        customerOper.setCustomerPhone(user.getPhone());
        customerOper.setCustomerName(customerName);
        customerOper.setBatchStatus(1);
        customerOperMapper.insert(customerOper);
        return batchNumber;
    }

    @Override
    public int updateStatus(int batchStatus,String batchNumber) {
        return customerOperMapper.updateStatus(batchStatus,batchNumber);
    }

    @Override
    public int countHasNotFinish(String customerName) {
        return customerOperMapper.countHasNotFinish(customerName);
    }
}
