package com.hfkj.service.gas.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsGasOilPriceWeekMapper;
import com.hfkj.entity.BsGasOilPrice;
import com.hfkj.entity.BsGasOilPriceWeek;
import com.hfkj.entity.BsGasOilPriceWeekExample;
import com.hfkj.entity.BsMerchant;
import com.hfkj.service.gas.BsGasOilPriceService;
import com.hfkj.service.gas.BsGasOilPriceWeekService;
import com.hfkj.service.gas.BsGasService;
import com.hfkj.service.merchant.BsMerchantService;
import com.hfkj.sysenum.merchant.MerchantSourceTypeEnum;
import com.hfkj.sysenum.gas.GasOilNoEnum;
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.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BsGasOilPriceWeekServiceImpl
 * @author: HuRui
 * @date: 2024/7/29
 **/
@Service("gasOilPriceWeekService")
public class BsGasOilPriceWeekServiceImpl implements BsGasOilPriceWeekService {

    @Resource
    private BsGasOilPriceWeekMapper gasOilPriceWeekMapper;
    @Resource
    private BsGasOilPriceService gasOilPriceService;
    @Resource
    private BsGasService gasService;
    @Resource
    private BsMerchantService merchantService;
    @Resource
    private RedisUtil redisUtil;
    private final static String CACHE_KEY = "GAS_OIL_PRICE_WEEK:";
    @Override
    public void editData(BsGasOilPriceWeek data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            gasOilPriceWeekMapper.insert(data);
        } else {
            gasOilPriceWeekMapper.updateByPrimaryKey(data);
        }
        redisUtil.set(CACHE_KEY + data.getMerNo() + "_" + data.getOilNo(), data);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW,rollbackFor= {RuntimeException.class})
    public void updatePrice(BsGasOilPriceWeek priceWeek) {
        editData(priceWeek);
        // 当前星期
        int week = LocalDate.now().getDayOfWeek().getValue();
        // 如果是当天
        if (priceWeek.getWeeks().equals(week)) {
            // 查询油站油价数据
            BsGasOilPrice oilPrice = gasOilPriceService.getGasOilPrice(priceWeek.getMerId(), priceWeek.getOilNo());
            if (oilPrice == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到油品价格数据");
            }
            oilPrice.setPriceOfficial(priceWeek.getPriceOfficial());
            oilPrice.setPriceGun(priceWeek.getPriceGun());
            oilPrice.setPriceVip(priceWeek.getPriceVip());
            oilPrice.setGasStationDrop(oilPrice.getPriceOfficial().subtract(oilPrice.getPriceGun()));
            gasOilPriceService.editOilPrice(oilPrice);
        }
    }

    @Override
    public void deletePrice(String merNo, String oilNo) {
        BsGasOilPriceWeekExample example = new BsGasOilPriceWeekExample();
        example.createCriteria().andMerNoEqualTo(merNo).andOilNoEqualTo(oilNo);
        gasOilPriceWeekMapper.deleteByExample(example);
    }

    @Override
    public void updatePriceWeek(Integer week) {
        // 更新价格
        gasOilPriceWeekMapper.updatePrice(week);

        Map<String,Object> param = new HashMap<>();
        param.put("sourceType", MerchantSourceTypeEnum.type1.getNumber());
        List<BsMerchant> merchantList = merchantService.getMerchantList(param);
        for (BsMerchant merchant : merchantList) {
            // 清除油站缓存
            gasService.clean(merchant.getMerNo());
        }
    }

    @Override
    public BsGasOilPriceWeek getOilPrice(String merNo, GasOilNoEnum oilNo) {
        String key = CACHE_KEY + merNo + "_" + oilNo;
        // 缓存
        Object cache = redisUtil.get(key);
        if (cache != null) {
            return (BsGasOilPriceWeek) cache;
        }
        BsGasOilPriceWeekExample example = new BsGasOilPriceWeekExample();
        example.createCriteria().andMerNoEqualTo(merNo).andOilNoEqualTo(oilNo.getCode().toString());
        List<BsGasOilPriceWeek> list = gasOilPriceWeekMapper.selectByExample(example);
        if (!list.isEmpty()) {
            BsGasOilPriceWeek oilPriceWeek = list.get(0);
            redisUtil.set(key, oilPriceWeek);
            return oilPriceWeek;
        }
        return null;
    }

    @Override
    public BsGasOilPriceWeek getOilPrice(Long priceWeekId) {
        return gasOilPriceWeekMapper.selectByPrimaryKey(priceWeekId);
    }

    @Override
    public List<BsGasOilPriceWeek> getOilPriceList(Map<String, Object> param) {
        BsGasOilPriceWeekExample example = new BsGasOilPriceWeekExample();
        BsGasOilPriceWeekExample.Criteria criteria = example.createCriteria();

        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("weeks");
        return gasOilPriceWeekMapper.selectByExample(example);
    }
}
