package com.yida.data.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.attendance.feign.RemoteAttendanceStrategyService;
import com.yida.data.common.core.entity.user.*;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.user.dto.*;
import com.yida.data.user.mapper.EduFaceGroupStrategyMapper;
import com.yida.data.user.service.*;
import com.yida.data.user.vo.StrategyInfoVO;
import com.yida.data.user.vo.StrategyPlanVO;
import com.yida.data.user.vo.StrategySpecialDateVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 人脸组-策略Service实现
 *
 * @author ZYJ
 * @date 2022-12-12 11:04:39
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduFaceGroupStrategyServiceImpl extends ServiceImpl
        <EduFaceGroupStrategyMapper, EduFaceGroupStrategy> implements EduFaceGroupStrategyService {

    @Lazy
    @Resource
    private EduFaceGroupUserService eduFaceGroupUserService;

    private final EduFaceGroupDeviceService eduFaceGroupDeviceService;
    private final EduFaceGroupStrategyPlanService eduFaceGroupStrategyPlanService;
    private final EduFaceGroupStrategyPlanTimeService eduFaceGroupStrategyPlanTimeService;
    private final EduFaceGroupStrategySpecialDateService eduFaceGroupStrategySpecialDateService;
    private final EduFaceGroupStrategySpecialTimeService eduFaceGroupStrategySpecialTimeService;

    private final RemoteAttendanceStrategyService remoteAttendanceStrategyService;

    @Override
    public StrategyInfoVO getStrategyInfo(Long faceGroupId) {
        EduFaceGroupStrategy groupStrategy = getOne(Wrappers.lambdaQuery(new EduFaceGroupStrategy())
                .eq(EduFaceGroupStrategy::getFaceGroupId, faceGroupId));
        // 返回数据
        StrategyInfoVO strategyInfoVO = new StrategyInfoVO();
        if (Objects.nonNull(groupStrategy)) {
            BeanUtils.copyProperties(groupStrategy, strategyInfoVO);
            strategyInfoVO.setStrategyId(groupStrategy.getId());
            // 查询人脸组-策略-通行计划
            List<StrategyPlanVO> planList = eduFaceGroupStrategyPlanService.listStrategyPlan(groupStrategy.getId());
            strategyInfoVO.setPlanList(planList);
            // 查询特殊日期
            List<StrategySpecialDateVO> specialDateList = eduFaceGroupStrategySpecialDateService.listStrategySpecialDate(groupStrategy.getId());
            strategyInfoVO.setSpecialList(specialDateList);
            // 处理周一到周日的唯一编码
            if (Objects.nonNull(groupStrategy.getMondayPlanId())) {
                // 周一
                strategyInfoVO.setMondayUniqueCode(
                        eduFaceGroupStrategyPlanService.getById(groupStrategy.getMondayPlanId()).getUniqueCode());
            }
            if (Objects.nonNull(groupStrategy.getTuesdayPlanId())) {
                // 周二
                strategyInfoVO.setTuesdayUniqueCode(
                        eduFaceGroupStrategyPlanService.getById(groupStrategy.getTuesdayPlanId()).getUniqueCode());
            }
            if (Objects.nonNull(groupStrategy.getWednesdayPlanId())) {
                // 周三
                strategyInfoVO.setWednesdayUniqueCode(
                        eduFaceGroupStrategyPlanService.getById(groupStrategy.getWednesdayPlanId()).getUniqueCode());
            }
            if (Objects.nonNull(groupStrategy.getThursdayPlanId())) {
                // 周四
                strategyInfoVO.setThursdayUniqueCode(
                        eduFaceGroupStrategyPlanService.getById(groupStrategy.getThursdayPlanId()).getUniqueCode());
            }
            if (Objects.nonNull(groupStrategy.getFridayPlanId())) {
                // 周五
                strategyInfoVO.setFridayUniqueCode(
                        eduFaceGroupStrategyPlanService.getById(groupStrategy.getFridayPlanId()).getUniqueCode());
            }
            if (Objects.nonNull(groupStrategy.getSaturdayPlanId())) {
                // 周六
                strategyInfoVO.setSaturdayUniqueCode(
                        eduFaceGroupStrategyPlanService.getById(groupStrategy.getSaturdayPlanId()).getUniqueCode());
            }
            if (Objects.nonNull(groupStrategy.getSundayPlanId())) {
                // 周日
                strategyInfoVO.setSundayUniqueCode(
                        eduFaceGroupStrategyPlanService.getById(groupStrategy.getSundayPlanId()).getUniqueCode());
            }
        }
        return strategyInfoVO;
    }

    @Override
    public void saveStrategyInfo(StrategySaveDTO strategySaveDTO) {
        // 查询关联的设备信息
        List<EduFaceGroupDevice> list = eduFaceGroupDeviceService.list(
                Wrappers.lambdaQuery(new EduFaceGroupDevice()).eq(EduFaceGroupDevice::getFaceGroupId, strategySaveDTO.getFaceGroupId()));
        if (CollUtil.isEmpty(list)) {
            throw new FebsException("请关联对应的设备信息");
        }

        EduFaceGroupStrategy groupStrategy = new EduFaceGroupStrategy();
        BeanUtils.copyProperties(strategySaveDTO, groupStrategy);
        groupStrategy.setId(strategySaveDTO.getStrategyId());

        if (Objects.isNull(strategySaveDTO.getStrategyId())) {
            // 新增
        } else {
            // 编辑, 需要删除对应的附表数据
            eduFaceGroupStrategyPlanService.remove(Wrappers.lambdaQuery(new EduFaceGroupStrategyPlan())
                    .eq(EduFaceGroupStrategyPlan::getFaceGroupStrategyId, strategySaveDTO.getStrategyId()));
            eduFaceGroupStrategyPlanTimeService.remove(Wrappers.lambdaQuery(new EduFaceGroupStrategyPlanTime())
                    .eq(EduFaceGroupStrategyPlanTime::getFaceGroupStrategyId, strategySaveDTO.getStrategyId()));

            eduFaceGroupStrategySpecialDateService.remove(Wrappers.lambdaQuery(new EduFaceGroupStrategySpecialDate())
                    .eq(EduFaceGroupStrategySpecialDate::getFaceGroupStrategyId, strategySaveDTO.getStrategyId()));
            eduFaceGroupStrategySpecialTimeService.remove(Wrappers.lambdaQuery(new EduFaceGroupStrategySpecialTime())
                    .eq(EduFaceGroupStrategySpecialTime::getFaceGroupStrategyId, strategySaveDTO.getStrategyId()));
        }
        saveOrUpdate(groupStrategy);

        if (CollUtil.isNotEmpty(strategySaveDTO.getPlanList())) {
            // 处理通行计划
            for (StrategyPlanSaveDTO planDTO : strategySaveDTO.getPlanList()) {
                EduFaceGroupStrategyPlan strategyPlan = new EduFaceGroupStrategyPlan();
                strategyPlan.setFaceGroupId(strategySaveDTO.getFaceGroupId());
                strategyPlan.setFaceGroupStrategyId(groupStrategy.getId());
                strategyPlan.setUniqueCode(planDTO.getUniqueCode());
                eduFaceGroupStrategyPlanService.save(strategyPlan);

                List<EduFaceGroupStrategyPlanTime> planTimeList = new ArrayList<>();
                // 处理时间
                for (StrategyPlanTimeSaveDTO timeDTO : planDTO.getPlanTimeList()) {
                    EduFaceGroupStrategyPlanTime strategyPlanTime = new EduFaceGroupStrategyPlanTime();
                    strategyPlanTime.setFaceGroupId(strategySaveDTO.getFaceGroupId());
                    strategyPlanTime.setFaceGroupStrategyId(groupStrategy.getId());
                    strategyPlanTime.setFaceGroupStrategyPlanId(strategyPlan.getId());
                    strategyPlanTime.setPlanStartTime(timeDTO.getPlanStartTime());
                    strategyPlanTime.setPlanEndTime(timeDTO.getPlanEndTime());
                    planTimeList.add(strategyPlanTime);

                    eduFaceGroupStrategyPlanTimeService.save(strategyPlanTime);
                }

                // 处理一周的通行计划
                if (strategyPlan.getUniqueCode().equals(strategySaveDTO.getMondayUniqueCode())) {
                    // 周一
                    groupStrategy.setMondayPlanId(strategyPlan.getId());
                    groupStrategy.setMondayTimeList(planTimeList);
                }
                if (strategyPlan.getUniqueCode().equals(strategySaveDTO.getTuesdayUniqueCode())) {
                    // 周二
                    groupStrategy.setTuesdayPlanId(strategyPlan.getId());
                    groupStrategy.setTuesdayTimeList(planTimeList);
                }
                if (strategyPlan.getUniqueCode().equals(strategySaveDTO.getWednesdayUniqueCode())) {
                    // 周三
                    groupStrategy.setWednesdayPlanId(strategyPlan.getId());
                    groupStrategy.setWednesdayTimeList(planTimeList);
                }
                if (strategyPlan.getUniqueCode().equals(strategySaveDTO.getThursdayUniqueCode())) {
                    // 周四
                    groupStrategy.setThursdayPlanId(strategyPlan.getId());
                    groupStrategy.setThursdayTimeList(planTimeList);
                }
                if (strategyPlan.getUniqueCode().equals(strategySaveDTO.getFridayUniqueCode())) {
                    // 周五
                    groupStrategy.setFridayPlanId(strategyPlan.getId());
                    groupStrategy.setFridayTimeList(planTimeList);
                }
                if (strategyPlan.getUniqueCode().equals(strategySaveDTO.getSaturdayUniqueCode())) {
                    // 周六
                    groupStrategy.setSaturdayPlanId(strategyPlan.getId());
                    groupStrategy.setSaturdayTimeList(planTimeList);
                }
                if (strategyPlan.getUniqueCode().equals(strategySaveDTO.getSundayUniqueCode())) {
                    // 周日
                    groupStrategy.setSundayPlanId(strategyPlan.getId());
                    groupStrategy.setSundayTimeList(planTimeList);
                }
            }
        }
        List<EduFaceGroupStrategySpecialDate> specialDateList = new ArrayList<>();
        if (CollUtil.isNotEmpty(strategySaveDTO.getSpecialList())) {
            // 处理特殊日期
            for (StrategySpecialDateSaveDTO specialDateSaveDTO : strategySaveDTO.getSpecialList()) {
                if (Objects.isNull(specialDateSaveDTO.getSpecialStartDate())) {
                    continue;
                }
                EduFaceGroupStrategySpecialDate specialDate = new EduFaceGroupStrategySpecialDate();
                specialDate.setFaceGroupId(strategySaveDTO.getFaceGroupId());
                specialDate.setFaceGroupStrategyId(groupStrategy.getId());
                specialDate.setSpecialStartDate(specialDateSaveDTO.getSpecialStartDate());
                specialDate.setSpecialEndDate(specialDateSaveDTO.getSpecialEndDate());
                eduFaceGroupStrategySpecialDateService.save(specialDate);
                // 处理特殊日期时间
                List<EduFaceGroupStrategySpecialTime> specialTimeList = new ArrayList<>();
                for (StrategySpecialTimeSaveDTO specialTimeSaveDTO : specialDateSaveDTO.getSpecialTimeList()) {
                    EduFaceGroupStrategySpecialTime specialTime = new EduFaceGroupStrategySpecialTime();
                    specialTime.setFaceGroupId(strategySaveDTO.getFaceGroupId());
                    specialTime.setFaceGroupStrategyId(groupStrategy.getId());
                    specialTime.setFaceGroupStrategySpecialDateId(specialDate.getId());
                    specialTime.setSpecialStartTime(specialTimeSaveDTO.getSpecialStartTime());
                    specialTime.setSpecialEndTime(specialTimeSaveDTO.getSpecialEndTime());
                    eduFaceGroupStrategySpecialTimeService.save(specialTime);
                    specialTimeList.add(specialTime);
                }
                specialDate.setSpecialTimeList(specialTimeList);
                specialDateList.add(specialDate);
            }
        }
        groupStrategy.setSpecialDateList(specialDateList);
        // 处理一周的通行计划
        baseMapper.updateWeekPlan(groupStrategy);
        // 设置设备信息
        groupStrategy.setGroupDeviceList(list);
        // 设置人员信息
        List<EduFaceGroupUser> groupUserList = eduFaceGroupUserService.list(Wrappers.lambdaQuery(new EduFaceGroupUser())
                .eq(EduFaceGroupUser::getFaceGroupId, groupStrategy.getFaceGroupId())
                // 查询已下发人脸
                .eq(EduFaceGroupUser::getStatus, 1)
        );
        groupStrategy.setGroupUserList(groupUserList);
        // 保存策略并绑定人员信息
        remoteAttendanceStrategyService.saveStrategyAndUser(groupStrategy);
    }
}
