package org.jeecg.modules.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.constant.SendChangeMsgConstant;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.mapper.SysTenantMapper;
import org.jeecg.modules.system.service.*;
import org.jeecg.modules.system.to.SysAreaTo;
import org.jeecg.modules.system.to.SysDepartTo;
import org.jeecg.modules.system.to.SysUserRoleTo;
import org.jeecg.modules.system.to.SysUserTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author liushuai
 */
@Service
@Slf4j
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements ISysTenantService {


    @Autowired
    private ISysTenantService tenantService;
    @Resource
    private ISysChangeMsgService sysChangeMsgService;
    @Resource
    private ISysAreaService sysAreaService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserDepartService sysUserDepartService;
    @Autowired
    private ISysDepartService sysDepartService;

    @Override
    public SysTenant getTenantByPermissionId(String lsAdd) {
        return baseMapper.getTenantByPermissionId(lsAdd);
    }

    @Override
    public void sendUserMsg(SysUser user, SysDepart dept, String type) {
        SysUserTo sysUserTo = new SysUserTo();
        BeanUtils.copyProperties(user,sysUserTo);
        if (type.equals(SendChangeMsgConstant.INSERT)){
            sysUserTo.setCreateTime(new Date());
        }else {
            sysUserTo.setUpdateTime(new Date());
        }
        if (oConvertUtils.isNotEmpty(dept)){
            sysUserTo.setDepartName(dept.getDepartName());
            sysUserTo.setDepartCode(dept.getDepartCode());
            sysUserTo.setDepartId(dept.getId());
        }else {
            sysUserTo.setDepartCode("");
            sysUserTo.setDepartId("");
        }
        sendUserMsgToTenant(sysUserTo,type);
    }

    @Override
    public void sendAreaMsg(SysArea sysArea, String type) {
        SysAreaTo sysAreaTo = new SysAreaTo();
        BeanUtils.copyProperties(sysArea, sysAreaTo);
        if (type.equals(SendChangeMsgConstant.INSERT)) {
            sysAreaTo.setCreateTime(new Date());
        } else {
            sysAreaTo.setUpdateTime(new Date());
        }
        if (!StringUtils.isEmpty(sysArea.getParentId())) {
            SysArea sysArea1 = sysAreaService.getById(sysArea.getParentId());
            sysAreaTo.setParentAreaCode(sysArea1.getAreaCode());
        } else {
            sysAreaTo.setParentAreaCode("");
        }
        sendAreaMsgToTenant(sysAreaTo, type);
    }

    @Override
    public void sendDeptMsg(SysDepart sysDepart, String type) {
        SysDepartTo sysDepartTo = new SysDepartTo();
        BeanUtils.copyProperties(sysDepart,sysDepartTo);
        if(type.equals(SendChangeMsgConstant.INSERT)){
            sysDepartTo.setCreateTime(new Date());
        }else{
            sysDepartTo.setUpdateTime(new Date());
        }
        List<SysTenant> list = tenantService.list(new QueryWrapper<SysTenant>().isNotNull("msg_url").eq("is_send","1"));
        list.forEach(sysTenant -> {
            String json = JSON.toJSONString(sysDepartTo);
            sysChangeMsgService.saveChangeMsg(sysTenant.getId(),sysTenant.getMsgUrl(),json, type,SendChangeMsgConstant.TYPE_DEPT);
        });
    }

    @Override
    public void sendUserByDept(SysDepart ls) {
        ls.setStatus("0");
        boolean b = sysDepartService.updateById(ls);
        tenantService.sendDeptMsg(ls, SendChangeMsgConstant.UPDATE);
        this.sendDeptByDeptChange(ls);
    }

    @Override
    public void sendUserRoleMsg(SysUser user, SysRole sysRole, SysDepart sysDepart, String type) {
        SysUserRoleTo sysUserRoleTo = new SysUserRoleTo();
        if (type.equals(SendChangeMsgConstant.INSERT)){
            sysUserRoleTo.setCreateTime(new Date());
        }else {
            sysUserRoleTo.setUpdateTime(new Date());
        }
        sysUserRoleTo.setRoleId(sysRole.getId());
        sysUserRoleTo.setUserId(user.getId());
        sysUserRoleTo.setRoleCode(sysRole.getRoleCode());
        sysUserRoleTo.setDeptId(sysDepart.getId());
        List<SysTenant> list = tenantService.list(new QueryWrapper<SysTenant>().lambda().isNotNull(SysTenant::getMsgUrl).eq(SysTenant::getIsSend,"1"));
        list.forEach(sysTenant -> {
            String json = JSON.toJSONString(sysUserRoleTo);
            sysChangeMsgService.saveChangeMsg(sysTenant.getId(),sysTenant.getMsgUrl(),json,type,SendChangeMsgConstant.TYPE_USER_ROLE);
        });
    }

    @Override
    public void sendDeptByDeptChange(SysDepart sysDepart) {
        List<SysUser> sysUser = sysUserDepartService.queryUserByDepId(sysDepart.getId());
        if (sysUser != null && !sysUser.isEmpty()){
            sysUser.forEach(user->{
                user.setStatus(0);
                boolean a = sysUserService.updateById(user);
                tenantService.sendUserMsg(user, sysDepart, SendChangeMsgConstant.UPDATE);
            });
        }
        List<SysDepart> list = sysDepartService.list(new QueryWrapper<SysDepart>().eq("parent_id", sysDepart.getId()));
        if (list!=null&& !list.isEmpty()){
            list.forEach(ls->{
                tenantService.sendUserByDept(ls);
            });
        }
    }

    void sendAreaMsgToTenant(SysAreaTo sysAreaTo, String type) {
        List<SysTenant> list = tenantService.list(new QueryWrapper<SysTenant>().isNotNull("msg_url").eq("is_send", "1"));
        list.forEach(sysTenant -> {
            String json = JSON.toJSONString(sysAreaTo);
            sysChangeMsgService.saveChangeMsg(sysTenant.getId(), sysTenant.getMsgUrl(), json, type, SendChangeMsgConstant.TYPE_AREA);
        });
    }

    void sendUserMsgToTenant(SysUserTo sysUserTo,String type) {
        List<SysTenant> list = tenantService.list(new QueryWrapper<SysTenant>().isNotNull("msg_url").eq("is_send","1"));
        list.forEach(sysTenant -> {
            String json = JSON.toJSONString(sysUserTo);
            sysChangeMsgService.saveChangeMsg(sysTenant.getId(),sysTenant.getMsgUrl(),json, type,SendChangeMsgConstant.TYPE_USER);
        });
    }
}
