package org.ta.pddserver.serviceutil;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.ta.pdd.entity.ExpMailEntity;
import org.ta.pdd.entity.ExpMailLogsEntity;
import org.ta.pddserver.model.enums.MailNoStatusEnum;
import org.ta.pddserver.service.impl.ExpMailImpl;
import org.ta.pddserver.service.impl.ExpMailLogsImpl;

@Component
public class MailNoStatusOperator {

    @Resource
    ExpMailImpl expMailImpl;
    @Resource
    ExpMailLogsImpl expMailLogsImpl;

    public String checkCanHandle(String mailNo, MailNoStatusEnum aimStatusEnum) {
        ExpMailEntity expMailEntity = this.getMailByMailNo(mailNo);
        MailNoStatusEnum currStatus = MailNoStatusEnum.getByCode(expMailEntity.getStatus());

        switch (aimStatusEnum) {
            case SIGNED, REJECTED:
                return currStatus.getCode() == MailNoStatusEnum.START.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是初始下发状态，不可做此操作";
            case INBOUND:
                return currStatus.getCode() == MailNoStatusEnum.SIGNED.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是已签收状态，不可做此操作";
            case POSITION:
                return currStatus.getCode() == MailNoStatusEnum.INBOUND.getCode() || currStatus.getCode() == MailNoStatusEnum.PICKED.getCode() || currStatus.getCode() == MailNoStatusEnum.PROBLEM.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是已入库/已拣货/问题件状态，不可做此操作";
            case ONBOARD:
                return currStatus.getCode() == MailNoStatusEnum.POSITION.getCode() || currStatus.getCode() == MailNoStatusEnum.ONBOARD.getCode() || currStatus.getCode() == MailNoStatusEnum.PICKING.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是分配库位/已上架/拣货中状态，不可做此操作";
            case PICKING, DESTROYING:
                return currStatus.getCode() == MailNoStatusEnum.ONBOARD.getCode() || currStatus.getCode() == MailNoStatusEnum.PICKING.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是已上架/拣货中状态，不可做此操作";
            case PICKED:
                return currStatus.getCode() == MailNoStatusEnum.PICKING.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是拣货中状态，不可做此操作";
            case PROBLEM, PACKAGING:
                return currStatus.getCode() == MailNoStatusEnum.PICKED.getCode() || currStatus.getCode() == MailNoStatusEnum.PACKAGING.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是已拣货/打包中状态，不可做此操作";
            case PACKAGED:
                return currStatus.getCode() == MailNoStatusEnum.PACKAGING.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是拣货中状态，不可做此操作";
            case OUTING:
                return currStatus.getCode() == MailNoStatusEnum.PACKAGED.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是已拣货状态，不可做此操作";
            case OUTBOUND:
                return currStatus.getCode() == MailNoStatusEnum.OUTING.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是出库中状态，不可做此操作";
            case DESTROYED:
                return currStatus.getCode() == MailNoStatusEnum.DESTROYING.getCode()?
                        null : "当前状态为" + currStatus.getDesc() + "，不是销毁中状态，不可做此操作";

        }
        return null;
    }

    public boolean changeStatus(String mailNo, MailNoStatusEnum aimStatusEnum) {
        ExpMailEntity expMailEntity = expMailImpl.findByMailNo(mailNo);
        if (expMailEntity == null) {
            return false;
        }
        expMailEntity.setStatus(aimStatusEnum.getCode());
        if (expMailImpl.updateById(expMailEntity)) {
            ExpMailLogsEntity logsEntity = new ExpMailLogsEntity();
            logsEntity.setMailNo(mailNo);
            logsEntity.setStatus(aimStatusEnum.getCode());
//            logsEntity.setOperator(SecurityContextHolder.getContext().getAuthentication().getName());
            if (expMailLogsImpl.save(logsEntity)) {
                return true;
            }
        }
        return false;
    }

    public boolean changeStatus(String mailNo, MailNoStatusEnum aimStatusEnum, String place) {
        ExpMailEntity expMailEntity = expMailImpl.findByMailNo(mailNo);
        if (expMailEntity == null) {
            return false;
        }
        expMailEntity.setStatus(aimStatusEnum.getCode());
        if (expMailImpl.updateById(expMailEntity)) {
            ExpMailLogsEntity logsEntity = new ExpMailLogsEntity();
            logsEntity.setMailNo(mailNo);
            logsEntity.setStatus(aimStatusEnum.getCode());
//            logsEntity.setOperator(SecurityContextHolder.getContext().getAuthentication().getName());
            logsEntity.setRemarks(place);
            if (expMailLogsImpl.save(logsEntity)) {
                return true;
            }
        }
        return false;
    }

    public ExpMailEntity getMailByMailNo(String mailNo) {
        ExpMailEntity expMailEntity = expMailImpl.findByMailNo(mailNo);
        if (expMailEntity == null) {
            expMailEntity = new ExpMailEntity();
            expMailEntity.setMailNo(mailNo);
            expMailEntity.setStatus(MailNoStatusEnum.START.getCode());
            expMailImpl.save(expMailEntity);
        }
        return expMailEntity;
    }
}
