package digital.laboratory.platform.inspection.service.impl;
/*
 *@title TestRecordMethodServiceImpl
 *@description
 *@author xy
 *@version 1.0
 *@create 2023/12/19 14:51
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.inspection.dto.TestRecordArgumentDto;
import digital.laboratory.platform.inspection.entity.TestRecordMethod;
import digital.laboratory.platform.inspection.enums.TestRecordArgumentType;
import digital.laboratory.platform.inspection.mapper.TestRecordMapper;
import digital.laboratory.platform.inspection.mapper.TestRecordMethodMapper;
import digital.laboratory.platform.inspection.mapper.TestTemplateMapper;
import digital.laboratory.platform.inspection.service.TestRecordMethodService;
import digital.laboratory.platform.inspection.service.TestRecordService;
import digital.laboratory.platform.inspection.service.TestTemplateService;
import digital.laboratory.platform.inspetion.api.vo.TestRecordVo;
import digital.laboratory.platform.inspection.vo.TestTemplateVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class TestRecordMethodServiceImpl extends ServiceImpl<TestRecordMethodMapper, TestRecordMethod> implements TestRecordMethodService {

    @Resource
    private TestRecordService testRecordService;

    @Resource
    private TestRecordMapper testRecordMapper;

    @Resource
    private TestTemplateMapper testTemplateMapper;

    @Resource
    private TestTemplateService testTemplateService;

    @Override
    public TestRecordMethod addTestRecordMethod(TestRecordMethod testRecordMethod) {
        if (StringUtils.isBlank(testRecordMethod.getId())) {
            testRecordMethod.setId(IdWorker.get32UUID());
            log.info("保存对象testRecordMethod的ID为空，由系统生成一个给它使用 {}", testRecordMethod.getId());
        }
        boolean ret = this.save(testRecordMethod);
        if (ret) {
            log.info("{} 保存成功", testRecordMethod);
            return testRecordMethod;
        } else {
            log.info("{} 保存失败", testRecordMethod);
            return null;
        }
    }

    @Override
    public TestRecordMethod updateTestRecordMethod(TestRecordMethod testRecordMethod) {
        if (testRecordMethod.getSource() == 0) {
            log.info("数据是其他系统推送录入的，不能修改");
            return null;
        }
        boolean ret = this.updateById(testRecordMethod);
        if (ret) {
            return testRecordMethod;
        } else {
            return null;
        }
    }

    /**
     * 删除方法
     * @param id
     * @return
     */

    @Override
    public Boolean deleteTestRecordMethod(String id) {
        List<TestRecordVo> testRecordList = testRecordMapper.getTestRecordMapList(new LambdaQueryWrapper<>());
        testRecordList.forEach(item -> {
            if (item.getTestMethodList() != null && item.getTestMethodList().size() > 0 && item.getTestMethodList().contains(id)) {
                throw new RuntimeException(String.format("当前方法已绑定编号为%s的实验，无法删除!", item.getTestSerialNumber()));
            }
        });
        List<TestTemplateVo> testTemplateMapList = testTemplateMapper.getTestTemplateMapList(new LambdaQueryWrapper());
        testTemplateMapList.forEach(item -> {
            if (item.getTestMethods() != null && item.getTestMethods().size() > 0 && item.getTestMethods().contains(id)) {
                throw new RuntimeException(String.format("当前方法已绑定ID为%s的模板，无法删除!", item.getId()));
            }
        });
        this.list(Wrappers.lambdaQuery());
        return this.removeById(id);
    }

    @Override
    public IPage getTestRecordMethodPageList(Page page, TestRecordMethod testRecordMethod) {
        IPage ret = this.pageMaps(page, Wrappers.<TestRecordMethod>lambdaQuery()
                .like(StringUtils.isNotBlank(testRecordMethod.getMethodName()), TestRecordMethod::getMethodName, testRecordMethod.getMethodName()));
        return ret;
    }

    @Override
    public List<TestRecordMethod> getTestRecordMethodList(TestRecordMethod testRecordMethod) {
        List<TestRecordMethod> retList = this.list(Wrappers.<TestRecordMethod>lambdaQuery()
                .like(StringUtils.isNotBlank(testRecordMethod.getMethodName()), TestRecordMethod::getMethodName, testRecordMethod.getMethodName())
                .orderByDesc(TestRecordMethod::getCreateTime));
        return retList;
    }

    @Override
    public Boolean checkExist(String id) {
        List<TestRecordMethod> retList = this.list(Wrappers.<TestRecordMethod>lambdaQuery()
                .eq(TestRecordMethod::getId, id));
        if (retList.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 向实验添加或移除方法
     * @param testRecordArgumentDto
     * @return
     */
    @Override
    public boolean useTestRecordMethod(TestRecordArgumentDto testRecordArgumentDto) {
        return testRecordService.updateTestRecordArgument(testRecordArgumentDto.getTestRecordId(), testRecordArgumentDto.getArgumentId(), TestRecordArgumentType.TEST_RECORD_ARGUMENT_METHOD.getType(), testRecordArgumentDto.getOpCode());
    }

    /**
     * 向模板添加或移除方法
     * @param testRecordArgumentDto
     * @return
     */
    @Override
    public boolean useTemplateMethod(TestRecordArgumentDto testRecordArgumentDto) {
        return testTemplateService.updateTestTemplate(testRecordArgumentDto.getTemplateId(), testRecordArgumentDto.getArgumentId(), TestRecordArgumentType.TEST_RECORD_ARGUMENT_METHOD.getType(), testRecordArgumentDto.getOpCode());
    }
}
