package cn.ysk.cashier.service.impl;

import cn.ysk.cashier.dto.shop.TbShopPermissionDto;
import cn.ysk.cashier.entity.TbShopPermission;
import cn.ysk.cashier.service.TbShopPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ysk.cashier.mybatis.mapper.TbShopStaffPermissionMapper;
import cn.ysk.cashier.entity.TbShopStaffPermission;
import cn.ysk.cashier.service.TbShopStaffPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 店铺员工权限关联表(TbShopStaffPermission)表服务实现类
 *
 * @author makejava
 * @since 2024-09-14 10:54:01
 */
@Service("tbShopStaffPermissionService")
public class TbShopStaffPermissionServiceImpl extends ServiceImpl<TbShopStaffPermissionMapper, TbShopStaffPermission> implements TbShopStaffPermissionService {

    @Autowired
    private TbShopPermissionService shopPermissionService;

    @Override
    public boolean userHasPermission(Integer userId, String permissionCode) {
        TbShopPermission permission = shopPermissionService.queryByCode(permissionCode);
        if (permission == null) {
            return false;
        }

        QueryWrapper<TbShopStaffPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("permission_id", permission.getId());
        TbShopStaffPermission staffPermission = this.getOne(queryWrapper);

        return staffPermission != null;
    }

    @Override
    public List<TbShopPermissionDto> getStaffPermissionList(Integer userId) {
        List<TbShopPermissionDto> permissionList = shopPermissionService.getShopPermissionList();

        QueryWrapper<TbShopStaffPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<TbShopStaffPermission> staffPermissions = this.list(queryWrapper);

        List<Integer> permissionIds = staffPermissions.stream().collect(ArrayList::new, (list, staffPermission) -> list.add(staffPermission.getPermissionId()), ArrayList::addAll);

        for (TbShopPermissionDto permission : permissionList) {
            permission.setHasPermission(permissionIds.contains(permission.getId()) ? 1 : 0);

            if (permission.getChildren().isEmpty()) {
                continue;
            }

            for (TbShopPermissionDto child : permission.getChildren()) {
                child.setHasPermission(permissionIds.contains(child.getId()) ? 1 : 0);
            }
        }
        return permissionList;
    }

    @Override
    @Transactional
    public void updateStaffPermission(Integer userId, Integer shopId, Integer staffId, List<TbShopPermissionDto> permissionIds) {
        QueryWrapper<TbShopStaffPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        this.remove(queryWrapper);

        List<TbShopStaffPermission> staffPermissions = new ArrayList<>();
        for (TbShopPermissionDto permission : permissionIds) {
            TbShopStaffPermission staffPermission = new TbShopStaffPermission();
            staffPermission.setUserId(userId);
            staffPermission.setPermissionId(permission.getId());
            staffPermission.setStaffId(staffId);
            staffPermission.setShopId(shopId);
            staffPermissions.add(staffPermission);

            if (permission.getChildren().isEmpty()) {
                continue;
            }

            for (TbShopPermissionDto child : permission.getChildren()) {
                TbShopStaffPermission childStaffPermission = new TbShopStaffPermission();
                childStaffPermission.setUserId(userId);
                childStaffPermission.setPermissionId(child.getId());
                staffPermission.setStaffId(staffId);
                staffPermission.setShopId(shopId);
                staffPermissions.add(childStaffPermission);
            }
        }

        this.saveBatch(staffPermissions);
    }
}

