package com.jd.lightapplication.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.lightapplication.mapper.InspectionRecordMapper;
import com.jd.lightapplication.model.InspectionRecord;
import com.jd.lightapplication.model.InspectionResult;
import com.jd.lightapplication.model.UploadRecord;
import com.jd.lightapplication.model.dto.InspectionRecordAddDto;
import com.jd.lightapplication.model.dto.InspectionRecordQueryDto;
import com.jd.lightapplication.model.dto.InspectionRecordResultAddDto;
import com.jd.lightapplication.model.vo.InspectionRecordVo;
import com.jd.lightapplication.model.vo.InspectionResultVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class InspectionRecordService extends ServiceImpl<InspectionRecordMapper, InspectionRecord> {

    private InspectionResultService inspectionResultService;
    private UploadRecordService uploadRecordService;

    @Transactional(rollbackFor = Exception.class)
    public Boolean save(InspectionRecordAddDto inspectionRecordAddDto) {
        //先保存巡检记录主表信息
        InspectionRecord inspectionRecord = new InspectionRecord()
                .setInspectionType(inspectionRecordAddDto.getInspectionType())
                .setInspectionPersonnel(inspectionRecordAddDto.getInspectionPersonnel())
                .setMeetingId(inspectionRecordAddDto.getMeetingId())
                .setNormalNum(inspectionRecordAddDto.getInspectionResultList().stream().filter(t -> {
                    return t.getResult() == 10;
                }).collect(Collectors.toList()).size())
                .setErrorNum(inspectionRecordAddDto.getInspectionResultList().stream().filter(t -> {
                    return t.getResult() == 20;
                }).collect(Collectors.toList()).size())
                .setCreateTime(DateUtil.now());
        baseMapper.insert(inspectionRecord);
        //再保存巡检结果从表信息
        List<InspectionRecordResultAddDto> resultList = inspectionRecordAddDto.getInspectionResultList();
        resultList.forEach(t -> {
            InspectionResult inspectionResult = new InspectionResult()
                    .setInspectionRecordId(inspectionRecord.getId())
                    .setInspectionProjectId(t.getInspectionProjectId())
                    .setResult(t.getResult())
                    .setRemark(t.getRemark())
                    .setCreateTime(DateUtil.now());
            inspectionResultService.save(inspectionResult);
            //最后把结果id绑定到文件上传记录
            if (t.getFileIds() != null && t.getFileIds().size() > 0) {
                uploadRecordService.updateBatch(t.getFileIds(), inspectionResult.getId());
            }
        });
        return true;
    }

    public IPage<InspectionRecord> queryByPage(Page page, InspectionRecordQueryDto inspectionRecordQueryDto) {
        return baseMapper.queryByPage(page, inspectionRecordQueryDto);
    }

    public InspectionRecordVo queryDetails(long id) {
        //现根据巡检记录id查询巡检记录主信息
        InspectionRecordVo inspectionRecordVo = new InspectionRecordVo();
        BeanUtil.copyProperties(baseMapper.selectById(id), inspectionRecordVo);
        //再查询所有的巡检结果
        List<InspectionResultVo> resultList = inspectionResultService.queryVoByRecordId(inspectionRecordVo.getId());
        inspectionRecordVo.setInspectionResultVoList(resultList.stream().map(t -> {
            //再查询巡检结果的文件
            LambdaQueryWrapper<UploadRecord> uploadRecordWrapper = new LambdaQueryWrapper<>();
            uploadRecordWrapper.eq(UploadRecord::getFileType, 20);
            uploadRecordWrapper.eq(UploadRecord::getInspectionResultId, t.getId());
            t.setFileList(uploadRecordService.list(uploadRecordWrapper));
            return t;
        }).collect(Collectors.toList()));
        return inspectionRecordVo;
    }
}