package com.hfkj.service.gas.impl;

import com.hfkj.dao.BsGasChannelPriceWeekMapper;
import com.hfkj.entity.BsGasChannelPriceWeek;
import com.hfkj.entity.BsGasChannelPriceWeekExample;
import com.hfkj.service.gas.BsGasChannelPriceWeekService;
import com.hfkj.sysenum.merchant.MerchantSourceTypeEnum;
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: BsGasChannelPriceWeekServiceImpl
 * @author: HuRui
 * @date: 2025/3/11
 **/
@Service("gasChannelPriceWeekService")
public class BsGasChannelPriceWeekServiceImpl implements BsGasChannelPriceWeekService {
    @Resource
    private BsGasChannelPriceWeekMapper gasChannelPriceWeekMapper;

    @Override
    public void editData(BsGasChannelPriceWeek data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            gasChannelPriceWeekMapper.insert(data);
        } else {
            gasChannelPriceWeekMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public BsGasChannelPriceWeek getDetail(MerchantSourceTypeEnum sourceType,Integer weeks, String oilNo) {
        BsGasChannelPriceWeekExample example = new BsGasChannelPriceWeekExample();
        example.createCriteria()
                .andGasChannelTypeEqualTo(sourceType.getNumber())
                .andWeeksEqualTo(weeks)
                .andOilNoEqualTo(oilNo)
                .andStatusNotEqualTo(0);
        example.setOrderByClause("update_time desc");
        List<BsGasChannelPriceWeek> list = gasChannelPriceWeekMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsGasChannelPriceWeek getDetail(Long id) {
        return gasChannelPriceWeekMapper.selectByPrimaryKey(id);
    }

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

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

        if (StringUtils.isNotBlank(MapUtils.getString(param, "oilNo"))) {
            criteria.andOilNoEqualTo(MapUtils.getString(param, "oilNo"));
        }

        example.setOrderByClause("weeks");
        return gasChannelPriceWeekMapper.selectByExample(example);
    }
}
