package com.czg.service.account.service.impl;

import com.czg.account.dto.pagepath.PagePathPermissionAddDTO;
import com.czg.account.entity.ShopPagePath;
import com.czg.account.entity.ShopStaff;
import com.czg.account.service.ShopPagePathService;
import com.czg.exception.CzgException;
import com.czg.service.account.mapper.ShopStaffMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.ShopStaffPagePermission;
import com.czg.account.service.ShopStaffPagePermissionService;
import com.czg.service.account.mapper.ShopStaffPagePermissionMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *  服务层实现。
 *
 * @author zs
 * @since 2025-04-02
 */
@Service
public class ShopStaffPagePermissionServiceImpl extends ServiceImpl<ShopStaffPagePermissionMapper, ShopStaffPagePermission> implements ShopStaffPagePermissionService{
    @Resource
    private ShopPagePathService shopPagePathService;
    @Resource
    private ShopStaffMapper shopStaffMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(Long shopId, PagePathPermissionAddDTO pagePathPermissionAddDTO) {
        ShopStaff shopStaff = shopStaffMapper.selectOneByQuery(new QueryWrapper().eq(ShopStaff::getId, pagePathPermissionAddDTO.getStaffId()).eq(ShopStaff::getShopId, shopId));
        if (shopStaff == null) {
            throw new CzgException("员工不存在");
        }

        if (!pagePathPermissionAddDTO.getPagePathIdList().isEmpty()) {
            long count = shopPagePathService.count(new QueryWrapper().in(ShopPagePath::getId, pagePathPermissionAddDTO.getPagePathIdList()));
            if (count != pagePathPermissionAddDTO.getPagePathIdList().size()) {
                throw new CzgException("存在错误id");
            }
        }

        Map<Long, ShopStaffPagePermission> map = list(new QueryWrapper().eq(ShopStaffPagePermission::getStaffId,
                pagePathPermissionAddDTO.getStaffId())).stream().collect(Collectors.toMap(ShopStaffPagePermission::getPagePathId, item -> item));

        pagePathPermissionAddDTO.getPagePathIdList().forEach(item -> {
            ShopStaffPagePermission permission1 = map.get(item);
            if (permission1 == null) {
                ShopStaffPagePermission permission = new ShopStaffPagePermission();
                permission.setShopId(shopId);
                permission.setPagePathId(item);
                permission.setStaffId(pagePathPermissionAddDTO.getStaffId());
                permission.setIsUse(1);
                save(permission);
            }else {
                permission1.setIsUse(1);
                updateById(permission1);
                map.remove(item);
            }

        });
        Set<Long> collect = map.values().stream().map(ShopStaffPagePermission::getId).collect(Collectors.toSet());
        if (!collect.isEmpty()) {
            updateChain().set(ShopStaffPagePermission::getIsUse, 0).in(ShopStaffPagePermission::getId, collect).update();
        }
        return true;
    }
}
