package org.dromara.business.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.CaseTenantOrganization;
import org.dromara.business.domain.bo.CaseTenantOrganizationBo;
import org.dromara.business.domain.vo.CaseTenantOrganizationVo;
import org.dromara.business.mapper.CaseTenantOrganizationMapper;
import org.dromara.business.service.ICaseTenantOrganizationService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.springframework.stereotype.Service;

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

/**
 * 合作组织Service业务层处理
 *
 * @author wuzhigang
 * @date 2023-12-19
 */
@RequiredArgsConstructor
@Service
public class CaseTenantOrganizationServiceImpl implements ICaseTenantOrganizationService {

    private final CaseTenantOrganizationMapper baseMapper;

    /**
     * 查询合作组织
     */
    @Override
    public CaseTenantOrganizationVo queryById(Long id) {
        CaseTenantOrganizationVo vo = baseMapper.selectVoById(id);
        if (StringUtils.isNotBlank(vo.getOrganizationLogoStr())) {
            // json字符串转协议文件对象
            vo.setOrganizationLogo(JSONUtil.toList(vo.getOrganizationLogoStr(), SysOssUploadVo.class));
        }
        return vo;
    }

    /**
     * 查询合作组织列表
     */
    @Override
    public TableDataInfo<CaseTenantOrganizationVo> queryPageList(CaseTenantOrganizationBo bo, PageQuery pageQuery) {
        /*LambdaQueryWrapper<CaseTenantOrganization> lqw = buildQueryWrapper(bo);
        Page<CaseTenantOrganizationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);*/

        Wrapper<CaseTenantOrganization> wrapper = buildQueryWrapperOld(bo);
        Page<CaseTenantOrganizationVo> result = baseMapper.selectVoPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    private Wrapper<CaseTenantOrganization> buildQueryWrapperOld(CaseTenantOrganizationBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<CaseTenantOrganization> wrapper = Wrappers.query();
        wrapper.eq("t.del_flag", "0")
            .like(StringUtils.isNotBlank(bo.getOrganizationName()), "t.organization_name", bo.getOrganizationName())
            .like(StringUtils.isNotBlank(bo.getOrganizationType()), "t.organization_type", bo.getOrganizationType())
            .orderByDesc("t.id");
        return wrapper;
    }

    /**
     * 查询合作组织列表
     */
    @Override
    public List<CaseTenantOrganizationVo> queryList(CaseTenantOrganizationBo bo) {
        LambdaQueryWrapper<CaseTenantOrganization> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaseTenantOrganization> buildQueryWrapper(CaseTenantOrganizationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaseTenantOrganization> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getOrganizationName()), CaseTenantOrganization::getOrganizationName, bo.getOrganizationName());
        lqw.eq(StringUtils.isNotBlank(bo.getOrganizationType()), CaseTenantOrganization::getOrganizationType, bo.getOrganizationType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CaseTenantOrganization::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增合作组织
     */
    @Override
    public Boolean insertByBo(CaseTenantOrganizationBo bo) {
        CaseTenantOrganization add = MapstructUtils.convert(bo, CaseTenantOrganization.class);
        // 当前租户ID
        add.setTenantId(TenantHelper.getTenantId());

        if (bo.getOrganizationLogo() != null && bo.getOrganizationLogo().size() > 0) {
            // logo OSS信息转json字符串
            String jsonStr = JSONUtil.toJsonStr(bo.getOrganizationLogo());
            add.setOrganizationLogoStr(jsonStr);
        }
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改合作组织
     */
    @Override
    public Boolean updateByBo(CaseTenantOrganizationBo bo) {
        CaseTenantOrganization update = MapstructUtils.convert(bo, CaseTenantOrganization.class);
        if (bo.getOrganizationLogo() != null && bo.getOrganizationLogo().size() > 0) {
            // logo OSS信息转json字符串
            String jsonStr = JSONUtil.toJsonStr(bo.getOrganizationLogo());
            update.setOrganizationLogoStr(jsonStr);
        }
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 查询合作组织选择列表
     *
     * @return
     */
    @Override
    public List<CaseTenantOrganizationVo> querySelectList() {
        LambdaQueryWrapper<CaseTenantOrganization> lqw = Wrappers.lambdaQuery();
        // 启用状态
        lqw.eq(CaseTenantOrganization::getStatus, "0");

        return baseMapper.selectVoList(lqw);
    }
}
