package com.boman.web.core.service;



import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.DateUtils;
import com.boman.common.core.utils.SecurityUtils;
import com.boman.domain.SysUser;
import com.boman.domain.dto.AjaxResult;
import com.boman.domain.dto.FormDataDto;
import com.boman.system.api.RemoteUserService;
import com.boman.web.core.service.common.ICommonService;
import com.boman.web.core.utils.AuthUtils;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.json.Json;

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

import static com.boman.domain.constant.AttendanceConst.*;
import static com.boman.domain.constant.AttendanceConst.ATTENDANCE_DEPT_ID;


/**
 * @author tjf
 * @Date: 2021/08/04/17:49
 */
@Component
public class TaskService {
    @Resource
    private TableServiceCmdService tableServiceCmdService;

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private ICommonService commonService;
    private static final String tableName= "attendance_table";

    /**
     * 每月1号往考勤记录表根据用户插入当月的数据 每月一号00:00分
     */
    public void configureTasks() {
        //获取当月每一天
        String date = DateUtils.getDate();
        List<String> monthEveryDays = DateUtils.getMonthEveryDays(date);
        //获取所有用户
        List<SysUser> sysUsers = remoteUserService.selectUserListAll();
        if (sysUsers != null && sysUsers.size() > 0){
            if (monthEveryDays != null) {
                JSONObject condition = new JSONObject();
                //需要ATTENDANCE_TABLE_TODAYTIME字段是查询可见
                condition.put(ATTENDANCE_TABLE_TODAYTIME,monthEveryDays);
                condition.put(IS_DEL,"N");
                //先去查询是否已经存在当前日期的记录
                List<JSONObject> byMap = commonService.getByMap(tableName, condition);
                if (byMap != null){
                    List<String> days = new ArrayList<>();
                    for (JSONObject jsonObject : byMap) {
                        String day = jsonObject.get(ATTENDANCE_TABLE_TODAYTIME).toString();
                        days.add(day);
                    }
                    monthEveryDays.removeAll(days);
                }
                for (String monthEveryDay : monthEveryDays) {
                    for (SysUser sysUser : sysUsers) {
                        JSONObject commitData = new JSONObject();
                        commitData.put(ATTENDANCE_USER_NAME,sysUser.getUserName());
                        commitData.put(ATTENDANCE_USER_ID, sysUser.getId());
                        commitData.put(ATTENDANCE_DEPT_ID,sysUser.getDeptId());
                        commitData.put(ATTENDANCE_TABLE_TODAYTIME,monthEveryDay);
                        commitData.put(ATTENDANCE_TABLE_CREATE_TIME,DateUtils.getNowDate());
                        commitData.put(ATTENDANCE_TABLE_CREATE_BY,"admin");
                        FormDataDto dto = new FormDataDto();
                        dto.setObjId(-1L);
                        dto.setTable(tableName);
                        dto.setFixedData(commitData);
                        tableServiceCmdService.objectSave(dto);
                    }
                }
            }
        }
    }
}
