//package com.gree.mall.manager.logic.manual;
//
//import org.apache.commons.collections4.CollectionUtils;
//import com.alibaba.fastjson.JSON;
//import com.gree.mall.manager.bean.admin.AdminUserCom;
//import com.gree.mall.manager.enums.UserTypeEnum;
//import com.gree.mall.manager.exception.RemoteServiceException;
//import com.gree.mall.manager.logic.common.CommonLogic;
//import com.gree.mall.manager.plus.entity.User;
//import com.gree.mall.manager.plus.service.UserService;
//import io.swagger.annotations.ApiParam;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.bind.annotation.RequestParam;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.List;
//
//@Service
//@Slf4j
//public class ManualWorkLogic {
//    @Autowired
//    private UserService userService;
////    @Autowired
////    private EnterpriseWechatWorkerService enterpriseWechatWorkerService;
//    @Autowired
//    private CommonLogic commonLogic;
//
//    @Transactional
//    public String assignWorker(HttpServletRequest request, String personalPhone, String workPhone) throws RemoteServiceException {
//        AdminUserCom adminUser = commonLogic.getAdminUser(request);
//
//        //判断该手机号是否为业务员
//        List<EnterpriseWechatWorker> list = enterpriseWechatWorkerService.lambdaQuery()
//                .eq(EnterpriseWechatWorker::getMobile, workPhone)
//                .eq(EnterpriseWechatWorker::getCorpId, adminUser.getAdminCompanyWechat().getCorpId())
//                .list();
//        if (CollectionUtils.isEmpty(list)) {
//            throw new RemoteServiceException("不是企业微信成员");
//        }
//
//        List<User> workUserList = userService.lambdaQuery()
//                .eq(User::getCompanyWechatId, adminUser.getCompanyWechatId())
//                .eq(User::getWorkUserId, list.get(0).getWechatUserId())
//                .list();
//        List<User> personalUserList = userService.lambdaQuery()
//                .eq(User::getCompanyWechatId, adminUser.getCompanyWechatId())
//                .eq(User::getMobile, personalPhone)
//                .list();
//
//        if (!CollectionUtils.isEmpty(workUserList)) {
//            throw new RemoteServiceException("已经是业务员了");
//        }
//        if (workUserList == null || workUserList.size() > 1) {
//            throw new RemoteServiceException("用户异常.");
//        }
//        if (personalUserList == null || personalUserList.size() > 1) {
//            throw new RemoteServiceException("用户异常...");
//        }
//
//        //根据手机号查询是否存在企业微信用户
//        User workUser = workUserList.size() > 0 ? workUserList.get(0) : null;
//        User personalUser = personalUserList.size() > 0 ? personalUserList.get(0) : null;
////        if (workUser != null && StringUtils.isNotBlank(workUser.getOpenId())) {
////            throw new RemoteServiceException("已经是业务员了,操作失败");
////        }
//
//        if (workUser != null && personalUser != null && workUser.getUserId().equals(personalUser.getUserId())) {
//            throw new RemoteServiceException("已经是业务员了,操作失败");
//        }
//        if (workUser == null && personalUser == null) {
//            throw new RemoteServiceException("用户不存在");
//        }
//
//        if (personalUser != null) {
//            personalUser.setWorkPhone(workPhone);
//            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.setWebsitNumber("");
//            personalUser.setPosition(list.get(0).getPosition());
//            userService.updateById(personalUser);
//        }
//
//        if (workUser != null && personalUser == null) {
//            workUser.setMobile(personalPhone);
//            workUser.setType(UserTypeEnum.SERVICE.toString());
//            userService.updateById(workUser);
//        }
//
//        if (workUser != null && personalUser != null && !workUser.getUserId().equals(personalUser.getUserId())) {
//            log.info("降级的用户数据{}", JSON.toJSON(workUser));
//            userService.removeById(workUser);
//        }
//        return personalUser.getUserId();
//    }
//}
