package com.hai.service.impl;

import com.hai.common.security.UserCenter;
import com.hai.dao.HighFleetOilCardRecordMapper;
import com.hai.entity.HighFleetOilCardRecord;
import com.hai.entity.HighFleetOilCardRecordExample;
import com.hai.model.HighUserModel;
import com.hai.model.UserInfoModel;
import com.hai.service.HighFleetOilCardRecordService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: HighFleetOilCardRecordServiceImpl
 * @author: HuRui
 * @date: 2022/12/12
 **/
@Service("fleetOilCardRecordService")
public class HighFleetOilCardRecordServiceImpl implements HighFleetOilCardRecordService {

    @Resource
    private HighFleetOilCardRecordMapper fleetOilCardRecordMapper;

    @Resource
    private UserCenter userCenter;

    @Override
    public void insertRecord(HighFleetOilCardRecord fleetOilCardRecord) {
        fleetOilCardRecord.setOpUserType(0);
        fleetOilCardRecord.setOpUserId(0L);
        fleetOilCardRecord.setOpUserName("未知");
        fleetOilCardRecord.setOpUserPhone("未知");

        UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
        if (userInfoModel != null) {
            fleetOilCardRecord.setOpUserType(1);
            fleetOilCardRecord.setOpUserId(userInfoModel.getSecUser().getId());
            fleetOilCardRecord.setOpUserName(userInfoModel.getSecUser().getUserName());
            fleetOilCardRecord.setOpUserPhone(userInfoModel.getSecUser().getTelephone());
        }
        HighUserModel userModel = userCenter.getSessionModel(HighUserModel.class);
        if (userModel != null) {
            fleetOilCardRecord.setOpUserType(2);
            fleetOilCardRecord.setOpUserId(userModel.getHighUser().getId());
            fleetOilCardRecord.setOpUserName(userModel.getHighUser().getName());
            fleetOilCardRecord.setOpUserPhone(userModel.getHighUser().getPhone());
        }
        fleetOilCardRecord.setCreateTime(new Date());
        fleetOilCardRecord.setStatus(1);
        fleetOilCardRecordMapper.insert(fleetOilCardRecord);
    }

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

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

        if (MapUtils.getInteger(param, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(param, "type"));
        }

        if (MapUtils.getInteger(param, "sourceType") != null) {
            criteria.andSourceTypeEqualTo(MapUtils.getInteger(param, "sourceType"));
        }

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

        if (MapUtils.getInteger(param, "opUserType") != null) {
            criteria.andOpUserTypeEqualTo(MapUtils.getInteger(param, "opUserType"));
        }

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

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