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

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.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.bo.SysSydLogBo;
import org.dromara.payment.sys.domain.vo.SysSydLogVo;
import org.dromara.payment.sys.domain.SysSydLog;
import org.dromara.payment.sys.mapper.SysSydLogMapper;
import org.dromara.payment.sys.service.ISysSydLogService;

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

/**
 * 服务商变动记录Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-26
 */
@RequiredArgsConstructor
@Service
public class SysSydLogServiceImpl implements ISysSydLogService {

    private final SysSydLogMapper baseMapper;

    /**
     * 查询服务商变动记录
     */
    @Override
    public SysSydLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务商变动记录列表
     */
    @Override
    public TableDataInfo<SysSydLogVo> queryPageList(SysSydLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSydLog> lqw = buildQueryWrapper(bo);
        Page<SysSydLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务商变动记录列表
     */
    @Override
    public List<SysSydLogVo> queryList(SysSydLogBo bo) {
        LambdaQueryWrapper<SysSydLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSydLog> buildQueryWrapper(SysSydLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSydLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSydId() != null, SysSydLog::getSydId, bo.getSydId());
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), SysSydLog::getNo, bo.getNo());
        lqw.eq(StringUtils.isNotBlank(bo.getGsmc()), SysSydLog::getGsmc, bo.getGsmc());
        lqw.eq(StringUtils.isNotBlank(bo.getKfd()), SysSydLog::getKfd, bo.getKfd());
        lqw.eq(StringUtils.isNotBlank(bo.getKfh()), SysSydLog::getKfh, bo.getKfh());
        lqw.eq(bo.getBankType() != null, SysSydLog::getBankType, bo.getBankType());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), SysSydLog::getBankCard, bo.getBankCard());
        lqw.eq(bo.getJbsl() != null, SysSydLog::getJbsl, bo.getJbsl());
        lqw.eq(bo.getQuoteNum() != null, SysSydLog::getQuoteNum, bo.getQuoteNum());
        lqw.eq(bo.getAddTime() != null, SysSydLog::getAddTime, bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), SysSydLog::getAddBy, bo.getAddBy());
        lqw.eq(StringUtils.isNotBlank(bo.getEditer()), SysSydLog::getEditer, bo.getEditer());
        lqw.eq(bo.getEditTime() != null, SysSydLog::getEditTime, bo.getEditTime());
        lqw.eq(bo.getProvinceId() != null, SysSydLog::getProvinceId, bo.getProvinceId());
        lqw.like(StringUtils.isNotBlank(bo.getProvinceName()), SysSydLog::getProvinceName, bo.getProvinceName());
        lqw.eq(bo.getCityId() != null, SysSydLog::getCityId, bo.getCityId());
        lqw.like(StringUtils.isNotBlank(bo.getCityName()), SysSydLog::getCityName, bo.getCityName());
        lqw.eq(bo.getCountyId() != null, SysSydLog::getCountyId, bo.getCountyId());
        lqw.like(StringUtils.isNotBlank(bo.getCountyName()), SysSydLog::getCountyName, bo.getCountyName());
        lqw.eq(StringUtils.isNotBlank(bo.getNsrSbh()), SysSydLog::getNsrSbh, bo.getNsrSbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFr()), SysSydLog::getFr, bo.getFr());
        lqw.eq(StringUtils.isNotBlank(bo.getFrCardid()), SysSydLog::getFrCardid, bo.getFrCardid());
        lqw.eq(StringUtils.isNotBlank(bo.getFrPhone()), SysSydLog::getFrPhone, bo.getFrPhone());
        lqw.like(StringUtils.isNotBlank(bo.getConName()), SysSydLog::getConName, bo.getConName());
        lqw.eq(StringUtils.isNotBlank(bo.getConPhone()), SysSydLog::getConPhone, bo.getConPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getConCardid()), SysSydLog::getConCardid, bo.getConCardid());
        lqw.eq(StringUtils.isNotBlank(bo.getConEmail()), SysSydLog::getConEmail, bo.getConEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), SysSydLog::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getBusiness()), SysSydLog::getBusiness, bo.getBusiness());
        lqw.eq(StringUtils.isNotBlank(bo.getAbridge()), SysSydLog::getAbridge, bo.getAbridge());
        lqw.eq(bo.getLimitAmount() != null, SysSydLog::getLimitAmount, bo.getLimitAmount());
        lqw.eq(bo.getSignMode() != null, SysSydLog::getSignMode, bo.getSignMode());
        lqw.eq(bo.getSignValidTime() != null, SysSydLog::getSignValidTime, bo.getSignValidTime());
        lqw.eq(bo.getLogTime() != null, SysSydLog::getLogTime, bo.getLogTime());
        return lqw;
    }

    /**
     * 新增服务商变动记录
     */
    @Override
    public Boolean insertByBo(SysSydLogBo bo) {
        SysSydLog add = MapstructUtils.convert(bo, SysSydLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务商变动记录
     */
    @Override
    public Boolean updateByBo(SysSydLogBo bo) {
        SysSydLog update = MapstructUtils.convert(bo, SysSydLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSydLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

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