/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.wxgl.wechatbasicmgt.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxUser;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxUserLs;
import com.tofly.feesapi.wxgl.wechatbasicmgt.mapper.WxUserMapper;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxUserLsService;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 微信解绑
 *
 * @author sxb
 * @date Mon Jun 05 00:00:00 CST 2023
 */
@Service("wxCustomerService")
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements WxUserService {


    @Autowired
    private WxUserLsService wxUserLsService;

    @Override
    @Transactional
    public boolean update2(WxUser wxUser) {
        LambdaUpdateWrapper<WxUser> updateWrapper = Wrappers.lambdaUpdate(WxUser.class)
                .eq(WxUser::getOpenid, wxUser.getOpenid())
                .eq(WxUser::getCustomerNo, wxUser.getCustomerNo());
        wxUser.setUnbindDate(new Date());
        wxUser.setUnbindName(SecurityUtils.getUser().getRealName());

        wxUser.setUnbindRemark("营销系统解绑");
        boolean update = this.update(wxUser, updateWrapper);

        String bindFlag = wxUser.getBindFlag();
        if (update && "0".equals(bindFlag)) {
            WxUserLs ls = new WxUserLs();
            BeanUtils.copyProperties(wxUser, ls);
            ls.setUnbindDate(new Date());
            ls.setUnbindName(SecurityUtils.getUser().getRealName());
            wxUserLsService.save(ls);
        }

        return update;
    }
}
