package com.hai.service.impl;

import com.hai.common.security.UserCenter;
import com.hai.dao.HighOilCardRecordMapper;
import com.hai.entity.HighOilCardRecord;
import com.hai.entity.HighOilCardRecordExample;
import com.hai.model.HighUserModel;
import com.hai.model.UserInfoModel;
import com.hai.service.HighOilCardRecordService;
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;

@Service("oilCardRecordService")
public class HighOilCardRecordServiceImpl implements HighOilCardRecordService {

    @Resource
    private HighOilCardRecordMapper oilCardRecordMapper;

    @Resource
    private UserCenter userCenter;

    @Override
    public void insertRecord(HighOilCardRecord oilCardRecord) {
        oilCardRecord.setOpUserId(0L);
        oilCardRecord.setOpUserName("无");

        UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
        if (userInfoModel != null) {
            oilCardRecord.setOpUserId(userInfoModel.getSecUser().getId());
            oilCardRecord.setOpUserName(userInfoModel.getSecUser().getUserName());
        }
        HighUserModel userModel = userCenter.getSessionModel(HighUserModel.class);
        if (userModel != null) {
            oilCardRecord.setOpUserId(userModel.getHighUser().getId());
            oilCardRecord.setOpUserName(userModel.getHighUser().getName());
        }

        oilCardRecord.setCreateTime(new Date());
        oilCardRecord.setStatus(1);
        oilCardRecordMapper.insert(oilCardRecord);
    }

    @Override
    public List<HighOilCardRecord> getRecordList(Map<String, Object> map) {
        HighOilCardRecordExample example = new HighOilCardRecordExample();
        HighOilCardRecordExample.Criteria criteria = example.createCriteria().andStatusEqualTo(1);

        if (MapUtils.getLong(map, "oilCardId") != null) {
            criteria.andOilCardIdEqualTo(MapUtils.getLong(map, "oilCardId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "cardNo"))) {
            criteria.andCardNoEqualTo(MapUtils.getString(map, "cardNo"));
        }

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

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

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

        if (MapUtils.getLong(map, "createTimeS") != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(map, "createTimeS")));
        }

        if (MapUtils.getLong(map, "createTimeE") != null) {
            criteria.andCreateTimeLessThanOrEqualTo(new Date(MapUtils.getLong(map, "createTimeE")));
        }

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