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.seeyon.cbs8.dao.IAgentPaymentDao;
import com.hzya.frame.seeyon.cbs8.dao.IAgentPaymentDetailDao;
import com.hzya.frame.seeyon.cbs8.entity.AgentPaymentDetailEntity;
import com.hzya.frame.seeyon.cbs8.entity.AgentPaymentEntity;
import com.hzya.frame.seeyon.cbs8.service.IAgentPaymentDetailService;
import com.hzya.frame.seeyon.cbs8.service.IAgentPaymentService;
import com.hzya.frame.web.exception.BaseSystemException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author xiangerlin
 * @Date 2024/6/18 15:06
 **/
@Service(value = "cbs8AgentPaymentServiceImpl")
public class AgentPaymentServiceImpl extends BaseService<AgentPaymentEntity,String> implements IAgentPaymentService {

    private IAgentPaymentDao agentPaymentDao;

    @Autowired
    private IAgentPaymentDetailService agentPaymentDetailService;
    @Autowired
    public void setAgentPaymentDao(IAgentPaymentDao agentPaymentDao) {
        this.agentPaymentDao = agentPaymentDao;
        this.dao = agentPaymentDao;
    }
    /**
     * 查询待支付待代发代扣 主表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @DS("#entity.dataSourceCode")
    @Override
    public List<AgentPaymentEntity> queryUnpaid(AgentPaymentEntity entity) throws Exception {
        List<AgentPaymentEntity> list = agentPaymentDao.queryList(entity, "com.hzya.frame.seeyon.cbs8.dao.impl.AgentPaymentDaoImpl.entity_list_base_unpaid");
        return list;
    }

    /**
     * 根据支付申请单号查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public AgentPaymentEntity queryByApplyCode(AgentPaymentEntity entity) throws Exception {
        if (null != entity && StrUtil.isNotEmpty(entity.getApplyCode())){
            List<AgentPaymentEntity> list = agentPaymentDao.query(entity);
            if (CollectionUtils.isNotEmpty(list)){
                if (list.size() > 1){
                    throw new BaseSystemException("根据"+entity.getApplyCode()+"查询到多条记录");
                }
                return list.get(0);
            }
        }
        return null;
    }

    /**
     * 查询明细表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @DS("#entity.dataSourceCode")
    @Override
    public List<AgentPaymentDetailEntity> queryDetails(AgentPaymentDetailEntity entity) throws Exception {
        List<AgentPaymentDetailEntity> list = agentPaymentDetailService.query(entity);
        return list;
    }

    /**
     * @param entity
     */
    @DS("#entity.dataSourceCode")
    @Override
    public void updateResult(AgentPaymentEntity entity) {
        agentPaymentDao.update("com.hzya.frame.seeyon.cbs8.dao.impl.AgentPaymentDaoImpl.entity_update",entity);
    }
}
