package com.gci.boot.modules.basedata.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.constant.CommonConstant;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.basedata.entity.TbInspectionTemplate;
import com.gci.boot.modules.basedata.entity.TbInspectionTemplateProject;
import com.gci.boot.modules.basedata.mapper.TbInspectionTemplateMapper;
import com.gci.boot.modules.basedata.service.ITbInspectionTemplateProjectService;
import com.gci.boot.modules.basedata.service.ITbInspectionTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 巡检模板表 服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-09-09
 */
@Service
public class TbInspectionTemplateServiceImpl extends ServiceImpl<TbInspectionTemplateMapper, TbInspectionTemplate> implements ITbInspectionTemplateService {

    @Autowired
    private ITbInspectionTemplateProjectService templateProjectService;

    @Override
    public void saveItem(TbInspectionTemplate tbInspectionTemplate) {
        checkContent(tbInspectionTemplate);
        save(tbInspectionTemplate);
        List<TbInspectionTemplateProject> projectList = tbInspectionTemplate.getProjectList();
        projectList.forEach(p->p.setTemplateId(tbInspectionTemplate.getId()));
        templateProjectService.saveBatch(projectList);
    }

    /**
     * 校验巡检内容是否重复
     * @param tbInspectionTemplate
     */
    private void checkContent(TbInspectionTemplate tbInspectionTemplate) {
        List<TbInspectionTemplateProject> projectList = tbInspectionTemplate.getProjectList();
        long count = projectList.stream().map(TbInspectionTemplateProject::getContent).distinct().count();
        if (count<projectList.size()) {
            throw new GciBootException("同一巡检模板中巡检内容不能重复");
        }
    }

    @Override
    public void updateItem(TbInspectionTemplate tbInspectionTemplate) {
        checkContent(tbInspectionTemplate);
        updateById(tbInspectionTemplate);
        List<TbInspectionTemplateProject> list = templateProjectService.list(new LambdaQueryWrapper<TbInspectionTemplateProject>().eq(TbInspectionTemplateProject::getTemplateId, tbInspectionTemplate.getId()));
        List<String> collect = null;
        if (ObjectUtil.isNotEmpty(list)) {
            collect = list.stream().map(TbInspectionTemplateProject::getId).collect(Collectors.toList());
        }
        List<TbInspectionTemplateProject> projectList = tbInspectionTemplate.getProjectList();
        ArrayList<TbInspectionTemplateProject> needAdd = new ArrayList<>();
        ArrayList<TbInspectionTemplateProject> needEdit = new ArrayList<>();
        for (TbInspectionTemplateProject tbInspectionTemplateProject : projectList) {
            if (StrUtil.isBlank(tbInspectionTemplateProject.getId())) {
                tbInspectionTemplateProject.setTemplateId(tbInspectionTemplate.getId());
                needAdd.add(tbInspectionTemplateProject);
            } else {
                if (ObjectUtil.isNotEmpty(collect)) {
                    if (collect.contains(tbInspectionTemplateProject.getId())) {
                        tbInspectionTemplateProject.setTemplateId(tbInspectionTemplate.getId());
                        needEdit.add(tbInspectionTemplateProject);
                        collect.remove(tbInspectionTemplateProject.getId());
                    }
                }
            }
        }
        if (ObjectUtil.isNotEmpty(needAdd)) {
            templateProjectService.saveBatch(needAdd);
        }
        if (ObjectUtil.isNotEmpty(needEdit)) {
            templateProjectService.updateBatchById(needEdit);
        }
        if (ObjectUtil.isNotEmpty(collect)) {
            templateProjectService.removeByIds(collect);
        }
    }

    @Override
    public void removeItemById(String id) {
        if (id.equals(CommonConstant.DEFAULT_INSPECTION)) {
            throw new GciBootException("通用模板不可删除！");
        }
        removeById(id);
        templateProjectService.remove(new LambdaQueryWrapper<TbInspectionTemplateProject>().eq(TbInspectionTemplateProject::getTemplateId,id));
    }

    @Override
    public IPage<TbInspectionTemplate> pageBuild(Page<TbInspectionTemplate> page, TbInspectionTemplate inspectionTemplate) {
        LambdaQueryWrapper<TbInspectionTemplate> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(TbInspectionTemplate.class, inspectionTemplate);
        Page<TbInspectionTemplate> pageList = page(page, lambdaQueryWrapper);
        for (TbInspectionTemplate record : pageList.getRecords()) {
            LambdaQueryWrapper<TbInspectionTemplateProject> wrapper = new LambdaQueryWrapper<TbInspectionTemplateProject>().eq(TbInspectionTemplateProject::getTemplateId, record.getId()).orderByAsc(TbInspectionTemplateProject::getSort);
            record.setProjectList(templateProjectService.list(wrapper));
        }
        return pageList;
    }
}
