package com.muyu.fence.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.fence.domain.database.ElectronicFence;
import com.muyu.fence.domain.database.ElectronicFenceGroup;
import com.muyu.fence.domain.req.*;
import com.muyu.fence.domain.resp.ElectronicFenceGroupResp;
import com.muyu.fence.domain.resp.GroupFenceListresp;
import com.muyu.fence.service.ElectronicFenceGroupService;
import com.muyu.fence.service.FenceGroupMidService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * @Author：yuping
 * @Package：com.muyu.fence.controller
 * @Project：cloud-server
 * @name：FenceGroupController
 * @Date：2024/9/18 15:15
 */

@Log4j2
@RestController
@RequestMapping("/group")
@Tag(name = "围栏组")
public class ElectronicFenceGroupController {

    @Autowired
    private ElectronicFenceGroupService electronicFenceGroupService;

    @Autowired
    private FenceGroupMidService fenceGroupMidService;


    @PostMapping("/selectGroupList")
    public Result<List<GroupFenceListresp>> selectGroupList(@RequestBody ElectronicFenceGroupListReq req) {
//查询所有的围栏组
        List<ElectronicFenceGroup> fenceListList = electronicFenceGroupService.selectGroupList(req);

        List<GroupFenceListresp> list = fenceListList.stream().map(ElectronicFenceGroup::buildGroupFence).toList();

        return Result.success(list);
    }

    @Transactional
    @PostMapping("/addGroup")
    public Result addGroup(@RequestBody ElectronicFenceGroupAddReq addReq) {

        ElectronicFenceGroup electronicFenceGroup = ElectronicFenceGroup.buildByAdd(addReq);
        //添加围栏组返回添加后的主键自增id
        electronicFenceGroupService.save(electronicFenceGroup);
        Long id = electronicFenceGroup.getId();
//添加中间表
        fenceGroupMidService.addGroupAndFenceMid(id, addReq.getElectronicFenceRespList());


        return Result.success();
    }

    @PostMapping("/findGroupByid/{id}")
    @Operation(description = "通过id回显围栏组信息")
    public Result<ElectronicFenceGroupResp> findGroupByid(@PathVariable("id") Long id) {

        ElectronicFenceGroupResp fenceGroupResp = electronicFenceGroupService.findGroupByid(id);
        return Result.success(fenceGroupResp);

    }


    @Transactional
    @PostMapping("/updGroup/{id}")
    @Operation(description = "修改")
    public Result UpdFence(@PathVariable("id") Long id, @RequestBody ElectronicFenceGroupUpdReq req) {

        electronicFenceGroupService.updateById(ElectronicFenceGroup.buildByUpd(req, () -> id));

        if (!CollectionUtils.isEmpty(req.getElectronicFenceRespList())) {
//删除中间表
            fenceGroupMidService.deliteMid(id);
            //增加中间表
            fenceGroupMidService.addGroupAndFenceMid(id, req.getElectronicFenceRespList());
        }
        return Result.success();
    }


}
