package cd.casic.ci.process.process.service.cloudResource.impl;

import cd.casic.ci.process.dto.req.cloudResource.CloudResourceQueryReq;
import cd.casic.ci.process.dto.req.cloudResource.CloudResourceReq;
import cd.casic.ci.process.dto.resp.cloudResource.CloudResourceFindResp;
import cd.casic.ci.process.process.converter.CloudResourceConverter;
import cd.casic.ci.process.process.dao.cloudResource.CloudResourceDao;
import cd.casic.ci.process.process.dataObject.cloudResource.CloudResource;
import cd.casic.ci.process.process.service.cloudResource.CloudResourceService;
import cd.casic.framework.commons.pojo.PageResult;
import cd.casic.framework.security.dal.user.AdminUserDO;
import cd.casic.framework.tenant.core.service.AdminUserServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author HopeLi
 * @version v1.0
 * @ClassName ResourceServiceImpl
 * @Date: 2025/5/13 10:31
 * @Description:
 */
@Service
@Slf4j
public class CloudResourceServiceImpl extends ServiceImpl<CloudResourceDao, CloudResource> implements CloudResourceService {
    @Resource
    private CloudResourceDao cloudResourceDao;

    @Resource
    private AdminUserServiceImpl adminUserService;


    @Override
    public String createCloudResource(CloudResourceReq req) {
        CloudResource cloudResource = CloudResourceConverter.INSTANCE.toBean(req);
        cloudResourceDao.insert(cloudResource);
        return cloudResource.getId();
    }

    @Override
    public PageResult<CloudResourceFindResp> findCloudResourcePage(CloudResourceQueryReq query) {
        QueryWrapper<CloudResource> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(query.getId())){
            wrapper.eq("id",query.getId());
        }
        if (!ObjectUtils.isEmpty(query.getIdList())){
            wrapper.in("id",query.getId());
        }
        if (!ObjectUtils.isEmpty(query.getCreator())){
            wrapper.eq("creator",query.getCreator());
        }
        Page<CloudResource> cloudResourcePage = cloudResourceDao.selectPage(new Page<>(query.getPageNo(), query.getPageSize()), wrapper);

        if (ObjectUtils.isEmpty(cloudResourcePage)){
            return new PageResult<>();
        }

        List<CloudResourceFindResp> respList = CloudResourceConverter.INSTANCE.toRespList(cloudResourcePage.getRecords());

        //对资源进行赋值
        respList.forEach(this::setUserName);

        PageResult<CloudResourceFindResp> pageResult = new PageResult<>(respList,cloudResourcePage.getTotal(),cloudResourcePage.getCurrent(),cloudResourcePage.getSize());
        return pageResult;
    }


    private void setUserName(CloudResourceFindResp resp) {
        if (!StringUtils.isEmpty(resp.getCreator())){
            AdminUserDO user = adminUserService.getUser(Long.valueOf(resp.getCreator()));
            if (!ObjectUtils.isEmpty(user)){
                resp.setCreatorName(user.getUsername());
            }
        }

        if (!StringUtils.isEmpty(resp.getUpdater())){
            AdminUserDO user = adminUserService.getUser(Long.valueOf(resp.getUpdater()));
            if (!ObjectUtils.isEmpty(user)){
                resp.setUpdaterName(user.getUsername());
            }
        }
    }


}
