package com.yuchen.saas.park.service.controller.face;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.park.PmgBtnPeopleEquipment;
import com.yuchen.saas.park.api.vo.park.PmgBtnPeopleEquipmentVo;
import com.yuchen.saas.park.service.service.PmgBtnPeopleEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘通
 * @date 2020/12/30 2:53
 */
@RestController
@AllArgsConstructor
@RequestMapping("/peopleEquipment")
@Api(value = "人员设备关联对象", tags = "人员设备关联接口")
public class PmgBtnPeopleEquipmentController {
    @Autowired
    PmgBtnPeopleEquipmentService pmgBtnPeopleEquipmentService;
    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "园区空间主题类型信息")
    public R<PmgBtnPeopleEquipment> detail(PmgBtnPeopleEquipment mpgBtnPeopleEquipment) {
        PmgBtnPeopleEquipment detail =
                pmgBtnPeopleEquipmentService.getOne(Condition.getQueryWrapper(mpgBtnPeopleEquipment));
        return R.data(detail);
    }
    /**
     * 通过people id 查询详情
     */
    @GetMapping("/getByPeopleId")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "详情", notes = "传入院内人员/访客/公司")
    public R<List<PmgBtnPeopleEquipmentVo>> getByPeopleId(String peopleId) {
        List<PmgBtnPeopleEquipmentVo> detail = pmgBtnPeopleEquipmentService.getByPeopleId(peopleId);
        return R.data(detail);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入对象")
    public R<IPage<PmgBtnPeopleEquipment>> list(PmgBtnPeopleEquipment PmgBtnPeopleEquipment, Query query) {
        IPage<PmgBtnPeopleEquipment> pages = pmgBtnPeopleEquipmentService.page(Condition.getPage(query),
                Condition.getQueryWrapper(PmgBtnPeopleEquipment));
        return  R.data(pages);
    }
    @GetMapping("/getListByPeopleId")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页", notes = "传入人员ID")
    public R<List<PmgBtnPeopleEquipment>> getListByPeopleId(String peopleId, Query query) {
        LambdaQueryWrapper<PmgBtnPeopleEquipment> peopleEquipment = Wrappers.lambdaQuery();
        List<PmgBtnPeopleEquipment> peopleEquipmentList =
                pmgBtnPeopleEquipmentService.list(peopleEquipment.eq(PmgBtnPeopleEquipment::getPeopleId, peopleId));
        return  R.data(peopleEquipmentList);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "新增", notes = "传入对象")
    public R save(@Valid @RequestBody PmgBtnPeopleEquipment pmgBtnPeopleEquipment) {
        return R.status(pmgBtnPeopleEquipmentService.save(pmgBtnPeopleEquipment));
    }
    @PostMapping("/saveList")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增", notes = "传入对象")
    public R saveList(@Valid @RequestBody List<PmgBtnPeopleEquipment> pmgBtnPeopleEquipmentList) {
        List<Long> ids = new ArrayList<Long>();
        if(Func.isNotEmpty(pmgBtnPeopleEquipmentList)
                && pmgBtnPeopleEquipmentList.size() > 0) {
            for(PmgBtnPeopleEquipment perpleEquipment: pmgBtnPeopleEquipmentList) {
                ids.add(Long.valueOf(perpleEquipment.getPeopleId()));
            }
            // 删除人员id对应的人员设备关联信息
            pmgBtnPeopleEquipmentService.deleteLogic(ids);
        }
        // 批量保存
        return R.status(pmgBtnPeopleEquipmentService.saveOrUpdateBatch(pmgBtnPeopleEquipmentList));
    }
    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "修改", notes = "传入对象")
    public R update(@Valid @RequestBody PmgBtnPeopleEquipment pmgBtnPeopleEquipment) {
        return R.status(pmgBtnPeopleEquipmentService.updateById(pmgBtnPeopleEquipment));
    }

    /**
     * 删除
     */
    @DeleteMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(pmgBtnPeopleEquipmentService.deleteLogic(Func.toLongList(ids)));
    }
}
