package com.hfkj.service.gas.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsGasOilPriceMapper;
import com.hfkj.entity.BsGasOilPrice;
import com.hfkj.entity.BsGasOilPriceExample;
import com.hfkj.entity.BsGasOilPriceWeek;
import com.hfkj.service.gas.BsGasOilGunNoService;
import com.hfkj.service.gas.BsGasOilPriceService;
import com.hfkj.service.gas.BsGasOilPriceWeekService;
import com.hfkj.service.gas.BsGasService;
import com.hfkj.sysenum.gas.GasOilNoEnum;
import com.hfkj.sysenum.gas.GasOilPriceStatusEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @className: BsGasOilPriceServiceImpl
 * @author: HuRui
 * @date: 2024/3/5
 **/
@Service("gasOilPriceService")
public class BsGasOilPriceServiceImpl implements BsGasOilPriceService {

    @Resource
    private BsGasOilPriceMapper gasOilPriceMapper;
    @Resource
    private BsGasOilGunNoService gasOilGunNoService;
    @Resource
    private BsGasService gasService;
    @Resource
    private BsGasOilPriceWeekService gasOilPriceWeekService;
    @Override
    public void editData(BsGasOilPrice data) {
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            data.setUpdateTime(new Date());
            gasOilPriceMapper.insert(data);
        } else {
            data.setUpdateTime(new Date());
            gasOilPriceMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public void batchUpdate(String merNo, BsGasOilPrice gasOilPrice) {
        BsGasOilPriceExample example = new BsGasOilPriceExample();
        example.createCriteria().andMerNoEqualTo(merNo);
        gasOilPriceMapper.updateByExampleSelective(gasOilPrice, example);
    }

    @Override
    public void editOilPrice(BsGasOilPrice data) {
        editData(data);

        // 清除油站缓存
        gasService.clean(data.getMerNo());
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void delete(BsGasOilPrice data) {
        data.setStatus(GasOilPriceStatusEnum.status0.getNumber());
        editOilPrice(data);

        // 删除油价日期任务
        gasOilPriceWeekService.deletePrice(data.getMerNo(), data.getOilNo());

        // 删除抢号
        gasOilGunNoService.delete(data.getMerNo(), data.getOilNo());
    }

    @Override
    public BsGasOilPrice getGasOilPrice(Long id) {
        return gasOilPriceMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsGasOilPrice getGasOilPrice(Long merId, String oilNo) {
        BsGasOilPriceExample example = new BsGasOilPriceExample();
        example.createCriteria()
                .andMerIdEqualTo(merId)
                .andOilNoEqualTo(oilNo)
                .andStatusNotEqualTo(GasOilPriceStatusEnum.status0.getNumber());
        List<BsGasOilPrice> list = gasOilPriceMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsGasOilPrice> getGasOilPriceList(Long merId) {
        BsGasOilPriceExample example = new BsGasOilPriceExample();
        example.createCriteria()
                .andMerIdEqualTo(merId)
                .andStatusNotEqualTo(GasOilPriceStatusEnum.status0.getNumber());
        return gasOilPriceMapper.selectByExample(example);
    }

    @Override
    public List<BsGasOilPrice> getGasOilPriceList(Map<String, Object> param) {
        BsGasOilPriceExample example = new BsGasOilPriceExample();
        BsGasOilPriceExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(GasOilPriceStatusEnum.status0.getNumber());

        if (StringUtils.isNotBlank(MapUtils.getString(param, "merNo"))) {
            criteria.andMerNoEqualTo(MapUtils.getString(param, "merNo"));
        }

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

        example.setOrderByClause("oil_no");
        return gasOilPriceMapper.selectByExample(example);
}

    @Override
    public List<BsGasOilPrice> getPriceListByRegionAndOilNo(Long regionId,Integer oilPriceZoneId, String oilNo) {
        return gasOilPriceMapper.selectPriceListByRegionAndOilNo(regionId, oilPriceZoneId, oilNo);
    }


}
