package com.template.controller;


import com.template.api.SmartFamilyIndexControllerAPI;
import com.template.model.pojo.SmartFamilyIndex;
import com.template.model.result.CommonResult;
import com.template.services.SmartFamilyIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 楼栋表 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-04-23
 */
@RestController
public class SmartFamilyIndexController implements SmartFamilyIndexControllerAPI {

    @Autowired
    private SmartFamilyIndexService smartFamilyIndexService;

    @Override
    public CommonResult exitsData() {
        List<SmartFamilyIndex> familys = smartFamilyIndexService.list(null);
        List<String> studentNos = familys.stream().map(SmartFamilyIndex::getStudentNo).distinct().collect(Collectors.toList());

        List<Integer> deleteIds = new ArrayList<>();
        for (String data : studentNos) {
            List<SmartFamilyIndex> datas = familys.stream().filter(e -> e.getStudentNo().equals(data)).collect(Collectors.toList());
            if (datas.size() > 1) {
                if (datas.get(0).getParentPhone().equals(datas.get(1).getParentPhone())) {

                    Optional<SmartFamilyIndex> firstData = datas.stream().filter(e -> e.getIndexData().intValue() == 1).findFirst();
                    if (firstData != null && firstData.isPresent()) {
                        deleteIds.add(firstData.get().getId());
                    }
                }
            }
        }

        if(deleteIds != null && deleteIds.size() > 0){
            int deleteDatas = smartFamilyIndexService.deleteSmartFamilyByIds(deleteIds);
            if(deleteDatas <= 0){
                return CommonResult.fail("操作失败");
            }
        }
        return CommonResult.ok("操作成功");
    }
}

