//package com.gree.mall.miniapp.logic.user;
//
//import com.alibaba.fastjson.JSON;
//import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
//import com.gree.mall.miniapp.commonmapper.CustomWebsitNumberMapper;
//import com.gree.mall.miniapp.enums.UserTypeEnum;
//import com.gree.mall.miniapp.exception.RemoteServiceException;
//import com.gree.mall.miniapp.logic.common.CommonLogic;
//import com.gree.mall.miniapp.logic.common.SMSLogic;
//import com.gree.mall.miniapp.plus.entity.User;
//import com.gree.mall.miniapp.plus.service.UserService;
//import com.gree.mall.miniapp.utils.CommonUtils;
//import com.gree.mall.miniapp.utils.PhoneFormatCheckUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Date;
//import java.util.List;
//
//@Slf4j
//@Service
//public class WorkerLogic {
//
//    @Autowired
//    private SMSLogic smsLogic;
//    @Autowired
//    CommonLogic commonLogic;
//    @Autowired
//    private EnterpriseWechatWorkerService enterpriseWechatWorkerService;
//    @Autowired
//    private CustomWebsitNumberMapper customWebsitNumberMapper;
//    @Autowired
//    private UserService userService;
//
//    public void sendSms(String phone, String key, String vrifyCode) throws Exception {
//        if (!PhoneFormatCheckUtils.isPhoneLegal(phone)) {
//            throw new RemoteServiceException("手机号不正确");
//        }
//        List<EnterpriseWechatWorker> list = enterpriseWechatWorkerService.lambdaQuery().eq(EnterpriseWechatWorker::getMobile, phone).list();
//        if (list == null || list.size() < 1) {
//            throw new RemoteServiceException("不是企业微信成员");
//        }
//        smsLogic.sendSms(phone, key, vrifyCode);
//    }
//
//
//    public void bind(HttpServletRequest request, String phone, String code) throws RemoteServiceException {
//        smsLogic.checkSmsCode(phone, code);
//        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
//
//        //判断该手机号是否为业务员
//        List<EnterpriseWechatWorker> list = enterpriseWechatWorkerService.lambdaQuery().eq(EnterpriseWechatWorker::getMobile, phone).list();
//        if (list == null || list.size() < 1) {
//            throw new RemoteServiceException("不是企业微信成员");
//        }
//        if (list.size() > 1) {
//            throw new RemoteServiceException("企业微信异常");
//        }
//        List<User> workUserList = userService.lambdaQuery().eq(User::getWorkUserId, list.get(0).getWechatUserId()).list();
//        //多个账号绑定一个手机号
//        if (workUserList != null && workUserList.size() > 0) {
//            throw new RemoteServiceException("手机号已被绑定");
//        }
//        //根据手机号查询是否存在企业微信用户
//        String userId = CommonUtils.getUserId(request);
//
//        User personalUser = userService.getById(userId);
//        User workUser = workUserList == null || workUserList.size() < 1 ? null : workUserList.get(0);
//
//        //以企业微信的数据为准,更新一次
//        personalUser.setWorkPhone(phone);
//        personalUser.setType(UserTypeEnum.SERVICE.toString());
//        personalUser.setWorkName(list.get(0).getName());
//        personalUser.setWorkPhone(list.get(0).getMobile());
//        personalUser.setWorkQrcode(list.get(0).getQrCode());
//        String workUserId = list.get(0).getWechatUserId();
//        personalUser.setWorkUserId(workUserId);
//        personalUser.setPosition(list.get(0).getPosition());
//        personalUser.setWorkCreateTime(new Date());
//        userService.updateById(personalUser);
//        //删除多余的账号,保留一个
//        if (workUser != null && personalUser != null && !workUser.getUserId().equals(personalUser.getUserId())) {
//            log.info("降级的用户数据{}", JSON.toJSON(workUser));
//            workUser.setStatus(false);
//            userService.updateById(workUser);
//        }
//
//    }
//}
