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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.TbCustomizedTemplate;
import com.gci.boot.modules.vehicles.entity.TbCustomizedTemplatePart;
import com.gci.boot.modules.vehicles.mapper.TbCustomizedTemplateMapper;
import com.gci.boot.modules.vehicles.query.CustomizedTemplateQuery;
import com.gci.boot.modules.vehicles.service.ITbCustomizedTemplatePartService;
import com.gci.boot.modules.vehicles.service.ITbCustomizedTemplateService;
import com.gci.boot.modules.vehicles.vo.CustomizedTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.CheckForNull;
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 TbCustomizedTemplateServiceImpl extends ServiceImpl<TbCustomizedTemplateMapper, TbCustomizedTemplate> implements ITbCustomizedTemplateService {

    @Autowired
    private IProjectsService projectsService;

    @Autowired
    private ITbCustomizedTemplatePartService customizedTemplatePartService;

    @Resource
    private TbCustomizedTemplateMapper customizedTemplateMapper;


    @Override
    public IPage<CustomizedTemplateVo> pageBuild(Page<CustomizedTemplateVo> page, CustomizedTemplateQuery query) {
        IPage<CustomizedTemplateVo> list = customizedTemplateMapper.listVo(page, query);
        return list;
    }

    @Override
    public void saveItem(TbCustomizedTemplate tbCustomizedTemplate) {
        check(tbCustomizedTemplate);
        save(tbCustomizedTemplate);
        List<TbCustomizedTemplatePart> projectList = tbCustomizedTemplate.getPartList();
        projectList.forEach(p -> p.setTemplateId(tbCustomizedTemplate.getId()));
        customizedTemplatePartService.saveBatch(projectList);
    }

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

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

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

    @Override
    public void removeItem(String projectId, String vehicleType) {
        TbCustomizedTemplate byId = getOne(new LambdaQueryWrapper<TbCustomizedTemplate>().eq(TbCustomizedTemplate::getProjectId, projectId).eq(TbCustomizedTemplate::getVehicleType,vehicleType).last("limit 1"));
        if (ObjectUtil.isNotEmpty(byId)) {
            String id = byId.getId();
            removeById(id);
            customizedTemplatePartService.remove(new LambdaQueryWrapper<TbCustomizedTemplatePart>().eq(TbCustomizedTemplatePart::getTemplateId, id));
        }
    }
}
