package jnpf.personnels.controller.web.uchisuike;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import jnpf.model.personnels.po.FtbPersonnelsStaffRegistrationFormData;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.personnels.mapper.FtbPersonnelsStaffRegistrationFormDataMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/web/uchisuike/personnelInformationSupplement")
public class PersonnelInformationSupplementController {

    @Resource
    private FtbPersonnelsStaffRegistrationFormDataMapper ftbPersonnelsStaffRegistrationFormDataMapper;

    /**
    * 补录人事员工类型数据
    */
    @GetMapping("/supplementEmployeeType")
    @Transactional(rollbackFor = Exception.class)
    public void supplementEmployeeType() {
        List<FtbPersonnelsStaffRegistrationFormData> result = new ArrayList<>();
        // 员工状态
        List<FtbPersonnelsStaffRoster> workerStatus = ftbPersonnelsStaffRegistrationFormDataMapper.queryStaffRosterList("workerStatus");
        workerStatus.forEach(a->{
            if (StrUtil.isNotBlank(a.getWorkerStatus())) {
                FtbPersonnelsStaffRegistrationFormData ftbPersonnelsStaffRegistrationFormData = new FtbPersonnelsStaffRegistrationFormData();
                ftbPersonnelsStaffRegistrationFormData.setRosterId(a.getId());
                ftbPersonnelsStaffRegistrationFormData.setPhone(a.getPhone());
                ftbPersonnelsStaffRegistrationFormData.setValue(a.getWorkerStatus());
                ftbPersonnelsStaffRegistrationFormData.setFormFieldId("workerStatus");
                ftbPersonnelsStaffRegistrationFormData.setFormTypeId("3");
                result.add(ftbPersonnelsStaffRegistrationFormData);
            }
        });
        // 员工类型
        List<FtbPersonnelsStaffRoster> employeeType = ftbPersonnelsStaffRegistrationFormDataMapper.queryStaffRosterList("workerType");
        employeeType.forEach(a -> {
            if (StrUtil.isNotBlank(a.getWorkerType())) {
                FtbPersonnelsStaffRegistrationFormData ftbPersonnelsStaffRegistrationFormData = new FtbPersonnelsStaffRegistrationFormData();
                ftbPersonnelsStaffRegistrationFormData.setRosterId(a.getId());
                ftbPersonnelsStaffRegistrationFormData.setPhone(a.getPhone());
                ftbPersonnelsStaffRegistrationFormData.setValue(a.getWorkerType());
                ftbPersonnelsStaffRegistrationFormData.setFormFieldId("workerType");
                ftbPersonnelsStaffRegistrationFormData.setFormTypeId("3");
                result.add(ftbPersonnelsStaffRegistrationFormData);
            }
        });
        // 实际入职日期
        List<FtbPersonnelsStaffRoster> actualStartDate = ftbPersonnelsStaffRegistrationFormDataMapper.queryStaffRosterList("actualStartDate");
        actualStartDate.forEach(a -> {
            if (Objects.nonNull(a.getActualStartDate())) {
                FtbPersonnelsStaffRegistrationFormData ftbPersonnelsStaffRegistrationFormData = new FtbPersonnelsStaffRegistrationFormData();
                ftbPersonnelsStaffRegistrationFormData.setRosterId(a.getId());
                ftbPersonnelsStaffRegistrationFormData.setPhone(a.getPhone());
                ftbPersonnelsStaffRegistrationFormData.setValue(DateUtil.format(a.getActualStartDate(), "yyyy-MM-dd"));
                ftbPersonnelsStaffRegistrationFormData.setFormFieldId("actualStartDate");
                ftbPersonnelsStaffRegistrationFormData.setFormTypeId("3");
                result.add(ftbPersonnelsStaffRegistrationFormData);
            }
        });
        // 姓名
        List<FtbPersonnelsStaffRoster> name = ftbPersonnelsStaffRegistrationFormDataMapper.queryStaffRosterList("workerName");
        name.forEach(a -> {
            if (StrUtil.isNotBlank(a.getName())) {
                FtbPersonnelsStaffRegistrationFormData ftbPersonnelsStaffRegistrationFormData = new FtbPersonnelsStaffRegistrationFormData();
                ftbPersonnelsStaffRegistrationFormData.setRosterId(a.getId());
                ftbPersonnelsStaffRegistrationFormData.setPhone(a.getPhone());
                ftbPersonnelsStaffRegistrationFormData.setValue(a.getName());
                ftbPersonnelsStaffRegistrationFormData.setFormFieldId("workerName");
                ftbPersonnelsStaffRegistrationFormData.setFormTypeId("1");
                result.add(ftbPersonnelsStaffRegistrationFormData);
            }
        });
        // 手机号
        List<FtbPersonnelsStaffRoster> phone = ftbPersonnelsStaffRegistrationFormDataMapper.queryStaffRosterList("phone");
        phone.forEach(a -> {
            if (StrUtil.isNotBlank(a.getPhone())) {
                FtbPersonnelsStaffRegistrationFormData ftbPersonnelsStaffRegistrationFormData = new FtbPersonnelsStaffRegistrationFormData();
                ftbPersonnelsStaffRegistrationFormData.setRosterId(a.getId());
                ftbPersonnelsStaffRegistrationFormData.setPhone(a.getPhone());
                ftbPersonnelsStaffRegistrationFormData.setValue(a.getPhone());
                ftbPersonnelsStaffRegistrationFormData.setFormFieldId("phone");
                ftbPersonnelsStaffRegistrationFormData.setFormTypeId("1");
                result.add(ftbPersonnelsStaffRegistrationFormData);
            }
        });
        Db.saveBatch(result);
    }

}
