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.TbCustomerFollowTemplate;
import com.gci.boot.modules.basedata.entity.TbCustomerFollowTemplatePart;
import com.gci.boot.modules.basedata.mapper.TbCustomerFollowTemplateMapper;
import com.gci.boot.modules.basedata.service.ITbCustomerFollowTemplatePartService;
import com.gci.boot.modules.basedata.service.ITbCustomerFollowTemplateService;
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.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户回访模板 服务实现类
 * </p>
 *
 * @author 2464
 * @since 2023-10-11
 */
@Service
public class TbCustomerFollowTemplateServiceImpl extends ServiceImpl<TbCustomerFollowTemplateMapper, TbCustomerFollowTemplate> implements ITbCustomerFollowTemplateService {

    @Autowired
    private ITbCustomerFollowTemplatePartService customerFollowTemplatePartService;
    
    @Override
    public void saveItem(TbCustomerFollowTemplate tbCustomerFollowTemplate) {
        checkContent(tbCustomerFollowTemplate);
        save(tbCustomerFollowTemplate);
        List<TbCustomerFollowTemplatePart> projectList = tbCustomerFollowTemplate.getPartList();
        projectList.forEach(p->p.setTemplateId(tbCustomerFollowTemplate.getId()));
        customerFollowTemplatePartService.saveBatch(projectList);
    }

    private void checkContent(TbCustomerFollowTemplate tbCustomerFollowTemplate) {
        List<TbCustomerFollowTemplatePart> partList = tbCustomerFollowTemplate.getPartList();
        Map<String, List<TbCustomerFollowTemplatePart>> collect = partList.stream().collect(Collectors.groupingBy(TbCustomerFollowTemplatePart::getCategoryName));
        for (Map.Entry<String, List<TbCustomerFollowTemplatePart>> stringListEntry : collect.entrySet()) {
            long count = stringListEntry.getValue().stream().map(TbCustomerFollowTemplatePart::getFollowName).distinct().count();
            if (count < stringListEntry.getValue().size()) {
                throw new GciBootException("同一回访类别下回访题目不能重复！");
            }
        }
    }

    @Override
    public IPage<TbCustomerFollowTemplate> pageBuild(Page<TbCustomerFollowTemplate> page, TbCustomerFollowTemplate customerFollowTemplate) {
        LambdaQueryWrapper<TbCustomerFollowTemplate> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(TbCustomerFollowTemplate.class, customerFollowTemplate);
        Page<TbCustomerFollowTemplate> list = page(page, lambdaQueryWrapper);
        if (!list.getRecords().isEmpty()) {
            for (TbCustomerFollowTemplate record : list.getRecords()) {
                List<TbCustomerFollowTemplatePart> partList = customerFollowTemplatePartService.list(new LambdaQueryWrapper<TbCustomerFollowTemplatePart>().eq(TbCustomerFollowTemplatePart::getTemplateId, record.getId()).orderByAsc(TbCustomerFollowTemplatePart::getSort));
                record.setPartList(partList);
            }
        }
        return list;
    }

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

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