package com.willalp.clockingin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.clockingin.domain.HsPnsClockingIn;
import com.willalp.clockingin.domain.HsPnsVacateTime;
import com.willalp.clockingin.mapper.HsPnsClockingInMapper;
import com.willalp.clockingin.mapper.HsPnsVacateTimeMapper;
import com.willalp.clockingin.service.IHsPnsClockingInService;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.bean.BeanUtils;
import com.willalp.yiqing.domain.vo.UserQueResultVo;
import com.willalp.yiqing.service.HsYiQingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 每日考勤信息Service业务层处理
 *
 * @author willalp
 * @date 2022-04-26
 */
@Service
public class HsPnsClockingInServiceImpl extends ServiceImpl<HsPnsClockingInMapper, HsPnsClockingIn> implements IHsPnsClockingInService {
    @Resource
    private HsPnsClockingInMapper clockingInMapper;

    @Resource
    private HsYiQingService yqService;

    @Resource
    private HsPnsVacateTimeMapper vacateTimeMapper;

    /**
     * 查询每日考勤信息列表
     *
     * @param hsPnsClockingIn 每日考勤信息
     * @return 每日考勤信息
     */
    @Override
    public List<HsPnsClockingIn> selectHsPnsClockingInList(HsPnsClockingIn hsPnsClockingIn) {
        return clockingInMapper.selectHsPnsClockingInList(hsPnsClockingIn);
    }

    @Override
    public int addDailyClockIn(UserQueResultVo userQueResultVo) {
        //数据验证
        userQueResultVo.verify(userQueResultVo);
        Integer clockInType = userQueResultVo.getClockInType();
        HsPnsClockingIn clockingIn = new HsPnsClockingIn();
        boolean isInsert = false;
        if (0 == clockInType) {
            isInsert = true;
            yqService.verifyUserQueAndInsert(userQueResultVo);
            BeanUtils.copyBeanProp(clockingIn, userQueResultVo);
            clockingIn.setTodayTime(DateUtils.getDate());
        } else {
            clockingIn = clockingInMapper.selectOne(new QueryWrapper<HsPnsClockingIn>()
                    .eq("user_no", userQueResultVo.getUserNo())
                    .eq("org_no", userQueResultVo.getOrgNo())
                    .eq("today_time", DateUtils.getDate()));
        }
        //上班卡
        if (0 == clockInType) {
            clockingIn.setDutyClockTime(new Date());
            clockingIn.setBelateTimeNum("未迟到");
            //添加请假信息
            HsPnsVacateTime vacateTime = new HsPnsVacateTime();
            BeanUtils.copyBeanProp(vacateTime, userQueResultVo);
            vacateTime.setVacateType("0");
            vacateTimeMapper.insert(vacateTime);
        } else if (1 == clockInType) {
            //迟到
            Date date = new Date();
            clockingIn.setDutyClockTime(date);
            Calendar calendar = Calendar.getInstance();
            //控制时
            calendar.set(Calendar.HOUR_OF_DAY, 9);
            //控制分
            calendar.set(Calendar.MINUTE, 0);
            //控制秒
            calendar.set(Calendar.SECOND, 0);
            String datePoor = DateUtils.getDatePoor(calendar.getTime(), date);
            clockingIn.setBelateTimeNum(datePoor);
        } else if (5 == clockInType) {
            //下班卡
            if (StringUtils.isBlank(userQueResultVo.getRemark())) {
                throw new BaseException("请提交打卡说明");
            }
            long time = clockingIn.getDutyClockTime().getTime() + (1000 * 3600 * 2);
            //是否已满俩个小时
            if (time >= System.currentTimeMillis()) {
                throw new BaseException("未满俩个小时，无法打卡");
            }
            clockingIn.setOffDutyClockTime(new Date());
        } else {
            if (StringUtils.isBlank(clockingIn.getRemark())) {
                throw new BaseException("请填写备注");
            }
        }

        //其他类型
        clockingIn.setClockingInType(String.valueOf(clockInType));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        clockingIn.setWeek(String.valueOf(calendar.get(Calendar.DAY_OF_WEEK) - 1));
        clockingIn.setShiftId("0");

        if (isInsert) {
            clockingInMapper.insert(clockingIn);
        }
        return clockingInMapper.updateById(clockingIn);
    }

    @Override
    public HsPnsClockingIn todayIsClockingIn(UserQueResultVo userQueResultVo) {
        userQueResultVo.verify(userQueResultVo);
        return clockingInMapper.selectOne(new QueryWrapper<HsPnsClockingIn>()
                .eq("user_no", userQueResultVo.getUserNo())
                .eq("org_no", userQueResultVo.getOrgNo())
                .eq("today_time", DateUtils.getDate()));
    }
}
