package com.hfkj.service.impl;

import com.hfkj.dao.BsTradeTakeConfigMapper;
import com.hfkj.entity.BsTradeTakeConfig;
import com.hfkj.entity.BsTradeTakeConfigExample;
import com.hfkj.service.BsTradeTakeConfigService;
import com.hfkj.sysenum.PlatformTypeEnum;
import com.hfkj.sysenum.ProfitSharingReceiverRoleEnum;
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;

/**
 * @className: BsTradeTakeConfigServiceImpl
 * @author: HuRui
 * @date: 2023/2/27
 **/
@Service("tradeTakeConfigService")
public class BsTradeTakeConfigServiceImpl implements BsTradeTakeConfigService {

    @Resource
    private BsTradeTakeConfigMapper tradeTakeConfigMapper;
    @Override
    public void editData(BsTradeTakeConfig tradeTakeConfig) {
        if (tradeTakeConfig.getId() == null) {
            tradeTakeConfig.setStatus(1);
            tradeTakeConfig.setCreateTime(new Date());
            tradeTakeConfig.setUpdateTime(new Date());
            tradeTakeConfigMapper.insert(tradeTakeConfig);
        } else {
            tradeTakeConfig.setUpdateTime(new Date());
            tradeTakeConfigMapper.updateByPrimaryKey(tradeTakeConfig);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void editBatchData(List<BsTradeTakeConfig> tradeTakeConfigList) {
        for (BsTradeTakeConfig tradeTakeConfig : tradeTakeConfigList) {
            editData(tradeTakeConfig);
        }
    }

    @Override
    public BsTradeTakeConfig getTradeTakeConfig(Long companyId, PlatformTypeEnum platformType, ProfitSharingReceiverRoleEnum profitSharingReceiverRoleEnum, Long receiverObjectId, Long subReceiverObjectId) {
        BsTradeTakeConfigExample example = new BsTradeTakeConfigExample();
        BsTradeTakeConfigExample.Criteria criteria = example.createCriteria()
                .andCompanyIdEqualTo(companyId)
                .andPlatformTypeEqualTo(platformType.getNumber())
                .andReceiverRoleEqualTo(profitSharingReceiverRoleEnum.getNumber())
                .andStatusEqualTo(1);

        if (receiverObjectId != null) {
            criteria.andReceiverObjectIdEqualTo(receiverObjectId);
        }
        if (subReceiverObjectId != null) {
            criteria.andSubReceiverObjectIdEqualTo(subReceiverObjectId);
        }

        List<BsTradeTakeConfig> list = tradeTakeConfigMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


}
