package jnpf.personnels.controller.web.roster;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.base.ActionResult;
import jnpf.model.personnels.dto.staff.field.FormTypeDto;
import jnpf.model.personnels.dto.staff.growth.FtbPersonnelsStaffGrowthLogDto;
import jnpf.model.personnels.dto.staff.registerform.GroupFieldDataDto;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.model.personnels.vo.roster.FtbPersonnelsStaffArchivesHistoryVo;
import jnpf.personnels.mapper.FtbPersonnelsStaffRosterMapper;
import jnpf.personnels.service.FtbPersonnelsStaffArchivesHistoryService;
import jnpf.personnels.service.FtbPersonnelsStaffGrowthLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * 离职员工档案信息模块
 *
 * @author xxxxx
 */
@RestController
@Slf4j
@RequestMapping("/web/personnelsStaffArchivesHistory")
@Deprecated(since = "v2.1 二次入职时需要将登记表复原回填")
public class FtbPersonnelsStaffArchivesHistoryController {

    @Autowired
    private FtbPersonnelsStaffGrowthLogService growthLogService;

    @Autowired
    private FtbPersonnelsStaffArchivesHistoryService archivesHistoryService;

    @Autowired
    private FtbPersonnelsStaffRosterMapper ftbPersonnelsStaffRosterMapper;

    /**
     * 查询离职档案信息
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/get/{userId}")
    public ActionResult<List<FormTypeDto>> get(@PathVariable("userId") String userId) {
        FtbPersonnelsStaffArchivesHistoryVo info = archivesHistoryService.getInfo(userId);
        if (null != info && StringUtils.isNotEmpty(info.getArchives())) {
            List<FormTypeDto> list = JSONUtil.toList(info.getArchives(), FormTypeDto.class);
            // 实际入职日期
            Stream<GroupFieldDataDto> groupFieldDataDtoStream = list.stream().flatMap(a -> a.getGroupFieldDataDtoList().stream());
            GroupFieldDataDto first = groupFieldDataDtoStream.filter(a -> Objects.nonNull(a.getFormFieldDto()) && "actualStartDate".equals(a.getFormFieldDto().getId()))
                    .findFirst().orElse(null);
            GroupFieldDataDto companyAge = list.stream().flatMap(a -> a.getGroupFieldDataDtoList().stream())
                    .filter(a -> Objects.nonNull(a.getFormFieldDto()) && "companyAge".equals(a.getFormFieldDto().getId()))
                    .findFirst().orElse(null);
            LambdaQueryWrapper<FtbPersonnelsStaffRoster> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FtbPersonnelsStaffRoster::getUserId, userId);
            FtbPersonnelsStaffRoster staffRoster = ftbPersonnelsStaffRosterMapper.selectOne(queryWrapper);
            if (Objects.nonNull(first) && Objects.nonNull(staffRoster) && Objects.nonNull(staffRoster.getDepartDate())) {
                Date departDate = staffRoster.getDepartDate();
                String userValue = first.getFormFieldDto().getUserValue();
                DateTime dateTime = DateUtil.parse(userValue, "yyyy-MM-dd");
                long between = DateUtil.between(dateTime, departDate, DateUnit.DAY);
                if (Objects.nonNull(companyAge)) {
                    companyAge.getFormFieldDto().setUserValue(String.valueOf(between));
                }
            }
            return ActionResult.success(list);
        }
        return ActionResult.fail("未查询到该用户的档案信息");
    }


    /**
     * 查询离职员工成长列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/query-list/{userId}")
    public ActionResult<List<FtbPersonnelsStaffGrowthLogDto>> pageLists(@PathVariable("userId") String userId) {
        List<FtbPersonnelsStaffGrowthLogDto> list = growthLogService.queryAll(userId);
        return ActionResult.success(list);
    }

}
