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.ElevatorPersonGroupDTO;
import com.yuchen.saas.park.api.dto.elevator.PermissionDTO;
import com.yuchen.saas.park.api.entity.elevator.SaasParkElevatorPermissionPersonGroup;
import com.yuchen.saas.park.api.request.ElevatorPersonGroupRequest;
import com.yuchen.saas.park.api.vo.elevator.ElevatorPersonGroupVO;
import com.yuchen.saas.park.service.mapper.elevator.SaasParkElevatorPermissionPersonGroupMapper;
import com.yuchen.saas.park.service.mapper.property.PropertyBuildingUnitMapper;
import com.yuchen.saas.park.service.mapper.property.PropertyBuildingsMapper;
import com.yuchen.saas.park.service.service.elevator.ISaasParkElevatorPermissionPersonGroupService;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 梯控人员分组 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-07-08
 */
@Service
public class SaasParkElevatorPermissionPersonGroupServiceImpl extends BaseServiceImpl<SaasParkElevatorPermissionPersonGroupMapper, SaasParkElevatorPermissionPersonGroup> implements ISaasParkElevatorPermissionPersonGroupService {

    @Override
    public R<IPage<ElevatorPersonGroupVO>> list(ElevatorPersonGroupRequest qryDTO, Query query) {
        Page<SaasParkElevatorPermissionPersonGroup> page = new Page<>(query.getCurrent(), query.getSize());

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

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

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

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

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

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

    }


    public R<ElevatorPersonGroupVO> detail(Long id, SaasParkElevatorPermissionPersonGroup saasParkElevatorPermissionPersonGroup) {
        ElevatorPersonGroupVO elevatorPersonGroupVO = new ElevatorPersonGroupVO();
        BeanUtils.copyProperties(saasParkElevatorPermissionPersonGroup, elevatorPersonGroupVO);

        List<PermissionDTO> permissionList = null;
        if (Func.isNotEmpty(saasParkElevatorPermissionPersonGroup.getElevatorRange())) {
            permissionList = JSON.parseArray(saasParkElevatorPermissionPersonGroup.getElevatorRange(), PermissionDTO.class);
        }
        elevatorPersonGroupVO.setPermission(permissionList);

        return R.data(elevatorPersonGroupVO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(ElevatorPersonGroupDTO elevatorPersonGroupDTO) {

        SaasParkElevatorPermissionPersonGroup saasParkElevatorPermissionPersonGroup = new SaasParkElevatorPermissionPersonGroup();
        BeanUtil.copyProperties(elevatorPersonGroupDTO, saasParkElevatorPermissionPersonGroup);

        PermissionDTO permission = new PermissionDTO();
        if (elevatorPersonGroupDTO != null) {
            if (elevatorPersonGroupDTO.getPermission().size() > 0) {
                permission = elevatorPersonGroupDTO.getPermission().get(0);
            }
        }
        if (Func.isNotEmpty(permission)) {

            List<PermissionDTO> listPermission = new ArrayList<>();
            listPermission.add(permission);

            String jsonStr = JSON.toJSONString(listPermission);
            saasParkElevatorPermissionPersonGroup.setElevatorRange(jsonStr);
        } else {
            throw new RuntimeException("请设置人员分组权限");
        }
        //保存分组
        boolean result=saveOrUpdate(saasParkElevatorPermissionPersonGroup);

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


        return result;
    }
}
