package com.hfkj.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.hfkj.dao.BsBankActivityRosterRateMapper;
import com.hfkj.entity.BsBankActivityRosterRate;
import com.hfkj.entity.BsBankActivityRosterRateExample;
import com.hfkj.service.BsBankActivityRosterRateService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: BsBankActivityRosterRateServiceImpl
 * @author: HuRui
 * @date: 2023/5/18
 **/
@Service("bankActivityRosterRateService")
public class BsBankActivityRosterRateServiceImpl implements BsBankActivityRosterRateService {

    @Resource
    private BsBankActivityRosterRateMapper bsBankActivityRosterRateMapper;

    @Override
    public void editData(BsBankActivityRosterRate bankActivityRosterRate) {
        if (bankActivityRosterRate.getId() == null) {
            bankActivityRosterRate.setCreateTime(new Date());
            bankActivityRosterRate.setUpdateTime(new Date());
            bsBankActivityRosterRateMapper.insert(bankActivityRosterRate);
        } else {
            bankActivityRosterRate.setUpdateTime(new Date());
            bsBankActivityRosterRateMapper.updateByPrimaryKey(bankActivityRosterRate);
        }
    }

    @Override
    public List<BsBankActivityRosterRate> getListByRosterId(Long rosterId) {
        BsBankActivityRosterRateExample example = new BsBankActivityRosterRateExample();
        example.createCriteria().andBankActivityRosterIdEqualTo(rosterId);
        example.setOrderByClause("start_time");
        List<BsBankActivityRosterRate> rates = bsBankActivityRosterRateMapper.selectByExample(example);
        for (BsBankActivityRosterRate bankActivityRosterRate : rates) {
            JSONArray rateArray = JSONArray.parseArray(bankActivityRosterRate.getRates());
            bankActivityRosterRate.setRateArray(rateArray);

            if (StringUtils.isBlank(bankActivityRosterRate.getPreferentialRates())) {
                if (rateArray.size() > 0) {
                    bankActivityRosterRate.setExt1(rateArray.get(0).toString());
                } else {
                    bankActivityRosterRate.setExt1("0");
                }
            } else {
                bankActivityRosterRate.setExt1(bankActivityRosterRate.getPreferentialRates());
            }
        }
        return rates;
    }

    @Override
    public List<BsBankActivityRosterRate> getListByMerId(Long activityId, Long merId) {
        BsBankActivityRosterRateExample example = new BsBankActivityRosterRateExample();
        example.createCriteria().andBankActivityIdEqualTo(activityId).andMerIdEqualTo(merId);
        example.setOrderByClause("create_time desc");
        return bsBankActivityRosterRateMapper.selectByExample(example);
    }

    @Override
    public BsBankActivityRosterRate getRosterRateById(Long id) {
        return bsBankActivityRosterRateMapper.selectByPrimaryKey(id);
    }
}
