package org.qinan.safetyeval.app.support;

import org.qinan.safetyeval.domain.entity.OrgPersonnelChangeEntity;
import org.qinan.safetyeval.domain.entity.OrgPersonnelEntity;
import org.qinan.safetyeval.domain.gateway.OrgPersonnelChangeGateway;
import org.qinan.safetyeval.infrastructure.adapter.auth.AuthUserContextAdapter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

/**
 * 人员信息修改时自动写入变更记录。
 */
@Component
public class OrgPersonnelChangeRecorder {

    @Resource
    private OrgPersonnelChangeGateway orgPersonnelChangeGateway;

    public void recordChanges(OrgPersonnelEntity before, OrgPersonnelEntity after) {
        if (before == null || after == null || after.getId() == null) {
            return;
        }
        List<String> items = new ArrayList<>();
        if (!Objects.equals(before.getUserName(), after.getUserName())) {
            items.add("姓名");
        }
        if (!Objects.equals(before.getAccount(), after.getAccount())) {
            items.add("账号");
        }
        if (!Objects.equals(before.getDeptId(), after.getDeptId())) {
            items.add("部门");
        }
        if (!Objects.equals(before.getPostId(), after.getPostId())) {
            items.add("岗位");
        }
        if (!Objects.equals(before.getGenderCode(), after.getGenderCode())) {
            items.add("性别");
        }
        if (!Objects.equals(before.getBirthDate(), after.getBirthDate())) {
            items.add("出生日期");
        }
        if (!Objects.equals(before.getIdCardNo(), after.getIdCardNo())) {
            items.add("身份证号");
        }
        if (!Objects.equals(before.getCurrentAddress(), after.getCurrentAddress())) {
            items.add("现住地址");
        }
        if (!Objects.equals(before.getOfficeAddress(), after.getOfficeAddress())) {
            items.add("办公地址");
        }
        if (!Objects.equals(before.getEducationName(), after.getEducationName())) {
            items.add("学历");
        }
        if (!Objects.equals(before.getGraduateSchool(), after.getGraduateSchool())) {
            items.add("毕业院校");
        }
        if (!Objects.equals(before.getMajor(), after.getMajor())) {
            items.add("专业");
        }
        if (!Objects.equals(before.getEmploymentStatusCode(), after.getEmploymentStatusCode())) {
            items.add("就职状态");
        }
        if (items.isEmpty()) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        String operator = StringUtils.hasText(AuthUserContextAdapter.getCurrentUserName()) ? AuthUserContextAdapter.getCurrentUserName() : "系统";
        for (String item : items) {
            OrgPersonnelChangeEntity change = new OrgPersonnelChangeEntity();
            change.setPersonnelId(after.getId());
            change.setChangeItem(item);
            change.setChangeTime(now);
            change.setOperatorName(operator);
            orgPersonnelChangeGateway.save(change);
        }
    }
}
