package com.bs.web.controller.system;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.common.annotation.Log;
import com.bs.common.config.BsConfig;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.domain.entity.TenantConfig;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.file.FileUploadUtils;
import com.bs.common.utils.file.MimeTypeUtils;
import com.bs.common.utils.poi.ExcelUtil;
import com.bs.system.service.ITenantConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 租户配置Controller
 *
 * @author ruoyi
 * @date 2023-04-02
 */
@RestController
@RequestMapping("/tenant/config")
public class TenantConfigController extends BaseController {
    @Autowired
    private ITenantConfigService tenantConfigService;

    /**
     * 分页查询租户配置列表
     */
    @PreAuthorize("@ss.hasPermi('tenant:config:pageList')")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TenantConfig tenantConfig) {
        startPage();
        LambdaQueryWrapper<TenantConfig> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, tenantConfig);
        List<TenantConfig> list = tenantConfigService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询租户配置列表
     */
    @PreAuthorize("@ss.hasPermi('tenant:config:list')")
    @GetMapping("/list")
    public AjaxResult list(TenantConfig tenantConfig) {
        LambdaQueryWrapper<TenantConfig> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, tenantConfig);
        List<TenantConfig> list = tenantConfigService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出租户配置列表
     */
    @Log(title = "租户配置导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantConfig tenantConfig) {
        LambdaQueryWrapper<TenantConfig> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, tenantConfig);
        List<TenantConfig> list = tenantConfigService.list(queryWrapper);
        ExcelUtil<TenantConfig> util = new ExcelUtil<TenantConfig>(TenantConfig.class);
        util.exportExcel(response, list, "租户配置数据");
    }

    /**
     * 获取租户配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        TenantConfig tenantConfig = tenantConfigService.getById(id);
        tenantConfig.setTenantId(tenantConfig.getId()) ;
        return success(tenantConfig);
    }

    /**
     * 新增租户配置
     */
    @Log(title = "租户配置新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TenantConfig tenantConfig) {
        tenantConfig.setId(tenantConfig.getTenantId()) ;
        return toAjax(tenantConfigService.save(tenantConfig));
    }

    /**
     * 修改租户配置
     */
    @Log(title = "租户配置修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TenantConfig tenantConfig) {
        tenantConfig.setId(tenantConfig.getTenantId()) ;
        return toAjax(tenantConfigService.updateById(tenantConfig));
    }

    /**
     * 删除租户配置
     */
    @Log(title = "租户配置删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(tenantConfigService.removeBatchByIds(ids));
    }

    /**
     * 租户logo图片上传
     */
    @Log(title = "租户logo图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/logo")
    public AjaxResult avatar(@RequestParam("tenantLogo") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String logo = FileUploadUtils.upload(BsConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = success();
            ajax.put("imgUrl", logo);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }



    /**
     * 条件设置
     */
    private void condition(LambdaQueryWrapper<TenantConfig> queryWrapper, TenantConfig tenantConfig) {


        //${column.columnComment}
        if (Validator.isNotEmpty(tenantConfig.getId())) {
            queryWrapper.eq(TenantConfig::getId, tenantConfig.getId());
        }

        //开始时间
        if (tenantConfig.getStartData() != null) {
            queryWrapper.eq(TenantConfig::getStartData, tenantConfig.getStartData());
        }

        //结束时间
        if (tenantConfig.getStopData() != null) {
            queryWrapper.eq(TenantConfig::getStopData, tenantConfig.getStopData());
        }

        //是否限制用户数
        if (Validator.isNotEmpty(tenantConfig.getIsUserAstrict())) {
            queryWrapper.eq(TenantConfig::getIsUserAstrict, tenantConfig.getIsUserAstrict());
        }

        //用户数
        if (tenantConfig.getUserQuantity() != null) {
            queryWrapper.eq(TenantConfig::getUserQuantity, tenantConfig.getUserQuantity());
        }

        //是否限制在线数
        if (Validator.isNotEmpty(tenantConfig.getIsOnLine())) {
            queryWrapper.eq(TenantConfig::getIsOnLine, tenantConfig.getIsOnLine());
        }

        //在线数
        if (tenantConfig.getOnLineUserQuantity() != null) {
            queryWrapper.eq(TenantConfig::getOnLineUserQuantity, tenantConfig.getOnLineUserQuantity());
        }

        //是否定义系统名称
        if (Validator.isNotEmpty(tenantConfig.getIsDefinitionName())) {
            queryWrapper.eq(TenantConfig::getIsDefinitionName, tenantConfig.getIsDefinitionName());
        }

        //系统名称
        if (Validator.isNotEmpty(tenantConfig.getSysName())) {
            queryWrapper.like(TenantConfig::getSysName, tenantConfig.getSysName());
        }

        //是否定义系统logo
        if (Validator.isNotEmpty(tenantConfig.getIsDefinitionLogo())) {
            queryWrapper.eq(TenantConfig::getIsDefinitionLogo, tenantConfig.getIsDefinitionLogo());
        }

        //系统log
        if (Validator.isNotEmpty(tenantConfig.getSysLog())) {
            queryWrapper.eq(TenantConfig::getSysLog, tenantConfig.getSysLog());
        }

        //是否定义系统色调
        if (Validator.isNotEmpty(tenantConfig.getIsDefinitionHue())) {
            queryWrapper.eq(TenantConfig::getIsDefinitionHue, tenantConfig.getIsDefinitionHue());
        }

        //系统色调
        if (Validator.isNotEmpty(tenantConfig.getSysHue())) {
            queryWrapper.eq(TenantConfig::getSysHue, tenantConfig.getSysHue());
        }

        //定制化数据，键值对{key:{type1:"",type2:"",value:""},key2:{type1:"",type2:"",value:""}}具体操作时候，做具体说明
        if (Validator.isNotEmpty(tenantConfig.getCustomizationFields())) {
            queryWrapper.eq(TenantConfig::getCustomizationFields, tenantConfig.getCustomizationFields());
        }


        //创建人员
        if (Validator.isNotEmpty(tenantConfig.getCreateBy())) {
            queryWrapper.eq(TenantConfig::getCreateBy, tenantConfig.getCreateBy());
        }


        //修改人员
        if (Validator.isNotEmpty(tenantConfig.getUpdateBy())) {
            queryWrapper.eq(TenantConfig::getUpdateBy, tenantConfig.getUpdateBy());
        }


        //部门ID
        if (Validator.isNotEmpty(tenantConfig.getCreateDept())) {
            queryWrapper.eq(TenantConfig::getCreateDept, tenantConfig.getCreateDept());
        }

        //删除标示 默认为1 有效数据 ,0为无效已删除
        if (Validator.isNotEmpty(tenantConfig.getDelFlag())) {
            queryWrapper.eq(TenantConfig::getDelFlag, tenantConfig.getDelFlag());
        }

    }

}
