package com.hzya.frame.seeyon.cbs8.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.cbs8.util.PayState;
import com.hzya.frame.seeyon.cbs8.dao.IPaymentDao;
import com.hzya.frame.seeyon.cbs8.entity.PaymentEntity;
import com.hzya.frame.seeyon.cbs8.service.IPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author xiangerlin
 * @Date 2024/6/6 16:34
 **/
@Service("OAPaymentServiceImpl")
public class PaymentServiceImpl extends BaseService<PaymentEntity,String> implements IPaymentService {



    private IPaymentDao paymentDao;

    @Autowired
    public void setPaymentDao(IPaymentDao paymentDao) {
        this.paymentDao = paymentDao;
        this.dao = paymentDao;
    }

    /**
     * 查询列表
     * @param entity
     * @return
     */
    @DS("#entity.dataSourceCode")
    @Override
    public List<PaymentEntity> query(PaymentEntity entity) {
        List<PaymentEntity> list = paymentDao.queryList(entity, "com.hzya.frame.seeyon.cbs8.entity.PaymentEntity.entity_list_base");
        return list;
    }

    /**
     * 查询待支付的数据 需要推送到CBS的
     * 如果需要查询流程状态已结束的 需要调用方设置finishedflag=1
     *
     * @param entity
     * @return
     * @throws Exception
     */


    @DS("#entity.dataSourceCode")
    @Override
    public List<PaymentEntity> queryUnpaid(PaymentEntity entity) throws Exception {
        List<PaymentEntity> list = paymentDao.queryList(entity, "com.hzya.frame.seeyon.cbs8.entity.PaymentEntity.PaymentEntity_list_base_unpaid");
        return list;
    }

    /**
     * 查询交易成功的数据
     * 内置了查询条件payResult = PayState.payStateGetValue("g"); 支付成功
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @DS("#entity.dataSourceCode")
    @Override
    public List<PaymentEntity> querySuccess(PaymentEntity entity) throws Exception {
        if (null == entity){
            entity = new PaymentEntity();
        }
        entity.setPayResult(PayState.payStateGetValue("g"));
        List<PaymentEntity> list = paymentDao.queryList(entity, "com.hzya.frame.seeyon.cbs8.entity.PaymentEntity.PaymentEntity_list_base");
        return list;
    }

    /**
     * 查询交易成功，且电子回单为空的
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @DS("#entity.dataSourceCode")
    @Override
    public List<PaymentEntity> queryElecIsNull(PaymentEntity entity) throws Exception {
        List<PaymentEntity> list = paymentDao.queryList(entity, "com.hzya.frame.seeyon.cbs8.entity.PaymentEntity.PaymentEntity_list_base_elec_isnull");
        return list;
    }

    /**
     * 查询支付中的数据
     * 内置了查询条件 payResult = '支付中' or payResult not in ('审批撤销','审批拒绝','处理失败','退票','支付成功','取消支付','修改支付','支付失败')
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @DS("#entity.dataSourceCode")
    @Override
    public List<PaymentEntity> queryInPayment(PaymentEntity entity) throws Exception {
        List<PaymentEntity> list = paymentDao.queryList(entity, "com.hzya.frame.seeyon.cbs8.entity.PaymentEntity.PaymentEntity_list_base_in_payment");
        return list;
    }

    /**
     * 更新支付状态
     *
     * @param entity
     * @throws Exception
     */
    @DS("#entity.dataSourceCode")
    @Override
    public void updatePayState(PaymentEntity entity) throws Exception {
        if (null != entity
                && StrUtil.isNotEmpty(entity.getTableName())
                && StrUtil.isNotEmpty(entity.getOaId())
                && StrUtil.isNotEmpty(entity.getFormsonId())
                && StrUtil.isNotEmpty(entity.getPayDateField())
                && StrUtil.isNotEmpty(entity.getPayResultField())){
            String formsonId = entity.getFormsonId();
            String[] formsonIdArray =  formsonId.split(",");
            for (String s : formsonIdArray) {
                entity.setFormsonId(s);
                paymentDao.update("com.hzya.frame.seeyon.cbs8.entity.PaymentEntity.PaymentEntity_update_payState",entity);
            }
        }
    }

    /**
     * 更新电子回单字段
     *
     * @param entity
     * @throws Exception
     */
    @DS("#entity.dataSourceCode")
    @Override
    public void updateElec(PaymentEntity entity) throws Exception {
        if (null != entity
                && StrUtil.isNotEmpty(entity.getTableName())
                && StrUtil.isNotEmpty(entity.getOaId())
                && StrUtil.isNotEmpty(entity.getReceiptFiled())){
            paymentDao.update("com.hzya.frame.seeyon.cbs8.entity.PaymentEntity.PaymentEntity_update_electronic",entity);
        }
    }
}
