package com.jd.lightapplication.service;

import cn.hutool.core.date.DateUtil;
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.jd.lightapplication.model.InspectionProject;
import com.jd.lightapplication.model.InspectionTemplate;
import com.jd.lightapplication.mapper.InspectionTemplateMapper;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.lightapplication.model.InspectionTemplateProject;
import com.jd.lightapplication.model.dto.InspectionTemplateAddOrUpdateDto;
import com.jd.lightapplication.model.dto.InspectionTemplateQueryDto;
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 InspectionTemplateService extends ServiceImpl<InspectionTemplateMapper, InspectionTemplate> {

    private InspectionTemplateProjectService inspectionTemplateProjectService;

    /**
     * 新增或者修改方法  id为null新增  id不为null修改
     *
     * @param inspectionTemplateAddOrUpdateDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdate(InspectionTemplateAddOrUpdateDto inspectionTemplateAddOrUpdateDto) {
        //如果修改或者是保存的日常巡检模板并且是启用状态，把之前启用的日常巡检模板设为禁用
        if (inspectionTemplateAddOrUpdateDto.getType() == 10 && inspectionTemplateAddOrUpdateDto.getIsEnable() == 10) {
            baseMapper.disableCurrentDailyInspectionTemplate();
        }
        //先保存或者修改巡检模板主表信息
        InspectionTemplate inspectionTemplate = new InspectionTemplate()
                .setId(inspectionTemplateAddOrUpdateDto.getId())
                .setName(inspectionTemplateAddOrUpdateDto.getName())
                .setType(inspectionTemplateAddOrUpdateDto.getType())
                .setIsEnable(inspectionTemplateAddOrUpdateDto.getIsEnable())
                .setCreateTime(DateUtil.now());
        this.saveOrUpdate(inspectionTemplate);
        //再根据巡检模板id删除中间表数据
        LambdaQueryWrapper<InspectionTemplateProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InspectionTemplateProject::getTemplateId, inspectionTemplate.getId());
        inspectionTemplateProjectService.remove(wrapper);
        //最后再保存巡检模板和巡检项目中间表数据
        if (inspectionTemplateAddOrUpdateDto.getInspectionProjectIds() == null) {
            return true;
        }
        List<InspectionTemplateProject> templateProjectList = inspectionTemplateAddOrUpdateDto.getInspectionProjectIds().stream().map(t -> {
            return new InspectionTemplateProject()
                    .setTemplateId(inspectionTemplate.getId())
                    .setProjectId(t)
                    .setCreateTime(DateUtil.now());
        }).collect(Collectors.toList());
        inspectionTemplateProjectService.saveBatch(templateProjectList);
        return true;
    }

    public List<InspectionProject> getProjectByTemplateId(Long templateId) {
        return baseMapper.getProjectByTemplateId(templateId);
    }

    public IPage<InspectionTemplate> page(Page page, InspectionTemplateQueryDto inspectionTemplateQueryDto) {
        LambdaQueryWrapper<InspectionTemplate> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(inspectionTemplateQueryDto.getName())) {
            wrapper.like(InspectionTemplate::getName, inspectionTemplateQueryDto.getName());
        }
        if (inspectionTemplateQueryDto.getType() != null && inspectionTemplateQueryDto.getType() != 0) {
            wrapper.eq(InspectionTemplate::getType, inspectionTemplateQueryDto.getType());
        }
        wrapper.orderByDesc(InspectionTemplate::getCreateTime);
        return this.page(page, wrapper);
    }

    public List<InspectionProject> getDailyInspectionProject() {
        //先找到日常巡检的模板
        LambdaQueryWrapper<InspectionTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InspectionTemplate::getType, 10);
        wrapper.eq(InspectionTemplate::getIsEnable, 10);
        wrapper.orderByDesc(InspectionTemplate::getCreateTime);
        List<InspectionTemplate> inspectionTemplateList = baseMapper.selectList(wrapper);
        if (inspectionTemplateList.size() == 0 || inspectionTemplateList.get(0) == null) {
            return null;
        }
        return this.getProjectByTemplateId(inspectionTemplateList.get(0).getId());
    }

    public Boolean updateIsEnableById(Long templateId, Integer isEnable) {
        //如果这个模板是日常巡检模板，并且是启用，要把之前启用的日常巡检模板禁用掉
        InspectionTemplate inspectionTemplate = baseMapper.selectById(templateId);
        if (inspectionTemplate.getType() == 10 && isEnable == 10) {
            baseMapper.disableCurrentDailyInspectionTemplate();
        }
        inspectionTemplate.setIsEnable(isEnable);
        return baseMapper.updateById(inspectionTemplate) > 0;
    }
}