package com.hfkj.service.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsOilPriceAdjustMapper;
import com.hfkj.entity.BsOilPriceAdjust;
import com.hfkj.entity.BsOilPriceAdjustExample;
import com.hfkj.service.BsOilPriceAdjustService;
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: BsOilPriceAdjustServiceImpl
 * @author: HuRui
 * @date: 2025/11/18
 **/
@Service("oilPriceAdjustService")
public class BsOilPriceAdjustServiceImpl implements BsOilPriceAdjustService {
    @Resource
    private BsOilPriceAdjustMapper oilPriceAdjustMapper;

    @Override
    public void editData(BsOilPriceAdjust data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            data.setStatus(1);
            oilPriceAdjustMapper.insert(data);
        } else {
            oilPriceAdjustMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public void del(Long id) {
        // 查询详情
        BsOilPriceAdjust detail = getDetail(id);
        if (detail == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "数据不存在");
        }
        detail.setStatus(0);
        editData(detail);
    }

    @Override
    public BsOilPriceAdjust getDetail(Long id) {
        return oilPriceAdjustMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsOilPriceAdjust getDetail(Date adjustDate, Long provinceCode, Integer oilPriceZone, String oilNo) {
        BsOilPriceAdjustExample example = new BsOilPriceAdjustExample();
        BsOilPriceAdjustExample.Criteria criteria = example.createCriteria()
                .andPriceAdjustDateEqualTo(adjustDate)
                .andProvinceCodeEqualTo(provinceCode)
                .andOilNoEqualTo(oilNo)
                .andStatusNotEqualTo(0);
        if (oilPriceZone != null) {
            criteria.andOilPriceZoneIdEqualTo(oilPriceZone);
        }
        example.setOrderByClause("oil_no");
        List<BsOilPriceAdjust> list = oilPriceAdjustMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<BsOilPriceAdjust> getList(Date adjustDate,Long provinceCode, Integer oilPriceZone) {
        BsOilPriceAdjustExample example = new BsOilPriceAdjustExample();
        BsOilPriceAdjustExample.Criteria criteria = example.createCriteria()
                .andPriceAdjustDateEqualTo(adjustDate)
                .andProvinceCodeEqualTo(provinceCode)
                .andStatusNotEqualTo(0);
        if (oilPriceZone != null) {
            criteria.andOilPriceZoneIdEqualTo(oilPriceZone);
        }
        example.setOrderByClause("oil_no");
        return oilPriceAdjustMapper.selectByExample(example);
    }

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

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

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

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

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

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

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