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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.thinkunion.park.service.common.utils.BeanUtil;
import com.yuchen.saas.device.api.vo.NetImportResVo;
import com.yuchen.saas.park.api.dto.park.SycnPersonDTO;
import com.yuchen.saas.park.api.dto.people.PeopleSetBuildingManagerReq;
import com.yuchen.saas.park.api.dto.people.PmgBatParkPeopleQryDto;
import com.yuchen.saas.park.api.entity.park.PmgBatParkPeople;
import com.yuchen.saas.park.api.param.PmgBatParkPeoplePageParam;
import com.yuchen.saas.park.api.vo.park.PmgBatParkPeopleGroupPageVo;
import com.yuchen.saas.park.api.vo.park.PmgBatParkPeopleVo;
import com.yuchen.saas.park.api.vo.people.PmgBatParkPeoplePolyVo;
import com.yuchen.saas.park.service.service.PmgBatParkPeopleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @description:
 * @author: 洪铭铭
 * @time: 2024/11/19 14:23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/managePeople")
@Api(value = "物业人员接口", tags = "物业人员接口")
@Validated
public class ManagePeopleController {

    /**
     * 注入园区人员信息操作接口
     */
    @Resource
    private PmgBatParkPeopleService pmgBatParkPeopleService;

    /**
     * 根据条件分页查询人员信息
     *
     * @param pmgBatParkPeople
     * @param query
     * @return
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页 物业人员信息", notes = "分页 物业人员信息")
    public R<IPage<PmgBatParkPeopleVo>> list(PmgBatParkPeople pmgBatParkPeople, Query query) {
        IPage<PmgBatParkPeopleVo> pages = pmgBatParkPeopleService.getPage(pmgBatParkPeople, query);
        return R.data(pages);
    }

    @PostMapping("/find")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页 物业人员信息（保利）", notes = "分页 物业人员信息")
    public R<IPage<PmgBatParkPeople>> find(@RequestBody PmgBatParkPeopleQryDto pmgBatParkPeopleQryDto) {
        return R.data(pmgBatParkPeopleService.selectPage(pmgBatParkPeopleQryDto));
    }

    @PostMapping("/listPoly")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "物业人员信息列表（保利）", notes = "物业人员信息列表（保利）")
    public R<IPage<PmgBatParkPeoplePolyVo>> listPoly(@RequestBody PmgBatParkPeopleQryDto pmgBatParkPeopleQryDto) {
        return R.data(pmgBatParkPeopleService.findParkPeople(pmgBatParkPeopleQryDto));
    }

    @GetMapping("/detail/{id}")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "物业人员信息详情", notes = "物业人员信息详情")
    public R<PmgBatParkPeople> detail(@PathVariable("id") Long id) {
        return R.data(pmgBatParkPeopleService.getById(id));
    }


    @GetMapping("/pageByGroup")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页 权限分组物业人员信息", notes = "分页 权限分组物业人员信息")
    public R<IPage<PmgBatParkPeopleGroupPageVo>> pageByGroup(@Valid PmgBatParkPeoplePageParam pmgBatParkPeoplePageParam) {
        IPage<PmgBatParkPeopleGroupPageVo> pages = pmgBatParkPeopleService.pageByGroup(pmgBatParkPeoplePageParam);
        return R.data(pages);
    }

    @GetMapping("/getAllFeign")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取全部物业人员信息", notes = "获取全部物业人员信息")
    public List<PmgBatParkPeople> getAllFeign(@RequestParam String parkId) {
        LambdaQueryWrapper<PmgBatParkPeople> query = new LambdaQueryWrapper<>();
        query.eq(PmgBatParkPeople::getParkId, Long.parseLong(parkId));
        List<PmgBatParkPeople> list = pmgBatParkPeopleService.list(query);
        return list;
    }

    @GetMapping("/getPeopleByIdFeign")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "根据获取全部物业人员信息", notes = "获取全部物业人员信息")
    public PmgBatParkPeople getPeopleByIdFeign(@RequestParam String id) {
        PmgBatParkPeople byId = pmgBatParkPeopleService.getById(Long.parseLong(id));
        return byId;
    }

    @GetMapping("/getPeopleByCardIdFeign")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "根据身份证获取物业人员信息", notes = "根据身份证获取物业人员信息")
    public PmgBatParkPeople getPeopleByCardIdFeign(@RequestParam String parkId, @RequestParam String cardId) {
        LambdaQueryWrapper<PmgBatParkPeople> query = new LambdaQueryWrapper<>();
        query.eq(PmgBatParkPeople::getParkId, Long.parseLong(parkId))
                .eq(PmgBatParkPeople::getIdCard, cardId).last("limit 1");

        PmgBatParkPeople people = pmgBatParkPeopleService.getOne(query);
        return people;
    }

    /**
     * 保存人员信息数据
     *
     * @param pmgBatParkPeople 人员信息数据
     * @return
     */
    @PostMapping("/savePeople")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "保存人员信息数据", notes = "保存人员信息数据")
    public R savePeople(@RequestBody PmgBatParkPeople pmgBatParkPeople) {
        return pmgBatParkPeopleService.savePeople(pmgBatParkPeople);
    }

    @PostMapping("/savePeopleNew")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "保存人员信息数据（新）", notes = "保存人员信息数据（新）")
    public R savePeopleNew(@RequestBody PmgBatParkPeople pmgBatParkPeople) {
        return pmgBatParkPeopleService.savePeopleNew(pmgBatParkPeople);
    }

    @PostMapping("/setBuildingManager")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "设置楼栋管家/工程师", notes = "设置楼栋管家/工程师")
    public R setBuildingManager(@RequestBody PeopleSetBuildingManagerReq peopleSetBuildingManagerReq) {

        PmgBatParkPeople people = BeanUtil.copyBean(peopleSetBuildingManagerReq, PmgBatParkPeople.class);
        return R.status(pmgBatParkPeopleService.saveOrUpdate(people));
    }

    @PostMapping("/savePeopleBatch")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "批量保存人员信息数据（新）", notes = "批量保存人员信息数据（新）")
    public R savePeopleBatch(@RequestBody List<PmgBatParkPeople> list) {
        return R.data(pmgBatParkPeopleService.saveOrUpdateBatch(list));
    }

    /**
     * 删除人员信息
     *
     * @param ids 人员ID
     * @return
     */
    @DeleteMapping("/delPeople")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除人员信息数据", notes = "删除人员信息数据")
    public R delPeople(@RequestParam String ids) {
        return pmgBatParkPeopleService.delPeople(ids);
    }

    /**
     * 获取默认的组织架构信息
     *
     * @param regionIndexCode
     * @return
     */
    @GetMapping("/peopleOrganizeList")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "获取默认的组织架构信息", notes = "获取默认的组织架构信息")
    public R peopleOrganizeList(@RequestParam(required = false) String regionIndexCode) {
        return R.data(pmgBatParkPeopleService.peopleOrganizeList(regionIndexCode));
    }

    /**
     * 同步 hik isc 人员信息（火炬园区-旧ISC）
     */
    @GetMapping("/syncPeopleInfo")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "同步 hik isc 人员信息", notes = "同步 hik isc 人员信息")
    public R syncPeopleInfo() {
        // TODO:目前固定单个园区的操作
        Long parkId = 1457553705928477026L;
        pmgBatParkPeopleService.syncPeopleInfo(parkId);
        return R.data("操作成功");
    }

    // --------------------------  访客远程调用专用 begin ------------------------------------

    /**
     * 根据人员ID查询人员信息
     *
     * @param id 人员ID
     * @return
     */
    @GetMapping("/getById")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "根据人员ID查询人员信息", notes = "根据人员ID查询人员信息")
    public PmgBatParkPeople getById(@RequestParam("id") Long id) {
        return pmgBatParkPeopleService.getById(id);
    }

    /**
     * 根据手机号获取用户信息（访客处使用，其他模块慎用）
     *
     * @param phone 手机号
     * @return
     */
    @GetMapping("/getByPhone")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "根据手机号获取用户信息（访客处使用，其他模块慎用）", notes = "根据手机号获取用户信息（访客处使用，其他模块慎用）")
    public PmgBatParkPeople getByPhone(@RequestParam String phone) {
        return pmgBatParkPeopleService.getByPhone(phone);
    }

    // --------------------------  访客远程调用专用 end ------------------------------------

    @PostMapping("/updatePeople")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "修改人员信息数据", notes = "修改人员信息数据")
    public R updatePeople(@RequestBody PmgBatParkPeople pmgBatParkPeople) {
        return R.status(pmgBatParkPeopleService.updatePeople(pmgBatParkPeople));
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "导入人员")
    public R<NetImportResVo> importExcel(@RequestParam("file") MultipartFile file) {
        return R.data(pmgBatParkPeopleService.importExcel(file));
    }

    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出人员")
    public void exportExcel(@RequestBody PmgBatParkPeopleQryDto qryDto, HttpServletResponse response) {
        pmgBatParkPeopleService.exportExcel(qryDto, response);
    }

    /**
     * @param pmgBatParkPeopleQryDto
     * @description: 房产认证
     * @return: org.nest.springwrap.core.tool.api.R
     * @author: 洪铭铭
     * @time: 2024/5/24 16:04
     */
    @PostMapping("/propertyAuth")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "房产认证", notes = "房产认证")
    public R propertyAuth(@RequestBody @Validated PmgBatParkPeopleQryDto pmgBatParkPeopleQryDto) {
        return pmgBatParkPeopleService.propertyAuth(pmgBatParkPeopleQryDto);
    }

    /**
     * @param pmgBatParkPeopleQryDto
     * @description:
     * @return: org.nest.springwrap.core.tool.api.R
     * @author: 洪铭铭
     * @time: 2024/5/29 15:11
     */
    @PostMapping("/ownerConfirm")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "业主确认", notes = "业主确认")
    public R ownerConfirm(@RequestParam("ownerPhone") String ownerPhone, @RequestBody @Validated PmgBatParkPeopleQryDto pmgBatParkPeopleQryDto) {
        return R.status(pmgBatParkPeopleService.ownerConfirm(ownerPhone, pmgBatParkPeopleQryDto));
    }

    /**
     * @param pmgBatParkPeople
     * @description: 添加住户
     * @return: org.nest.springwrap.core.tool.api.R
     * @author: 洪铭铭
     * @time: 2024/6/11 16:05
     */
    @PostMapping("/saveTenant")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "保存人员信息数据", notes = "保存人员信息数据")
    public R saveTenant(@RequestBody PmgBatParkPeople pmgBatParkPeople) {
        return pmgBatParkPeopleService.saveTenant(pmgBatParkPeople);
    }


    /**
     * 手动同步人员信息到电梯的人员中心
     * @param sycnPersonDTO
     * @return
     */
    @PostMapping("/handleSyncElevatorPerson")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "手动同步人员信息到电梯的人员中心", notes = "手动同步人员信息到电梯的人员中心")
    public R handleSyncElevatorPerson(@Valid @RequestBody SycnPersonDTO sycnPersonDTO) {
        Long parkId = sycnPersonDTO.getParkId();
        pmgBatParkPeopleService.syncElevatorPerson(parkId,sycnPersonDTO.getPersonId());
        return R.success();
    }


}
