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


import cd.casic.ci.process.dto.req.testCase.TestCaseInfoQueryReq;
import cd.casic.ci.process.dto.req.testCase.TestCaseInfoReq;
import cd.casic.ci.process.dto.resp.testCase.TestCaseInfoResp;
import cd.casic.ci.process.process.converter.TestCaseInfoConverter;
import cd.casic.ci.process.process.dao.testCase.TestCaseInfoDao;
import cd.casic.ci.process.process.dataObject.testCase.TestCaseInfo;
import cd.casic.ci.process.process.service.testCase.TestCaseInfoService;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
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.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HopeLi
 * @version v1.0
 * @ClassName PipelineServiceImpl
 * @Date: 2025/5/13 10:31
 * @Description:
 */
@Service
@Slf4j
public class TestCaseInfoServiceImpl extends ServiceImpl<TestCaseInfoDao, TestCaseInfo> implements TestCaseInfoService {

    @Resource
    private TestCaseInfoDao testCaseInfoDao;

    @Resource
    private AdminUserServiceImpl adminUserService;

    @Override
    public String create(TestCaseInfoReq req) {
        TestCaseInfo testCaseInfo = TestCaseInfoConverter.INSTANCE.toBean(req);
        testCaseInfoDao.insert(testCaseInfo);
        return testCaseInfo.getId();
    }

    @Override
    public void delete(String id) {
        testCaseInfoDao.deleteById(id);
    }

    @Override
    public void updateTestCase(TestCaseInfoReq req) {
        TestCaseInfo testCaseInfo = TestCaseInfoConverter.INSTANCE.toBean(req);
        testCaseInfoDao.updateById(testCaseInfo);
    }

    @Override
    public List<TestCaseInfoResp> findList(TestCaseInfoQueryReq query) {
        QueryWrapper<TestCaseInfo> wrapper = new QueryWrapper<>();
        List<TestCaseInfo> testCaseInfos = testCaseInfoDao.selectList(wrapper);

        if (ObjectUtils.isEmpty(testCaseInfos)){
            return new ArrayList<>();
        }
        List<TestCaseInfoResp> respList = TestCaseInfoConverter.INSTANCE.toRespList(testCaseInfos);
        respList.forEach(this::setUserName);
        return respList;
    }



    @Override
    public PageResult<TestCaseInfoResp> findPage(TestCaseInfoQueryReq query) {
        QueryWrapper<TestCaseInfo> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(query.getTestFileType())){
            wrapper.eq("test_file_type", query.getTestFileType());
        }
        Page<TestCaseInfo> testCaseInfoPage = testCaseInfoDao.selectPage(new Page<>(query.getPageNo(), query.getPageSize()), wrapper);
        if (ObjectUtils.isEmpty(testCaseInfoPage)){
            return new PageResult<>();
        }
        List<TestCaseInfoResp> respList = TestCaseInfoConverter.INSTANCE.toRespList(testCaseInfoPage.getRecords());
        respList.forEach(this::setUserName);
        return new PageResult<>(respList,testCaseInfoPage.getTotal(),testCaseInfoPage.getCurrent(),testCaseInfoPage.getSize());
    }

    @Override
    public TestCaseInfoResp findById(String id) {
        TestCaseInfo testCaseInfo = testCaseInfoDao.selectById(id);
        if (ObjectUtils.isEmpty(testCaseInfo)){
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"数据错误请联系管理员");
        }
        TestCaseInfoResp testCaseInfoResp = TestCaseInfoConverter.INSTANCE.toResp(testCaseInfo);
        setUserName(testCaseInfoResp);
        return testCaseInfoResp;
    }

    @Override
    public List<String> findFileTypeList() {
        List<TestCaseInfo> testCaseInfos = testCaseInfoDao.selectList(new QueryWrapper<>());

        if (ObjectUtils.isEmpty(testCaseInfos)){
            return new ArrayList<>();
        }

        //将文件类型取出并去重
        List<String> fileTypeList = testCaseInfos.stream().map(TestCaseInfo::getTestFileType).distinct().toList();

        if (CollectionUtils.isEmpty(fileTypeList)){
            return new ArrayList<>();
        }
        return fileTypeList;
    }


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

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