package com.jeeplus.human.practice.logwrite.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.human.practice.logwrite.domain.HolidayVo;
import com.jeeplus.human.practice.logwrite.domain.RegistrationWorkLog;
import com.jeeplus.human.practice.logwrite.service.RegistrationWorkLogService;
import com.jeeplus.human.practice.logwrite.service.dto.RegistrationWorkLogDto;
import com.jeeplus.human.practice.logwrite.util.HolidayUtil;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-09-15 10:02
 */
@RestController
@Api("实习日志信息")
@RequestMapping(value = "/workLog")
public class RegistrationWorkLogController {

    @Resource
    private RegistrationWorkLogService workLogService;

    /**
     * 查询财务项目信息列表
     * @param projectReportData
     * @param page
     * @return
     */
    @ApiLog("查询实习日志信息列表")
    @PreAuthorize("hasAuthority('workLog:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<RegistrationWorkLogDto>> data(RegistrationWorkLogDto projectReportData, Page<RegistrationWorkLogDto> page) throws Exception {
        IPage<RegistrationWorkLogDto> result = new Page<RegistrationWorkLogDto>();
        result = workLogService.findList (page,projectReportData);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询财务项目信息列表
     * @param projectReportData
     * @param page
     * @return
     */
    @ApiLog("查询实习日志信息列表")
    @GetMapping("completeData")
    public ResponseEntity<IPage<RegistrationWorkLogDto>> completeData(RegistrationWorkLogDto projectReportData, Page<RegistrationWorkLogDto> page) throws Exception {
        IPage<RegistrationWorkLogDto> result = new Page<RegistrationWorkLogDto>();
        projectReportData.setType("5");
        result = workLogService.findList (page,projectReportData);
        return ResponseEntity.ok (result);
    }

    /**
     * 保存实习人员登记信息
     * @param workLog
     * @return
     */
    @ApiLog(value = "保存实习人员登记信息", type = LogTypeEnum.SAVE)
    //@PreAuthorize("hasAnyAuthority('workLog:add','workLog:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody RegistrationWorkLog workLog) throws Exception {
        RegistrationWorkLog s = workLogService.saveRegistrationWorkLog(workLog);
        return ResponseUtil.newInstance().add("businessTable", "human_resources_practice_registration_work_log")
                .add("businessId", s.getId())
                .ok ("操作成功");
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/findById")
    public ResponseEntity<RegistrationWorkLog> findById(@RequestParam String id) throws Exception{
        RegistrationWorkLog dto = workLogService.findById(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 根据id修改状态status
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "updateStatusById")
    public void updateStatusById(@RequestBody RegistrationWorkLogDto dto) {
        workLogService.updateStatusById(dto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @GetMapping("/remove")
    public ResponseEntity<String> remove(@RequestParam String id){
        String s = workLogService.remove(id);
        return ResponseEntity.ok(s);
    }

    /**
     * 根据开始时间与结束时间获取实际工作天数
     * @return
     */
    @ApiOperation(value = "根据开始时间与结束时间获取实际工作天数")
    @GetMapping("/getWorkDay")
    public ResponseEntity<Integer> getWorkDay(@RequestParam String startTime, @RequestParam String endTime) throws IOException, ParseException {
        //根据当前年份获取今年的所有节假日工作日信息
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 解析日期时间字符串
        Date dateTime = dateTimeFormat.parse(startTime);
        Date dateTime2 = dateTimeFormat.parse(endTime);
        // 创建日期格式化对象，只包含日期部分
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 格式化日期对象为日期字符串
        String start = dateFormat.format(dateTime);
        String end = dateFormat.format(dateTime2);
        int holidayCounts = HolidayUtil.getHolidayCounts(start, end);
        return ResponseEntity.ok(holidayCounts);
    }
}
