package com.hfkj.service.agent.impl;

import com.hfkj.dao.BsAgentPriceMapper;
import com.hfkj.entity.BsAgentPrice;
import com.hfkj.entity.BsAgentPriceExample;
import com.hfkj.service.agent.BsAgentPriceService;
import com.hfkj.sysenum.agent.AgentPriceTypeEnum;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsAgentPriceServiceImpl
 * @author: HuRui
 * @date: 2024/6/19
 **/
@Service("agentPriceService")
public class BsAgentPriceServiceImpl implements BsAgentPriceService {
    @Resource
    private BsAgentPriceMapper agentPriceMapper;


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

    @Override
    public BsAgentPrice getDetail(AgentPriceTypeEnum type, String merNo, String oilNo) {
        BsAgentPriceExample example = new BsAgentPriceExample();
        example.createCriteria().andTypeEqualTo(type.getCode()).andMerNoEqualTo(merNo).andOilNoEqualTo(oilNo);
        List<BsAgentPrice> list = agentPriceMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsAgentPrice getDetail(Long agentMerId, String oilNo) {
        BsAgentPriceExample example = new BsAgentPriceExample();
        example.createCriteria().andAgentMerIdEqualTo(agentMerId).andOilNoEqualTo(oilNo);
        List<BsAgentPrice> list = agentPriceMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BsAgentPrice getPrice(Long agentId, Long merId, String oilNo) {
        BsAgentPriceExample example = new BsAgentPriceExample();
        example.createCriteria().andAgentIdEqualTo(agentId).andMerIdEqualTo(merId).andOilNoEqualTo(oilNo).andStatusEqualTo(1);
        example.setOrderByClause("create_time desc");
        List<BsAgentPrice> list = agentPriceMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BsAgentPrice> getPriceList(AgentPriceTypeEnum type, String merNo) {
        BsAgentPriceExample example = new BsAgentPriceExample();
        example.createCriteria().andTypeEqualTo(type.getCode()).andMerNoEqualTo(merNo);
        return agentPriceMapper.selectByExample(example);
    }

    @Override
    public List<Map<String, Object>> getAgentPriceList(Map<String, Object> param) {
        return agentPriceMapper.queryAgentPriceList(param);
    }

    @Override
    public List<Map<String, Object>> getApiAgentDiscountList(Map<String, Object> param) {
        return agentPriceMapper.queryApiAgentDiscountList(param);
    }
}

