package me.zhengjie.modules.thirdparty.v1;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.base.BaseResponse;
import me.zhengjie.base.QueryPageParams;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.dto.DmAttendanceRecordQueryCriteria;
import me.zhengjie.modules.dm.dmCard.service.DmCardService;
import me.zhengjie.modules.dm.dmCard.service.dto.DmCardQueryCriteria;
import me.zhengjie.modules.dmAccessTraffic.service.dto.DmAccessTrafficQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "用户接口")
@RequestMapping("/api/thirdparty/v2/user")
public class OpenApiV2Controller {

    @Autowired
    JdbcTemplate jdbcTemplate;

//    @AnonymousPostMapping("/getAttendance")
//    @Log("查询当前前一天的考勤表中的所有数据")
//    @ApiOperation("查询当前前一天的考勤表中的所有数据")
//    public List<Map<String, Object>> getAll(@RequestBody QueryPageParams<DmAttendanceRecordQueryCriteria> criteria) {
//        SecurityUtils.CheckApiAuth(criteria);
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        Date date = new Date();
//        Calendar calendar = Calendar.getInstance(); //得到日历
//        calendar.setTime(date);//把当前时间赋给日历
//        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
//        Date beforeDate = calendar.getTime();   //得到前一天的时间
//        // 获取前一天的表名
//        String tableName = "dm_attendance_record_" + sdf.format(beforeDate);
//        // 查询前一天考勤表中的所有数据
//        String sql = "select id, user_number, card_number, room_device, property, attendance_time, attendance_photo from " + tableName;
//        List<Map<String, Object>> attendanceList = jdbcTemplate.queryForList(sql);
//        return attendanceList;
//    }
//
//    //每天凌晨12点10分执行
//    @Scheduled(cron = "0 10 0 * * ?")
//    //异步执行,指定线程池(配置类里配置)
//    @Async("threadPoolTaskExecutor1")
//    @Log("同步通行记录表中的数据到考勤表")
//    @ApiOperation("同步通行记录表中的数据到考勤表")
//    @GetMapping(value = "/syncData")
//    @AnonymousGetMapping("@el.check('dmAttendanceRecord:list')")
//    public void syncData() {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        Date date = new Date();
//        Calendar calendar = Calendar.getInstance(); //得到日历
//        calendar.setTime(date);//把当前时间赋给日历
//        calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
//        Date beforeDate = calendar.getTime();   //得到前一天的时间
//        // 获取前一天的表名
//        String tableName = "dm_attendance_record_" + sdf.format(beforeDate);
////        dmAttendanceRecordRepository.inertTable(tableName);
//        // 创建当前的前一天的考勤表
//        String sql = "create table " + tableName + " (" +
//                "`id` bigint(20) NOT NULL AUTO_INCREMENT," +
//                "`user_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '用户'," +
//                "`card_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '卡号'," +
//                "`room_device` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '门禁设备编号'," +
//                "`property` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '考勤属性(00-上班、01-下班)'," +
//                "`attendance_time` datetime(0) NULL DEFAULT NULL COMMENT '考勤时间'," +
//                "`attendance_photo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '考勤照片'," +
//                "`create_by` varchar(255) DEFAULT NULL COMMENT '创建者'," +
//                "`update_by` varchar(255) DEFAULT NULL COMMENT '更新者'," +
//                "`create_time` datetime DEFAULT NULL COMMENT '创建时间'," +
//                "`update_time` datetime DEFAULT NULL COMMENT '更新时间'," +
//                "PRIMARY KEY (`id`)" +
//                ")";
//        jdbcTemplate.execute(sql);
//        // 查询通行记录表中当前前一天每个人的上下班记录
//        String accessSql = "select * from dm_access_traffic as a," +
//                "(select user_number, MAX(access_time) as access_time from dm_access_traffic " +
//                "where DATE_FORMAT(access_time,'%Y%m%d') = " + sdf.format(beforeDate) + " " +
//                "and come_go_out = '00' " +
//                "GROUP BY user_number) as b " +
//                "where a.user_number = b.user_number and a.access_time = b.access_time " +
//                "UNION " +
//                "select * from dm_access_traffic as a," +
//                "(select user_number, MIN(access_time) as access_time from dm_access_traffic " +
//                "where DATE_FORMAT(access_time,'%Y%m%d') = " + sdf.format(beforeDate) + " " +
//                "and come_go_out = '01' " +
//                "GROUP BY user_number) as b " +
//                "where a.user_number = b.user_number and a.access_time = b.access_time ";
//        List<Map<String, Object>> accessList = jdbcTemplate.queryForList(accessSql);
//        // 将数据同步到考勤表
//        String addAttendanceSql = "insert into " + tableName + " (user_number, card_number, room_device, property, attendance_time, attendance_photo, create_time) values ";
//        String dot = "";
//        for(Map<String, Object> map : accessList) {
//            addAttendanceSql += dot + "(" +
//                    (map.get("user_number") == null ? null : ("'" + map.get("user_number") + "'")) +  "," +
//                    (map.get("card_number") == null ? null : ("'" + map.get("card_number") + "'")) +  "," +
//                    (map.get("room_device") == null ? null : ("'" + map.get("room_device") + "'")) +  "," +
//                    (map.get("come_go_out") == null ? null : ("'" + map.get("come_go_out") + "'")) +  "," +
//                    (map.get("access_time") == null ? null : ("'" + map.get("access_time") + "'")) +  "," +
//                    (map.get("access_photo") == null ? null : ("'" + map.get("access_photo") + "'")) +  "," +
//                    "now()" +
//                    ")";
//            dot = ",";
//        }
//        // 将数据插入到考勤表
//        jdbcTemplate.execute(addAttendanceSql);
//    }
}