package com.hai.service.impl;

import com.hai.dao.HighFleetOilCardCarMapper;
import com.hai.entity.HighFleetOilCardCar;
import com.hai.entity.HighFleetOilCardCarExample;
import com.hai.service.HighFleetOilCardCarService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @className: HighFleetOilCardCarServiceImpl
 * @author: HuRui
 * @date: 2022/12/19
 **/
@Service("fleetOilCardCarService")
public class HighFleetOilCardCarServiceImpl implements HighFleetOilCardCarService {

    @Resource
    private HighFleetOilCardCarMapper fleetOilCardCarMapper;

    @Override
    public void editData(HighFleetOilCardCar fleetOilCardCar) {
        if (fleetOilCardCar.getId() == null) {
            fleetOilCardCar.setCreateTime(new Date());
            fleetOilCardCar.setUpdateTime(new Date());
            fleetOilCardCarMapper.insert(fleetOilCardCar);
        } else {
            fleetOilCardCar.setUpdateTime(new Date());
            fleetOilCardCarMapper.updateByPrimaryKey(fleetOilCardCar);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void batchInsert(List<HighFleetOilCardCar> fleetOilCardCarList) {
        for (HighFleetOilCardCar fleetOilCardCar : fleetOilCardCarList){
            editData(fleetOilCardCar);
        }
    }

    @Override
    public HighFleetOilCardCar getDetailById(Long id) {
        return fleetOilCardCarMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HighFleetOilCardCar> getCarList(Map<String, Object> param) {
        HighFleetOilCardCarExample example = new HighFleetOilCardCarExample();
        HighFleetOilCardCarExample.Criteria criteria = example.createCriteria()
                .andStatusNotEqualTo(0);

        if (MapUtils.getLong(param, "fleetOilCardId") != null) {
            criteria.andFleetOilCardIdEqualTo(MapUtils.getLong(param, "fleetOilCardId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "fleetOilCardNo"))) {
            criteria.andFleetOilCardNoLike("%"+MapUtils.getString(param, "fleetOilCardNo")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "carLicensePlate"))) {
            criteria.andCarLicensePlateLike("%"+MapUtils.getString(param, "carLicensePlate")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "contactName"))) {
            criteria.andContactNameLike("%"+MapUtils.getString(param, "contactName")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "contactPhone"))) {
            criteria.andContactPhoneLike("%"+MapUtils.getString(param, "contactPhone")+"%");
        }

        example.setOrderByClause("create_time desc");
        return fleetOilCardCarMapper.selectByExample(example);
    }

}
