package com.jwipc.order.logic.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.order.convert.AppointmentTimeConvert;
import com.jwipc.order.dto.AppointmentTime2DTO;
import com.jwipc.order.dto.AppointmentTimeDTO;
import com.jwipc.order.entity.AppointmentTime;
import com.jwipc.order.logic.AppointmentTimeLogic;
import com.jwipc.order.model.po.AppointmentTime2PO;
import com.jwipc.order.model.po.AppointmentTimePO;
import com.jwipc.order.model.query.AppointmentRecordQuery;
import com.jwipc.order.model.query.AppointmentTimeQuery;
import com.jwipc.order.service.IAppointmentTimeService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/5
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AppointmentTimeLogicImpl implements AppointmentTimeLogic {

    private final AppointmentTimeConvert appointmentTimeConvert;
    private final IAppointmentTimeService appointmentTimeService;


    @Override
    public PageView getList(AppointmentTimeQuery query) {
        final Page<AppointmentTime> dbPage = PageUtil.toPage(query);
        List<AppointmentTimePO> poList = appointmentTimeService.findPageByQuery(dbPage,query);

        List<AppointmentTimeDTO> dtoList = appointmentTimeConvert.po2PageListDTO(poList);
        return PageUtil.getPV(dtoList, dbPage.getTotal());
    }

    @Override
    public AppointmentTimeDTO getById(Long id) {
        AppointmentTime appointmentTime = appointmentTimeService.getById(id);
        return appointmentTimeConvert.EntityToDto(appointmentTime);
    }

    @Override
    public void saveData(AppointmentTimeDTO appointmentTimeDto) {
////        AppointmentTime appointmentTime = appointmentTimeConvert.dtoToEntity(appointmentTimeDto);
////        if(appointmentTimeDto.getStartTime() = )
//        List<AppointmentTime> appointmentTime1 = appointmentTimeService.list(new LambdaQueryWrapper<AppointmentTime>().eq(AppointmentTime::getWeek,appointmentTimeDto.getWeek()));
        AppointmentTime appointmentTime = appointmentTimeConvert.dtoToEntity(appointmentTimeDto);
//        for (AppointmentTime time : appointmentTime1) {
//            if(time.getWeek().equals(appointmentTime.getWeek())) {
//                if (time.getStartTime().equals(appointmentTime.getStartTime()) && time.getEndTime().equals(appointmentTime.getEndTime())) {
//                    throw new DaoException("同一天的预约时间不可重复，请重新填写预约起始时间！");
//                }
//            }
//        }
        boolean b = appointmentTimeService.saveOrUpdate(appointmentTime);
        if (!b) {
            throw new DaoException("保存失败，请联系管理员");
        }
    }

    @Override
    public void removeById(Long id) {
        AppointmentTime appointmentTime = new AppointmentTime();
        appointmentTime.setId(id);
        appointmentTime.setDeleteFlag(1);
        boolean b = appointmentTimeService.updateById(appointmentTime);
        if (!b) {
            throw new DaoException("删除失败，请联系管理员");
        }
    }

    @Override
    public List<AppointmentTimeDTO> selectorList(AppointmentTimeQuery query) {
        List<AppointmentTimePO> poList = appointmentTimeService.findPageByQuery(null, query);

        return appointmentTimeConvert.po2PageListDTO(poList);
    }

    @Override
    public List<AppointmentTime2DTO> getTimeRangeData(AppointmentRecordQuery query) {

        String selectDay = query.getSelectDay();
        int weekOfDay = getWeekOfDay(selectDay);
        query.setWeekOfDay(weekOfDay);
        List<AppointmentTime2PO> poList = appointmentTimeService.getTimeRangeData(query);

        List<AppointmentTime2DTO> dtoList = appointmentTimeConvert.po2ToListDTO(poList);
        dtoList.forEach(t -> {
            String startTime = t.getStartTime();
            String endTime = t.getEndTime();
            t.setRang(startTime + StringPool.DASH + endTime);
            t.setStartTime(selectDay + " " + startTime);
            t.setEndTime(selectDay + " " + endTime);
        });
        return dtoList;
    }

    @Override
    public AppointmentTime2DTO getTimeRangeCouldNum(String selectDay, String appointmentId, String dockerCode, String factoryCode) {
        AppointmentRecordQuery query = new AppointmentRecordQuery();
        query.setFactoryCode(factoryCode);
        query.setDockerCode(dockerCode);
        query.setSelectDay(selectDay);
        query.setWeekOfDay(getWeekOfDay(selectDay));
        query.setAppointmentId(Long.parseLong(appointmentId));
        List<AppointmentTime2PO> poList = appointmentTimeService.getTimeRangeData(query);
        List<AppointmentTime2DTO> dtoList = appointmentTimeConvert.po2ToListDTO(poList);
        return CollectionUtil.isEmpty(dtoList) ? new AppointmentTime2DTO() : dtoList.get(0);
    }

    /**
     * 获取日期对应的周几  和系统配置表中的item_value相匹配
     *
     * @param day 日期
     * @return item_value
     */
    private int getWeekOfDay(String day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtil.parse(day));
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return w + 1;
    }
}
