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.BsGasOilGunNoMapper;
import com.hfkj.entity.BsGasOilGunNo;
import com.hfkj.entity.BsGasOilGunNoExample;
import com.hfkj.service.gas.BsGasOilGunNoService;
import com.hfkj.service.gas.BsGasService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: BsGasOilGunNoServiceImpl
 * @author: HuRui
 * @date: 2024/3/12
 **/
@Service("gasOilGunNoService")
public class BsGasOilGunNoServiceImpl implements BsGasOilGunNoService {

    @Resource
    private BsGasOilGunNoMapper gasOilGunNoMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private BsGasService gasService;

    private final static String GAS_OIL_NO_KEY = "GAS_OIL_NO:";

    @Override
    public void editData(BsGasOilGunNo data) {
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            gasOilGunNoMapper.insert(data);
        } else {
            gasOilGunNoMapper.updateByPrimaryKey(data);
        }
        // 刷新缓存
        joinCache(data.getMerNo());
    }

    @Override
    public void cleanCache(String merNo) {
        redisUtil.del(GAS_OIL_NO_KEY + merNo);
    }

    @Override
    public void batchInsert(List<BsGasOilGunNo> dataList) {
        if (!dataList.isEmpty()) {
            gasOilGunNoMapper.batchInsert(dataList);
        }
    }

    @Override
    public void batchUpdate(List<BsGasOilGunNo> dataList) {
        if (!dataList.isEmpty()) {
            gasOilGunNoMapper.batchUpdate(dataList);
        }
    }

    @Override
    public void delete(Long gunNoId) {
        BsGasOilGunNo oilGunNo = getDetail(gunNoId);
        if (oilGunNo == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的数据");
        }
        oilGunNo.setStatus(0);
        editData(oilGunNo);
        // 删除油站缓存
        gasService.clean(oilGunNo.getMerNo());
    }

    @Override
    public void delete(String merNo, String oilNo) {
        BsGasOilGunNoExample example = new BsGasOilGunNoExample();
        example.createCriteria().andMerNoEqualTo(merNo).andOilNoEqualTo(oilNo).andStatusEqualTo(1);
        List<BsGasOilGunNo> list = gasOilGunNoMapper.selectByExample(example);
        for (BsGasOilGunNo oilGunNo : list) {
            oilGunNo.setStatus(0);
            gasOilGunNoMapper.updateByPrimaryKey(oilGunNo);
        }
        joinCache(merNo);
    }

    @Override
    public List<BsGasOilGunNo> getOilGunNoList(String merNo) {
        Object obj = redisUtil.get(GAS_OIL_NO_KEY + merNo);
        if (obj != null) {
            return (List<BsGasOilGunNo>) obj;
        }
        return joinCache(merNo);
    }

    @Override
    public List<BsGasOilGunNo> getOilGunNoList(String merNo, String oilNo) {
        return getOilGunNoList(merNo).stream()
                .filter(o->o.getOilNo().equals(oilNo))
                .sorted(Comparator.comparing(o-> Integer.parseInt(o.getGunNo().replaceAll("\\D", ""))))
                .collect(Collectors.toList());
    }

    @Override
    public BsGasOilGunNo getDetail(String merNo, String oilNo, String gunNo) {
        BsGasOilGunNoExample example = new BsGasOilGunNoExample();
        example.createCriteria().andMerNoEqualTo(merNo).andOilNoEqualTo(oilNo).andGunNoEqualTo(gunNo).andStatusNotEqualTo(0);
        List<BsGasOilGunNo> list = gasOilGunNoMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsGasOilGunNo getDetail(Long gunNoId) {
        return gasOilGunNoMapper.selectByPrimaryKey(gunNoId);
    }

    /**
     * 加入缓存
     * @param merNo
     * @return
     */
    private List<BsGasOilGunNo> joinCache(String merNo) {
        BsGasOilGunNoExample example = new BsGasOilGunNoExample();
        example.createCriteria().andMerNoEqualTo(merNo).andStatusNotEqualTo(0);
        example.setOrderByClause("gun_no");
        List<BsGasOilGunNo> list = gasOilGunNoMapper.selectByExample(example);
        redisUtil.set(GAS_OIL_NO_KEY + merNo, list);
        return list;
    }
}
