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.BsOilPriceAdjustDateMapper;
import com.hfkj.entity.BsOilPriceAdjustDate;
import com.hfkj.entity.BsOilPriceAdjustDateExample;
import com.hfkj.service.BsOilPriceAdjustDateService;
import com.hfkj.sysenum.OilPriceAdjustDateResultEnum;
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: BsOilPriceAdjustDateServiceImpl
 * @author: HuRui
 * @date: 2025/11/18
 **/
@Service("oilPriceAdjustDateService")
public class BsOilPriceAdjustDateServiceImpl implements BsOilPriceAdjustDateService {
    @Resource
    private BsOilPriceAdjustDateMapper oilPriceAdjustDateMapper;

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

    @Override
    public void result(Long id, OilPriceAdjustDateResultEnum result) {
        // 查询详情
        BsOilPriceAdjustDate data = getDetail(id);
        if (data == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "数据不存在");
        }
        data.setResult(result.getType());
        editData(data);

        // todo 处理业务
    }

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

    @Override
    public BsOilPriceAdjustDate getDetail(Long id) {
        BsOilPriceAdjustDateExample example = new BsOilPriceAdjustDateExample();
        example.createCriteria().andStatusNotEqualTo(0).andIdEqualTo(id);
        List<BsOilPriceAdjustDate> list = oilPriceAdjustDateMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public BsOilPriceAdjustDate getDetailByDate(Date date) {
        BsOilPriceAdjustDateExample example = new BsOilPriceAdjustDateExample();
        example.createCriteria().andStatusNotEqualTo(0).andDateEqualTo(date);
        List<BsOilPriceAdjustDate> list = oilPriceAdjustDateMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public BsOilPriceAdjustDate getNextPriceDate() {
        BsOilPriceAdjustDateExample example = new BsOilPriceAdjustDateExample();
        example.createCriteria().andStatusNotEqualTo(0);
        example.setOrderByClause("date desc");
        List<BsOilPriceAdjustDate> list = oilPriceAdjustDateMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public BsOilPriceAdjustDate getCurrentPriceDate() {
        BsOilPriceAdjustDateExample example = new BsOilPriceAdjustDateExample();
        example.createCriteria().andStatusNotEqualTo(0).andResultNotEqualTo(0);
        example.setOrderByClause("date desc");
        List<BsOilPriceAdjustDate> list = oilPriceAdjustDateMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }

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

        if (MapUtils.getLong(param, "date") != null) {
            criteria.andDateEqualTo(new Date(MapUtils.getLong(param, "date")));
        }

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

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

        if (MapUtils.getInteger(param, "orderBy") != null) {
            example.setOrderByClause("date asc");
        } else {
            example.setOrderByClause("date desc");
        }
        return oilPriceAdjustDateMapper.selectByExample(example);
    }
}
