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

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
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.enrollment.enrollmentRegistration.domain.EnrollmentRegistration;
import com.jeeplus.human.enrollment.enrollmentRegistration.service.EnrollmentRegistrationService;
import com.jeeplus.human.practice.register.domain.Registration;
import com.jeeplus.human.practice.register.service.RegistrationBankService;
import com.jeeplus.human.practice.register.service.RegistrationService;
import com.jeeplus.human.practice.register.service.dto.RegistrationDto;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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.util.List;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-09-13 10:55
 */
@RestController
@Api("实习人员登记表信息")
@RequestMapping(value = "/register")
public class RegisterController {

    @Resource
    private RegistrationService service;
    @Resource
    private RegistrationBankService bankService;

    @Resource
    private EnrollmentRegistrationService enrollmentRegistrationService;

    /**
     * 查询
     * @param idCard
     * @return
     */
    @ApiOperation(value = "查询身份证号码唯一性")
    @GetMapping("/findIdCardOnly")
    public ResponseEntity<Integer> findIdCardOnly(@RequestParam String idCard) throws Exception{
        Integer idCardOnly = service.findIdCardOnly(idCard);
        return ResponseEntity.ok(idCardOnly);
    }

    /**
     * 查询财务项目信息列表
     * @param projectReportData
     * @param page
     * @return
     */
    @ApiLog("查询实习人员登记信息列表")
    @GetMapping("list")
    public ResponseEntity<IPage<RegistrationDto>> data(RegistrationDto projectReportData, Page<RegistrationDto> page) throws Exception {
        IPage<RegistrationDto> result = new Page<RegistrationDto>();
        result = service.findList (page,projectReportData);
        return ResponseEntity.ok (result);
    }

    /**
     * 根据id查询实习生信息
     * @param projectReportData
     * @param page
     * @return
     */
    @ApiLog("查询实习人员登记信息列表")
    @GetMapping("list2")
    public ResponseEntity<IPage<RegistrationDto>> data2(RegistrationDto projectReportData, Page<RegistrationDto> page) throws Exception {
        IPage<RegistrationDto> result = new Page<RegistrationDto>();
        result = service.findList2 (page,projectReportData);
        return ResponseEntity.ok (result);
    }

    /**
     * 保存实习人员登记信息
     * @param registration
     * @return
     */
    @ApiLog(value = "保存实习人员登记信息", type = LogTypeEnum.SAVE)
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody Registration registration) throws Exception {
        Registration s = service.saveRegistration(registration);
        return ResponseUtil.newInstance().add("businessTable", "human_resources_practice_registration")
                .add("businessId", s.getId())
                .ok ("操作成功");
    }

    /**
     * 保存实习人员登记信息
     * @param registration
     * @return
     */
    @ApiLog(value = "修改实习人员登记信息", type = LogTypeEnum.SAVE)
    @PostMapping("updateInfo")
    public ResponseEntity updateInfo(@Valid @RequestBody Registration registration) throws Exception {
        service.updateRegistration(registration);
        return ResponseUtil.newInstance().ok("操作成功");
    }

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

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

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

    /**
     * 查询3天未发送日志状态为实习中的人员信息
     * @return
     */
    @GetMapping("getTimeOutRegisters")
    public String getTimeOutRegisters(){
        List<Registration> listInfos = service.getTimeOutRegisters();
        return JSON.toJSONString(listInfos);
    }

    /**
     * 获取最后一次发起报销且实习状态为结束，并且报销状态为完成的
     * @return
     */
    @GetMapping("getTimeOutReimburseRegisters")
    public String getTimeOutReimburseRegisters(){
        List<Registration> listInfos = service.getTimeOutReimburseRegisters();
        return JSON.toJSONString(listInfos);
    }

    /**
     * 获取正式工在职状态为离职的，并且user表中未删除的
     * @return
     */
    @GetMapping("getFormalUserInfo")
    public String getFormalUserInfo(){
        List<EnrollmentRegistration> listInfos = enrollmentRegistrationService.getFormalUserInfo();
        return JSON.toJSONString(listInfos);
    }
}
