package com.bonus.core.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.core.customer.constants.CustConstant;
import com.bonus.core.customer.mapper.CustAccTempMapper;
import com.bonus.core.customer.model.CustAccTemp;
import com.bonus.core.customer.service.CustAccTempService;
import com.bonus.core.device.manage.model.DeviceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class CustAccTempServiceImpl extends ServiceImpl<CustAccTempMapper, CustAccTemp> implements CustAccTempService {
   private static final Logger log = LoggerFactory.getLogger(CustAccTempServiceImpl.class);
   @Autowired
   @Lazy
   private CustMqServiceImpl custMqService;

   public void update(CustAccTemp custAccTemp) {
     // ((CustAccTempMapper)this.baseMapper).update(custAccTemp, (Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(CustAccTemp.class).eq(CustAccTemp::getCustId, custAccTemp.getCustId())).set(ObjectUtil.isNull(custAccTemp.getPsnType()), CustAccTemp::getPsnType, CustConstant.CUST_ACC_TEMP_DEFAULT_PSN_TYPE));
      this.baseMapper.update(custAccTemp, Wrappers.lambdaUpdate(CustAccTemp.class)
              .eq(CustAccTemp::getCustId, custAccTemp.getCustId())
              .set(ObjectUtil.isNull(custAccTemp.getPsnType()), CustAccTemp::getPsnType, CustConstant.CUST_ACC_TEMP_DEFAULT_PSN_TYPE));
      this.custMqService.sendMsg(this.getByCustId(custAccTemp.getCustId()));
   }

   protected CustAccTemp getByCustId(Long custId) {
      return this.baseMapper.selectOne(Wrappers.lambdaQuery(CustAccTemp.class).eq(CustAccTemp::getCustId, custId));
      //return (CustAccTemp)this.baseMapper.selectOne((Wrapper)Wrappers.lambdaQuery(DeviceInfo.class).eq(CustAccTemp::getCustId, custId));

   }

   @Override
   public void updateAccStatus(List<Long> custIdList, Integer accStatus) {
      CustAccTemp custAccTemp = new CustAccTemp();
      custAccTemp.setAccStatus(accStatus);
      this.update(custAccTemp, (Wrapper)Wrappers.lambdaUpdate(CustAccTemp.class).in(CustAccTemp::getCustId, custIdList));
   }
}
