U8PersonService.java 4.15 KB
package com.huaheng.pc.U8.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.framework.datasource.DynamicDataSource;
import com.huaheng.framework.shiro.service.PasswordService;
import com.huaheng.pc.U8.domain.U8Person;
import com.huaheng.pc.U8.mapper.U8PersonMapper;
import com.huaheng.pc.system.dept.domain.Dept;
import com.huaheng.pc.system.dept.service.IDeptService;
import com.huaheng.pc.system.user.domain.SysUserWarehouse;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.domain.UserCompany;
import com.huaheng.pc.system.user.domain.UserRole;
import com.huaheng.pc.system.user.mapper.SysUserWarehouseMapper;
import com.huaheng.pc.system.user.mapper.UserCompanyMapper;
import com.huaheng.pc.system.user.mapper.UserRoleMapper;
import com.huaheng.pc.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;

@Service
public class U8PersonService extends ServiceImpl<U8PersonMapper, U8Person> {
    @Resource
    private IUserService service;
    @Resource
    private IDeptService deptService;
    @Resource
    private SysUserWarehouseMapper sysUserWarehouseMapper;
    @Resource
    private UserCompanyMapper userCompanyMapper;
    @Autowired
    private PasswordService passwordService;
    @Resource
    private UserRoleMapper userRoleMapper;

    public List getU8PersonList() {
        // 切换到从库
        DynamicDataSource.slave();
        List<U8Person> list = this.list();
        DynamicDataSource.master();
        return list;
    }

    public List sync() throws ParseException {
        List<U8Person> list = this.getU8PersonList();
        List<User> lst = service.selectAll();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (U8Person dep : list) {
            for (User de : lst) {
                if (Objects.equals(dep.getCode(), de.getLoginName())) {
                    if (!sdf.format(dep.getMaintenanceDate()).equals(sdf.format(de.getMaintenanceDate()))) {
                        Dept dept = deptService.selectDepts(dep.getDeptCode());
                        User user = new User();
                        user.setId(de.getId());
                        user.setUserName(dep.getName());
                        user.setDeptId(dept.getId());
                        user.setMaintenanceDate(sdf.parse(sdf.format(dep.getMaintenanceDate())));
                        service.updateUsers(user);
                    }
                }
            }
            if (lst.stream().noneMatch(w -> String.valueOf(w.getLoginName()).equals(dep.getCode()))) {
                Dept dept = deptService.selectDepts(dep.getDeptCode());
                User user = new User();
                user.setLoginName(dep.getCode());
                user.setUserName(dep.getName());
                user.randomSalt();
                user.setPassword(passwordService.encryptPassword(user.getLoginName(), "123456", user.getSalt()));
                user.setDeptId(dept.getId());
                user.setMaintenanceDate(sdf.parse(sdf.format(dep.getMaintenanceDate())));
                Integer id = service.insertUsers(user);
                //用户绑定仓库
                SysUserWarehouse warehouse = new SysUserWarehouse();
                warehouse.setUserId(id);
                warehouse.setWarehouseCode(QuantityConstant.WAREHOUSECODE);
                sysUserWarehouseMapper.insert(warehouse);
                //用户绑定货主
                UserCompany company = new UserCompany();
                company.setUserId(id);
                company.setCompanyId(QuantityConstant.COMPANYID);
                userCompanyMapper.insert(company);
                //用户绑定角色
                UserRole role = new UserRole();
                role.setUserId(id);
                role.setRoleId(QuantityConstant.ROLE_ID);
                userRoleMapper.insert(role);
            }
        }
        return lst;
    }
}