package org.dromara.payment.merchant.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.merchant.domain.bo.PubMerchantBo;
import org.dromara.payment.merchant.domain.vo.PubMerchantVo;
import org.dromara.payment.merchant.domain.PubMerchant;
import org.dromara.payment.merchant.mapper.PubMerchantMapper;
import org.dromara.payment.merchant.service.IPubMerchantService;

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

/**
 * 商户申请表Service业务层处理
 *
 * @author LionLi
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class PubMerchantServiceImpl implements IPubMerchantService {

    private final PubMerchantMapper baseMapper;

    /**
     * 查询商户申请表
     */
    @Override
    public PubMerchantVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商户申请表列表
     */
    @Override
    public TableDataInfo<PubMerchantVo> queryPageList(PubMerchantBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PubMerchant> lqw = buildQueryWrapper(bo);
        Page<PubMerchantVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商户申请表列表
     */
    @Override
    public List<PubMerchantVo> queryList(PubMerchantBo bo) {
        LambdaQueryWrapper<PubMerchant> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PubMerchant> buildQueryWrapper(PubMerchantBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PubMerchant> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), PubMerchant::getNo, bo.getNo());
        lqw.eq(StringUtils.isNotBlank(bo.getIndustry()), PubMerchant::getIndustry, bo.getIndustry());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PubMerchant::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getNameNc()), PubMerchant::getNameNc, bo.getNameNc());
        lqw.eq(StringUtils.isNotBlank(bo.getNsrSbh()), PubMerchant::getNsrSbh, bo.getNsrSbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFrSjh()), PubMerchant::getFrSjh, bo.getFrSjh());
        lqw.eq(StringUtils.isNotBlank(bo.getWtrSjh()), PubMerchant::getWtrSjh, bo.getWtrSjh());
        lqw.eq(StringUtils.isNotBlank(bo.getWtr()), PubMerchant::getWtr, bo.getWtr());
        lqw.eq(StringUtils.isNotBlank(bo.getWtrZjlx()), PubMerchant::getWtrZjlx, bo.getWtrZjlx());
        lqw.eq(StringUtils.isNotBlank(bo.getWtrSfz()), PubMerchant::getWtrSfz, bo.getWtrSfz());
        lqw.eq(StringUtils.isNotBlank(bo.getWtrEmail()), PubMerchant::getWtrEmail, bo.getWtrEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitArea()), PubMerchant::getUnitArea, bo.getUnitArea());
        lqw.like(StringUtils.isNotBlank(bo.getUnitAreaName()), PubMerchant::getUnitAreaName, bo.getUnitAreaName());
        lqw.eq(StringUtils.isNotBlank(bo.getFrSfz()), PubMerchant::getFrSfz, bo.getFrSfz());
        lqw.eq(StringUtils.isNotBlank(bo.getFrZjlx()), PubMerchant::getFrZjlx, bo.getFrZjlx());
        lqw.eq(StringUtils.isNotBlank(bo.getFr()), PubMerchant::getFr, bo.getFr());
        lqw.eq(StringUtils.isNotBlank(bo.getBankCard()), PubMerchant::getBankCard, bo.getBankCard());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), PubMerchant::getAddress, bo.getAddress());
        lqw.eq(bo.getYyzzBeginDate() != null, PubMerchant::getYyzzBeginDate, bo.getYyzzBeginDate());
        lqw.eq(bo.getYyzzEndDate() != null, PubMerchant::getYyzzEndDate, bo.getYyzzEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getYyzz()), PubMerchant::getYyzz, bo.getYyzz());
        lqw.eq(bo.getStatus() != null, PubMerchant::getStatus, bo.getStatus());
        lqw.eq(bo.getAddTime() != null, PubMerchant::getAddTime, bo.getAddTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAddBy()), PubMerchant::getAddBy, bo.getAddBy());
        lqw.eq(StringUtils.isNotBlank(bo.getEditer()), PubMerchant::getEditer, bo.getEditer());
        lqw.eq(bo.getEditTime() != null, PubMerchant::getEditTime, bo.getEditTime());
        lqw.eq(StringUtils.isNotBlank(bo.getKhm()), PubMerchant::getKhm, bo.getKhm());
        lqw.eq(StringUtils.isNotBlank(bo.getKhd()), PubMerchant::getKhd, bo.getKhd());
        lqw.eq(StringUtils.isNotBlank(bo.getKhh()), PubMerchant::getKhh, bo.getKhh());
        lqw.eq(StringUtils.isNotBlank(bo.getKhhNo()), PubMerchant::getKhhNo, bo.getKhhNo());
        lqw.eq(StringUtils.isNotBlank(bo.getJbckzhbm()), PubMerchant::getJbckzhbm, bo.getJbckzhbm());
        lqw.eq(StringUtils.isNotBlank(bo.getPassage()), PubMerchant::getPassage, bo.getPassage());
        lqw.eq(bo.getFrCardidBeginDate() != null, PubMerchant::getFrCardidBeginDate, bo.getFrCardidBeginDate());
        lqw.eq(bo.getFrCardidEndDate() != null, PubMerchant::getFrCardidEndDate, bo.getFrCardidEndDate());
        lqw.eq(bo.getJbrCardidBeginDate() != null, PubMerchant::getJbrCardidBeginDate, bo.getJbrCardidBeginDate());
        lqw.eq(bo.getJbrCardidEndDate() != null, PubMerchant::getJbrCardidEndDate, bo.getJbrCardidEndDate());
        lqw.eq(bo.getYwyId() != null, PubMerchant::getYwyId, bo.getYwyId());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoceHeader()), PubMerchant::getInvoceHeader, bo.getInvoceHeader());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoceCreditCode()), PubMerchant::getInvoceCreditCode, bo.getInvoceCreditCode());
        lqw.like(StringUtils.isNotBlank(bo.getInvoceBankName()), PubMerchant::getInvoceBankName, bo.getInvoceBankName());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoceBankCard()), PubMerchant::getInvoceBankCard, bo.getInvoceBankCard());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoceAddress()), PubMerchant::getInvoceAddress, bo.getInvoceAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoceDwdh()), PubMerchant::getInvoceDwdh, bo.getInvoceDwdh());
        lqw.eq(StringUtils.isNotBlank(bo.getFhxkzPic()), PubMerchant::getFhxkzPic, bo.getFhxkzPic());
        lqw.eq(StringUtils.isNotBlank(bo.getFrSfzzm()), PubMerchant::getFrSfzzm, bo.getFrSfzzm());
        lqw.eq(StringUtils.isNotBlank(bo.getFrSfzfm()), PubMerchant::getFrSfzfm, bo.getFrSfzfm());
        lqw.eq(StringUtils.isNotBlank(bo.getJbrSfzzm()), PubMerchant::getJbrSfzzm, bo.getJbrSfzzm());
        lqw.eq(StringUtils.isNotBlank(bo.getJbrSfzfm()), PubMerchant::getJbrSfzfm, bo.getJbrSfzfm());
        lqw.eq(StringUtils.isNotBlank(bo.getMtqjzPic()), PubMerchant::getMtqjzPic, bo.getMtqjzPic());
        lqw.eq(StringUtils.isNotBlank(bo.getBgzp1Pic()), PubMerchant::getBgzp1Pic, bo.getBgzp1Pic());
        lqw.eq(StringUtils.isNotBlank(bo.getBgzp2Pic()), PubMerchant::getBgzp2Pic, bo.getBgzp2Pic());
        lqw.eq(StringUtils.isNotBlank(bo.getYwcjzsxcnh()), PubMerchant::getYwcjzsxcnh, bo.getYwcjzsxcnh());
        lqw.eq(StringUtils.isNotBlank(bo.getYwcjsm()), PubMerchant::getYwcjsm, bo.getYwcjsm());
        lqw.eq(StringUtils.isNotBlank(bo.getFddsqxy()), PubMerchant::getFddsqxy, bo.getFddsqxy());
        lqw.eq(StringUtils.isNotBlank(bo.getTshyxkz()), PubMerchant::getTshyxkz, bo.getTshyxkz());
        lqw.eq(StringUtils.isNotBlank(bo.getLcqrh()), PubMerchant::getLcqrh, bo.getLcqrh());
        lqw.eq(bo.getVerifyStatus() != null, PubMerchant::getVerifyStatus, bo.getVerifyStatus());
        lqw.eq(bo.getVerifyTime() != null, PubMerchant::getVerifyTime, bo.getVerifyTime());
        lqw.eq(StringUtils.isNotBlank(bo.getVerifyBy()), PubMerchant::getVerifyBy, bo.getVerifyBy());
        lqw.eq(StringUtils.isNotBlank(bo.getVerifyReason()), PubMerchant::getVerifyReason, bo.getVerifyReason());
        lqw.eq(StringUtils.isNotBlank(bo.getZyyw()), PubMerchant::getZyyw, bo.getZyyw());
        return lqw;
    }

    /**
     * 新增商户申请表
     */
    @Override
    public Boolean insertByBo(PubMerchantBo bo) {
        PubMerchant add = MapstructUtils.convert(bo, PubMerchant.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商户申请表
     */
    @Override
    public Boolean updateByBo(PubMerchantBo bo) {
        PubMerchant update = MapstructUtils.convert(bo, PubMerchant.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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