package org.dromara.system.service.impl.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.api.client.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.dromara.common.core.domain.AjaxResult;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.dto.RoleDTO;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.*;
import org.dromara.system.domain.bo.FormalParentsStudentBo;
import org.dromara.system.domain.bo.FormalTeacherClassBo;
import org.dromara.system.domain.bo.SysDeptBo;
import org.dromara.system.domain.bo.XiaoyuanInfoBo;
import org.dromara.system.domain.info.XiaoyuanInfo;
import org.dromara.system.domain.info.vo.XiaoyuanInfoVo;
import org.dromara.system.domain.notice.XiaoyuanNotice;
import org.dromara.system.domain.notice.vo.XiaoyuanNoticeVo;
import org.dromara.system.domain.vo.*;
import org.dromara.system.domain.xiake.XiakeConfig;
import org.dromara.system.domain.xiake.bo.XiakeConfigBo;
import org.dromara.system.domain.xiake.vo.XiakeConfigVo;
import org.dromara.system.mapper.*;
import org.dromara.system.mapper.info.XiaoyuanInfoMapper;
import org.dromara.system.mapper.notice.XiaoyuanNoticeMapper;
import org.dromara.system.mapper.xiake.XiakeConfigMapper;
import org.dromara.system.service.IFormalParentsStudentService;
import org.dromara.system.service.ISysDeptService;
import org.dromara.system.service.common.IAppletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static org.dromara.common.core.constant.CacheConstants.*;

/**
 * @Author: tjf
 * @Date: 2023/8/21 10:22
 * @Describe:
 */
@RequiredArgsConstructor
@Service
public class AppletServiceImpl implements IAppletService {


    private final XiakeConfigMapper xiakeConfigMapper;

    private final RegisterParentsStudentMapper registerParentsStudentMapper;


    private final CourseChangeMapper courseChangeMapper;


    private final FormalTeacherClassMapper formalTeacherClassMapper;

    private final IFormalParentsStudentService formalParentsStudentServicel;


    private final RegisterTeacherMapper registerTeacherMapper;

    private final SysDeptMapper sysDeptMapper;


    private final XiaoyuanNoticeMapper xiaoyuanNoticeMapper;

    private final XiaoyuanInfoMapper xiaoyuanInfoMapper;
    private final ISysDeptService deptService;

    @Override
    public R<Void> xiake(FormalTeacherClass formalTeacherClass) {
        String key = DateUtils.getDate() + ":" + formalTeacherClass.getSchoolId() + ":" + formalTeacherClass.getClassId();
        //key = 学校id:班级id
        //Redis根据key键，查询对应的值
        String value = RedisUtils.getCacheObject(key);
        if ("2".equals(formalTeacherClass.getType())) {
            if (value.contains("time")) {
                return R.fail("未准备放学,请勿点击延迟放学");
            } else {
                //延迟放学
                String[] split = value.split(":");
                //下课时间
                String xiakeTime = split[1] + ":" + split[2] + ":" + split[3];
                //获取参数中默认下课时间
                XiakeConfigBo config = new XiakeConfigBo();
                config.setConfigKey(formalTeacherClass.getClassId() + ":2");
                XiakeConfigVo retConfig = xiakeConfigMapper.selectConfig(config);
                //延迟下课的默认值
                String configValue = retConfig.getConfigValue();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                    date = sdf.parse(xiakeTime);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    calendar.add(Calendar.MINUTE, Integer.parseInt(configValue));
                    String time = sdf.format(calendar.getTime());
                    value = split[0] + ":" + time;
                    RedisUtils.setCacheObject(key, value);
                    //如果是延迟放学,给后台首页记录延迟放学
                    //key = TODAY_YAN_CHI:学校id:日期 value = 数量
                    Object num = RedisUtils.getCacheObject(TODAY_YAN_CHI + formalTeacherClass.getSchoolId() + ":" + DateUtils.getDate());
                    if (ObjectUtils.isEmpty(num)) {
                        //如果是空,则说明今天还没有延迟放学,则插入
                        RedisUtils.setCacheObject(TODAY_YAN_CHI + formalTeacherClass.getSchoolId() + ":" + DateUtils.getDate(), 1);
                        RedisUtils.expire(TODAY_YAN_CHI + formalTeacherClass.getSchoolId() + ":" + DateUtils.getDate(), 86400 - (System.currentTimeMillis() / 1000 + 8 * 3600) % 86400);
                    } else {
                        //不为空则+1
                        RedisUtils.setCacheObject(TODAY_YAN_CHI + formalTeacherClass.getSchoolId() + ":" + DateUtils.getDate(), (int) num + 1);
                        RedisUtils.expire(TODAY_YAN_CHI + formalTeacherClass.getSchoolId() + ":" + DateUtils.getDate(), 86400 - (System.currentTimeMillis() / 1000 + 8 * 3600) % 86400);
                    }

                    return R.ok("延迟放学成功");
                } catch (ParseException e) {
                    return R.fail("延迟放学失败");
                }
            }
        }
        if ("1".equals(formalTeacherClass.getType())) {
            if (!value.contains("time")) {
                return R.fail("请勿重复点击");
            }
            //value = 班级名称:放学时间
            //获取当前时间
            Calendar nowTime = Calendar.getInstance();
            //获取参数中默认下课时间
            XiakeConfigBo config = new XiakeConfigBo();
            config.setConfigKey(formalTeacherClass.getClassId() + ":1");
            XiakeConfigVo retConfig = xiakeConfigMapper.selectConfig(config);
            String configValue = retConfig.getConfigValue();
            nowTime.add(Calendar.MINUTE, Integer.parseInt(configValue));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            value = value.replace("time", sdf.format(nowTime.getTime()));
            RedisUtils.setCacheObject(key, value);
        }
        return R.ok();
    }


    @Override
    public AjaxResult index(FormalTeacherClass formalTeacherClass) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("all", 0);
        Collection<String> keys = RedisUtils.keys(DateUtils.getDate() + ":" + formalTeacherClass.getSchoolId() + "*");
        if (keys == null || keys.size() == 0) {
            afterClass();
            keys = RedisUtils.keys(DateUtils.getDate() + ":" + formalTeacherClass.getSchoolId() + "*");
        }
        if (keys != null && keys.size() > 0) {
            map.put("all", keys.size());
            map.put("n", 0);
            map.put("y", 0);
            int n = 0;
            for (String key : keys) {
                //Redis根据key键，查询对应的值
                String value = RedisUtils.getCacheObject(key);
                //value = 班级名称:放学时间
                //判断有多少放学时间是time,占位符
                String[] split = value.split(":");
                String time = split[1];
                if ("time".equals(time)) {
                    n = n + 1;
                }
            }
            if (n > 0) {
                map.put("n", n);
                int y = keys.size() - n;
                map.put("y", Math.max(y, 0));
            }
        }
        return AjaxResult.success(map);
    }

    public void afterClass() {

        LoginUser user = LoginHelper.getLoginUser();
        //获取所有部门
        SysDeptBo bo = new SysDeptBo();
        bo.setTenantId(user.getTenantId());
        List<SysDeptVo> sysDeptList = sysDeptMapper.selectDeptListMapper(new SysDeptBo());

        //学校部门
        List<SysDeptVo> fSysDeptList = new ArrayList<>();
        //班级部门
        List<SysDeptVo> pSysDeptList = new ArrayList<>();

        for (SysDeptVo sysDept : sysDeptList) {
            if (sysDept.getParentId() == 0) {
                fSysDeptList.add(sysDept);
            } else {
                pSysDeptList.add(sysDept);
            }
        }

        for (SysDeptVo sysDept : fSysDeptList) {
            //设置昨日延迟放学数量 = TODAY_YAN_CHI + formalTeacherClass.getSchoolId() + ":" + DateUtils.getDate()
            Object num = RedisUtils.getCacheObject(TODAY_YAN_CHI + sysDept.getDeptId() + ":" + DateUtils.getDateByDays(-1));
            long count = pSysDeptList.stream().filter(s -> s.getParentId().equals(sysDept.getDeptId())).count();
            if (ObjectUtils.isEmpty(num)) {
                //设置昨日延迟放学数量 = 0
                RedisUtils.setCacheObject(LAST_DAY_YAN_CHI + sysDept.getDeptId() + ":" + DateUtils.getDateByDays(-1), 0);
                //设置昨日准时放学数量 =该学校下所有班级的数量
                RedisUtils.setCacheObject(LAST_DAY_ZHUN_SHI + sysDept.getDeptId() + ":" + DateUtils.getDateByDays(-1),count );
            } else {
                RedisUtils.setCacheObject(LAST_DAY_YAN_CHI + sysDept.getDeptId() + ":" + DateUtils.getDateByDays(-1), num);
                RedisUtils.setCacheObject(LAST_DAY_ZHUN_SHI + sysDept.getDeptId() + ":" + DateUtils.getDateByDays(-1), count - (int) num);
            }
            //设置今日延迟放学数量
            RedisUtils.setCacheObject(TODAY_YAN_CHI + sysDept.getDeptId() + ":" + DateUtils.getDate(), 0);
            for (SysDeptVo dept : pSysDeptList) {
                if (sysDept.getDeptId().equals(dept.getParentId())) {
                    String key = DateUtils.getDate() + ":" + sysDept.getDeptId() + ":" + dept.getDeptId();
                    String value = dept.getDeptName() + ":time";
                    RedisUtils.setCacheObject(key, value);
                }
            }
        }
    }

    @Override
    public AjaxResult indexList(FormalTeacherClass formalTeacherClass) {
        List<Map<String, Object>> list = new ArrayList<>();
        Collection<String> keys = RedisUtils.keys(DateUtils.getDate() + ":" + formalTeacherClass.getSchoolId() + "*");
        if (keys != null && keys.size() > 0) {
            for (String key : keys) {
                String value = RedisUtils.getCacheObject(key);
                //value = 班级名称:放学时间
                String[] split = value.split(":");
                Map<String, Object> map = new HashMap<>();
                map.put("className", split[0]);
                if ("time".equals(split[1])) {
                    map.put("time", split[1]);
                } else {
                    map.put("time", value.split(" ")[1]);
                }
                list.add(map);
            }
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult pcStatistics() {
        Map<String, Object> map = new HashMap<>();
        //获取当前月第一天
        String monthFirst = DateUtils.timeMonthFirst();
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<RoleDTO> roles = loginUser.getRoles();
        boolean blt = false;
        boolean bls = false;
        for (RoleDTO role : roles) {
            if ("teacher".equals(role.getRoleKey())) {
                blt = true;
            }
            if ("school".equals(role.getRoleKey())) {
                bls = true;
            }
        }
        //账号
        int tzhysh = 0;
        int tzhwsh = 0;
        //调课
        int ttkysh = 0;
        int ttkwsh = 0;
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();

        //计算本人
        XiaoyuanNotice xiaoyuanNotice = new XiaoyuanNotice();
        xiaoyuanNotice.setSenderId(String.valueOf(loginUser.getUserId()));
        List<XiaoyuanNoticeVo> xiaoyuanNoticeList = xiaoyuanNoticeMapper.selectXiaoyuanNoticeMyList(xiaoyuanNotice);
        int xwsl = 0;
        if (xiaoyuanNoticeList != null && xiaoyuanNoticeList.size() > 0) {
            xwsl = xiaoyuanNoticeList.size();
        }
        map.put("xwsl", xwsl);

        map1.put("zhysh", tzhysh);
        map1.put("zhwsh", tzhwsh);
        map.put("byzhsh", map1);
        map2.put("tkysh", ttkysh);
        map2.put("tkwsh", ttkwsh);
        map.put("bytksh", map2);

        //老师
        if (blt) {
            //获取老师所属班级
            FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
            formalTeacherClass.setTeacherId(loginUser.getUserId());
            List<FormalTeacherClassVo> formalTeacherClasses = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
            List<Long> classIds = formalTeacherClasses.stream().map(FormalTeacherClassVo::getClassId).collect(Collectors.toList());
            //查询本月家长的审核数据
            List<RegisterParentsStudentVo> registerParentsStudentList = registerParentsStudentMapper.selectRegisterParentsStudentListByTime(monthFirst, classIds);
            if (registerParentsStudentList != null && registerParentsStudentList.size() > 0) {
                for (RegisterParentsStudentVo registerParentsStudent : registerParentsStudentList) {
                    if ("1".equals(registerParentsStudent.getIsPass())) {
                        tzhwsh++;
                    } else {
                        tzhysh++;
                    }
                }
            }
            //查询本月调课审核
            List<CourseChangeVo> courseChangeList = courseChangeMapper.selectCourseChangeListByTime(monthFirst, loginUser.getUserId(), 0L);
            if (courseChangeList != null && courseChangeList.size() > 0) {
                for (CourseChangeVo courseChange : courseChangeList) {
                    if ("1".equals(courseChange.getIsPass())) {
                        ttkwsh++;
                    } else {
                        ttkysh++;
                    }
                }
            }
            map1.put("zhysh", tzhysh);
            map1.put("zhwsh", tzhwsh);
            map.put("byzhsh", map1);
            map2.put("tkysh", ttkysh);
            map2.put("tkwsh", ttkwsh);
            map.put("bytksh", map2);

        }


        //学校
        //账号
        int szhysh = 0;
        int szhwsh = 0;
        //调课
        int stkysh = 0;
        int stkwsh = 0;
        if (bls) {
            //查询本月老师的审核数据
            List<RegisterTeacherVo> registerTeacherList = registerTeacherMapper.selectRegisterTeacherListByTime(monthFirst, loginUser.getDeptId());
            if (registerTeacherList != null && registerTeacherList.size() > 0) {
                for (RegisterTeacherVo registerTeacher : registerTeacherList) {
                    if ("1".equals(registerTeacher.getIsPass())) {
                        szhysh++;
                    } else {
                        szhwsh++;
                    }
                }
            }
            //查询本月学校的调课审核
            List<CourseChangeVo> courseChangeList = courseChangeMapper.selectCourseChangeListByTime(monthFirst, 0L, loginUser.getDeptId());
            if (courseChangeList != null && courseChangeList.size() > 0) {
                for (CourseChangeVo courseChange : courseChangeList) {
                    if ("1".equals(courseChange.getIsPass())) {
                        stkysh++;
                    } else {
                        stkwsh++;
                    }
                }
            }

            map1.put("zhysh", szhysh);
            map1.put("zhwsh", szhwsh);
            map.put("byzhsh", map1);
            map2.put("tkysh", stkysh);
            map2.put("tkwsh", stkwsh);
            map.put("bytksh", map2);
        }
        //两个都有

        if (blt && bls) {

            //账号
            int zzhysh = tzhysh + szhysh;
            int zzhwsh = tzhwsh + szhwsh;
            //调课
            int ztkysh = stkysh;
            int ztkwsh = stkwsh;

            map1.put("zhysh", zzhysh);
            map1.put("zhwsh", zzhwsh);
            map.put("byzhsh", map1);
            map2.put("tkysh", ztkysh);
            map2.put("tkwsh", ztkwsh);
            map.put("bytksh", map2);

        }
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult pcClassStudentNum() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<RoleDTO> roles = loginUser.getRoles();
        boolean bls = false;
        for (RoleDTO role : roles) {
            if ("school".equals(role.getRoleKey())) {
                bls = true;
                break;
            }
        }
        if ("admin".equals(loginUser.getUsername())) {
            bls = true;
        }
        //查看所有班级
        if (bls) {
            List<SysDeptVo> sysDeptVos = deptService.selectDeptList(new SysDeptBo());
            //查询所有班级的学生
            List<Long> deptIdList = sysDeptVos.stream().map(SysDeptVo::getDeptId).collect(Collectors.toList());
            FormalParentsStudentBo sbo = new FormalParentsStudentBo();
            sbo.setClassIds(deptIdList);
            List<FormalParentsStudentVo> formalParentsStudent = formalParentsStudentServicel.queryList(sbo);
            Map<Long, List<FormalParentsStudentVo>> studentMap = formalParentsStudent.stream().collect(Collectors.groupingBy(FormalParentsStudentVo::getClassId));
            for (SysDeptVo sysDeptVo : sysDeptVos) {
                if (sysDeptVo.getParentId() != 0L) {
                    Map<String, Object> cMap = new HashMap<>();
                    int index = 0;
                    if (studentMap.get(sysDeptVo.getDeptId()) != null) {
                        index = studentMap.get(sysDeptVo.getDeptId()).size();
                    }
                    sysDeptVo.setStudentNum(index);
                    cMap.put("className", sysDeptVo.getDeptName());
                    cMap.put("value", sysDeptVo);
                    mapList.add(cMap);
                }
            }

        } else {
            //老师，查询自己的班级
            FormalTeacherClassBo formalTeacherClass = new FormalTeacherClassBo();
            formalTeacherClass.setTeacherId(loginUser.getUserId());
            List<FormalTeacherClassVo> formalTeacherClassList = formalTeacherClassMapper.queryMapperList(formalTeacherClass);
            List<Long> classIds = formalTeacherClassList.stream().map(FormalTeacherClassVo::getClassId).collect(Collectors.toList());
            Set<Long> classIdList = formalTeacherClassList.stream().map(FormalTeacherClassVo::getClassId).collect(Collectors.toSet());
            FormalParentsStudentBo sbo = new FormalParentsStudentBo();
            sbo.setClassIds(classIds);
            List<FormalParentsStudentVo> formalParentsStudent = formalParentsStudentServicel.queryList(sbo);
            Map<Long, List<FormalParentsStudentVo>> studentMap = formalParentsStudent.stream().collect(Collectors.groupingBy(FormalParentsStudentVo::getClassId));
            for (Long classId : classIdList) {
                //查询部门
                SysDeptVo sysDeptVo = sysDeptMapper.selectDeptById(classId);
                if (sysDeptVo != null) {
                    int index = 0;
                    if (studentMap.get(classId) != null) {
                        index = studentMap.get(classId).size();
                    }
                    sysDeptVo.setStudentNum(index);
                    Map<String, Object> tMap = new HashMap<>();
                    tMap.put("className", sysDeptVo.getDeptName());
                    tMap.put("value", sysDeptVo);
                    mapList.add(tMap);
                }

            }
        }

        return AjaxResult.success(mapList);
    }

    /**
     * 首页统计今日准时放学/延迟放学/本周发布文章
     */
    @Override
    public AjaxResult fangXueStatistics() {
        //今日准时放学 = 所有班级数量- 延迟放学
        Map<String, Object> map = new HashMap();
        Collection<String> keys = RedisUtils.keys(DateUtils.getDate() + ":" + LoginHelper.getDeptId() + "*");
        if (keys == null || keys.size() < 1) {
            afterClass();
        }
        Collection<String> schoolKeys = RedisUtils.keys(DateUtils.getDate() + ":" + LoginHelper.getDeptId() + "*");
        //获取今日延迟放学数量
        Object todayYanChiNum = RedisUtils.getCacheObject(TODAY_YAN_CHI + LoginHelper.getDeptId() + ":" + DateUtils.getDate());
        //获取昨日准时放学
        Object lastDayZhunShiNum = RedisUtils.getCacheObject(LAST_DAY_ZHUN_SHI + LoginHelper.getDeptId() + ":" + DateUtils.getDateByDays(-1));
        map.put("zhunShiType", 0);
        map.put("lastDayZhunShi", 0);
        if (ObjectUtils.isNotEmpty(todayYanChiNum)) {
            //今日准时放学
            map.put("todayZhunShi", schoolKeys.size() - (int) todayYanChiNum);
            //昨日准时放学
            if (ObjectUtils.isNotEmpty(lastDayZhunShiNum) && (schoolKeys.size() - (int) todayYanChiNum) > (int) lastDayZhunShiNum) {
                //0平1上2下
                map.put("zhunShiType", 1);
                //昨日准时放学差值
                map.put("lastDayZhunShi", schoolKeys.size() - (int) todayYanChiNum - (int) lastDayZhunShiNum);
            } else if (ObjectUtils.isNotEmpty(lastDayZhunShiNum) && (schoolKeys.size() - (int) todayYanChiNum) < (int) lastDayZhunShiNum) {
                map.put("zhunShiType", 2);
                //昨日准时放学差值
                map.put("lastDayZhunShi", (int) lastDayZhunShiNum - (schoolKeys.size() - (int) todayYanChiNum));
            } else {
                //昨日准时放学差值
                map.put("lastDayZhunShi", 0);
            }
        } else {
            map.put("todayZhunShi", schoolKeys.size());
        }

        //今日延迟
        map.put("todayYanChi", 0);
        Object lastDayYanChi = RedisUtils.getCacheObject(LAST_DAY_YAN_CHI + LoginHelper.getDeptId() + ":" + DateUtils.getDateByDays(-1));
        map.put("yanChiType", 0);
        map.put("lastDayYanChi", 0);
        if (ObjectUtils.isNotEmpty(todayYanChiNum) && ObjectUtils.isNotEmpty(lastDayYanChi)) {
            map.put("todayYanChi", todayYanChiNum);
            //昨日准时放学
            if ((int) todayYanChiNum > (int) lastDayYanChi) {
                //0平1上2下
                map.put("yanChiType", 1);
                //昨日准时放学差值
                map.put("lastDayYanChi", (int) todayYanChiNum - (int) lastDayYanChi);
            } else if ((int) todayYanChiNum < (int) lastDayYanChi) {
                map.put("yanChiType", 2);
                //昨日延迟放学差值
                map.put("lastDayYanChi", (int) lastDayYanChi - (int) todayYanChiNum);
            }
        }

        //本周文章发布数量
        LambdaQueryWrapper<XiaoyuanInfo> lqw = buildQueryWrapper();
        Long weekInfo = xiaoyuanInfoMapper.selectCount(lqw);
        map.put("weekInfo", weekInfo);
        LambdaQueryWrapper<XiaoyuanInfo> lqwLast = buildQueryWrapperLast();
        Long lastWeekInfo = xiaoyuanInfoMapper.selectCount(lqwLast);
        map.put("weekInfoType", 0);
        map.put("lastWeekInfo", 0);
        //昨日准时放学
        if (weekInfo > lastWeekInfo) {
            //0平1上2下
            map.put("weekInfoType", 1);
            //上周发布文章差值
            map.put("lastWeekInfo", weekInfo - lastWeekInfo);
        } else if (weekInfo < lastWeekInfo) {
            map.put("weekInfoType", 2);
            //上周发布文章差值
            map.put("lastWeekInfo", lastWeekInfo - weekInfo);
        }
        return AjaxResult.success(map);
    }

    private LambdaQueryWrapper<XiaoyuanInfo> buildQueryWrapper() {
        LambdaQueryWrapper<XiaoyuanInfo> lqw = Wrappers.lambdaQuery();
        lqw.apply(" WEEK(`fa_bu_time`,1) = WEEK(NOW(),1)");
        return lqw;
    }

    private LambdaQueryWrapper<XiaoyuanInfo> buildQueryWrapperLast() {
        LambdaQueryWrapper<XiaoyuanInfo> lqw = Wrappers.lambdaQuery();
        lqw.apply(" WEEK(`fa_bu_time`,1) =  WEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK), 1)");
        return lqw;
    }
}
