package com.yd.dal.service.agms.impl;

import com.yd.api.agms.vo.fortune.CommissionPayoutAddRemarkRequestVO;
import com.yd.dal.entity.agms.fortune.*;
import com.yd.dal.entity.customer.AclCustomerFortune;
import com.yd.dal.mapper.agms.AgmsFortuneMapper;
import com.yd.dal.mapper.customer.AclCustomerFortuneMapper;
import com.yd.dal.service.agms.AgmsFortuneDALService;
import com.yd.util.deshandler.DESTypeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author xxy
 */
@Service("agmsFortuneDALService")
public class AgmsFortuneDALServiceImpl implements AgmsFortuneDALService {

    @Autowired
    private AgmsFortuneMapper agmsFortuneMapper;
    @Autowired
    private AclCustomerFortuneMapper aclCustomerFortuneMapper;


    @Override
    public Map<Long,CustomerFortuneStatisticalInfo> findFortuneStatisticalByCustomers(Set<Long> customerIds) {
        List<Long> customerIdList = new ArrayList<>(customerIds);
        List<CustomerFortuneStatisticalInfo> statisticalInfoList = agmsFortuneMapper.findFortuneStatisticalByCustomers(customerIdList);
        Map<Long,CustomerFortuneStatisticalInfo> customerFortuneStatisticalMap = new HashMap<>(statisticalInfoList.size());
        for (CustomerFortuneStatisticalInfo statisticalInfo: statisticalInfoList){
            //可提现金额 历史累积财富-已提现财富-已退保财富-暂不可提现财富-申请中
            BigDecimal cashableFortune = statisticalInfo.getAccumulatedFortune()
                    .subtract(statisticalInfo.getUncashableFortune())
                    .subtract(statisticalInfo.getDrawnFortune())
                    .subtract(statisticalInfo.getCancelledFortune())
                    .subtract(statisticalInfo.getProcessing());
            statisticalInfo.setCashableFortune(cashableFortune);
            //计算剩余总财富 历史累积财富-已提现财富-已退保财富
            BigDecimal totalFortune = statisticalInfo.getAccumulatedFortune()
                    .subtract(statisticalInfo.getDrawnFortune())
                    .subtract(statisticalInfo.getCancelledFortune());
            statisticalInfo.setTotalFortune(totalFortune);
            //净资产  历史累积财富-已提现财富
            BigDecimal netAssets = statisticalInfo.getAccumulatedFortune()
                    .subtract(statisticalInfo.getCancelledFortune());
            statisticalInfo.setNetAssets(netAssets);
            customerFortuneStatisticalMap.put(statisticalInfo.getCustomerId(),statisticalInfo);
        }
        return customerFortuneStatisticalMap;
    }

    @Override
    public List<CommissionPayoutStatus> commissionPayoutStatusQuery(CommissionPayoutStatusQueryInfo requestVO) {
        return agmsFortuneMapper.commissionPayoutStatusQuery(requestVO);
    }

    @Override
    public List<FortunePayToOrderInfo> fortunePayToOrder( Long[] payIds) {
        DESTypeHandler desTypeHandler = new DESTypeHandler();
        List<FortunePayToOrderInfo> fortunePayToOrderInfos = agmsFortuneMapper.fortunePayToOrder(payIds);
        for (FortunePayToOrderInfo fortunePayToOrderInfo: fortunePayToOrderInfos){
            String mobileNo = fortunePayToOrderInfo.getMobileNo();
            mobileNo = desTypeHandler.decode(mobileNo);
            fortunePayToOrderInfo.setMobileNo(mobileNo);
        }
        return fortunePayToOrderInfos;
    }

    @Override
    public List<WithdrawLabelInfo> transformForWithdrawLabel(WithdrawQueryInfo info) {
        return agmsFortuneMapper.transformForWithdrawLabel(info);
    }

    @Override
    public List<WithdrawLabelInfo> salaryPolicyNoDetails(WithdrawQueryInfo info) {
        return agmsFortuneMapper.salaryPolicyNoDetails(info);
    }

    @Override
    public List<Map<String, Object>> querySheet2Data(List<Long> fortuneIdList, List<Long> fortuneIdList2) {
        return agmsFortuneMapper.querySheet2Data(fortuneIdList, fortuneIdList2);
    }

    @Override
    public List<Long> querySalePractitionerFortune(List<Long> fortuneIdList) {
        return agmsFortuneMapper.querySalePractitionerFortune(fortuneIdList);
    }

    @Override
    public List<Long> querySalePractitionerFortune1(List<Long> fortuneIdList) {
        return agmsFortuneMapper.querySalePractitionerFortune1(fortuneIdList);
    }

    @Override
    public void commissionPayoutAddRemark(CommissionPayoutAddRemarkRequestVO requestVO) {
        AclCustomerFortune fortune = new AclCustomerFortune();
        fortune.setId(requestVO.getFortuneId());
        fortune.setRemark(requestVO.getRemark());
        fortune.setUpdatedBy(requestVO.getLoginId());
        fortune.setUpdatedAt(new Date());
        aclCustomerFortuneMapper.updateByPrimaryKeySelective(fortune);
    }

    @Override
    public List<Map<String, Object>> querySheet3Data(List<Long> fortuneIdList) {
        return agmsFortuneMapper.querySheet3Data(fortuneIdList);
    }
}
