/*
 * Copyright (c) 2024 codvision.com All Rights Reserved.
 */

package com.codvision.basic.service.impl.inspection;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codvision.basic.entity.inspection.InspectionRecord;
import com.codvision.basic.entity.inspection.InspectionRecordItem;
import com.codvision.basic.entity.vo.inspection.InspectionRecordVO;
import com.codvision.basic.mapper.inspection.InspectionRecordItemMapper;
import com.codvision.basic.mapper.inspection.InspectionRecordMapper;
import com.codvision.basic.service.inspection.InspectionRecordService;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.QueryPageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author codvision
 */
@Service
@RequiredArgsConstructor
public class InspectionRecordServiceImpl extends ServiceImpl<InspectionRecordMapper, InspectionRecord> implements InspectionRecordService {

    private final InspectionRecordItemMapper itemMapper;

    @Override
    public IPage<InspectionRecordVO> getPageResult(QueryPage<Map<String, Object>> queryPage) {
        return getBaseMapper().getPageResult(queryPage.getPage(), QueryPageUtil.queryMap(queryPage));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addInspectRecord(InspectionRecord record, String checklist) {
        this.save(record);
        List<InspectionRecordItem> checkItems = JSON.parseArray(checklist, InspectionRecordItem.class);
        for (InspectionRecordItem item : checkItems) {
            item.setRecordId(record.getId());
            itemMapper.insert(item);
        }
    }

    @Override
    public InspectionRecordVO getById(Integer key) {
        return getBaseMapper().getById(key);
    }

    @Override
    public List<Map> statByTask(Map<String, Object> params) {
        return getBaseMapper().statByTask(params);
    }
}
