package com.yuchen.saas.park.service.service.property.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.dto.property.PropertyOfflineCollectionAccountAddDto;
import com.yuchen.saas.park.api.entity.property.PropertyOfflineCollectionAccount;
import com.yuchen.saas.park.api.vo.property.PropertyOfflineCollectionAccountVo;
import com.yuchen.saas.park.service.mapper.property.PropertyOfflineCollectionAccountMapper;
import com.yuchen.saas.park.service.service.property.IPropertyOfflineCollectionAccountService;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 园区线下收费设置(园区线下收款账户配置) 服务实现类
 * </p>
 *
 * @author cxj
 * @since 2023-12-15
 */
@Service
public class PropertyOfflineCollectionAccountServiceImpl extends BaseServiceImpl<PropertyOfflineCollectionAccountMapper, PropertyOfflineCollectionAccount> implements IPropertyOfflineCollectionAccountService {

    @Override
    public List<PropertyOfflineCollectionAccountVo> listByParkId(Long parkId,Integer collectionAccountType) {
        LambdaQueryWrapper<PropertyOfflineCollectionAccount> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyOfflineCollectionAccount::getParkId,parkId)
                .eq(Func.isNotEmpty(collectionAccountType),PropertyOfflineCollectionAccount::getCollectionAccountType,collectionAccountType)
                .orderByDesc(PropertyOfflineCollectionAccount::getCreateTime);
        List<PropertyOfflineCollectionAccount> list = list(queryWrapper);
        return  Func.copy(list,PropertyOfflineCollectionAccountVo.class);
    }

    @Override
    public Boolean add(PropertyOfflineCollectionAccountAddDto dto) {
        if(Func.isNotEmpty(dto.getCollectionAccountType())&&dto.getCollectionAccountType()==1){
            if(Func.isEmpty(dto.getBankName())||Func.isEmpty(dto.getBankAccount())||
                    Func.isEmpty(dto.getAddress())||Func.isEmpty(dto.getSubjectName())){
                throw new ServiceException("缺少必填字段");
            }
        }
        if(Func.isNotEmpty(dto.getCollectionAccountType())&&dto.getCollectionAccountType()==2){
            if(Func.isEmpty(dto.getTripartiteType())||Func.isEmpty(dto.getOidPartner())||
                    Func.isEmpty(dto.getSubjectName())){
                throw new ServiceException("缺少必填字段");
            }
        }
        return save(Func.copy(dto, PropertyOfflineCollectionAccount.class));
    }

    @Override
    public Boolean del(Long id) {
        return deleteLogic(Arrays.asList(id));
    }
}
