package com.yida.data.device.telephone.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.telephone.EduTelephoneFamily;
import com.yida.data.device.telephone.service.EduTelephoneFamilyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 亲情电话-亲情号码 Controller
 *
 * @author zhanghaijun
 * @date 2021-08-10 16:59:43
 */
@Api(tags = "亲情电话-亲情号码")
@Slf4j
@Validated
@RestController
@RequestMapping("/telephone/family")
@RequiredArgsConstructor
public class EduTelephoneFamilyController {

    private final EduTelephoneFamilyService eduTelephoneFamilyService;

    @ApiOperation("查询亲情电话列表")
    @GetMapping("/listFamily")
    public ResultBean<List<EduTelephoneFamily>> listFamily(@ApiParam("卡id") @RequestParam Long cardId) {
        return ResultBean.buildSuccess(eduTelephoneFamilyService.list(Wrappers.lambdaQuery(new EduTelephoneFamily())
                .eq(EduTelephoneFamily::getCardId, cardId)));
    }

    @ApiOperation("新增或编辑亲情电话")
    @PostMapping("/saveOrUpdateFamily")
    public ResultBean saveOrUpdateFamily(@RequestBody List<EduTelephoneFamily> eduTelephoneFamilies) {
        ArrayList<EduTelephoneFamily> list = new ArrayList<>();

        for (EduTelephoneFamily eduTelephoneFamily : eduTelephoneFamilies) {
            if (!eduTelephoneFamily.getPhone().isEmpty()) {
                list.add(eduTelephoneFamily);
            }
        }
        //删除对应ic卡的所有亲情电话
        eduTelephoneFamilyService.remove(Wrappers.lambdaQuery(new EduTelephoneFamily())
                .eq(EduTelephoneFamily::getCardId, eduTelephoneFamilies.get(0).getCardId()));
        if (CollectionUtil.isNotEmpty(list) && !(list.get(0).getPhone().isEmpty())) {
            eduTelephoneFamilyService.saveOrUpdateBatch(list);
        }
        return ResultBean.buildSuccess();
    }
}


