package com.usky.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.util.GlobalUtils;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.FileUtils;
import com.usky.system.domain.SysMenu;
import com.usky.system.domain.SysTenantConfig;
import com.usky.system.domain.SysUser;
import com.usky.system.mapper.SysTenantConfigMapper;
import com.usky.system.mapper.SysUserMapper;
import com.usky.system.service.ISysUserService;
import com.usky.system.service.SysTenantConfigService;
import com.usky.system.service.vo.SysTenantConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 租户配置表 服务实现类
 * </p>
 *
 * @author ya
 * @since 2022-05-06
 */
@Service
public class SysTenantConfigServiceImpl extends AbstractCrudService<SysTenantConfigMapper, SysTenantConfig> implements SysTenantConfigService {
    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private ISysUserService userService;


    @Override
    public List<SysTenantConfigVo> getTenantConfig(String url) {
        List<SysTenantConfigVo> list = baseMapper.getTenantConfig(url);
        if (CollectionUtils.isEmpty(list)){
            list = baseMapper.getTenantConfig1();
        }
        return list;
    }

    @Override
    public List<SysTenantConfig> getMiddleConfig() {
//        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
//        if ("".equals(sysUser) && sysUser == null) {
//            throw new BusinessException("无此租户，请联系管理员");
//        }
        LambdaQueryWrapper<SysTenantConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysTenantConfig::getTenantId, SecurityUtils.getTenantId());
        List<SysTenantConfig> list = this.list(queryWrapper);
        return list;
    }


    @Override
    public List<SysTenantConfig> getMiddleConfigList(Integer tenantId) {
        LambdaQueryWrapper<SysTenantConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysTenantConfig::getTenantId, tenantId);
        List<SysTenantConfig> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public void addMiddleConfig(SysTenantConfig sysTenantConfig) {
        sysTenantConfig.setCreateBy(SecurityUtils.getUsername());
        sysTenantConfig.setCreateTime(LocalDateTime.now());
        this.save(sysTenantConfig);
    }

    @Override
    public void updaMiddleConfig(SysTenantConfig sysTenantConfig) {
        sysTenantConfig.setUpdateBy(SecurityUtils.getUsername());
        sysTenantConfig.setUpdateTime(LocalDateTime.now());
        this.updateById(sysTenantConfig);
    }


    @Override
    public List<SysMenu> getMenuBoxList(Integer tenantId) {
        List<SysMenu> list = baseMapper.getMenuBoxList(tenantId, "C");
        return list;
    }

    @Override
    public String pictureUpload(@NotNull MultipartFile file, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        fileName = UUID.randomUUID() + suffixName; // 新文件名

        File file1 = FileUtils.getFile(GlobalUtils.getTempBaseDir(), fileName);
        try {
            file.transferTo(file1);
        } catch (Exception e) {
            throw new BusinessException("文件上传异常", e);
        }

//        String fileName = file.getOriginalFilename();  // 文件名
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
////        String filePath = "D://temp-rainy//"; // 上传后的路径
//        fileName = UUID.randomUUID() + suffixName; // 新文件名
//
//        String destFileName = request.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;
//        File dest = new File(destFileName);
////        File dest = new File(filePath + fileName);
//        if (!dest.getParentFile().exists()) {
//            dest.getParentFile().mkdirs();
//        }
//        try {
//            file.transferTo(dest);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        String filename = "/uploaded/" + fileName;
        return fileName;
    }
}
