/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.base.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.base.entity.User;
import com.tofly.base.entity.UserMiddle;
import com.tofly.base.entity.UserRoles;
import com.tofly.base.mapper.UserMiddleMapper;
import com.tofly.base.service.UserMiddleService;
import com.tofly.base.service.UserService;
import com.tofly.base.util.AESUtil;
import com.tofly.base.util.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户中间表
 *
 * @author admin
 * @date Thu Mar 03 00:00:00 CST 2022
 */
@Service("userMiddleService")
public class UserMiddleServiceImpl extends ServiceImpl<UserMiddleMapper, UserMiddle> implements UserMiddleService {

    @Value("${aes.key:tysw@Zhsw#@2126y}")
    private String key;

    @Value("${aes.iv:zhsw@TYsw#@20122}")
    private String iv;
    @Value("${aes.marketingUrl:http://192.168.2.99:8021/api/custom/Sys_Handler.ashx?action=AddSysOper}")
    private String marketingUrl;
    @Autowired
    private UserService userService;

    @Override
    public boolean synchronizeUser(String userName, String passWord, String realName, String flag) {
        UserMiddle userMiddle = new UserMiddle();
        // 判断是否为内部调用,flag不为空
        if (StringUtils.isNotBlank(flag)) {
            if (StringUtils.isNotBlank(passWord)) {
                try {
                    // 密码 RSA解密
                    String pwd = RSAUtils.priKeyDecryption(passWord);
                    // AES 加密
                    passWord = AESUtil.encryptData(pwd, key, iv);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
        // 同步用户中间表
        UserMiddle middle = new UserMiddle();
        List<UserMiddle> list = this.list(new QueryWrapper<UserMiddle>().lambda().eq(UserMiddle::getUserName, userName));
        if(null!=list && list.size()>0){
            middle = list.get(0);
        }
        if (null != middle.getId()) {
            userMiddle.setId(middle.getId());
        }
        userMiddle.setUserName(userName);
        userMiddle.setRealName(realName);
        if (StringUtils.isNotBlank(passWord)) {
            userMiddle.setMarketingPassWord(passWord);
        }
        return this.saveOrUpdate(userMiddle);
    }

    @Override
    public boolean sendUserNewUpdate(UserRoles user) {
        // 判断当前用户是否存在
        User one = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getUsername, user.getUsername()));
        // 获取当前用户新增/修改 绑定公司/部门/角色/中文名称
        if (null != one) {
            user.setId(one.getId());
            userService.updateByIdNew(null, user, null, user.getRoles(), user.getUserdeptList());
        } else {
            // 新增用户
            userService.saveNew(null, user, null);
        }
        return true;
    }

    @Override
    public boolean sendMarketingUser(String userName, String passWord, String realName, String flag) {
        // 请求头封装
       /* Map<String, String> headerMap = new HashMap<>(12);
        // 请求参数封装
        Map<String, Object> map = new HashMap<>(12);
        map.put("userCode", userName);
        map.put("userName", realName);
        // 判断是否为内部调用,flag不为空
        if (StringUtils.isNotBlank(flag)) {
            if (StringUtils.isNotBlank(passWord)) {

                try {
                    // 密码 sha1解密
                    String pwd = RSAUtils.priKeyDecryption(passWord);
                    // AES 加密
                    passWord = AESUtil.encryptData(pwd, key, iv);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
        if (StringUtils.isNotBlank(passWord)) {
            map.put("userPwd", passWord);
        }
        map.put("departMent", "05");
        HttpResponse httpResponse = HttpClientUtil.doPost(marketingUrl, headerMap, map);
        String body = httpResponse.body();
        JSONObject jsonObject = JSON.parseObject(body);
        return 1 == (int) jsonObject.get("code");*/
        return true;
    }


}
