package com.xjrsoft.module.weekly.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.module.weekly.entity.WeeklyDutySchedule;
import com.xjrsoft.module.weekly.mapper.WeeklyDutyScheduleMapper;
import com.xjrsoft.module.weekly.service.IWeeklyDutyScheduleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @title: 值周排班
* @Author dzx
* @Date: 2023-12-30
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class WeeklyDutyScheduleServiceImpl extends MPJBaseServiceImpl<WeeklyDutyScheduleMapper, WeeklyDutySchedule> implements IWeeklyDutyScheduleService {
    private final WeeklyDutyScheduleMapper weeklyDutyScheduleMapper;
    @Override
    public void deleteAll() {
//        weeklyDutyScheduleMapper.delete(
//            Wrappers.<WeeklyDutySchedule>query().lambda().gt(WeeklyDutySchedule::getCreateDate, new Date())
//        );
        weeklyDutyScheduleMapper.deleteAll();
    }

    @Override
    public Boolean insertMany(List<Map<Integer, Object>> savedDataList) {
        int sortCode = 1;
        long createUserId = StpUtil.getLoginIdAsLong();
        Date createDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        for (Map<Integer, Object> ScheduleVo : savedDataList) {
            if(ObjectUtil.isNull(ScheduleVo.get(0)) || ObjectUtil.isNull(ScheduleVo.get(1)) || ObjectUtil.isNull(ScheduleVo.get(3))){
                throw new MyException("表中有必填项信息未填写，请填写" );
            }
            WeeklyDutySchedule schedule = new WeeklyDutySchedule();
            schedule.setUserNumber(ScheduleVo.get(0).toString());
            schedule.setName(ScheduleVo.get(1).toString());
            schedule.setGender(ScheduleVo.get(2).toString());
            schedule.setCredentialNumber(ScheduleVo.get(3).toString());
            if(ObjectUtil.isNotNull(ScheduleVo.get(4))){
                try {
                    schedule.setDateBirth(new Timestamp(sdf.parse(ScheduleVo.get(4).toString()).getTime()));
                } catch (ParseException e) {
                    throw new MyException("出生日期格式错误" );
                }
            }

            schedule.setAge(Integer.valueOf(ScheduleVo.get(5).toString()));
            schedule.setRemark(ScheduleVo.get(6).toString());
            schedule.setSortCode(sortCode);
            schedule.setCreateDate(createDate);
            schedule.setCreateUserId(createUserId);
            schedule.setDeleteMark(DeleteMark.NODELETE.getCode());
            schedule.setEnabledMark(EnabledMark.ENABLED.getCode());
            weeklyDutyScheduleMapper.insert(schedule);
            sortCode ++;
        }
        return true;
    }
}
