package com.yuchen.saas.park.service.service.elevator.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.elevator.ElevatorFloorGroupDTO;
import com.yuchen.saas.park.api.dto.elevator.ElevatorPersonGroupDTO;
import com.yuchen.saas.park.api.dto.elevator.PermissionDTO;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorPermissionFloorGroup;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorPermissionPersonGroup;
import com.yuchen.saas.park.api.request.ElevatorFloorGroupRequest;
import com.yuchen.saas.park.api.vo.elevator.ElevatorFloorGroupVO;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorPermissionFloorGroupMapper;
import com.yuchen.saas.park.service.service.elevator.ISaasParkElevatorPermissionFloorGroupService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
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.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 梯控楼层分组 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-07-08
 */
@Service
public class SaasParkElevatorPermissionFloorGroupServiceImpl extends BaseServiceImpl<SaasParkElevatorPermissionFloorGroupMapper, SaasParkElevatorPermissionFloorGroup> implements ISaasParkElevatorPermissionFloorGroupService {
    @Override
    public R<IPage<ElevatorFloorGroupVO>> list(ElevatorFloorGroupRequest qryDTO, Query query) {
        Page<SaasParkElevatorPermissionFloorGroup> page = new Page<>(query.getCurrent(), query.getSize());

        LambdaQueryWrapper<SaasParkElevatorPermissionFloorGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Func.isNotEmpty(qryDTO.getParkId()), SaasParkElevatorPermissionFloorGroup::getParkId, qryDTO.getParkId());
        queryWrapper.orderByDesc(SaasParkElevatorPermissionFloorGroup::getId);

        IPage<SaasParkElevatorPermissionFloorGroup> pages = this.page(page, queryWrapper);

        List<SaasParkElevatorPermissionFloorGroup> saasParkElevatorPermissionFloorGroupList = pages.getRecords();
        if (saasParkElevatorPermissionFloorGroupList.isEmpty()) {
            return R.data(Condition.getPage(query));
        }

        //组装返回response
        IPage<ElevatorFloorGroupVO> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
        List<ElevatorFloorGroupVO> records = this.listVO(pages.getRecords());

        pageVo.setRecords(records);
        return R.data(pageVo);
    }

    public List<ElevatorFloorGroupVO> listVO(List<SaasParkElevatorPermissionFloorGroup> listRecords) {
        return (List) listRecords.stream().map(entity -> {
            ElevatorFloorGroupVO elevatorFloorGroupVO = detail(entity.getId(), entity).getData();
            return elevatorFloorGroupVO;
        }).collect(Collectors.toList());

    }


    public R<ElevatorFloorGroupVO> detail(Long id, SaasParkElevatorPermissionFloorGroup saasParkElevatorPermissionFloorGroup) {
        ElevatorFloorGroupVO elevatorFloorGroupVO = new ElevatorFloorGroupVO();
        BeanUtils.copyProperties(saasParkElevatorPermissionFloorGroup, elevatorFloorGroupVO);

        PermissionDTO permissionDTO = null;
        if (Func.isNotEmpty(saasParkElevatorPermissionFloorGroup.getElevatorRange())) {
            permissionDTO = JSON.parseObject(saasParkElevatorPermissionFloorGroup.getElevatorRange(), PermissionDTO.class);
        }
        elevatorFloorGroupVO.setPermission(permissionDTO);

        return R.data(elevatorFloorGroupVO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(ElevatorFloorGroupDTO elevatorFloorGroupDTO) {

        SaasParkElevatorPermissionFloorGroup saasParkElevatorPermissionFloorGroup = new SaasParkElevatorPermissionFloorGroup();
        BeanUtil.copyProperties(elevatorFloorGroupDTO, saasParkElevatorPermissionFloorGroup);

        PermissionDTO permission = elevatorFloorGroupDTO.getPermission();
        if (Func.isNotEmpty(permission)) {
            String jsonStr = JSON.toJSONString(permission);
            saasParkElevatorPermissionFloorGroup.setElevatorRange(jsonStr);
        } else {
            throw new RuntimeException("请设置人员分组权限");
        }
        //保存分组
        boolean result=saveOrUpdate(saasParkElevatorPermissionFloorGroup);

        //把对应的人员权限修改到下发表


        return result;
    }
}
