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

import cn.hutool.core.bean.BeanUtil;
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.exception.GciBootException;
import com.gci.boot.modules.projects.entity.Projects;
import com.gci.boot.modules.projects.service.IProjectsService;
import com.gci.boot.modules.vehicles.entity.TbConsumablesTemplate;
import com.gci.boot.modules.vehicles.entity.TbConsumablesTemplatePart;
import com.gci.boot.modules.vehicles.mapper.TbConsumablesTemplateMapper;
import com.gci.boot.modules.vehicles.query.ConsumablesTemplateQuery;
import com.gci.boot.modules.vehicles.service.ITbConsumablesTemplatePartService;
import com.gci.boot.modules.vehicles.service.ITbConsumablesTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gci.boot.modules.vehicles.vo.ConsumablesTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 耗材模板 服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-09-12
 */
@Service
public class TbConsumablesTemplateServiceImpl extends ServiceImpl<TbConsumablesTemplateMapper, TbConsumablesTemplate> implements ITbConsumablesTemplateService {

    @Autowired
    private ITbConsumablesTemplatePartService consumablesTemplatePartService;

    @Autowired
    private IProjectsService projectsService;

    @Resource
    private TbConsumablesTemplateMapper consumablesTemplateMapper;

    @Override
    public IPage<ConsumablesTemplateVo> pageBuild(Page<ConsumablesTemplateVo> page, ConsumablesTemplateQuery query) {
        IPage<ConsumablesTemplateVo> list = consumablesTemplateMapper.listVo(page,query);
        return list;
    }

    @Override
    public void saveItem(TbConsumablesTemplate tbConsumablesTemplate) {
        check(tbConsumablesTemplate);
        save(tbConsumablesTemplate);
        List<TbConsumablesTemplatePart> projectList = tbConsumablesTemplate.getPartList();
        projectList.forEach(p->p.setTemplateId(tbConsumablesTemplate.getId()));
        consumablesTemplatePartService.saveBatch(projectList);
    }

    private void check(TbConsumablesTemplate tbConsumablesTemplate) {
        Projects byId = projectsService.getById(tbConsumablesTemplate.getProjectId());
        if ("0".equals(byId.getStatus())) {
            throw new GciBootException("该项目已禁用，请重新选择！");
        }
        List<TbConsumablesTemplatePart> partList = tbConsumablesTemplate.getPartList();
        long count = partList.stream().map(TbConsumablesTemplatePart::getName).distinct().count();
        if (count<partList.size()) {
            throw new GciBootException("名称不能重复！");
        }
    }

    @Override
    public void updateItem(TbConsumablesTemplate tbConsumablesTemplate) {
        check(tbConsumablesTemplate);
        updateById(tbConsumablesTemplate);
        List<TbConsumablesTemplatePart> list = consumablesTemplatePartService.list(new LambdaQueryWrapper<TbConsumablesTemplatePart>().eq(TbConsumablesTemplatePart::getTemplateId, tbConsumablesTemplate.getId()));
        List<String> collect = null;
        if (ObjectUtil.isNotEmpty(list)) {
            collect = list.stream().map(TbConsumablesTemplatePart::getId).collect(Collectors.toList());
        }
        List<TbConsumablesTemplatePart> partList = tbConsumablesTemplate.getPartList();
        ArrayList<TbConsumablesTemplatePart> needAdd = new ArrayList<>();
        ArrayList<TbConsumablesTemplatePart> needEdit = new ArrayList<>();
        for (TbConsumablesTemplatePart tbConsumablesTemplatePart : partList) {
            if (StrUtil.isBlank(tbConsumablesTemplatePart.getId())) {
                tbConsumablesTemplatePart.setTemplateId(tbConsumablesTemplate.getId());
                needAdd.add(tbConsumablesTemplatePart);
            } else {
                if (ObjectUtil.isNotEmpty(collect)) {
                    if (collect.contains(tbConsumablesTemplatePart.getId())) {
                        tbConsumablesTemplatePart.setTemplateId(tbConsumablesTemplate.getId());
                        needEdit.add(tbConsumablesTemplatePart);
                        collect.remove(tbConsumablesTemplatePart.getId());
                    }
                }
            }
        }
        if (ObjectUtil.isNotEmpty(needAdd)) {
            consumablesTemplatePartService.saveBatch(needAdd);
        }
        if (ObjectUtil.isNotEmpty(needEdit)) {
            consumablesTemplatePartService.updateBatchById(needEdit);
        }
        if (ObjectUtil.isNotEmpty(collect)) {
            consumablesTemplatePartService.removeByIds(collect);
        }
    }

    @Override
    public TbConsumablesTemplate getByProjectIdAndVehicleType(String projectId, String vehicleType) {
        TbConsumablesTemplate byId = getOne(new LambdaQueryWrapper<TbConsumablesTemplate>().eq(TbConsumablesTemplate::getProjectId,projectId).eq(TbConsumablesTemplate::getVehicleType,vehicleType).last("limit 1"));
        if (BeanUtil.isEmpty(byId)) {
            throw new GciBootException("项目id错误或车辆类型错误！");
        }
        List<TbConsumablesTemplatePart> list = consumablesTemplatePartService.list(new LambdaQueryWrapper<TbConsumablesTemplatePart>().eq(TbConsumablesTemplatePart::getTemplateId, byId.getId()).orderByAsc(TbConsumablesTemplatePart::getSort));
        byId.setPartList(list);
        return byId;
    }

    @Override
    public void removeItem(String projectId, String vehicleType) {
        TbConsumablesTemplate one = getOne(new LambdaQueryWrapper<TbConsumablesTemplate>().eq(TbConsumablesTemplate::getProjectId, projectId).eq(TbConsumablesTemplate::getVehicleType,vehicleType));
        if (ObjectUtil.isNotEmpty(one)) {
            String id = one.getId();
            removeById(id);
            consumablesTemplatePartService.remove(new LambdaQueryWrapper<TbConsumablesTemplatePart>().eq(TbConsumablesTemplatePart::getTemplateId,id));
        }
    }
}
