package org.jeecg.modules.okki.google.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.okki.google.entity.OkkiGoogleAdsAccount;
import org.jeecg.modules.okki.google.mapper.OkkiGoogleAdsAccountMapper;
import org.jeecg.modules.okki.google.param.OkkiGoogleAdsAccountParam;
import org.jeecg.modules.okki.google.service.IOkkiGoogleAdsAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: Google Ads账号管理
 * @Author: jeecg-boot
 * @Date:   2024-05-17
 * @Version: V1.0
 */
@Service
public class OkkiGoogleAdsAccountServiceImpl extends ServiceImpl<OkkiGoogleAdsAccountMapper, OkkiGoogleAdsAccount> implements IOkkiGoogleAdsAccountService {

    @Resource
    private OkkiGoogleAdsAccountMapper okkiGoogleAdsAccountMapper;

    @Override
    public void okkiSave(OkkiGoogleAdsAccountParam okkiGoogleAdsAccountParam){
        LambdaUpdateWrapper<OkkiGoogleAdsAccount> okkiGoogleAdsAccountLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        okkiGoogleAdsAccountLambdaUpdateWrapper.eq(OkkiGoogleAdsAccount::getCid, okkiGoogleAdsAccountParam.getCid())
                        .last("limit 1");
        try {
            OkkiGoogleAdsAccount okkiGoogleAdsAccount = okkiGoogleAdsAccountMapper.selectOne(okkiGoogleAdsAccountLambdaUpdateWrapper);
            if (okkiGoogleAdsAccount == null) {
                okkiGoogleAdsAccount = new OkkiGoogleAdsAccount();
                okkiGoogleAdsAccount.setSiteId(okkiGoogleAdsAccountParam.getSite_id());
                okkiGoogleAdsAccount.setCid(okkiGoogleAdsAccountParam.getCid());
                okkiGoogleAdsAccount.setAmount(okkiGoogleAdsAccountParam.getAmount());
                okkiGoogleAdsAccount.setGoogleAccount(okkiGoogleAdsAccountParam.getGoogle_account());
                okkiGoogleAdsAccount.setType(okkiGoogleAdsAccountParam.getType());
                okkiGoogleAdsAccount.setCompanyName(okkiGoogleAdsAccountParam.getCompany_name());
                okkiGoogleAdsAccountMapper.insert(okkiGoogleAdsAccount);
            }else {
                okkiGoogleAdsAccount.setSiteId(okkiGoogleAdsAccountParam.getSite_id());
                okkiGoogleAdsAccount.setAmount(okkiGoogleAdsAccountParam.getAmount());
                okkiGoogleAdsAccount.setGoogleAccount(okkiGoogleAdsAccountParam.getGoogle_account());
                okkiGoogleAdsAccount.setType(okkiGoogleAdsAccountParam.getType());
                okkiGoogleAdsAccount.setCompanyName(okkiGoogleAdsAccountParam.getCompany_name());
                okkiGoogleAdsAccountMapper.updateById(okkiGoogleAdsAccount);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
