package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.LineAuditInfoDAO;
import com.cool.store.dao.LineInfoDAO;
import com.cool.store.entity.LineAuditInfoDO;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.enums.AuditResultTypeEnum;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.LineStatusEnum;
import com.cool.store.enums.WorkflowSubStageEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.AuditCloseRequest;
import com.cool.store.request.AuditPassRequest;
import com.cool.store.request.AuditRejectRequest;
import com.cool.store.request.LineFollowLogRequest;
import com.cool.store.service.LineFollowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zhangchenbiao
 * @FileName: LineFlowService
 * @Description:
 * @date 2024-03-20 14:32
 */
@Service("lineFlowService")
public abstract class LineFlowService {

    @Resource
    private LineAuditInfoDAO lineAuditInfoDAO;
    @Resource
    private LineInfoDAO lineInfoDAO;
    @Resource
    private LineFollowService lineFollowService;

    /**
     * 审核通过
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean auditPass(AuditPassRequest request,String userId){
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(request.getLineId());
        if(!lineInfo.getWorkflowSubStage().equals(request.getWorkflowSubStage())){
            throw new ServiceException(ErrorCodeEnum.WORK_FLOW_STAGE_PASS_ERROR);
        }
        String partnerId = lineInfo.getPartnerId();
        LineAuditInfoDO auditInfo = new LineAuditInfoDO();
        auditInfo.setLineId(request.getLineId());
        auditInfo.setPartnerId(partnerId);
        auditInfo.setResultType(AuditResultTypeEnum.PASS.getCode());
        auditInfo.setPassReason(request.getPassReason());
        auditInfo.setCertifyFile(JSONObject.toJSONString(request.getCertifyFile()));
        Long auditId = lineAuditInfoDAO.addAuditInfo(auditInfo);
        return auditPass(auditId, lineInfo,userId);
    }

    /**
     * 审核拒绝
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean auditReject(AuditRejectRequest request,String userId){
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(request.getLineId());
        if(!lineInfo.getWorkflowSubStage().equals(request.getWorkflowSubStage())){
            throw new ServiceException(ErrorCodeEnum.WORK_FLOW_STAGE_PASS_ERROR);
        }
        String partnerId = lineInfo.getPartnerId();
        LineAuditInfoDO auditInfo = new LineAuditInfoDO();
        auditInfo.setLineId(request.getLineId());
        auditInfo.setPartnerId(partnerId);
        auditInfo.setResultType(AuditResultTypeEnum.REJECT.getCode());
        auditInfo.setRejectPublicReason(request.getRejectPublicReason());
        auditInfo.setRejectRealReason(request.getRejectRealReason());
        auditInfo.setCertifyFile(JSONObject.toJSONString(request.getCertifyFile()));
        Long auditId = lineAuditInfoDAO.addAuditInfo(auditInfo);
        return auditReject(auditId, lineInfo,userId);
    }

    /**
     * 结束跟进
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean auditClose(AuditCloseRequest request, LoginUserInfo user){
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(request.getLineId());
        String partnerId = lineInfo.getPartnerId();
        LineAuditInfoDO auditInfo = new LineAuditInfoDO();
        auditInfo.setLineId(request.getLineId());
        auditInfo.setPartnerId(partnerId);
        auditInfo.setResultType(AuditResultTypeEnum.CLOSE.getCode());
        auditInfo.setRejectPublicReason(request.getRejectPublicReason());
        auditInfo.setRejectRealReason(request.getRejectRealReason());
        auditInfo.setCertifyFile(JSONObject.toJSONString(request.getCertifyFile()));
        lineAuditInfoDAO.addAuditInfo(auditInfo);
        LineInfoDO updateLineInfo  = new LineInfoDO();
        updateLineInfo.setId(request.getLineId());
        updateLineInfo.setUpdateUserId(user.getUserId());
        updateLineInfo.setLineStatus(LineStatusEnum.PUBLIC_SEAS.getCode());
        lineInfoDAO.updateLineInfo(updateLineInfo);
        LineFollowLogRequest lineFollowLogRequest = new LineFollowLogRequest();
        lineFollowLogRequest.setLineId(request.getLineId());
        lineFollowLogRequest.setMessage(String.format("结束跟进原因：%s", request.getRejectRealReason()));
        lineFollowService.addFollowLog(lineFollowLogRequest,user.getUserId(),user.getName());
        return Boolean.TRUE;
    }

    /**
     * 审核通过 流转到下一阶段
     * @param auditId
     * @param lineInfo
     * @return
     */
    protected abstract Boolean auditPass(Long auditId, LineInfoDO lineInfo,String userId);

    /**
     * 审核拒绝
     * @param auditId
     * @param lineInfo
     * @return
     */
    protected abstract Boolean auditReject(Long auditId, LineInfoDO lineInfo,String userId);



}
