package org.dromara.system.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.system.domain.bo.SysUnitDomainBo;
import org.dromara.system.domain.vo.SysUnitDomainVo;
import org.dromara.system.domain.SysUnitDomain;
import org.dromara.system.mapper.SysUnitDomainMapper;
import org.dromara.system.service.ISysUnitDomainService;

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

/**
 * 单位域名配置Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-29
 */
@RequiredArgsConstructor
@Service
public class SysUnitDomainServiceImpl implements ISysUnitDomainService {

    private final SysUnitDomainMapper baseMapper;

    /**
     * 查询单位域名配置
     */
    @Override
    public SysUnitDomainVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public SysUnitDomainVo queryByDomainName(String domain) {
        LambdaQueryWrapper<SysUnitDomain> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysUnitDomain::getDomainName,domain);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询单位域名配置列表
     */
    @Override
    public TableDataInfo<SysUnitDomainVo> queryPageList(SysUnitDomainBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUnitDomain> lqw = buildQueryWrapper(bo);
        Page<SysUnitDomainVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询单位域名配置列表
     */
    @Override
    public List<SysUnitDomainVo> queryList(SysUnitDomainBo bo) {
        LambdaQueryWrapper<SysUnitDomain> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysUnitDomain> buildQueryWrapper(SysUnitDomainBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysUnitDomain> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDomainName()), SysUnitDomain::getDomainName, bo.getDomainName());
        lqw.like(StringUtils.isNotBlank(bo.getWebsiteName()), SysUnitDomain::getWebsiteName, bo.getWebsiteName());
        lqw.eq(StringUtils.isNotBlank(bo.getHotline()), SysUnitDomain::getHotline, bo.getHotline());
        lqw.eq(StringUtils.isNotBlank(bo.getCopyright()), SysUnitDomain::getCopyright, bo.getCopyright());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordNo()), SysUnitDomain::getRecordNo, bo.getRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getCopyrightUrl()), SysUnitDomain::getCopyrightUrl, bo.getCopyrightUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginPageLogo()), SysUnitDomain::getLoginPageLogo, bo.getLoginPageLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginPageBanner()), SysUnitDomain::getLoginPageBanner, bo.getLoginPageBanner());
        lqw.eq(StringUtils.isNotBlank(bo.getHomePageLogo()), SysUnitDomain::getHomePageLogo, bo.getHomePageLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getWapLogo()), SysUnitDomain::getWapLogo, bo.getWapLogo());
        lqw.eq(StringUtils.isNotBlank(bo.getWapBanner()), SysUnitDomain::getWapBanner, bo.getWapBanner());
        lqw.eq(StringUtils.isNotBlank(bo.getWeChatPub()), SysUnitDomain::getWeChatPub, bo.getWeChatPub());
        lqw.eq(StringUtils.isNotBlank(bo.getPcLoginProtocol()), SysUnitDomain::getPcLoginProtocol, bo.getPcLoginProtocol());
        lqw.eq(StringUtils.isNotBlank(bo.getMobileUserAgreement()), SysUnitDomain::getMobileUserAgreement, bo.getMobileUserAgreement());
        lqw.eq(StringUtils.isNotBlank(bo.getPrivacyAgreement()), SysUnitDomain::getPrivacyAgreement, bo.getPrivacyAgreement());
        lqw.eq(bo.getStatus() != null, SysUnitDomain::getStatus, bo.getStatus());
        lqw.eq(bo.getBusRole() != null, SysUnitDomain::getBusRole, bo.getBusRole());
        lqw.eq(bo.getBusId() != null, SysUnitDomain::getBusId, bo.getBusId());
        return lqw;
    }

    /**
     * 新增单位域名配置
     */
    @Override
    public Boolean insertByBo(SysUnitDomainBo bo) {
        SysUnitDomain add = MapstructUtils.convert(bo, SysUnitDomain.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改单位域名配置
     */
    @Override
    public Boolean updateByBo(SysUnitDomainBo bo) {
        SysUnitDomain update = MapstructUtils.convert(bo, SysUnitDomain.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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