/*
 * 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.warningInfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.modular.base.baseConfig.entity.BaseConfig;
import vip.xiaonuo.modular.base.baseConfig.mapper.BaseConfigMapper;
import vip.xiaonuo.modular.warning.warningInfo.entity.WarningInfo;
import vip.xiaonuo.modular.warning.warningInfo.entity.vo.WarningInfoListVo;
import vip.xiaonuo.modular.warning.warningInfo.entity.vo.WarningInfoProportionVo;
import vip.xiaonuo.modular.warning.warningInfo.entity.vo.WarningInfoRateVo;
import vip.xiaonuo.modular.warning.warningInfo.mapper.WarningInfoMapper;
import vip.xiaonuo.modular.warning.warningInfo.param.WarningInfoAddParam;
import vip.xiaonuo.modular.warning.warningInfo.param.WarningInfoEditParam;
import vip.xiaonuo.modular.warning.warningInfo.param.WarningInfoIdParam;
import vip.xiaonuo.modular.warning.warningInfo.param.WarningInfoPageParam;
import vip.xiaonuo.modular.warning.warningInfo.service.WarningInfoService;
import vip.xiaonuo.sys.modular.user.entity.SysUser;
import vip.xiaonuo.sys.modular.user.mapper.SysUserMapper;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预警Service接口实现类
 *
 * @author newspaper
 * @date  2023/12/26 13:40
 **/
@Service
public class WarningInfoServiceImpl extends ServiceImpl<WarningInfoMapper, WarningInfo> implements WarningInfoService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BaseConfigMapper baseConfigMapper;

    @Autowired
    private WarningInfoMapper warningInfoMapper;

    @Override
    public Page<WarningInfo> page(WarningInfoPageParam warningInfoPageParam) {
        String type = warningInfoPageParam.getType();
        QueryWrapper<WarningInfo> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(warningInfoPageParam.getWarningType())) {
            queryWrapper.lambda().eq(WarningInfo::getWarningType, warningInfoPageParam.getWarningType());
        }
        if(ObjectUtil.isNotEmpty(warningInfoPageParam.getUnitId())) {
            queryWrapper.lambda().eq(WarningInfo::getUnitId, warningInfoPageParam.getUnitId());
        }
        switch (type) {
            case "0":
                // 处理今日的逻辑
                queryWrapper.lambda().between(WarningInfo::getWarningTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date()));
                break;
            case "1":
                // 处理本周的逻辑
                queryWrapper.lambda().ge(WarningInfo::getWarningTime, DateUtil.beginOfWeek(new Date()));
                break;
            case "2":
                // 处理本月的逻辑
                queryWrapper.lambda().ge(WarningInfo::getWarningTime, DateUtil.beginOfMonth(new Date()));
                break;
            case "3":
                // 处理自定义时间范围的逻辑
                String startWarningTime = warningInfoPageParam.getStartWarningTime() + " 00:00:00";
                String endWarningTime = warningInfoPageParam.getEndWarningTime() + " 23:59:59";
                queryWrapper.lambda().between(WarningInfo::getWarningTime, startWarningTime, endWarningTime);
                break;
            default:
                throw new CommonException("显示类型不存在!");
        }
        queryWrapper.lambda().orderByDesc(WarningInfo::getWarningTime);
        Page<WarningInfo> warningInfoPage = this.page(CommonPageRequest.defaultPage(), queryWrapper);
        List<WarningInfo> warningInfoList = warningInfoPage.getRecords();
        for (WarningInfo warningInfo : warningInfoList) {
            String[] split = warningInfo.getWarningNotifierId().split(",");
            for (String s : split) {
                String name = sysUserMapper.selectById(s).getName();
                if (ObjectUtil.isNotEmpty(warningInfo.getWarningNotifierName())){
                    warningInfo.setWarningNotifierName(warningInfo.getWarningNotifierName() + "," + name);
                }else {
                    warningInfo.setWarningNotifierName(name);
                }
            }
        }
        return warningInfoPage;
    }

    @Override
    public WarningInfoProportionVo getProportion() {
        String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
        WarningInfoProportionVo warningInfoProportionVo = new WarningInfoProportionVo();
        long total = this.count(new QueryWrapper<WarningInfo>().lambda().eq(WarningInfo::getOrgId, orgId).ge(WarningInfo::getWarningTime, DateUtil.beginOfMonth(new Date())));
        warningInfoProportionVo.setTotal(total);

        String parentConfig = baseConfigMapper.selectOne(new QueryWrapper<BaseConfig>().lambda().eq(BaseConfig::getParentId, 0).eq(BaseConfig::getConfigName, "预警类型")).getId();
        List<BaseConfig> baseConfigs = baseConfigMapper.selectList(new QueryWrapper<BaseConfig>().lambda().eq(BaseConfig::getParentId, parentConfig).orderByAsc(BaseConfig::getSortCode));
        List<String> types = baseConfigs.stream().map(baseConfig -> baseConfig.getConfigName()).collect(Collectors.toList());
        List<WarningInfoRateVo> warningInfoRateVoList = new ArrayList<>();

        for (String type : types) {
            WarningInfoRateVo warningInfoRateVo = new WarningInfoRateVo();
            long count = this.count(new QueryWrapper<WarningInfo>().lambda().eq(WarningInfo::getOrgId, orgId).eq(WarningInfo::getWarningType, type).ge(WarningInfo::getWarningTime, DateUtil.beginOfMonth(new Date())));
            warningInfoRateVo.setTypeName(type);
            warningInfoRateVo.setWarningQuantity(count);

            // 使用 BigDecimal 进行比例计算
            BigDecimal countBigDecimal = BigDecimal.valueOf(count);
            BigDecimal totalBigDecimal = BigDecimal.valueOf(total);
            BigDecimal proportion = countBigDecimal.divide(totalBigDecimal, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));

            warningInfoRateVo.setProportion(proportion);
            warningInfoRateVoList.add(warningInfoRateVo);
        }
        warningInfoProportionVo.setRateList(warningInfoRateVoList);
        return warningInfoProportionVo;
    }

    @Override
    public List<WarningInfoListVo> listSevenDayWarning() {
        String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
        List<WarningInfoListVo> warningInfoListVoList = warningInfoMapper.listSevenDayWarning(orgId);
        return warningInfoListVoList;
    }
}
