package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.feigns.DeviceFeign;
import com.sptg.estate.api.entity.Member;
import com.sptg.estate.api.entity.MemberDevice;
import com.sptg.estate.api.feigns.MemberDeviceFeign;
import com.sptg.estate.api.feigns.MemberFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping(value = "/estate/memberDevice")
@RequiredArgsConstructor
public class MemberDeviceController {

    private final MemberDeviceFeign memberDeviceFeign;
    private final MemberFeign memberFeign;
    private final DeviceFeign deviceFeign;

    @GetMapping(value = "/pagingQuery")
    public ResultBean<Page<MemberDevice>> pagingQuery(MemberDevice memberDevice,Page<MemberDevice> page){
        memberDevice.setPage(page);
        return this.memberDeviceFeign.pagingQuery(memberDevice);
    }

    @PostMapping(value = "/enable")
    public ResultBean<Boolean> enable(MemberDevice memberDevice){
        return this.memberDeviceFeign.enable(memberDevice);
    }

    @PostMapping(value = "/addBatch")
    public ResultBean<Boolean> addBatch(MemberDevice memberDevice){
        Assert.notEmpty(memberDevice.getDeviceIds(),"请先选择设备");
        Member member = this.memberFeign.getMemberByMemberId(memberDevice.getMemberId());
        Assert.notNull(member,"该会员不存在");
        List<MemberDevice> memberDeviceList = new ArrayList<>();
        List<Device> deviceList = this.deviceFeign.getListByIds(memberDevice.getDeviceIds());
        for (Device device : deviceList){
            MemberDevice md = new MemberDevice();
            md.setMemberId(member.getId());
            md.setDeviceId(device.getId());
            md.setDeviceAlias(device.getAlias());
            md.setDeviceName(device.getName());
            md.setSerialNumber(device.getSerialNumber());
            md.setInstallPath(device.getInstallPath());
            md.setResidentialId(device.getXqId());
            md.setResidentialName(device.getXqName());
            memberDeviceList.add(md);
        }
        return this.memberDeviceFeign.addBatch(memberDeviceList);
    }

    @PostMapping(value = "/deleteBatch")
    public ResultBean<Boolean> deleteBatch(MemberDevice memberDevice){
        Assert.notEmpty(memberDevice.getIds(),"请先选择设备");
        return this.memberDeviceFeign.deleteBatch(memberDevice.getIds());
    }
}
