package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dao.BankDAO;
import com.cool.store.dao.LineAuditInfoDAO;
import com.cool.store.dao.LineInfoDAO;
import com.cool.store.entity.BankdocDO;
import com.cool.store.entity.BanktypeDO;
import com.cool.store.entity.LineAuditInfoDO;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.enums.*;
import com.cool.store.mapper.LinePayMapper;
import com.cool.store.request.AuditRejectRequest;
import com.cool.store.request.BranchBankPageRequest;
import com.cool.store.service.BankService;
import com.cool.store.utils.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
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 javax.annotation.Resource;
import java.util.List;

/**
 * @author wxp
 * @version 1.0
 */
@Service
@Slf4j
public class BankServiceImpl extends LineFlowService implements BankService {

    @Autowired
    private BankDAO bankDAO;
    @Resource
    LineInfoDAO lineInfoDAO;

    @Resource
    LinePayMapper linePayMapper;

    @Resource
    private LineAuditInfoDAO lineAuditInfoDAO;

    @Resource
    CommonService commonService;

    @Override
    public List<BanktypeDO> listBank() {
        List<BanktypeDO> bankList = bankDAO.listBank();
        if (CollectionUtils.isEmpty(bankList)) {
            log.info("银行类型为空");
            return Lists.newArrayList();
        }
        return bankList;
    }

    @Override
    public PageInfo<BankdocDO> listBranchBank(BranchBankPageRequest request) {
        Page<BankdocDO> pageInfo = bankDAO.listBranchBank(request);
        PageInfo resultPage = new PageInfo(pageInfo);
        return resultPage;
    }

    @Override
    protected Boolean auditPass(Long auditId, LineInfoDO lineInfo,String userId) {
        WorkflowSubStageEnum workflowSubStageEnum = WorkflowSubStageEnum.getWorkflowSubStageEnum(lineInfo.getWorkflowSubStage());
        WorkflowSubStageEnum nextStage = workflowSubStageEnum.getNextStage();
        //更新线索阶段
        lineInfoDAO.updateWorkflowStage(lineInfo.getId(), nextStage, nextStage.getInitStatus(), userId);
        return Boolean.TRUE;
    }

    @Override
    protected Boolean auditReject(Long auditId, LineInfoDO lineInfo,String userId) {
        lineInfo.setWorkflowSubStageStatus(WorkflowSubStageStatusEnum.PAY_FAIL_55.getCode());
        if(StringUtils.isNotEmpty(userId)){
            lineInfo.setUpdateUserId(userId);
        }
        lineInfoDAO.updateLineInfo(lineInfo);
        return Boolean.TRUE;
    }

    @Override
    public Boolean repayment(AuditRejectRequest request) {
        //更新线索
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(request.getLineId());
        lineInfo.setWorkflowSubStageStatus(WorkflowSubStageStatusEnum.PAY_FAIL_55.getCode());
        lineInfo.setWorkflowSubStage(WorkflowSubStageEnum.PAY_DEPOSIT.getCode());
        lineInfoDAO.updateLineInfo(lineInfo);
        //插入线索审核信息
        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);
        linePayMapper.updateByPidAndLid(request.getLineId(),lineInfo.getPartnerId(),auditId);

        return Boolean.TRUE;
    }

}
