package com.muyu.enterprise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.enterprise.domain.dateBase.ElectronicFenceGroup;
import com.muyu.enterprise.domain.req.ElectronicFenceGroupAddReq;
import com.muyu.enterprise.domain.req.ElectronicFenceGroupListReq;
import com.muyu.enterprise.domain.req.ElectronicFenceGroupUpdReq;
import com.muyu.enterprise.domain.resp.ElectronicFenceGroupResp;
import com.muyu.enterprise.domain.resp.GroupFenceListresp;
import com.muyu.enterprise.service.ElectronicFenceGroupService;
import com.muyu.enterprise.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.List;

/**
 * @Author：李庆帅
 * @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();
    }

    /**
     * 围栏组表信息表删除
     * @param findGroupId 请求对象
     * @return 返回结果
     */
    @DeleteMapping("/{findGroupId}")
    @Operation(summary = "围栏组表信息表删除",
            description = "通过ID删除围栏组表信息")
    public Result<String> remove(@PathVariable("findGroupId") Long findGroupId)
    {
        boolean b = electronicFenceGroupService.removeById(findGroupId);


        return Result.success(null, "操作成功");
    }
    /**
     * 围栏链信息表删除
     * @param findId 请求对象
     * @return 返回结果
     */
    @DeleteMapping("/findId/{findId}")
    @Operation(summary = "围栏链信息表删除",
            description = "通过ID删除围栏链表信息")
    public Result<String> findId(@PathVariable("findId") Long findId)
    {
        boolean b = fenceGroupMidService.removeById(findId);


        return Result.success(null, "操作成功");
    }


}
