package com.usky.system.service.impl;

import com.usky.common.core.bean.CommonPage;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.system.domain.SysDept;
import com.usky.system.domain.SysTenant;
import com.usky.system.mapper.SysTenantMapper;
import com.usky.system.service.ISysDeptService;
import com.usky.system.service.SysTenantService;
import com.usky.system.service.vo.SysTenantOneVo;
import com.usky.system.service.vo.SysTenantTwoVo;
import com.usky.system.service.vo.SysTenantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 租户信息表 服务实现类
 * </p>
 *
 * @author ya
 * @since 2022-05-05
 */
@Service
public class SysTenantServiceImpl extends AbstractCrudService<SysTenantMapper, SysTenant> implements SysTenantService {

    @Autowired
    private ISysDeptService iSysDeptService;

    @Override
    public List<SysTenantVo> getUserData() {
        Integer userId = SecurityUtils.getUserId().intValue();
        List<SysTenantVo> list = baseMapper.getUserData(userId);
        return list;
    }

    /**
     * 手机端APP租户管理-权限配置-目录查询
     *
     * @param domain
     * @return
     */
    @Override
    public List<SysTenant> getAppData(String domain){
        List<SysTenant> list = baseMapper.getAppData(domain);
        return list;
    }

    /**
     * 租户管理-租户列表查询
     *
     * @param sysTenantOneVo
     * @return
     */
    @Override
    public CommonPage<SysTenantTwoVo> getTenantData(SysTenantOneVo sysTenantOneVo) {
        if (sysTenantOneVo.getStatus() == null || sysTenantOneVo.getStatus().equals("") || sysTenantOneVo.getStatus().equals("null")) {
            sysTenantOneVo.setStatus("0");
        }
        List<SysTenantTwoVo> list1 = baseMapper.getTenantData(sysTenantOneVo.getTenantName(),
                sysTenantOneVo.getTenantManager(),
                sysTenantOneVo.getTenantType(),
                sysTenantOneVo.getStatus(),
                sysTenantOneVo.getPlatformName(),
                sysTenantOneVo.getStartTime(),
                sysTenantOneVo.getEndTime(), null, null, sysTenantOneVo.getId());
        int total = 0;
        if (list1.size() > 0) {
            total = list1.size();
        }
        Integer current = null;
        if (sysTenantOneVo.getPage() != null && sysTenantOneVo.getSize() > 0) {
            current = (sysTenantOneVo.getPage() - 1) * sysTenantOneVo.getSize();
        }
        List<SysTenantTwoVo> list = baseMapper.getTenantData(sysTenantOneVo.getTenantName(),
                sysTenantOneVo.getTenantManager(),
                sysTenantOneVo.getTenantType(),
                sysTenantOneVo.getStatus(),
                sysTenantOneVo.getPlatformName(),
                sysTenantOneVo.getStartTime(),
                sysTenantOneVo.getEndTime(),
                current,
                sysTenantOneVo.getSize(),
                sysTenantOneVo.getId());
        return new CommonPage<>(list, total, sysTenantOneVo.getSize(), sysTenantOneVo.getPage());
    }

    @Override
    public void addTenantData(SysTenant sysTenant) {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        sysTenant.setTenantCode(uuid);
//        sysTenant.setStatus("0");
        sysTenant.setCreateBy(SecurityUtils.getUsername());
        sysTenant.setCreateTime(LocalDateTime.now());
        this.save(sysTenant);
        Integer tenantId = sysTenant.getId();
        SysDept sysDept = new SysDept();
        sysDept.setTenantId(tenantId);
        sysDept.setDeptName(sysTenant.getTenantName());
        sysDept.setOrderNum("0");
        sysDept.setEmail(sysTenant.getEmail());
        sysDept.setPhone(sysTenant.getPhoneNumber());
        sysDept.setCreateBy(SecurityUtils.getUsername());
        iSysDeptService.insertDept(sysDept);
    }


    @Override
    public void updateTenantData(SysTenant sysTenant) {
        sysTenant.setUpdateBy(SecurityUtils.getUsername());
        sysTenant.setUpdateTime(LocalDateTime.now());
        this.updateById(sysTenant);
    }


    @Override
    public CommonPage<SysTenantTwoVo> getTenantDataOne(String tenantName, String tenantManager, Integer page, Integer size) {
        List<SysTenantTwoVo> list1 = baseMapper.getTenantDataOne(tenantName, tenantManager, null, null);
        int total = 0;
        if (list1.size() > 0) {
            total = list1.size();
        }
        Integer current = null;
        if (page != null && size > 0) {
            current = (page - 1) * size;
        }
        List<SysTenantTwoVo> list = baseMapper.getTenantDataOne(tenantName, tenantManager, current, size);
        return new CommonPage<>(list, total, size, page);
    }
}
