package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.HfMemberDao;
import com.ym.mec.biz.dal.entity.HfMember;
import com.ym.mec.biz.service.HfMemberService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.adapay.CorpMember;
import com.ym.mec.thirdparty.adapay.SettleAccount;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class HfMemberSserviceImpl extends BaseServiceImpl<Integer, HfMember> implements HfMemberService {
    @Autowired
    private HfMemberDao hfMemberDao;

    @Override
    public BaseDAO<Integer, HfMember> getDAO() {
        return hfMemberDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> createMember(HfMember member, File file) throws Exception {
        HfMember hasMember = hfMemberDao.getByMemberId(member.getMemberId());
        if (hasMember != null) {
            throw new BizException("商户号不能与已有商户相同，请核查");
        }
        hasMember = hfMemberDao.getByName(member.getName());
        if (hasMember != null) {
            throw new BizException("公司名与已有公司名相同，请核查");
        }

        Date nowDate = new Date();
        member.setCreateTime(nowDate);
        member.setUpdateTime(nowDate);
        member.setStatus("pending");
        insert(member);

        HashMap<String, Object> memberParams = new HashMap<>();
        memberParams.put("order_no", "corp_mem_" + System.currentTimeMillis());
        memberParams.put("member_id", member.getMemberId());
        memberParams.put("name", member.getName());
        memberParams.put("prov_code", member.getProvCode());
        memberParams.put("area_code", member.getAreaCode());
        memberParams.put("social_credit_code", member.getSocialCreditCode());
        memberParams.put("social_credit_code_expires", member.getSocialCreditCodeExpires());
        memberParams.put("business_scope", member.getBusinessScope());
        memberParams.put("legal_person", member.getLegalPerson());
        memberParams.put("legal_cert_id", member.getLegalCertId());
        memberParams.put("legal_cert_id_expires", member.getLegalCertIdExpires());
        memberParams.put("legal_mp", member.getLegalMp());
        memberParams.put("address", member.getAddress());
        memberParams.put("zip_code", member.getZipCode());
        memberParams.put("bank_code", member.getBankCode());
        memberParams.put("bank_acct_type", "1");
        memberParams.put("card_no", member.getCardNo());
        memberParams.put("card_name", member.getName());
        return CorpMember.executeCreateMember(memberParams, file);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateMember(HfMember member, File file) throws Exception {
        HfMember hfMember = hfMemberDao.get(member.getId());
        if (hfMember == null) {
            throw new BizException("商户不存在请核对");
        }

        if (!hfMember.getStatus().equals("failed")) {
            throw new BizException("商户不是审核失败状态不能修改");
        }

        Date nowDate = new Date();
        member.setUpdateTime(nowDate);
        member.setStatus("pending");
        if (update(member) <= 0) {
            throw new BizException("更新商户信息失败,请重试");
        }
        HashMap<String, Object> memberParams = new HashMap<>();
        memberParams.put("order_no", "corp_mem_" + System.currentTimeMillis());
        memberParams.put("member_id", member.getMemberId());
        memberParams.put("name", member.getName());
        memberParams.put("prov_code", member.getProvCode());
        memberParams.put("area_code", member.getAreaCode());
        memberParams.put("social_credit_code", member.getSocialCreditCode());
        memberParams.put("social_credit_code_expires", member.getSocialCreditCodeExpires());
        memberParams.put("business_scope", member.getBusinessScope());
        memberParams.put("legal_person", member.getLegalPerson());
        memberParams.put("legal_cert_id", member.getLegalCertId());
        memberParams.put("legal_cert_id_expires", member.getLegalCertIdExpires());
        memberParams.put("legal_mp", member.getLegalMp());
        memberParams.put("address", member.getAddress());
        memberParams.put("zip_code", member.getZipCode());
        memberParams.put("bank_code", member.getBankCode());
        memberParams.put("bank_acct_type", "1");
        memberParams.put("card_no", member.getCardNo());
        memberParams.put("card_name", member.getName());
        return CorpMember.executeCreateMember(memberParams, file);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> createSettleAccount(String memberId, String cardNo, String bankCode) throws Exception {
        HfMember hfMember = hfMemberDao.getByMemberId(memberId);
        if (!hfMember.getStatus().equals("succeeded")) {
            throw new BizException("商户审核通过，才能绑定结算卡");
        }
        hfMember.setCardNo(cardNo);
        hfMember.setBankCode(bankCode);

        //删除结算账户
        if (StringUtils.isNotBlank(hfMember.getSettleAccountId())) {
            SettleAccount.executeDeleteSettleAccount(hfMember.getSettleAccountId(), memberId);
        }

        Map<String, Object> accountInfo = new HashMap<String, Object>();
        accountInfo.put("card_id", cardNo);
        accountInfo.put("card_name", hfMember.getName());
        accountInfo.put("tel_no", hfMember.getLegalMp());
        accountInfo.put("bank_code", bankCode);
        accountInfo.put("bank_acct_type", "1");
        accountInfo.put("prov_code", hfMember.getProvCode());
        accountInfo.put("area_code", hfMember.getAreaCode());

        //创建结算账户
        Map<String, Object> objectMap = SettleAccount.executeCreateSettleAccount(memberId, accountInfo);

        hfMember.setSettleAccountId(objectMap.get("id").toString());
        if (hfMemberDao.update(hfMember) <= 0) {
            throw new BizException("绑卡失败，请重试");
        }
        return objectMap;
    }
}
