package com.ruoyi.docking.component;

import cn.hutool.core.util.CreditCodeUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.docking.entity.SmartDeclaration;
import com.ruoyi.docking.service.SmartDeclarationService;
import com.ruoyi.docking.service.UserService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 企业或者政务自定义定时提醒组件
 *
 * @author du
 * @since 2024/7/18 9:55
 */
@Service("timingRemind")
public class TimingRemindImpl implements TimingRemindService {

    private static final Logger log = LoggerFactory.getLogger(TimingRemindImpl.class);
    /**
     * 智能提醒关联表
     */
    @Resource
    private SmartDeclarationService smartDeclarationService;

    /**
     * 用户
     */
    @Resource
    private UserService userService;


    @Resource
    private SysUserMapper sysUserMapper;


    /**
     * 企业端要定时新增数据，并且还要发短信
     */
    public void enterpriseTiming(String t, Long id) {
        //模拟企业人员数据
        //给所有的企业都要添加一条智能提醒数据
        List<SysUser> sysUsers = sysUserMapper.selectUserList(new SysUser());
        //获取到所有的符合条件的政务用户
        List<SysUser> collect = sysUsers.stream().filter(x -> "0".equals(x.getDelFlag()) && "0".equals(x.getStatus()))
                .filter(y -> CreditCodeUtil.isCreditCode(y.getUserName()))
                .collect(Collectors.toList());
        List<SmartDeclaration> listAdd = new ArrayList<>();
        collect.forEach(x -> {
            SmartDeclaration sd = new SmartDeclaration();
            sd.setAlertTime(LocalDateTime.parse(t));
            sd.setIsRead(1);
            sd.setSmartRemindersId(id);
            sd.setTyshxydm(x.getUserName());
            listAdd.add(sd);
        });
        smartDeclarationService.saveBatch(listAdd);
        log.info("提醒成功!");
    }


    /**
     * 政务端要定时新增数据
     */
    public void chiefTiming(String time, Long id, String pid,String content) {
        List<SmartDeclaration> listAdd = new ArrayList<>();
        //给所有政务人员发通知和短信
        //添加用户进表的时候手机号已经经过处理
//        for (SysUser x : userService.getAllZwUser()) {
//            SmartDeclaration sd = new SmartDeclaration();
//            sd.setAlertTime(LocalDateTime.parse(t));
//            sd.setSmartRemindersId(id);
            if ("null".equals(pid)) {
                userService.getAllZwUser(content,null,id,time);
            } else {
                userService.getAllZwUser(content,Long.valueOf(pid),id,time);
            }
//            sd.setZwId(x.getUserId());
//            listAdd.add(sd);
//        }
        log.info("提醒成功!");
    }
}
