package org.dromara.payment.invoice.service.impl;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.payment.sys.domain.SysSyd;
import org.springframework.stereotype.Service;
import org.dromara.payment.invoice.domain.bo.MerInvoiceAddressBo;
import org.dromara.payment.invoice.domain.vo.MerInvoiceAddressVo;
import org.dromara.payment.invoice.domain.MerInvoiceAddress;
import org.dromara.payment.invoice.mapper.MerInvoiceAddressMapper;
import org.dromara.payment.invoice.service.IMerInvoiceAddressService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 发票地址Service业务层处理
 *
 * @author LionLi
 * @date 2024-05-06
 */
@RequiredArgsConstructor
@Service
public class MerInvoiceAddressServiceImpl implements IMerInvoiceAddressService {

    private final MerInvoiceAddressMapper baseMapper;

    /**
     * 查询发票地址
     */
    @Override
    public MerInvoiceAddressVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询发票地址列表
     */
    @Override
    public TableDataInfo<MerInvoiceAddressVo> queryPageList(MerInvoiceAddressBo bo, PageQuery pageQuery) {
        QueryWrapper<MerInvoiceAddress> lqw = buildQueryWrapper(bo);
        Page<MerInvoiceAddressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询发票地址列表
     */
    @Override
    public List<MerInvoiceAddressVo> queryList(MerInvoiceAddressBo bo) {
        QueryWrapper<MerInvoiceAddress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<MerInvoiceAddress> buildQueryWrapper(MerInvoiceAddressBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<MerInvoiceAddress> lqw = new QueryWrapper<>();
        lqw.eq(bo.getMerId() != null, "mer_id", bo.getMerId());
        lqw.eq(StringUtils.isNotBlank(bo.getMerNo()), "mer_no", bo.getMerNo());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "name", bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), "phone", bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), "address", bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), "email", bo.getEmail());
        lqw.eq(bo.getIsDefault() != null, "is_default", bo.getIsDefault());
        lqw.eq(bo.getAddTime() != null, "add_time", bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), "add_by", bo.getAddBy());
        lqw.eq(StringUtils.isNotBlank(bo.getHeader()), "header", bo.getHeader());
        lqw.eq(StringUtils.isNotBlank(bo.getNsrSbh()), "nsr_sbh", bo.getNsrSbh());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitAddress()), "unit_address", bo.getUnitAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitPhone()), "unit_phone", bo.getUnitPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), "bank_card", bo.getBankCard());
        lqw.eq(StringUtils.isNotBlank(bo.getBankAddress()), "bank_address", bo.getBankAddress());
        return lqw;
    }

    /**
     * 新增发票地址
     */
    @Override
    public Boolean insertByBo(MerInvoiceAddressBo bo) {
        MerInvoiceAddress add = MapstructUtils.convert(bo, MerInvoiceAddress.class);
        validEntityBeforeSave(add);
        add.setAddTime(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发票地址
     */
    @Override
    public Boolean updateByBo(MerInvoiceAddressBo bo) {
        MerInvoiceAddress update = MapstructUtils.convert(bo, MerInvoiceAddress.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MerInvoiceAddress entity){
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<MerInvoiceAddress> lqw = Wrappers.lambdaQuery();
        lqw.eq(MerInvoiceAddress::getMerId,entity.getMerId());
        MerInvoiceAddress exists = baseMapper.selectOne(lqw);
        if(exists != null && (entity.getId() == null || entity.getId() != exists.getId())){
            throw new ServiceException("发票地址已存在");
        }
    }

    /**
     * 批量删除发票地址
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
