/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.warning.warningManager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.biz.modular.user.entity.BizUser;
import vip.xiaonuo.modular.warning.warningManager.entity.WarningManager;
import vip.xiaonuo.modular.warning.warningManager.entity.vo.WarningManagerVo;
import vip.xiaonuo.modular.warning.warningManager.mapper.WarningManagerMapper;
import vip.xiaonuo.modular.warning.warningManager.param.WarningManagerOrgIdParam;
import vip.xiaonuo.modular.warning.warningManager.param.WarningManagerSaveParam;
import vip.xiaonuo.modular.warning.warningManager.param.WarningManagerTypeParam;
import vip.xiaonuo.modular.warning.warningManager.service.WarningManagerService;
import vip.xiaonuo.sys.modular.user.entity.SysUser;
import vip.xiaonuo.sys.modular.user.entity.vo.UserPhoneVo;
import vip.xiaonuo.sys.modular.user.service.SysUserService;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;


/**
 * 预警负责人Service接口实现类
 *
 * @author newspaper
 * @date  2023/12/25 09:36
 **/
@Service
public class WarningManagerServiceImpl extends ServiceImpl<WarningManagerMapper, WarningManager> implements WarningManagerService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public WarningManagerVo getManagerByType(WarningManagerTypeParam warningManagerTypeParam) {
        Integer type = warningManagerTypeParam.getType();
        String orgId = warningManagerTypeParam.getOrgId();
        WarningManager warningManager = this.getOne(new QueryWrapper<WarningManager>().lambda().eq(WarningManager::getType, type).eq(WarningManager::getOrgId, orgId));
        if (ObjectUtil.isEmpty(warningManager)){
            warningManager = new WarningManager();
            warningManager.setOrgId(orgId);
            warningManager.setType(type);
            this.save(warningManager);
        }
        WarningManagerVo warningManagerVo = new WarningManagerVo();
        List<UserPhoneVo> firstUserPhoneVoList = new ArrayList<>();
        List<UserPhoneVo> secondUserPhoneVoList = new ArrayList<>();
        String firstManagerId = warningManager.getFirstManagerId();
        if (ObjectUtil.isNotEmpty(firstManagerId)){
            String[] firstManagers = firstManagerId.split(",");
            for (String firstManager : firstManagers) {
                SysUser sysUser = sysUserService.queryEntity(firstManager);
                UserPhoneVo userPhoneVo = new UserPhoneVo();
                userPhoneVo.setName(sysUser.getName());
                userPhoneVo.setUserId(sysUser.getId());
                if (ObjectUtil.isNotEmpty(sysUser.getPhone())){
                    userPhoneVo.setPhone(sysUser.getPhone());
                }
                firstUserPhoneVoList.add(userPhoneVo);
            }
            warningManagerVo.setFirstManager(firstUserPhoneVoList);
        }else {
            UserPhoneVo userPhoneVo = new UserPhoneVo();
            firstUserPhoneVoList.add(userPhoneVo);
            warningManagerVo.setFirstManager(firstUserPhoneVoList);
        }
        String secondManager = warningManager.getSecondManagerId();
        if (ObjectUtil.isNotEmpty(secondManager)){
            String[] firstManagers = secondManager.split(",");
            for (String firstManager : firstManagers) {
                SysUser sysUser = sysUserService.queryEntity(firstManager);
                UserPhoneVo userPhoneVo = new UserPhoneVo();
                userPhoneVo.setName(sysUser.getName());
                userPhoneVo.setUserId(sysUser.getId());
                if (ObjectUtil.isNotEmpty(sysUser.getPhone())){
                    userPhoneVo.setPhone(sysUser.getPhone());
                }
                secondUserPhoneVoList.add(userPhoneVo);
            }
            warningManagerVo.setSecondManager(secondUserPhoneVoList);
        }else {
            UserPhoneVo userPhoneVo = new UserPhoneVo();
            secondUserPhoneVoList.add(userPhoneVo);
            warningManagerVo.setSecondManager(secondUserPhoneVoList);
        }
        if (ObjectUtil.isNotEmpty(warningManager.getWarningUpgradeTime())){
            warningManagerVo.setWarningUpgradeTime(warningManager.getWarningUpgradeTime());
        }
        return warningManagerVo;
    }

    @Override
    public void saveManager(WarningManagerSaveParam warningManagerSaveParam) {
        String orgId = warningManagerSaveParam.getOrgId();
        WarningManager warningManager = this.getOne(new QueryWrapper<WarningManager>().lambda().eq(WarningManager::getOrgId, orgId).eq(WarningManager::getType, warningManagerSaveParam.getType()));
        if (ObjectUtil.isEmpty(warningManager)){
            warningManager = new WarningManager();
        }
        BeanUtil.copyProperties(warningManagerSaveParam,warningManager);
        this.saveOrUpdate(warningManager);
    }

    @Override
    public List<SysUser> userSelector(WarningManagerOrgIdParam warningManagerOrgIdParam) {
        String orgId = warningManagerOrgIdParam.getOrgId();
        List<SysUser> users = sysUserService.list(new QueryWrapper<SysUser>().eq("ORG_ID", orgId));
        return users;
    }

}
