package jnpf.personnels.controller.web.formdata;

import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.model.personnels.dto.staff.field.FormTypeDto;
import jnpf.model.personnels.req.registerform.SaveFormDataReq;
import jnpf.model.personnels.req.registerform.SaveRegisterFormDataReq;
import jnpf.personnels.service.FtbPersonnelsStaffRegistrationFormDataService;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * web员工档案信息数据表模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/web/personnels-staff-registration-form-data")
public class FtbPersonnelsStaffRegistrationFormDataController {

    @Autowired
    private FtbPersonnelsStaffRegistrationFormDataService registrationFormDataService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 入职登记表表单(代为填写)
     *
     * @return
     */
    @GetMapping("/query-registration-form")
    public ActionResult<List<FormTypeDto>> queryRegistrationForm() {
        List<FormTypeDto> list = registrationFormDataService.queryRegistrationForm();
        return ActionResult.success(list);
    }

    /**
     * 查询入职内推登记表表单
     *
     * @param phone 手机号
     * @return
     */
    @GetMapping("/query-registration-form-onboarding/{phone}")
    public ActionResult<List<FormTypeDto>> queryRegistrationFormOnboarding(@PathVariable("phone") String phone) {
        List<FormTypeDto> list = registrationFormDataService.queryRegistrationForm(phone,true);
        return ActionResult.success(list);
    }
    /**
     * 查询登记表表单
     *
     * @param phone 手机号
     * @return
     */
    @GetMapping("/query-registration-form/{phone}")
    public ActionResult<List<FormTypeDto>> queryRegistrationForm(@PathVariable("phone") String phone) {
        List<FormTypeDto> list = registrationFormDataService.queryRegistrationForm(phone,false);
        return ActionResult.success(list);
    }


    /**
     * 根据userId查询档案表单
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/query-archival-form/roster/{userId}")
    public ActionResult<List<FormTypeDto>> queryArchivalFormForRoster(@PathVariable("userId") String userId) {
        List<FormTypeDto> list = registrationFormDataService.queryArchivalForm(userId);

        return ActionResult.success(list);
    }

    /**
     * 保存登记表
     *
     * @param req 参数
     * @return {@link ActionResult}<{@link String}>
     */
    @PostMapping("/save-register")
    public ActionResult<String> saveRegister(@Validated @RequestBody SaveRegisterFormDataReq req) {
        UserInfo userInfo = UserProvider.getUser();
        registrationFormDataService.saveRegistrationForm(userInfo.getTenantId(),req.getFieldValueList(),req.getFlag(),null, req.getModuleId());
        return ActionResult.success();
    }

    /**
     * 保存档案表单数据
     *
     * @param req 参数
     * @return {@link ActionResult}<{@link String}>
     */
    @PostMapping("/save-archives")
    public ActionResult<String> saveArchives(@Validated @RequestBody SaveFormDataReq req) {
        registrationFormDataService.saveArchives(req);
        return ActionResult.success();
    }
    /**
     * 保存 修改档案表单数据通过手机号
     *
     * @param req 参数
     * @return {@link ActionResult}<{@link String}>
     */
    @PutMapping("/save-archives-with-phone")
    public ActionResult<String> saveArchivesWithPhone(@Validated @RequestBody SaveFormDataReq req) {
        String lockKey = "save-archives-with-phone：" + req.getPhone();
        if (redisTemplate.opsForValue().setIfAbsent(lockKey, "1", 10, TimeUnit.SECONDS)) {
            try {
                registrationFormDataService.saveArchivesWithPhone(req);
                return ActionResult.success();
            } catch (Exception e) {
                throw e;
            } finally {
                redisTemplate.delete(lockKey);
            }
        } else {
            throw new RuntimeException("提交中,请稍候...");
        }
    }

    /**
     * 检查实际入职日期通过，返回true则通过，false及提示语则不通过
     *
     * @param actualStartDate 实际入职日期
     * @param userId          用户id
     * @return {@link ActionResult }<{@link Boolean }>
     */
    @GetMapping("/check-actual-joining-date-passed")
    public ActionResult<Boolean> checkActualJoiningDatePassed(@RequestParam("actualStartDate") String actualStartDate, @RequestParam("userId") String userId) {
        Boolean result = registrationFormDataService.checkActualJoiningDatePassed(actualStartDate, userId);
        return ActionResult.success(result);
    }

}
