package org.dromara.elevator.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.elevator.domain.EElevatorInfo;
import org.dromara.elevator.domain.ESignInInfo;
import org.dromara.elevator.mapper.EElevatorInfoMapper;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EAnnualPlanBo;
import org.dromara.elevator.domain.vo.EAnnualPlanVo;
import org.dromara.elevator.domain.EAnnualPlan;
import org.dromara.elevator.mapper.EAnnualPlanMapper;
import org.dromara.elevator.service.IEAnnualPlanService;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * 年检计划;Service业务层处理
 *
 * @author zy
 * @date 2024-09-03
 */
@RequiredArgsConstructor
@Service
public class EAnnualPlanServiceImpl implements IEAnnualPlanService {

    private final EAnnualPlanMapper baseMapper;
    private final EElevatorInfoMapper elevatorInfoMapper;

    /**
     * 查询年检计划;
     *
     * @param id 主键
     * @return 年检计划;
     */
    @Override
    public EAnnualPlanVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询年检计划;列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 年检计划;分页列表
     */
    @Override
    public TableDataInfo<EAnnualPlanVo> queryPageList(EAnnualPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EAnnualPlan> lqw = buildQueryWrapper(bo);
        Page<EAnnualPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 超期未年检数据统计
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<EAnnualPlanVo> overduePageList(EAnnualPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EAnnualPlan> query = Wrappers.lambdaQuery();
        query.lt( EAnnualPlan::getNextAnnual, LocalDate.now());
        Page<EAnnualPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), query);

        List<EAnnualPlanVo> annualList = result.getRecords();
        if (CollUtil.isNotEmpty(annualList)) {
            for (EAnnualPlanVo annual : annualList) {
                // 获取电梯名称
                LambdaQueryWrapper<EElevatorInfo> elevator = Wrappers.lambdaQuery();
                elevator.eq(EElevatorInfo::getElevatorNum, annual.getElevatorNum());
                EElevatorInfo elevatorInfo = elevatorInfoMapper.selectOne(elevator);
                annual.setElevatorName(elevatorInfo.getElevatorName());

                long days = ChronoUnit.DAYS.between(annual.getNextAnnual(), LocalDate.now());
                annual.setExceedDay(days);
            }
        }

        result.setRecords(annualList);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的年检计划;列表
     *
     * @param bo 查询条件
     * @return 年检计划;列表
     */
    @Override
    public List<EAnnualPlanVo> queryList(EAnnualPlanBo bo) {
        LambdaQueryWrapper<EAnnualPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EAnnualPlan> buildQueryWrapper(EAnnualPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EAnnualPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EAnnualPlan::getElevatorNum, bo.getElevatorNum());
        lqw.eq(bo.getAnnualDate() != null, EAnnualPlan::getAnnualDate, bo.getAnnualDate());
        lqw.eq(bo.getNextAnnual() != null, EAnnualPlan::getNextAnnual, bo.getNextAnnual());
        lqw.eq(bo.getState() != null, EAnnualPlan::getState, bo.getState());
        lqw.orderByDesc(EAnnualPlan::getCreateTime);
        return lqw;
    }

    /**
     * 新增年检计划;
     *
     * @param bo 年检计划;
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EAnnualPlanBo bo) {
        EAnnualPlan add = MapstructUtils.convert(bo, EAnnualPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改年检计划;
     *
     * @param bo 年检计划;
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EAnnualPlanBo bo) {
        EAnnualPlan update = MapstructUtils.convert(bo, EAnnualPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EAnnualPlan entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除年检计划;信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取年检日历
     * @return
     */
    @Override
    public Set<LocalDate> getDateList() {
        Set<LocalDate> retSet = new HashSet<>();
        List<EAnnualPlan> annualPlans = baseMapper.selectList();
        if (CollUtil.isEmpty(annualPlans)) {
            return retSet;
        }
        for (EAnnualPlan annual : annualPlans) {
            retSet.add(annual.getNextAnnual());
        }
        return retSet;
    }

    /**
     * 根据日期获取电梯年检信息
     * @param planDate
     * @return
     */
    @Override
    public List<EAnnualPlanVo> getAnnualByDate(LocalDate planDate) {
        List<EAnnualPlanVo> annualPlanList = baseMapper.selectVoList(Wrappers.<EAnnualPlan>lambdaQuery().eq(EAnnualPlan::getNextAnnual, planDate));
        if (CollUtil.isEmpty(annualPlanList)) {
            return annualPlanList;
        }
        for (EAnnualPlanVo annual : annualPlanList) {
            // 获取电梯名称
            EElevatorInfo elevatorInfo = elevatorInfoMapper.selectOne(Wrappers.<EElevatorInfo>lambdaQuery().eq(EElevatorInfo::getElevatorNum, annual.getElevatorNum()));
            annual.setElevatorName(elevatorInfo.getElevatorName());
        }
        return annualPlanList;
    }
}
