package cn.estsh.i3plus.ext.mes.pcn.pojo.util;

import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @DESCRIPTION:
 * @USER: wangjie
 * @DATE: 2022-11-24 16:04
 */
@Data
public class ConvertBeanExt {

    private String organizeCodeExt;

    private Integer isValidExt;

    private Integer isDeletedExt;

    private String createUserExt;

    private String createDatetimeExt;

    private String modifyUserExt;

    private String modifyDatetimeExt;

    private Integer systemSyncStatusExt;

    public ConvertBeanExt() {}

    public ConvertBeanExt(BaseBean bean) {
        this.organizeCodeExt = bean.getOrganizeCode();
        this.isValidExt = bean.getIsValid();
        this.isDeletedExt = bean.getIsDeleted();
        this.createUserExt = bean.getCreateUser();
        this.createDatetimeExt = bean.getCreateDatetime();
        this.modifyUserExt = bean.getModifyUser();
        this.modifyDatetimeExt = bean.getModifyDatetime();
        this.systemSyncStatusExt = MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue();
    }

    public ConvertBeanExt notSync() {
        this.systemSyncStatusExt = MesPcnExtEnumUtil.IF_SYNC_STATUS.ALREADY_SYNC.getValue();
        return this;
    }

    public void convertBean(Object o) {
        BeanUtils.copyProperties(this, o);
    }

    public BaseBean checkMdtAdd1s(BaseBean bean, String userName) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(MesPcnExtConstWords.DATETIME_FORMAT);
        Date date = new Date();
        String dateTime = simpleDateFormat.format(date);
        if (dateTime.compareTo(bean.getModifyDatetime()) <= 0) dateTime = getModifyDatetimeAdd1s(simpleDateFormat, date);
        bean.setModifyUser(userName);
        bean.setModifyDatetime(dateTime);
        return bean;
    }

    private String getModifyDatetimeAdd1s(SimpleDateFormat simpleDateFormat, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, 1);
        return simpleDateFormat.format(cal.getTime());
    }
}
