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.dromara.system.domain.Community;
import org.dromara.system.domain.bo.*;
import org.dromara.system.domain.vo.CommunityVo;
import org.dromara.system.domain.vo.ImgTypeVo;
import org.dromara.system.domain.vo.RegionFacilitiesVo;
import org.dromara.system.mapper.ImgTypeMapper;
import org.dromara.system.service.IImgTypeService;
import org.dromara.system.service.ISysOssService;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.vo.SysImgVo;
import org.dromara.system.domain.SysImg;
import org.dromara.system.mapper.SysImgMapper;
import org.dromara.system.service.ISysImgService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-16
 */
@RequiredArgsConstructor
@Service
public class SysImgServiceImpl implements ISysImgService {

    private final SysImgMapper baseMapper;

    private final IImgTypeService imgTypeService;

    private final ISysOssService sysOssService;

    private final ImgTypeMapper imgTypeMapper;

    private final ImgTypeServiceImpl imgTypeServiceImpl;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public SysImgVo queryById(String imgId){
        SysImgVo vo = baseMapper.selectVoById(imgId);
        if(vo!= null && "0".equals(vo.getImgType())){
            ImgTypeVo tVo = imgTypeMapper.selectByImgId(imgId);
            if(tVo!= null){
                vo.setPositionId(tVo.getTypeId());
            }
        }
        return vo;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<SysImgVo> queryPageList(SysImgBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysImg> lqw = buildQueryWrapper(bo);
        Page<SysImgVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<SysImgVo> queryList(SysImgBo bo) {
        LambdaQueryWrapper<SysImg> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysImg> buildQueryWrapper(SysImgBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysImg> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOssId()), SysImg::getOssId, bo.getOssId());
        lqw.eq(StringUtils.isNotBlank(bo.getImgType()), SysImg::getImgType, bo.getImgType());
        lqw.eq(StringUtils.isNotBlank(bo.getEnumC()), SysImg::getEnumC, bo.getEnumC());
//        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SysImg::getUrl, bo.getUrl());
//        lqw.like(StringUtils.isNotBlank(bo.getFileName()), SysImg::getFileName, bo.getFileName());
//        lqw.eq(StringUtils.isNotBlank(bo.getEnumC()), SysImg::getEnumC, bo.getEnumC());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(SysImgBo bo) {
        SysImg add = MapstructUtils.convert(bo, SysImg.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOssId(add.getOssId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(SysImgBo bo) {
        SysImg update = MapstructUtils.convert(bo, SysImg.class);
        validEntityBeforeSave(update);

        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        boolean status = baseMapper.deleteBatchIds(ids) > 0;
        if(status){
            ImgTypeBo bo = new ImgTypeBo();
            for (String i: ids){
                bo.setTypeId(i);
            }
            List<Long> longs = new ArrayList();
            for (String s : ids){
                longs.add(Long.valueOf(s));
            }
            sysOssService.deleteWithValidByIds(longs, true);
            //imgTypeService.batchDelete(new ArrayList(ids));
        }
        return status;
    }

    @Override
    public boolean batchDelete(List<String> list) {
        return baseMapper.batchDelete(list);
    }

    @Override
    public Boolean insert(SysImgBo bo) {
        for (SysImgVo sysImgVo : bo.getSysimg()) {
            ResponseBo res = sysImgVo.getResponse();
            if(res != null && res.getCode() == 200){
                InfoData infoData = res.getData();
                SysImgBo vo = new SysImgBo();
                vo.setUrl(infoData.getUrl().substring(8));
                vo.setFileName(infoData.getFileName());
                vo.setEnumC(sysImgVo.isDefaults() == true ? "0" : null);
                vo.setOssId(infoData.getOssId());
                vo.setImgType(bo.getImgType());
                SysImg add = MapstructUtils.convert(vo, SysImg.class);
                validEntityBeforeSave(add);
                boolean flag = baseMapper.insert(add) > 0;
                if (flag) {
                    bo.setOssId(add.getOssId());
                    ImgTypeBo types = new ImgTypeBo();
                    types.setImgId(infoData.getOssId());
                    types.setTypeId(add.getOssId());
                    types.setTypeC("0");           // 图片类型
                    if("0".equals(bo.getImgType()) && StringUtils.isNotEmpty(bo.getPositionId())){
                        types.setTypeId(bo.getPositionId());
                        types.setTypeC("6");           // 图片类型
                    }
                    imgTypeService.insertByBo(types);
                }
            }
        }
        this.setEnumValue(bo);
        return true;
    }

    @Override
    public Boolean update(SysImgBo bo) {
        List<Long> longs = new ArrayList();
        longs.add(Long.valueOf(bo.getOssId()));
        this.batchDelete(List.of(bo.getOssId()));
        //删除图片服务器图片
        //sysOssService.deleteWithValidByIds(longs, true);
        for (SysImgVo sysImgVo : bo.getSysimg()) {
            ResponseBo res = sysImgVo.getResponse();
            if(res != null && res.getCode() == 200){
                InfoData infoData = res.getData();
                SysImgBo vo = new SysImgBo();
                vo.setUrl(infoData.getUrl().substring(8));
                vo.setFileName(infoData.getFileName());
                vo.setEnumC(sysImgVo.isDefaults() == true ? "0" : null);
                vo.setOssId(infoData.getOssId());
                vo.setImgType(bo.getImgType());
                SysImg insert = MapstructUtils.convert(vo, SysImg.class);
                validEntityBeforeSave(insert);
                boolean status = baseMapper.insert(insert) > 0;
            }else{
                SysImgBo vo = new SysImgBo();
                vo.setUrl(sysImgVo.getUrl());
                vo.setFileName(sysImgVo.getFileName());
                vo.setEnumC(sysImgVo.isDefaults() == true ? "0" : null);
                vo.setOssId(sysImgVo.getOssId());
                vo.setImgType(bo.getImgType());
                SysImg update = MapstructUtils.convert(vo, SysImg.class);
                validEntityBeforeSave(update);
                boolean status = baseMapper.insert(update) > 0;
            }
        }
        this.setEnumValue(bo);

        return true;
    }

    @Override
    public List<SysImgBo> selectEnum() {
        return baseMapper.selectEnum();
    }

    @Override
    public TableDataInfo<SysImgVo> selectImg() {
        return TableDataInfo.build(baseMapper.selectImg());
    }

    @Override
    public List<SysImgVo> selectByRoomId(String roomId, String regionId) {
        return baseMapper.selectByRoomId(roomId, regionId);
    }

    @Override
    public List<SysImgVo> selectByList(String positionId) {
        ImgTypeBo vo = new ImgTypeBo();
        vo.setTypeId(positionId);
        vo.setTypeC("6");
        List<ImgTypeVo> imgVo =imgTypeServiceImpl.queryList(vo);
        if(imgVo != null && imgVo.size() > 0){
            List<String> imgIds = imgVo.stream().map(ImgTypeVo:: getImgId).collect(Collectors.toList());
            return baseMapper.selectByList(imgIds);
        }
        return null;
    }


    public void setEnumValue(SysImgBo bo){
        if(StringUtils.isNotBlank(bo.getImgType()) && "0".equals(bo.getImgType())){
            List<SysImgBo> s = this.selectEnum();
            if(!s.isEmpty() && s.size() > 0){
                for(int i=1; i< s.size(); i++){
                    SysImgBo b = new SysImgBo();
                    b.setOssId(s.get(i).getOssId());
                    b.setEnumC("1");
                    this.updateByBo(b);
                }
            }
        }
    }
}
