package com.ghy.payment.service.impl;

import com.ghy.common.adapay.model.PaymentDTO;
import com.ghy.common.core.text.Convert;
import com.ghy.common.enums.PayStatus;
import com.ghy.common.enums.PayTypeEnum;
import com.ghy.payment.domain.FinancialDetail;
import com.ghy.payment.domain.FinancialMaster;
import com.ghy.payment.mapper.FinancialMasterMapper;
import com.ghy.payment.mapper.PaymentMapper;
import com.ghy.payment.service.FinancialDetailService;
import com.ghy.payment.service.FinancialMasterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import static java.time.temporal.ChronoField.*;

/**
 * 商品模块实现类
 *
 * @author clunt
 */
@Service
public class FinancialMasterServiceImpl implements FinancialMasterService {

    private static final Logger logger = LoggerFactory.getLogger(FinancialMasterServiceImpl.class);

    private static final AtomicLong INDEX = new AtomicLong(1L);

    public static final DateTimeFormatter MINI_FORMATTER = new DateTimeFormatterBuilder()
            .appendValue(YEAR, 4).appendValue(MONTH_OF_YEAR, 2)
            .appendValue(DAY_OF_MONTH, 2).appendValue(HOUR_OF_DAY, 2)
            .appendValue(MINUTE_OF_HOUR, 2).appendValue(SECOND_OF_MINUTE, 2).toFormatter();

    @Resource
    private PaymentMapper paymentMapper;
    @Resource
    private FinancialMasterMapper financialMasterMapper;
    @Resource
    private FinancialDetailService financialDetailService;

    @Override
    public int insertFinancialMaster(FinancialMaster financialMaster) {
        return financialMasterMapper.insertFinancialMaster(financialMaster);
    }

    @Override
    public int updateFinancialMaster(FinancialMaster financialMaster) {
        return financialMasterMapper.updateFinancialMaster(financialMaster);
    }

    @Override
    public List<FinancialMaster> selectFinancialMasterList(FinancialMaster financialMaster) {
        return financialMasterMapper.selectFinancialMasterList(financialMaster);
    }

    @Override
    public List<FinancialMaster> selectAppList(FinancialMaster request) {
        List<FinancialMaster> masterList =this.selectFinancialMasterList(request);
        masterList.forEach(master->{
            FinancialDetail param = new FinancialDetail();
            param.setFinancialMasterId(master.getId());
            param.setPayeeId(request.getUserId());
            List<FinancialDetail> details = financialDetailService.selectFinancialDetailList(param);
            details.forEach(detail->{

            });
        });
        return null;
    }

    @Override
    public FinancialMaster selectById(Long financialMasterId) {
        return financialMasterMapper.selectById(financialMasterId);
    }

    @Override
    public int deleteFinancialMasterByIds(String ids) {
        Long[] financialMasterIds = Convert.toLongArray(ids);
        return financialMasterMapper.deleteFinancialMasterByIds(financialMasterIds);
    }

    @Override
    public void paySucceeded(String paymentId, String payChannel) {
        int payType;
        try {
            payType = PayTypeEnum.valueOf(payChannel.toUpperCase()).getCode();
        } catch (IllegalArgumentException e) {
            payType = -1;
            logger.error("paymentId[{}] Unknown payChannel [{}]!", paymentId, payChannel);
        }
        FinancialMaster financialMaster = financialMasterMapper.selectByPaymentId(paymentId);
        financialMasterMapper.updateOrderStatus(financialMaster.getOrderMasterCode(), PayStatus.PAID.getCode());
        financialMasterMapper.paySucceeded(paymentId, payType);
    }

    @Override
    public String createCode() {
        INDEX.compareAndSet(9999L, 1L);
        LocalDateTime now = LocalDateTime.now();
        return "fm" + now.format(MINI_FORMATTER) + INDEX.getAndIncrement();
    }

    @Override
    public FinancialMaster selectByOrderMasterId(Long orderMasterId) {
        return financialMasterMapper.selectByOrderMasterId(orderMasterId);
    }

    @Override
    public int insertPayment(PaymentDTO payment) {
        return paymentMapper.insertPayment(payment);
    }

    @Override
    public int updatePayment(PaymentDTO payment) {
        return paymentMapper.updatePayment(payment);
    }

    @Override
    public PaymentDTO selectPaymentById(String id) {
        return paymentMapper.selectById(id);
    }

    @Override
    public FinancialMaster selectByPaymentId(String paymentId) {
        return financialMasterMapper.selectByPaymentId(paymentId);
    }

}
