package com.cool.store.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.dto.log.LogFieldDTO;
import com.cool.store.entity.HyPartnerCertificationInfoDO;
import com.cool.store.entity.HyPartnerLineInfoDO;
import com.cool.store.entity.HyPartnerTaskInfoLogDO;
import com.cool.store.enums.OperateLogFieldValueEnum;
import com.cool.store.enums.OperateTypeEnum;
import com.cool.store.enums.WorkflowStageEnum;
import com.cool.store.enums.WorkflowStatusEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.mapper.HyPartnerCertificationInfoMapper;
import com.cool.store.mapper.HyPartnerLineInfoMapper;
import com.cool.store.mapper.HyPartnerTaskInfoLogMapper;
import com.cool.store.request.GetTipsInfoReq;
import com.cool.store.service.CommonService;
import com.cool.store.utils.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @Author: young.yu
 * @Date: 2023-06-28  21:35
 * @Description:
 */
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private HyPartnerTaskInfoLogMapper hyPartnerTaskInfoLogMapper;

    @Autowired
    private HyPartnerLineInfoMapper hyPartnerLineInfoMapper;

    @Autowired
    private HyPartnerCertificationInfoMapper hyPartnerCertificationInfoMapper;

    @Override
    public String getTipsInfo(GetTipsInfoReq request) throws ApiException {
        List<HyPartnerLineInfoDO> lineInfoList = hyPartnerLineInfoMapper.getHyPartnerLineInfoListByIds(Arrays.asList(Long.parseLong(request.getPartnerLineId())));
        if (CollectionUtils.isEmpty(lineInfoList)) {
            return null;
        }
        HyPartnerLineInfoDO hyPartnerLineInfoDO = lineInfoList.get(0);
        String workflowStage = hyPartnerLineInfoDO.getWorkflowStage();
        String workflowStatus = hyPartnerLineInfoDO.getWorkflowStatus();
        Long partnerLineId = hyPartnerLineInfoDO.getId();
        if ((workflowStage.equals(WorkflowStageEnum.RESERVATION.getCode()) && workflowStatus.equals(WorkflowStatusEnum.RESERVATION_1.getCode()))||
                (workflowStage.equals(WorkflowStageEnum.INTERVIEW.getCode()) && workflowStatus.equals(WorkflowStatusEnum.INTERVIEW_2.getCode()))) {
            return getInterviewTips(partnerLineId, OperateTypeEnum.INTERVIEW_APPOINTMENT, "提交面试预约时间");
        } else if (workflowStage.equals(WorkflowStageEnum.INTERVIEW.getCode()) && workflowStatus.equals(WorkflowStatusEnum.INTERVIEW_3.getCode())) {
            String tips = getInterviewTips(partnerLineId, OperateTypeEnum.MODIFY_INTERVIEW_TIME, "");
            if (StringUtils.isNotEmpty(tips)) {
                return tips + ("修改面试预约时间");
            } else {
                return getInterviewTips(partnerLineId, OperateTypeEnum.INTERVIEW_APPOINTMENT, "提交面试预约时间");
            }
        } else if (workflowStage.equals(WorkflowStageEnum.INTERVIEW.getCode()) && workflowStatus.equals(WorkflowStatusEnum.INTERVIEW_4.getCode())) {
            return getInterviewTips(partnerLineId, OperateTypeEnum.FINISH_INTERVIEW, "结束面试");
        }else if (workflowStage.equals(WorkflowStageEnum.INTERVIEW.getCode()) && workflowStatus.equals(WorkflowStatusEnum.INTERVIEW_5.getCode())) {
            return getInterviewTips(partnerLineId, OperateTypeEnum.CREATE_QUALIFYVERIFY, "发起加盟商资质审核");
        }else if (workflowStage.equals(WorkflowStageEnum.INTERVIEW.getCode()) && workflowStatus.equals(WorkflowStatusEnum.INTERVIEW_6.getCode())) {
            HyPartnerCertificationInfoDO partnerCertificationInfoDO = hyPartnerCertificationInfoMapper.selectByPartnerLineId(partnerLineId);
            String intentionContractNo = null;
            if(partnerCertificationInfoDO != null){
                intentionContractNo = partnerCertificationInfoDO.getIntentionContractNo();
            }
            return getVerifyResultTips(partnerLineId, OperateTypeEnum.QUALIFYVERIFY_PASS, "审核通过 | "+intentionContractNo);
        }

        return null;
    }

    public String getInterviewTips(Long partnerLineId, OperateTypeEnum operateTypeEnum, String action) {
        StringBuffer sb = new StringBuffer();
        sb.append(StringUtil.REPLACE_0)
                .append(" ")
                .append(StringUtil.REPLACE_1)
                .append(" 于 ").append(StringUtil.REPLACE_2).append(" ").append(action);
        String content = sb.toString();
        return getSuitableTipsInfo(content, partnerLineId,
                operateTypeEnum,
                OperateLogFieldValueEnum.OPERATE_USER_NAME.getCode(),
                OperateLogFieldValueEnum.MOBILE.getCode(),
                OperateLogFieldValueEnum.OPERATE_TIME.getCode());

    }

    public String getVerifyResultTips(Long partnerLineId, OperateTypeEnum operateTypeEnum, String action) {
        StringBuffer sb = new StringBuffer();
        sb.append("于 ").append(StringUtil.REPLACE_0).append(" ").append(action);
        String content = sb.toString();
        return getSuitableTipsInfo(content, partnerLineId,
                operateTypeEnum,
                OperateLogFieldValueEnum.OPERATE_TIME.getCode());
    }

    public String getSuitableTipsInfo(String content, Long partnerLineId, OperateTypeEnum operateTypeEnum, String... replaceKeys) {
        List<HyPartnerTaskInfoLogDO> hyPartnerTaskInfoLogDOS = hyPartnerTaskInfoLogMapper.selectByPartnerLineIdAndOperateType(partnerLineId, operateTypeEnum.getCode());
        if (CollectionUtils.isEmpty(hyPartnerTaskInfoLogDOS)) {
            return null;
        }
        HyPartnerTaskInfoLogDO hyPartnerTaskInfoLogDO = hyPartnerTaskInfoLogDOS.get(0);
        String fieldCopy = hyPartnerTaskInfoLogDO.getFieldCopy();
        List<LogFieldDTO> logFieldDTOS = JSONObject.parseArray(fieldCopy, LogFieldDTO.class);
        List<String> params = new ArrayList<>();
        for (String replaceKey : replaceKeys) {
            String value = "";
            Optional<LogFieldDTO> logFieldDTOOptional = logFieldDTOS.stream().filter(l -> l.getCode().equals(replaceKey)).findFirst();
            if (logFieldDTOOptional.isPresent()) {
                value = logFieldDTOOptional.get().getValue();
                try {
                    value = DateUtil.format(DateUtil.parseDateTime(value), "yyyy/MM/dd HH:mm:ss");
                } catch (Exception e) {
                    //异常代表不是时间格式，不做处理
                }

            }
            params.add(value);
        }

        return MessageFormat.format(content, params.toArray());
    }
}
