package com.zhongshu.card.server.core.service.paySetting;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.github.microservice.types.payment.PaymentChannelType;
import com.github.microservice.types.payment.PaymentType;
import com.zhongshu.card.client.model.projectAbout.*;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.dao.projectAbout.PayChannelConfigDao;
import com.zhongshu.card.server.core.domain.paySetting.PayChannelConfig;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目的 支付配置
 *
 * @author TRX
 * @date 2024/9/24
 */
@Slf4j
@Service
public class ProjectChannelConfigService extends SuperService {

    @Autowired
    private PayChannelConfigDao payChannelConfigDao;

    @Autowired
    private ProjectPaySettingServiceImpl projectPaySettingService;

    /**
     * 保存信息
     *
     * @param param
     * @return
     */
    public ResultContent savePayChannelConfig(PayChannelConfigParam param) {
        String oid = param.getOid();
        if (StringUtils.isEmpty(oid)) {
            oid = getCurrentOid();
        }
        PayChannelConfig entity = null;
        PayChannelConfig nameTemp = payChannelConfigDao.findTopByPaymentType(param.getPaymentType());

        if (ObjectUtils.isNotEmpty(param.getId())) {
            entity = payChannelConfigDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(entity)) {
                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
            }
            if (ObjectUtils.isNotEmpty(nameTemp) && !nameTemp.getId().equals(entity.getId())) {
                return ResultContent.buildFail(String.format("类型已存在：%s", param.getPaymentChannelType().getRemark()));
            }
            initUpdateEntity(entity);
        } else {
            if (ObjectUtils.isNotEmpty(nameTemp)) {
                return ResultContent.buildFail(String.format("类型已存在：%s", param.getPaymentChannelType().getRemark()));
            }
            param.setId(null);
            entity = new PayChannelConfig();
            entity.setIsDelete(Boolean.FALSE);
            initEntityNoOid(entity);
        }
        BeanUtils.copyPropertiesWithoutNull(param, entity);
        entity.setOid(oid);
        entity.setAboutOid(oid);
        payChannelConfigDao.save(entity);
        return ResultContent.buildSuccess();
    }

    public ResultContent updatePayChannelConfig(PayChannelConfigParam param) {
        if (StringUtils.isEmpty(param.getId())) {
            return ResultContent.buildFail("id不能为空");
        }
        PayChannelConfig channelConfig = payChannelConfigDao.findTopByPaymentType(param.getPaymentType());
        if (ObjectUtils.isEmpty(channelConfig)) {
            return ResultContent.buildFail("数据不存在");
        }
        BeanUtils.copyProperties(param, channelConfig, "paymentChannelType", "paymentType");
        initUpdateEntity(channelConfig);
        payChannelConfigDao.save(channelConfig);
        return ResultContent.buildSuccess();
    }

    public ResultContent initAllChannel() {
        log.info("-------------初始平台支付产品---------------");
        // 免密支付 拉卡拉
        PayChannelConfigParam param = new PayChannelConfigParam();
        param.setName("拉卡拉免密支付");
        param.setPaymentChannelType(PaymentChannelType.SecretFreePayment);
        param.setPaymentType(PaymentType.LakalaFrictionlessPay);
        savePayChannelConfig(param);

        PayChannelConfigParam unionPayParam = new PayChannelConfigParam();
        unionPayParam.setName("云闪付免密支付");
        unionPayParam.setPaymentChannelType(PaymentChannelType.SecretFreePayment);
        unionPayParam.setPaymentType(PaymentType.UnionFrictionlessPay);
        savePayChannelConfig(unionPayParam);

        // 余额支付  微信充值
        PayChannelConfigParam balanceParam = new PayChannelConfigParam();
        balanceParam.setName("余额支付");
        balanceParam.setPaymentChannelType(PaymentChannelType.BalancePayment);
        balanceParam.setSort(2L);
        balanceParam.setPaymentType(PaymentType.WeChat);
        savePayChannelConfig(balanceParam);

        return ResultContent.buildSuccess();
    }

    /**
     * 分页列表
     *
     * @param param
     * @param pageable
     * @return
     */
    public ResultContent<Page<PayChannelConfigAllPayModel>> page(PayChannelConfigSearch param, Pageable pageable) {
        Page<PayChannelConfig> page = payChannelConfigDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toAllModel));
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    public ResultContent deleteData(String id) {
        PayChannelConfig entity = payChannelConfigDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        // 支付产品不可删除
//        payChannelConfigDao.delete(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 数据详情
     *
     * @param id
     * @return
     */
    public ResultContent getDetail(String id) {
        PayChannelConfig entity = payChannelConfigDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        return ResultContent.buildSuccess(toAllModel(entity));
    }

    public PayChannelConfig getProjectPayChannel(PaymentType paymentType) {
        if (paymentType == null) {
            return null;
        }
        PayChannelConfig entity = payChannelConfigDao.findTopByPaymentType(paymentType);
        return entity;
    }

    /**
     * 得到支付渠道对应的 支付产品
     *
     * @param paymentType
     * @return
     */
    public PayChannelConfigModel getProjectPayChannelModel(PaymentType paymentType) {
        return toModel(getProjectPayChannel(paymentType));
    }

    public PayChannelSimpleModel getProjectPayChannelSimpleModel(PaymentType paymentType) {
        return toSimpleModel(getProjectPayChannel(paymentType));
    }

    /**
     * 改变状态
     *
     * @param id
     * @param state
     * @return
     */
    public ResultContent changeState(String id, DataState state) {
        PayChannelConfig entity = payChannelConfigDao.findTopById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        entity.setState(state);
        payChannelConfigDao.save(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 所有可用的支付产品 (可用的)
     *
     * @return
     */
    public ResultContent<List<PayChannelConfigModel>> getPlatFormAllPay() {
        List<PayChannelConfigModel> models = new ArrayList<>();
        // 得到平台可用的支付渠道
        List<PayChannelConfig> list = payChannelConfigDao.findByState(DataState.Enable);
        if (ObjectUtils.isNotEmpty(list)) {
            models = list.stream().map(this::toModel).collect(Collectors.toList());
        }
        return ResultContent.buildSuccess(models);
    }

    /**
     * 转换model
     *
     * @param entity
     * @return
     */
    public PayChannelConfigModel toModel(PayChannelConfig entity) {
        PayChannelConfigModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new PayChannelConfigModel();
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

    public PayChannelSimpleModel toSimpleModel(PayChannelConfig entity) {
        PayChannelSimpleModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new PayChannelSimpleModel();
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

    public PayChannelConfigAllPayModel toAllModel(PayChannelConfig entity) {
        PayChannelConfigAllPayModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new PayChannelConfigAllPayModel();
            BeanUtils.copyProperties(entity, model);

            // 支付渠道
            model.setPaymentChannelTypes(buildTypes());
        }
        return model;
    }

    /**
     * 创建平台支持的渠道
     *
     * @return
     */
    public List<PaymentChannelTypeModel> buildTypes() {
        List<PaymentChannelTypeModel> paymentChannelTypes = new ArrayList<>();
        for (PaymentChannelType channelType : PaymentChannelType.values()) {
            PaymentChannelTypeModel model = new PaymentChannelTypeModel();
            model.setPaymentChannelType(channelType);

            List<PaymentTypeInfoModel> paymentTypes = new ArrayList<PaymentTypeInfoModel>();
            for (PaymentType paymentType : PaymentType.values()) {
                if (paymentType.getChannelType() != null && paymentType.getChannelType() == channelType) {
                    PaymentTypeInfoModel paymentTypeInfoModel = new PaymentTypeInfoModel();
                    paymentTypeInfoModel.setPaymentType(paymentType);
                    paymentTypes.add(paymentTypeInfoModel);
                }
            }
            model.setPaymentTypes(paymentTypes);
            paymentChannelTypes.add(model);
        }
        return paymentChannelTypes;
    }

}
