package com.xueyi.system.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xueyi.common.web.annotation.TenantIgnore;
import com.xueyi.common.web.utils.DateUtils;
import com.xueyi.system.api.pass.domain.po.DmRecognizedRecordsPo;
import com.xueyi.system.api.staff.domain.dto.DmStaffDto;
import com.xueyi.system.api.staff.domain.po.DmStaffPo;
import com.xueyi.system.staff.domain.po.DmEmpAttendancePo;
import com.xueyi.system.staff.mapper.DmEmpAttendanceMapper;
import com.xueyi.system.staff.domain.query.DmStaffQuery;
import com.xueyi.system.api.staff.domain.vo.DmStaffFeature;
import com.xueyi.system.staff.manager.IDmStaffManager;
import com.xueyi.system.staff.mapper.DmStaffMapper;
import com.xueyi.system.staff.service.IDmStaffService;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class DmStaffServiceImpl extends BaseServiceImpl<DmStaffQuery, DmStaffDto, IDmStaffManager> implements IDmStaffService {

    @Autowired
    DmStaffMapper staffMapper;

    @Autowired
    DmEmpAttendanceMapper dmEmpAttendanceMapper;

    @Autowired
    private DmEmpAttendanceServiceImpl dmEmpAttendanceService;
    /**
     * 查询人员对象列表 | 数据权限
     *
     * @param dmStaff 人员对象
     * @return 人员对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"DmStaffMapper"})
    public List<DmStaffDto> selectListScope(DmStaffQuery dmStaff) {
        return baseManager.selectList(dmStaff);
    }

    @Override
    @TenantIgnore(tenantLine = true)
    public List<DmStaffPo> selectStaffList(DmStaffPo staff) {
        List<DmStaffPo> result = staffMapper.selectStaffList(staff);
        List<DmStaffPo> po = staffMapper.selectList(null);
        return result;
    }

    @Override
    @TenantIgnore(tenantLine = true)
    public Long selectTenantId(DmStaffPo staff) {
        QueryWrapper<DmStaffPo> wrapper = new QueryWrapper<>();
        wrapper.eq("sn_code",staff.getDeptId());
        DmStaffPo po= staffMapper.selectOne(wrapper);
        if (po != null) {
            return po.getEnterpriseId();
        }
        return null;

    }
    @Override
    public List<DmStaffFeature> selectStaffListByTimestamp(String devId,String timestamp) {
        List<DmStaffFeature> staffFeatures = staffMapper.selectStaffFeaturesList(devId, timestamp);
        staffFeatures.addAll(staffMapper.selectVisitorFeaturesList(devId, timestamp));
        return staffFeatures;
    }


    public void updateOrInsertAttendance(DmRecognizedRecordsPo checkRecords) {
        if (null == checkRecords.getUserId()) {
            return ;
        }
        Long userId = checkRecords.getUserId();
        DmStaffPo emp = this.selectById(userId);
        if (null == emp) {
            return ;
        }

        Date checkTime = checkRecords.getRecognizedTime();
        String checkTimeDateStr = DateUtils.formatDate(checkTime, "yyyy-MM-dd");
        if (StringUtils.isEmpty(checkTimeDateStr)) {
            return ;
        }

        DmEmpAttendancePo empAttendance = dmEmpAttendanceMapper.findByEmpAndDate(checkTimeDateStr, userId);

        if (null != empAttendance) {

            if (null == empAttendance.getCheckInTime()) {
                empAttendance.setCheckInTime(checkTime);
            } else {
                empAttendance.setCheckOutTime(checkTime);
            }

            if (null!=empAttendance.getCheckInTime() && null != empAttendance.getCheckOutTime()) {
                Date cin = empAttendance.getCheckInTime();
                Date cout = empAttendance.getCheckOutTime();
                Integer diffMin = DateUtils.dateDiffMin(cin, cout);
                empAttendance.setWorkDuration(diffMin);
            }
            QueryWrapper<DmEmpAttendancePo> wrapper = new QueryWrapper<>();
            wrapper.eq("id",empAttendance.getId());
            dmEmpAttendanceMapper.update(empAttendance, wrapper);

        } else {
            DmEmpAttendancePo ea = new DmEmpAttendancePo();
            Date checkDate = DateUtils.parseStrToDate(DateUtils.formatDate(checkTime, "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            ea.setCheckInTime(checkDate);

            ea.setDateStr(DateUtils.formatDate(new Date(), "yyyy-MM-dd"));
            ea.setUserId(userId);
            ea.setUserName(emp.getUserName());
            ea.setUserType(Long.parseLong(emp.getUserType()));
            ea.setDeptId(emp.getDeptId());
            ea.setWorkDuration(0);
            ea.setCreateTime(DateUtils.currentDateToLocalDateTime());
            ea.setUpdateTime(DateUtils.currentDateToLocalDateTime());
            dmEmpAttendanceService.updRedisCount();

            dmEmpAttendanceMapper.insert(ea);
        }
    }
}